function varargout = radarWaveformAnalyzer(importData)
%radarWaveformAnalyzer Analyze performance characteristics of pulsed,
%frequency modulated, and phase coded waveforms and create pulsed waveform
%library
%
%   radarWaveformAnalyzer opens the Radar Waveform Analyzer App.
%
%   radarWaveformAnalyzer is an interactive app for the analysis and
%   creation of library for pulsed and continuous waveforms that are
%   exploited by both radar as well as sonar . The app allows the user to
%   select a waveform from a list, change waveform parameters and interact
%   with waveform plots, such as the magnitude and phase response, the
%   ambiguity function, the spectrum and the spectrogram. It also allows
%   user to create and modify library of pulsed waveforms. The app
%   calculates waveform characteristics such as range resolution, doppler
%   resolution, minimum range, maximum range, maximum doppler, time
%   bandwidth product and duty cycle. The user can export scripts,
%   waveforms and PulseWaveformLibrary objects to a file, workspace or
%   Simulink block.
%
%   radarWaveformAnalyzer(WAV) opens the Radar Waveform Analyzer app with
%   the WAV. WAV can be pulsed, frequency modulated, phase coded waveforms
%   or PulseWaveformLibrary.

%   Copyright 2012-2020 The MathWorks, Inc.

import matlab.internal.lang.capability.Capability;
Capability.require([Capability.Swing, Capability.ComplexSwing]);

narginchk(0,1);
if nargin % Import data from workspace
    if isa(importData,'phased.PulseWaveformLibrary')
        % copy to temporary variable so that orignal object is not
        % affected
        tempData = clone(importData);
        for i = 1:numel(tempData.WaveformSpecification)
            if isa(tempData.WaveformSpecification{i}{1},'function_handle')
                error(message('phased:apps:waveformapp:CustomInput'));
            end
        end
        try
            step(tempData,1);
            release(tempData);
            out = phased.apps.internal.WaveformViewer.radarWaveformAnalyzer({importData},{inputname(1)});
        catch me
            error(me.message);
            return
        end
    elseif isa(importData,'phased.RectangularWaveform')...
            || isa(importData,'phased.LinearFMWaveform') || isa(importData,'phased.SteppedFMWaveform')...
            || isa(importData,'phased.PhaseCodedWaveform') || isa(importData,'phased.FMCWWaveform')
        try
            % copy to temporary variable so that orignal object is not
            % affected
            tempData = clone(importData);
            step(tempData);
            release(tempData);
            out = phased.apps.internal.WaveformViewer.radarWaveformAnalyzer({importData},{inputname(1)});
        catch me
            error(me.message);
            return
        end
    elseif isa(importData,'phased.PulseCompressionLibrary')
        tempData = clone(importData);
        pwl = phased.PulseWaveformLibrary('SampleRate',tempData.SampleRate,...
            'WaveformSpecification',tempData.WaveformSpecification);
        for i = 1:numel(tempData.WaveformSpecification)
            if isa(tempData.WaveformSpecification{i}{1},'function_handle')
                error(message('phased:apps:waveformapp:CustomInput'));
            end
            try
                wave = pwl(i);
                tempData(wave,i);
            catch me
                error(me.message);
                return
            end
        end
        release(tempData);
        out = phased.apps.internal.WaveformViewer.radarWaveformAnalyzer({importData},{inputname(1)});
    else
        try
            if isa(importData,'char')
                Session = load(importData);
                importData = Session.LibrarySession;
            end
            if  isa((importData.data{1}.Elements{1}),'phased.apps.internal.WaveformViewer.RectangularWaveform') || isa((importData.data{1}.Elements{1}),'phased.apps.internal.WaveformViewer.LinearFMWaveform')...
                    || isa((importData.data{1}.Elements{1}),'phased.apps.internal.WaveformViewer.SteppedFMWaveform') || isa((importData.data{1}.Elements{1}),'phased.apps.internal.WaveformViewer.PhaseCodedWaveform')...
                    || isa((importData.data{1}.Elements{1}),'phased.apps.internal.WaveformViewer.FMCWWaveform') || isa((importData.data{1}.Elements{1}),'phased.PulseCompressionLibrary')
                
                out = phased.apps.internal.WaveformViewer.radarWaveformAnalyzer(importData);
            else
                error(message('phased:apps:waveformapp:InvalidInput'))
            end
        catch
            error(message('phased:apps:waveformapp:InvalidInput'))
        end
    end
else
    out = phased.apps.internal.WaveformViewer.radarWaveformAnalyzer();
end
if nargout
    varargout{1} = out;
end
end