%-------------------------------------------------------------------------------
% $Id: max8_diagnostic_plots.m,v 1.7 2023/07/26 21:49:41 ddelore Exp $
%
%  David S. De Lorenzo
%  11 December 2019
%
%  Inter-loop diagnostic plots
%
%-------------------------------------------------------------------------------

if (n == N)
    clear('ms');
    t_vec = ((1:N)-1)*NREAD*1e3/fs;
end

%% --- Spectrum Estimate ---------------------------------------------------

% {
    % The reason that 'shift_T_at_RF_dB' is plotted is so
    % you can verify that the post-A/D spectrum is adjusted
    % properly with the gain, attenuation, ADC pseudo-gain,
    % and squared-volts-to-milliWatts conversion.
%     shift_T_at_RF_dB ...
%         = 10*log10(fftshift(T_at_RF_filt));
    
    shift_T_post_adc_dB ...
        = 10*log10(fftshift(T_post_adc_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    shift_T_post_iir_dB ...
        = 10*log10(fftshift(T_post_iir_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    shift_T_post_fir_A_dB ...
        = 10*log10(fftshift(T_post_fir_A_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    shift_T_post_fft_dB ...
        = 10*log10(fftshift(T_post_fft_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    shift_T_post_fir_B_dB ...
        = 10*log10(fftshift(T_post_fir_B_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    shift_T_out_dB ...
        = 10*log10(fftshift(T_out_filt)) ...
        - 1/2*(agc_gain_dB_old ...
               - prog_attenuation_dB_old ...
               + ADC_PSEUDO_GAIN_dB) ...
        + 1/2*10*log10(50.0/1000);
    
    figure(301);
    clf(301);
    if (size(monitor_positions,1) == 2)
        if all(monitor_positions(1,3:4) == [1280 800])
            % Laptop w/ secondary monitor
            set(301, 'Position', [420 100 850 530]);
        else
            % Dual monitor set-up
            set(301, 'Position', [990 215 920 585]);
        end
    end
    
    box('on');
    grid('on');
    hold('on');
    
    if exist('shift_T_at_RF_dB','var')
        plot(FFT_FREQ_VEC/1e6, shift_T_at_RF_dB, 'g--', ...
             'LineWidth', 3, 'DisplayName', '  Signal at RF');
    end
    
    plot(FFT_FREQ_VEC/1e6, shift_T_post_adc_dB, 'k-', ...
         'LineWidth', 2, 'DisplayName', '  Input Signal');
    
    if (FB_IIR_FILTER_IS_ACTIVE == true) || (IIR_FILTER_IS_ACTIVE == true)
        plot(FFT_FREQ_VEC/1e6, shift_T_post_iir_dB, '-', ...
             'LineWidth', 2, 'DisplayName', '    Post-IIR');
    end
    if (FIR_FILTER_A_IS_ACTIVE == true)
        plot(FFT_FREQ_VEC/1e6, shift_T_post_fir_A_dB, '-', ...
             'LineWidth', 2, 'DisplayName', '  Post-FIR ''A''');
    end
    if (FFT_FILTER_IS_ACTIVE == true)
        plot(FFT_FREQ_VEC/1e6, shift_T_post_fft_dB, '-', ...
             'LineWidth', 2, 'DisplayName', '   Post-FFT');
    end
    if (FIR_FILTER_B_IS_ACTIVE == true)
        plot(FFT_FREQ_VEC/1e6, shift_T_post_fir_B_dB, '-', ...
             'LineWidth', 2, 'DisplayName', '  Post-FIR ''B''');
    end
    
    plot(FFT_FREQ_VEC/1e6, shift_T_out_dB, 'b-', ...
         'LineWidth', 2, 'DisplayName', ' Output Signal');
    
%     for gg = 1:length(spectral_peaks)
%         if (spectral_peaks(gg).is_detected == true)
%             freq       = spectral_peaks(gg).freq;
%             ampl       = spectral_peaks(gg).ampl;
%             start_freq = spectral_peaks(gg).start_freq;
%             stop_freq  = spectral_peaks(gg).stop_freq;
%             ampl_dB ...
%                 = 10*log10(ampl) ...
%                 - 1/2*(agc_gain_old ...
%                        - prog_attenuation_old ...
%                        + ADC_PSEUDO_GAIN_dB) ...
%                 + 1/2*10*log10(50.0/1000);
%             plot(freq/1e6, ampl_dB, 'ro', ...
%                  'MarkerSize', 12-2*gg, 'LineWidth', 2, ...
%                  'DisplayName', sprintf('    Peak #%d', gg));
%             plot([start_freq stop_freq]/1e6, ampl_dB*[1 1], 'r', ...
%                  'LineWidth', 2, 'HandleVisibility', 'off');
%         end
%     end
    
    for hh = 1:length(rfi_states)
        if (rfi_states(hh).is_tracked == true)
            freq       = rfi_states(hh).freq;
            ampl       = rfi_states(hh).ampl;
            start_freq = rfi_states(hh).start_freq;
            stop_freq  = rfi_states(hh).stop_freq;
            ampl_dB ...
                = 10*log10(ampl) ...
                - 1/2*(agc_gain_dB_old ...
                       - prog_attenuation_dB_old ...
                       + ADC_PSEUDO_GAIN_dB) ...
                + 1/2*10*log10(50.0/1000);
            plot(freq/1e6, ampl_dB, 'ro', ...
                 'MarkerSize', 12-2*hh, 'LineWidth', 2, ...
                 'DisplayName', sprintf(' RFI State #%d', hh));
            plot([start_freq stop_freq]/1e6, ampl_dB*[1 1], 'r', ...
                 'LineWidth', 2, 'HandleVisibility', 'off');
        end
    end
    
%     if (FB_IIR_FILTER_IS_ACTIVE == true) || (IIR_FILTER_IS_ACTIVE == true)
%         for kk = 1:length(iir_filter_states)
%             if (iir_filter_states(kk).filter_is_active == true)
%                 freq = iir_filter_states(kk).freq;
%                 ampl = iir_filter_states(kk).ampl;
%                 ampl_dB ...
%                     = 10*log10(ampl) ...
%                     - 1/2*(agc_gain_old ...
%                            - prog_attenuation_old ...
%                            + ADC_PSEUDO_GAIN_dB) ...
%                     + 1/2*10*log10(50.0/1000);
%                 plot(freq/1e6, ampl_dB, 'bo', ...
%                      'MarkerSize', 12-2*kk, 'LineWidth', 2, ...
%                      'DisplayName', sprintf('      IIR #%d', kk));
%             end
%         end
%     end
    
    if (FFT_FILTER_IS_ACTIVE == true)
        blanking_threshold = fft_filter_state.blanking_threshold;
        ampl_dB ...
            = 10*log10(blanking_threshold) ...
            - 1/2*(agc_gain_dB_old ...
                   - prog_attenuation_dB_old ...
                   + ADC_PSEUDO_GAIN_dB) ...
            + 1/2*10*log10(50.0/1000);
        if isfinite(ampl_dB)
            plot([FFT_FREQ_VEC(1) FFT_FREQ_VEC(end)]/1e6, ampl_dB*[1 1], ...
                 'k--', 'LineWidth', 1, 'DisplayName', ' FFT THRESH');
        end
    end
    
    xlim([FFT_FREQ_VEC(1) FFT_FREQ_VEC(end)]/1e6);
    switch data_source
        case 'GPS Simulator'
            ylim([-40 20]);
        case 'Auger RF'
            ylim([10 65]);
    end
    xlabel('Frequency [MHz]', 'FontSize', 12);
    ylabel('Magnitude [dB]', 'FontSize', 12);
    title('Spectrum Estimate', 'FontSize', 12);
    legend('show', 'Location', 'NorthEast');
%}

%% --- Quantization, Gain, and Attenuation ---------------------------------

%{
if ~strcmp(data_source,'Auger RF')
    
    %--- Quantizer Function-----------------------------------------------------
    figure(302);
    clf(302);
    if (size(monitor_positions,1) == 2)
        if all(monitor_positions(1,3:4) == [1280 800])
            % Laptop w/ secondary monitor
            set(302, 'Position', [2700 270 560 420]);
        else
            % Dual monitor set-up
            set(302, 'Position', [2700 560 560 420]);
        end
    end
    
    box('on');
    grid('on');
    hold('on');
    
    plot(1000*[real(s_post_agc),  imag(s_post_agc)], ...
              [real(s_quantized), imag(s_quantized)], ...
        'bo', 'LineWidth', 1);
    
    xlabel('Input Sample Value [mV]', 'FontSize', 12);
    ylabel('Quantizer Output', 'FontSize', 12);
    title('Quantizer Function', 'FontSize', 12);
    
    switch QUANTIZER_TYPE
        case 'centered'
            xlim(1.25*1000*ADC_p2p/2*[-1 1]);
            ylim(1.2*(2^(M-1))*[-1 1]);
            line(-1000*ADC_p2p/2*[1 1], ylim, ...
                 'Color', 'r', 'LineWidth', 2);
            line(+1000*ADC_p2p/2*[1 1], ylim, ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (-2^(M-1)+1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (+2^(M-1)-1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (-2^(M0-1)+1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
            line(xlim, (+2^(M0-1)-1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
        case 'non-centered'
            xlim(1.25*1000*ADC_p2p/2*[-1 1]);
            ylim(1.2*(2^M)*[-1 1]);
            line(-1000*ADC_p2p/2*[1 1], ylim, ...
                 'Color', 'r', 'LineWidth', 2);
            line(+1000*ADC_p2p/2*[1 1], ylim, ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (-2^M+1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (+2^M-1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 2);
            line(xlim, (-2^M0+1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
            line(xlim, (+2^M0-1)*[1 1], ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
    end
    
    %--- Quantized Signal Histogram --------------------------------------------
    figure(303);
    clf(303);
    if (size(monitor_positions,1) == 2)
        if all(monitor_positions(1,3:4) == [1280 800])
            % Laptop w/ secondary monitor
            set(303, 'Position', [2700 -235 560 420]);
        else
            % Dual monitor set-up
            set(303, 'Position', [2700 50 560 420]);
        end
    end
    
    box('on');
    grid('on');
    hold('on');
    
    switch QUANTIZER_TYPE
        case 'centered'
            bin_limits = [(-2^(M-1)+1) (2^(M-1)-1)];
        case 'non-centered'
            bin_limits = [  (-2^M+1)     (2^M-1)  ];
        otherwise
            error('Invalid quantizer type!!');
    end
    
    if (M > 8)
        h = histogram([real(histogram_buffer), ...
                       imag(histogram_buffer)], ...
                      100, 'BinLimits', bin_limits, ...
                      'Normalization', 'probability');
        xlabel('Quantized Value', 'FontSize', 12);
        ylabel('Probability', 'FontSize', 12);
        title('Quantized Signal Histogram', 'FontSize', 12);
    else
        h = histogram([real(histogram_buffer), ...
                       imag(histogram_buffer)], ...
                      'BinLimits', bin_limits, ...
                      'BinMethod', 'integers', ...
                      'Normalization', 'count');
        xlabel('Quantized Value', 'FontSize', 12);
        ylabel('Counts', 'FontSize', 12);
        title('Quantized Signal Histogram', 'FontSize', 12);
    end
    
    switch QUANTIZER_TYPE
        case 'centered'
            xlim(1.2*(2^(M-1))*[-1 1]);
            ylim(h.Parent.YLim);
            line((-2^(M-1)+1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 2);
            line((+2^(M-1)-1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 2);
            line((-2^(M0-1)+1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
            line((+2^(M0-1)-1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
        case 'non-centered'
            xlim(1.2*(2^M)*[-1 1]);
            ylim(h.Parent.YLim);
            line((-2^M+1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 2);
            line((+2^M-1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 2);
            line((-2^M0+1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
            line((+2^M0-1)*[1 1], h.Parent.YLim, ...
                 'Color', 'r', 'LineWidth', 1, 'LineStyle', '--');
    end
    
    %--- Fraction of Samples > Max. Quantization Limit -------------------------
    figure(304);
    if (n == 1) || (n == N)
        clf(304);
        if all(monitor_positions(1,3:4) == [1280 800])
            % Laptop w/ secondary monitor
            set(304, 'Position', [3265 270 560 420]);
        else
            % Dual monitor set-up
            set(304, 'Position', [3265 560 560 420]);
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x = [0 ms];
            y = adc_overflow_fraction*[1 1];
        else
            x = t_vec;
            y = adc_overflow_fraction_vec;
        end
        
        p302 = plot(x, y, 'b-', 'LineWidth', 2);
        
        xlim([0 ms_to_process]);
        ylim([0 1]);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Fraction', 'FontSize', 12);
        title('Fraction of Samples > Max. Quantization Limit', ...
              'FontSize', 12);
    else
        p302.XData = [p302.XData, ms];
        p302.YData = [p302.YData, adc_overflow_fraction];
    end
    
    %--- Gain & Attenuation ----------------------------------------------------
    figure(305);
    if (n == 1) || (n == N)
        clf(305);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(305, 'Position', [3265 -235 560 420]);
            else
                % Dual monitor set-up
                set(305, 'Position', [3265 50 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x  = [0 ms];
            y1 = agc_gain_old*[1 1];
            y2 = prog_attenuation_old*[1 1];
        else
            x  = t_vec;
            y1 = agc_gain_storage_vector;
            y2 = prog_atten_storage_vector;
        end
        
        p304a = plot(x, y1, '-', 'Color', [0.85 0.33 0.10], 'LineWidth', 2, ...
            'DisplayName', '  AGC Gain');
        p304b = plot(x, y2, '-', 'Color', [0.72 0.27 1.00], 'LineWidth', 2, ...
            'DisplayName', ' Attenuation');
        
        xlim([0 ms_to_process]);
        ylim([-5 40]);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Gain/Attenuation [dB]', 'FontSize', 12);
        title('Gain and Attenuation', 'FontSize', 12);
        legend('show', 'Location', 'West');
    else
        p304a.XData = [p304a.XData, ms];
        p304a.YData = [p304a.YData, agc_gain_old];
        p304b.XData = [p304b.XData, ms];
        p304b.YData = [p304b.YData, prog_attenuation_old];
    end
    
end
%}

%% --- Signal & Noise Power at RF ------------------------------------------

% {
    %--- Signal & Noise Power at RF --------------------------------------------
    figure(306);
    if (n == 1) || (n == N)
        clf(306);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(306, 'Position', [2700 270 560 420]);
            else
                % Dual monitor set-up
                set(306, 'Position', [2700 560 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x  = [0 ms];
            y1 = noise_power_at_RF_dBm*[1 1];
            y2 = rssi_at_RF_dBm*[1 1];
        else
            x  = t_vec;
            y1 = noise_power_at_RF_vec;
            y2 = rssi_at_RF_storage_vec;
        end
        
        p306a = plot(x, y1, 'k-o', 'MarkerSize', 3, 'LineWidth', 3, ...
            'DisplayName', ' Noise Power');
        line([0 ms_to_process], TRUE_NOISE_POWER_dBm*[1 1], ...
             'LineStyle', '--', 'Color', 'k', ...
             'LineWidth', 1, 'DisplayName', ' TRUE WGN');
        p306b = plot(x, y2, 'b-', 'LineWidth', 3, ...
            'DisplayName', '     RSSI');
        
        xlim([0 ms_to_process]);
        switch data_source
            case 'GPS Simulator'
                ylim([-60 20]);
            case 'Auger RF'
                ylim([45 90]);
        end
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Power [dBm]', 'FontSize', 12);
        title('Signal & Noise Power at RF', 'FontSize', 12);
        legend('show', 'Location', 'NorthWest');
    else
        p306a.XData = [p306a.XData, ms];
        p306a.YData = [p306a.YData, noise_power_at_RF_dBm];
        p306b.XData = [p306b.XData, ms];
        p306b.YData = [p306b.YData, rssi_at_RF_dBm];
    end
%}

%% --- Post-Attenuation & Post-AGC -----------------------------------------

% {
if ~strcmp(data_source,'Auger RF')
    
    %--- Post-Attenuation & Post-AGC -------------------------------------------
    figure(307);
    if (n == 1) || (n == N)
        clf(307);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(307, 'Position', [3265 270 560 420]);
            else
                % Dual monitor set-up
                set(307, 'Position', [3265 560 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x  = [0 ms];
            y1 = rssi_post_atten_dBm*[1 1];
            y2 = noise_power_post_agc_dBm*[1 1];
            y3 = rssi_post_agc_dBm*[1 1];
        else
            x  = t_vec;
            y1 = rssi_post_atten_storage_vec;
            y2 = noise_power_post_agc_vec;
            y3 = rssi_post_agc_storage_vec;
        end
        
        p307a = plot(x, y1, '-', 'Color', [0.72 0.27 1.00], ...
            'LineWidth', 3, 'DisplayName', '  Post-Attenuation RSSI');
        line([0 ms_to_process], COMPRESSION_THRESHOLD_dBm*[1 1], ...
            'LineStyle', '--', 'Color', [0.72 0.27 1.00], ...
            'LineWidth', 1, 'DisplayName', '      COMPRESSION');
        p307b = plot(x, y2, 'k-o', 'LineWidth', 3, 'MarkerSize', 3, ...
            'DisplayName', ' Post-AGC Noise Power');
        line([0 ms_to_process], DESIRED_PRE_ADC_NOISE_POWER_dBm*[1 1], ...
            'LineStyle', '--', 'Color', 'k', 'LineWidth', 1, ...
            'DisplayName', '   Desired Noise Power');
        p307c = plot(x, y3, '-', 'Color', [0.85 0.33 0.10], ...
            'LineWidth', 3, 'DisplayName', '       Post-AGC RSSI');
        
        xlim([0 ms_to_process]);
        ylim([-60 20]);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Power [dBm]', 'FontSize', 12);
        title('Post-Attenuation & Post-AGC', 'FontSize', 12);
        legend('show', 'Location', 'NorthWest');
    else
        p307a.XData = [p307a.XData, ms];
        p307a.YData = [p307a.YData, rssi_post_atten_dBm];
        p307b.XData = [p307b.XData, ms];
        p307b.YData = [p307b.YData, noise_power_post_agc_dBm];
        p307c.XData = [p307c.XData, ms];
        p307c.YData = [p307c.YData, rssi_post_agc_dBm];
    end
    
end
%}

%% --- Signal & Noise Power Post-A/D ---------------------------------------

% {
    %--- Signal & Noise Power Post-A/D -----------------------------------------
    % The reason that 'rssi_at_RF_dBm' is plotted is so
    % you can verify that the post-A/D RSSI is adjusted
    % properly with the gain, attenuation, ADC pseudo-gain,
    % and squared-volts-to-milliWatts conversion.
%     if (n ~= N)
%         rssi_at_RF_dBm_adjusted ...
%             = rssi_at_RF_dBm ...
%             - prog_attenuation_old ...
%             + agc_gain_old ...
%             + ADC_PSEUDO_GAIN_dB;
%     else
%         rssi_at_RF_dBm_adjusted ...
%             = rssi_at_RF_storage_vec ...
%             - prog_atten_storage_vector ...
%             + agc_gain_storage_vector ...
%             + ADC_PSEUDO_GAIN_dB;
%     end
    
    figure(308);
    if (n == 1) || (n == N)
        clf(308);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(308, 'Position', [2700 -235 560 420]);
            else
                % Dual monitor set-up
                set(308, 'Position', [2700 50 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x  = [0 ms];
            y1 = noise_power_post_adc_dB*[1 1];
            y2 = rssi_post_adc_dB*[1 1];
            y3 = rssi_post_iir_dB*[1 1];
            y4 = rssi_post_fft_dB*[1 1];
            if exist('rssi_from_fft_post_adc_dB','var')
                y2a = rssi_from_fft_post_adc_dB*[1 1];
            end
        else
            x  = t_vec;
            y1 = noise_power_post_adc_vec;
            y2 = rssi_post_adc_storage_vec;
            y3 = rssi_post_iir_storage_vec;
            y4 = rssi_post_fft_storage_vec;
            if exist('rssi_from_fft_post_adc_dB','var')
                y2a = rssi_from_fft_post_adc_vec;
            end
        end
        
        if exist('rssi_at_RF_dBm_adjusted','var')
            if (n == 1)
                p308 = plot(x, rssi_at_RF_dBm_adjusted*[1 1], 'g--', ...
                    'LineWidth', 3, 'DisplayName', '   Power at RF');
            else
                p308 = plot(x, rssi_at_RF_dBm_adjusted, 'g--', ...
                    'LineWidth', 3, 'DisplayName', '   Power at RF');
            end
        end
        
        p308a = plot(x, y1, 'k-o', 'MarkerSize', 3, 'LineWidth', 3, ...
            'DisplayName', ' Post-A/D Noise Power');
        p308b = plot(x, y2, 'r-', 'LineWidth', 2, ...
            'DisplayName', '     Post-A/D RSSI');
        if (IIR_FILTER_IS_ACTIVE == true)
            p308c = plot(x, y3, '-', 'LineWidth', 2, ...
                'DisplayName', '     Post-IIR RSSI');
        end
        if (FFT_FILTER_IS_ACTIVE == true)
            p308d = plot(x, y4, '-', 'LineWidth', 2, ...
                'DisplayName', '     Post-FFT RSSI');
        end
        if exist('rssi_from_fft_post_adc_dB','var')
            p308e = plot(x, y2a, '-', 'LineWidth', 2, ...
                'Color', [0.49 0.18 0.56], ...
                'DisplayName', '     RSSI from FFT');
        end
        
        xlim([0 ms_to_process]);
        switch data_source
            case 'GPS Simulator'
                ylim([0 80]);
            case 'Auger RF'
                ylim([30 75]);
        end
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Power [dBm]', 'FontSize', 12);
        title('Signal & Noise Power Post-A/D', 'FontSize', 12);
        legend('show', 'Location', 'NorthWest');
    else
        if exist('rssi_at_RF_dBm_adjusted','var')
            p308.XData = [p308.XData, ms];
            p308.YData = [p308.YData, rssi_at_RF_dBm_adjusted];
        end
        p308a.XData = [p308a.XData, ms];
        p308a.YData = [p308a.YData, noise_power_post_adc_dB];
        p308b.XData = [p308b.XData, ms];
        p308b.YData = [p308b.YData, rssi_post_adc_dB];
        if (IIR_FILTER_IS_ACTIVE == true)
            p308c.XData = [p308c.XData, ms];
            p308c.YData = [p308c.YData, rssi_post_iir_dB];
        end
        if (FFT_FILTER_IS_ACTIVE == true)
            p308d.XData = [p308d.XData, ms];
            p308d.YData = [p308d.YData, rssi_post_fft_dB];
        end
        if exist('p308e','var')
            p308e.XData = [p308e.XData, ms];
            p308e.YData = [p308e.YData, rssi_from_fft_post_adc_dB];
        end
    end
%}

%% --- J/N at RF and Output J/N --------------------------------------------

% {
    %--- J/N at RF and Output J/N ----------------------------------------------
    if (n ~= N)
        j2n_at_RF ...
            = rssi_at_RF_dBm ...
            - TRUE_NOISE_POWER_dBm;
        j2n_out ...
            = rssi_out_dB ...
            - ADC_PSEUDO_GAIN_dB ...
            - agc_gain_dB_old ...
            + prog_attenuation_dB_old ...
            - TRUE_NOISE_POWER_dBm;
    else
        j2n_at_RF ...
            = rssi_at_RF_storage_vec ...
            - TRUE_NOISE_POWER_dBm;
        j2n_out ...
            = rssi_out_storage_vec ...
            - ADC_PSEUDO_GAIN_dB ...
            - agc_gain_storage_vector ...
            + prog_atten_storage_vector ...
            - TRUE_NOISE_POWER_dBm;
    end
    
    figure(309);
    if (n == 1) || (n == N)
        clf(309);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(309, 'Position', [3265 -235 560 420]);
            else
                % Dual monitor set-up
                set(309, 'Position', [3265 50 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        if (n == 1)
            x  = [0 ms];
            y1 = j2n_at_RF*[1 1];
            y2 = j2n_out*[1 1];
        else
            x  = t_vec;
            y1 = j2n_at_RF;
            y2 = j2n_out;
        end
        
        p309a = plot(x, y1, 'k-o', 'MarkerSize', 3, 'LineWidth', 3, ...
            'DisplayName', '  J/N at RF');
        p309b = plot(x, y2, 'r-', 'LineWidth', 2, ...
            'DisplayName', ' Output J/N');
        
        xlim([0 ms_to_process]);
        ylim([-5 60]);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('J/N [dB]', 'FontSize', 12);
        title('J/N at RF and Output J/N', 'FontSize', 12);
        legend('show', 'Location', 'NorthWest');
    else
        p309a.XData = [p309a.XData, ms];
        p309a.YData = [p309a.YData, j2n_at_RF];
        p309b.XData = [p309b.XData, ms];
        p309b.YData = [p309b.YData, j2n_out];
    end
%}

%% --- RFI States & IIR Filters --------------------------------------------

% {
    if (n == N)
        figure(310);
        clf(310);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(310, 'Position', [2135 270 560 420]);
            else
                % Dual monitor set-up
                set(310, 'Position', [2135 560 560 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        for hh = 1:MAXSTATES
            freq       = rfi_states_freq(hh,:);
            start_freq = rfi_states_start_freq(hh,:);
            stop_freq  = rfi_states_stop_freq(hh,:);
            if any(isfinite(freq))
                p310 = plot(t_vec, freq/1e6, '-', 'LineWidth', 2, ...
                    'DisplayName', sprintf(' RFI State #%d', hh));
                plot(t_vec, start_freq/1e6, '-', 'LineWidth', 1, ...
                    'Color', p310.Color, 'HandleVisibility', 'off');
                plot(t_vec, stop_freq/1e6, '-', 'LineWidth', 1, ...
                    'Color', p310.Color, 'HandleVisibility', 'off');
%                 plot(t_vec, (start_freq + stop_freq)/(2*1e6), 'r--', ...
%                     'LineWidth', 3, 'HandleVisibility', 'off');
            end
        end
        
%         for kk = 1:N_IIR
%             freq = iir_filter_states_freq(kk,:);
%             if any(isfinite(freq))
%                 if (FB_IIR_FILTER_IS_ACTIVE == true) && (kk == 1)
%                     plot(t_vec, freq/1e6, '-', 'LineWidth', 2, ...
%                          'DisplayName', sprintf('  F/B IIR'));
%                 else
%                     plot(t_vec, freq/1e6, '-', 'LineWidth', 2, ...
%                          'DisplayName', sprintf('   IIR #%d', kk));
%                 end
%             end
%         end
        
        xlim([0 ms_to_process]);
        ylim([FFT_FREQ_VEC(1) FFT_FREQ_VEC(end)]/1e6);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Frequency [MHz]', 'FontSize', 12);
        title('RFI States & IIR Filters', 'FontSize', 12);
        legend('show', 'Location', 'NorthEast');
    end
%}

%% --- RFI State Tracking Info. --------------------------------------------

%{
    if (n == N)
        figure(311);
        clf(311);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(311, 'Position', [2700 -235 560 930]);
            else
                % Dual monitor set-up
                set(311, 'Position', [2700 50 560 930]);
            end
        end
        
        for hh = 1:MAXSTATES
            freq       = rfi_states_freq(hh,:);
            start_freq = rfi_states_start_freq(hh,:);
            stop_freq  = rfi_states_stop_freq(hh,:);
            j2n        = rfi_states_j2n(hh,:);
            
            subplot(3,1,1);
            box('on');
            grid('on');
            hold('on');
            
            if any(isfinite(freq))
                p311 = plot(t_vec, freq/1e6, '-', 'LineWidth', 2, ...
                    'DisplayName', sprintf(' Peak #%d', hh));
                plot(t_vec, start_freq/1e6, '-', 'LineWidth', 1, ...
                    'Color', p311.Color, 'HandleVisibility', 'off');
                plot(t_vec, stop_freq/1e6, '-', 'LineWidth', 1, ...
                    'Color', p311.Color, 'HandleVisibility', 'off');
                plot(t_vec, (start_freq + stop_freq)/(2*1e6), 'r--', ...
                    'LineWidth', 3, 'HandleVisibility', 'off');
            end
            
            xlim([0 ms_to_process]);
            ylim([FFT_FREQ_VEC(1) FFT_FREQ_VEC(end)]/1e6);
            ylabel('Frequency [MHz]', 'FontSize', 12);
            title('RFI State Tracking Info', 'FontSize', 12);
            legend('show', 'Location', 'NorthEast');
            
            subplot(3,1,2);
            box('on');
            grid('on');
            hold('on');
            
            if any(isfinite(freq))
                plot(t_vec, (stop_freq - start_freq)/1e6, '-', ...
                    'Color', p311.Color, 'LineWidth', 2, ...
                    'DisplayName', sprintf(' Peak #%d', hh));
            end
            
            xlim([0 ms_to_process]);
            ylim([0 10]);
            ylabel('Bandwidth [MHz]', 'FontSize', 12);
            legend('show', 'Location', 'NorthEast');
            
            subplot(3,1,3);
            box('on');
            grid('on');
            hold('on');
            
            if any(isfinite(freq))
                plot(t_vec, j2n, '-', ...
                    'Color', p311.Color, 'LineWidth', 2, ...
                    'DisplayName', sprintf(' Peak #%d', hh));
            end
            
            xlim([0 ms_to_process]);
            ylim([0 60]);
            xlabel('Time [ms]', 'FontSize', 12);
            ylabel('J/N [dB]', 'FontSize', 12);
            legend('show', 'Location', 'NorthEast');
        end
    end
%}

%% --- Filters in the Freq. Domain -----------------------------------------

% {
    figure(312);
    clf(312);
    if (size(monitor_positions,1) == 2)
        if all(monitor_positions(1,3:4) == [1280 800])
            % Laptop w/ secondary monitor
            set(312, 'Position', [2135 -235 560 420]);
        else
            % Dual monitor set-up
            set(312, 'Position', [2135 50 560 420]);
        end
    end
    
    box('on');
    grid('on');
    hold('on');
    
    if (FB_IIR_FILTER_IS_ACTIVE == true) || (IIR_FILTER_IS_ACTIVE == true)
        for kk = 1:length(iir_filter_states)
            if (iir_filter_states(kk).filter_is_active == true)
                ka = iir_filter_states(kk).ka;
                fj = iir_filter_states(kk).freq;
                z0 = exp(2*pi*1i*(fj-fi)/fs);
                
                b = (1 + ka)/2 * [1 -z0];
                a = [1 -ka*z0];
                
                [h,w] = freqz(b,a,512,'whole',fs);
                
                f = (w + fi - fs/2);
                y = 10*log10(fftshift(abs(h)));
                
                if (FB_IIR_FILTER_IS_ACTIVE == true) && (kk == 1)
                    plot(f/1e6, y, 'b--', 'LineWidth', 1, ...
                         'DisplayName', sprintf('     F/B IIR'));
                else
                    plot(f/1e6, y, 'b--', 'LineWidth', 1, ...
                         'DisplayName', sprintf('      IIR #%d', kk));
                end
            end
        end
    end
    
    if (FIR_FILTER_A_IS_ACTIVE == true)
        f = (-fir_filter_states(1).ntaps/2:fir_filter_states(1).ntaps/2-1) ...
            * (fs/fir_filter_states(1).ntaps) + fi;
        y = 10*log10(abs(fftshift(fft(fir_filter_states(1).W))));
        
        plot(f/1e6, y, 'r--', 'LineWidth', 1, ...
             'DisplayName', '      FIR ''A''');
    end
    
    if (FFT_FILTER_IS_ACTIVE == true)
        f = (-fft_filter_state.nfft/2:fft_filter_state.nfft/2-1) ...
            * (fs/fft_filter_state.nfft) + fi;
        
        % Re-map "true" values to -15 dB
        y1 = -15*fftshift(fft_filter_state.idx_gt_threshold);
        y2 = -15*fftshift(fft_filter_state.idx_to_blank);
        
        % Suppress plotting "false" values
        y1(y1 == 0) = NaN;
        
        plot(f/1e6, y1, 'k.', 'MarkerSize', 12, ...
             'HandleVisibility', 'off');
        plot(f/1e6, y2, 'k--', 'LineWidth', 1, ...
             'DisplayName', ' FFT Blanking');
    end
    
    if (FIR_FILTER_B_IS_ACTIVE == true)
        f = (-fir_filter_states(2).ntaps/2:fir_filter_states(2).ntaps/2-1) ...
            * (fs/fir_filter_states(2).ntaps) + fi;
        y = 10*log10(abs(fftshift(fft(fir_filter_states(2).W))));
        
        plot(f/1e6, y, 'r--', 'LineWidth', 1, ...
             'DisplayName', '      FIR ''B''');
    end
    
    xlim([FFT_FREQ_VEC(1) FFT_FREQ_VEC(end)]/1e6);
    ylim([-20 5]);
    xlabel('Frequency [MHz]', 'FontSize', 12);
    ylabel('Magnitude [dB]', 'FontSize', 12);
    title('Filters in the Freq. Domain', 'FontSize', 12);
    legend('show', 'Location', 'East');
%}

%% --- FFT Filter in the Time Domain ---------------------------------------

%{
if (FFT_FILTER_IS_ACTIVE == true)
    if (n == N)
        figure(313);
        clf(313);
        if (size(monitor_positions,1) == 2)
            if all(monitor_positions(1,3:4) == [1280 800])
                % Laptop w/ secondary monitor
                set(313, 'Position', [2135 -235 1690 420]);
            else
                % Dual monitor set-up
                set(313, 'Position', [2135 50 1690 420]);
            end
        end
        
        box('on');
        grid('on');
        hold('on');
        
        plot(t_vec, fft_filter_state_n_blanked, 'b-', 'LineWidth', 2);
        
        xlim([0 ms_to_process]);
        ylim([0 4*NREAD]);
        xlabel('Time [ms]', 'FontSize', 12);
        ylabel('Blanking [# of bins]', 'FontSize', 12);
        title('FFT Filter in the Time Domain', 'FontSize', 12);
    end
end
%}

%% --- drawnow() -----------------------------------------------------------

drawnow();

%% -----------------------------------------------------------------------------
return
%===============================================================================