function helperBLEModulationTestVerdict(testWfm,txTestID,testParams,fOut1,fOut2,fOut3)
%helperBLEModulationTestVerdict displays the BLE test verdict for each BLE
%transmitter test corresponding to modulation characteristics, carrier
%frequency offset and drift.
%   helperBLEModulationTestVerdict(TESTWFM,TXTESTID,TESTPARAMS,FOUT1,...
%   FOUT2,FOUT3) plots frequency measurements and displays the BLE test
%   verdict on command window.
%   
%   TESTWFM, is a column vector containing FM demodulated waveform.
%   
%   TXTESTID is a character vector or string specifying the transmitter
%   test ID. It must be one of the following:
%   {'RF-PHY/TRM/BV-09-C','RF-PHY/TRM/BV-11-C',
%    'RF-PHY/TRM/BV-13-C','RF-PHY/TRM/BV-06-C',
%    'RF-PHY/TRM/BV-12-C','RF-PHY/TRM/BV-14-C'}.
%   
%   TESTPARAMS, a structure containing the following test parameters:
%   SPS             - Samples per symbol
%   SYMBOLRATE      - Symbol rate
%   BITSPERBYTE     - Bits per byte
%   PHYMODE         - Physical layer transmission mode
%   TESTSEQIDS      - Type of test sequence(s)
%   TESTSEQID       - Test sequence
%   NONPDULEN       - Length of BLE packet excluding PDU
%   DISCARDSTARTLEN - Number of bits to discard at the start of the packet
%   DISCARDENDLEN   - Number of bits to discard at the end of the packet
%   PREAMBLELEN     - Length of the preamble
%   CODINGFACTOR    - FEC coding scheme
%   NUMOFTESTSEQS   - Number of test sequence
%   SAMPLERATE      - Sampling rate
%     
%   For test IDs: {'RF-PHY/TRM/BV-09-C','RF-PHY/TRM/BV-11-C','RF-PHY/TRM/BV-13-C'}
%             FOUT1, Measured average frequency deviation(s) for the test
%             sequences.
%             FOUT2, Measured average sequence center frequencies for the
%             test sequences.
%             FOUT3, Measured maximum frequency deviation for the second
%             test sequence for 'RF-PHY/TRM/BV-09-C', 'RF-PHY/TRM/BV-11-C'
%             and first test sequence for 'RF-PHY/TRM/BV-13-C'.
%
%   For test IDs: {'RF-PHY/TRM/BV-06-C','RF-PHY/TRM/BV-12-C','RF-PHY/TRM/BV-14-C'}
%             FOUT1, Measured initial frequency offset.
%             FOUT2, Measured carrier frequency drift.

%   Copyright 2019-2020 The MathWorks, Inc.

testSeqId = testParams.testSeqId;
symbolRate = testParams.symbolRate;
sps = testParams.sps;
if any(strcmp(txTestID,{'RF-PHY/TRM/BV-09-C','RF-PHY/TRM/BV-11-C','RF-PHY/TRM/BV-13-C'}))
    avgFreqDev = fOut1;
    centerFreq = fOut2;
    numSamples = sps*20;
    t = (1:1:numSamples)/(sps);
    figure,
    % Plot frequency of the demodulated waveform
    for ind = 1:testParams.numOfTestSeqs
        seqCF = repmat(centerFreq(:,ind).',testParams.bitsPerByte*sps,1);
        seqCFRep = seqCF(:);
        subplot(testParams.numOfTestSeqs,1,ind);
        plot(t,testWfm(1:numSamples,ind),'r')
        hold on
        plot(t,ones(numSamples,1)*avgFreqDev(ind),'b')
        plot(t,seqCFRep(1:numSamples),'k')
        title(['Frequency Demodulated Waveform', newline,' for Sequence: ',testSeqId(ind,:)])
        legend({'FM Demodulated Waveform','Average Frequency Deviation','Sequence Center Frequency'})
        xlabel('Samples')
        ylabel('Frequency (in Hz)')
    end    
    % Test measurement limits as per test specifications
    if strcmp(txTestID,'RF-PHY/TRM/BV-13-C')
        lim1 = 225*symbolRate/1e6;
        lim2 = 275*symbolRate/1e6;
    else
        lim1 = 247.5*symbolRate/1e6;
        lim2 = 252.5*symbolRate/1e6;
    end
    % Message display on command window
    disp(['Test sequence: ', testSeqId(1,:)]);
    disp (['    Measured average frequency deviation = ', num2str(avgFreqDev(1)/1e3),' kHz']);
    disp (['    Expected average frequency deviation = ', num2str(lim1), ' kHz to ', num2str(lim2), ' kHz']);
    if ((avgFreqDev(1)/1e3 > lim1) && (avgFreqDev(1)/1e3 < lim2))
        disp('    Result: Pass');
    else
        disp('    Result: Fail');
    end
    if testParams.numOfTestSeqs == 2
    disp(['Test sequence: ', testSeqId(2,:)]);
    disp (['    Expected 99.9% of all maximum frequency deviation > ',num2str(185e3*(1+strcmp(testParams.phyMode,'LE2M'))), ' kHz']);
    if (all(fOut3 > 185e3*(1+strcmp(testParams.phyMode,'LE2M'))))
        disp('    Result: Pass');
    else
        disp('    Result: Fail');
    end
    disp(['Ratio of frequency deviations between two test sequences = ', num2str(avgFreqDev(2)/avgFreqDev(1))]);
    disp('Expected Ratio > 0.8 ');
    if (avgFreqDev(2)/avgFreqDev(1) > 0.8)
        disp('    Result: Pass');
    else
        disp('    Result: Fail');
    end
    else
        disp (['    Expected 99.9% of all maximum frequency deviation > ',num2str(185e3), ' kHz']);
    if (all(fOut3 > 185e3))
        disp('    Result: Pass');
    else
        disp('    Result: Fail');
    end
    end
else
    intervalDuration = testParams.intervalDuration;    
    preambleLen = testParams.preambleLen;
    discardStartLen = testParams.discardStartLen;
    f0 = fOut1;
    f = fOut2;
    figure,
    % Plot frequency of the demodulated waveform
    plot(testWfm);
    hold on
    fPayloadLen = intervalDuration*sps;
    fPayload = rectpulse(f,fPayloadLen);
    fPreamble = rectpulse(f0,testParams.preambleLen);
    fGuardLen = discardStartLen*sps-preambleLen;
    plotfOut = [fPreamble; zeros(fGuardLen,1); fPayload];
    plot(plotfOut,'k');
    plot(zeros(size(plotfOut)),'r'); % Reference line
    title(['Frequency Demodulated Waveform',newline,' for Sequence: ',testSeqId(1,:)])
    legend({'FM Demodulated Waveform','Frequency Drift','Average Center Frequency'})
    xlabel('Samples')
    ylabel('Frequency (in Hz)')

    % Display frequency offset results
    disp('Initial frequency offset: ')
    disp(['    Measured value: ',num2str(f0/1e3),' kHz.'])
    disp('    Expected range: [-150 kHz, 150 kHz].')
    if (((f0/1e3)>150) || ((f0/1e3)<-150))
        disp(['    Result: Fail' newline]);
    else
        disp(['    Result: Pass' newline]);
    end
    % Display frequency drift results
    disp('Frequency drift: ')
    fResult1 = abs(f(2:end)-f0)<=50e3;
    if ~any(fResult1==0)
        disp('    Measured frequency drift over each interval <= 50 kHz');
        disp(['    Result: Pass' newline]);
    else
        disp('    Measured frequency drift over each interval > 50 kHz');
        disp(['    Result: Fail' newline]);
    end
    if strcmp(txTestID,'RF-PHY/TRM/BV-14-C')        
        fResult2 = abs(f(3)-f0)<=19.2e3;
        fResult3 = abs(f(7:end)-f(4:end-3));
        if (any(fResult3<=19.2e3)&& (fResult2))
            disp('    Measured frequency drift over 3 intervals <= 19.2 kHz');
            disp(['    Result: Pass' newline]);            
        else
            disp('    Measured frequency drift over 3 intervals > 19.2 kHz');
            disp(['    Result: Fail' newline]);
        end
    else
        fResult2 = abs(f(1)-f0)<=23e3;
        if fResult2
            disp('    Measured frequency drift for first interval <= 23 kHz');
            disp(['    Result: Pass' newline]);  
        else
            disp('    Measured frequency drift for first interval > 23 kHz');
            disp(['    Result: Fail' newline]);
        end
        fResult3 = abs(f(6:end)-f(1:end-5));
        if any(fResult3<=20e3)
            disp('    Measured frequency drift over 5 intervals <= 20 kHz');
            disp(['    Result: Pass' newline]);  
        else
            disp('    Measured frequency drift over 5 intervals > 20 kHz');
            disp(['    Result: Fail' newline]);
        end
    end
end