
%

%   Copyright 2013-2019 The MathWorks, Inc.

if exist('AGC_gain', 'var') && (size(AGC_gain.data,1)>1)
    
    tscope1 = timescope(...
    'Name', 'AGC Gain', ...
    'YLabel', 'Gain', ...
    'SampleRate',1/AGC_gain.TimeInfo.Increment,...
    'MaximizeAxes', 'Auto', ... 
    'TimeSpanSource','property', ...
    'TimeSpan',AGC_gain.TimeInfo.End,...
    'TimeUnits','Seconds');
    tscope1(AGC_gain.data);
    release(tscope1);

    % Calculate 'K' where f_nco = K * stepSize_nco
    K = qam_params.sampleRate/2^16;

    tscope2 = timescope(...
    'Name', 'Frequency Offset Estimate', ...
    'YLabel', 'Frequency Offset [Hz]', ...
    'NumInputPorts', 2, ...
    'SampleRate',1/CFR_freqEst.TimeInfo.Increment,...
    'MaximizeAxes', 'Auto', ... 
    'TimeSpanSource','property', ...
    'TimeSpan',CFR_freqEst.TimeInfo.End,...
    'TimeUnits','Seconds');
    tscope2(real(K*CFR_freqEst.Data), -qam_params.frequencyOffset*(ones(CFR_freqEst.TimeInfo.Length,1)));
    release(tscope2);

    tscope3 = timescope(...
    'Name', 'Timing Recovery Position', ...
    'YLabel', 'mu [samples]', ...
    'SampleRate',1/TR_mu.TimeInfo.Increment,...
    'MaximizeAxes', 'Auto', ... 
    'TimeSpanSource','property', ...
    'TimeSpan',TR_mu.TimeInfo.End,...
    'TimeUnits','Seconds');
    tscope3(TR_mu.data);
    release(tscope3);

    % create sampled data from dataOut and validOut
    sampledData = timeseries(...
         TR_dataOut.Data(TR_validOut.Data),...
         TR_dataOut.Time(TR_validOut.Data));

    % plot real part of timing recovery output to show points converging
    constDiag1 = comm.ConstellationDiagram( ...
    'Name','Real Part of Timing Recovery Output', ...
    'XLimits', [0 0.02], ...
    'YLimits', [-1 1], ...
    'Position', [680 390 560 420], ...
    'XLabel', 'Time [Seconds]', ...
    'YLabel', 'Amplitude', ...
    'ReferenceConstellation', [], ...
    'ColorFading',false);
    constDiag1(complex(sampledData.Time, real(sampledData.Data)));
    release(constDiag1);

    % plot real part of symbol estimates
    constDiag2 = comm.ConstellationDiagram( ...
    'Name','Real Part of Symbol Estimates', ...
    'XLimits', [0 0.02], ...
    'YLimits', [-1 1], ...
    'Position', [680 390 560 420], ...
    'XLabel', 'Time [Seconds]', ...
    'YLabel', 'Amplitude', ...
    'ReferenceConstellation', [], ...
    'ColorFading',false);
    constDiag2(complex(symbolEstimates.Time, real(symbolEstimates.Data)));
    release(constDiag2);
    
    % plot constellation just before de-mapper
    refConst = (0.25/2) .* qammod((0:63)', 64);
    
    constDiag3 = comm.ConstellationDiagram( ...
        'Name','Recovered Constellation', ...
        'XLimits', [-1 1], ...
        'YLimits', [-1 1], ...
        'ReferenceConstellation', refConst, ...
        'Position', [680 390 560 420], ...
        'ColorFading',true);
    constDiag3(symbolEstimates.Data);
    release(constDiag3);
    
    clear K freqEst y sampledData refConst rqamMod;
end
