function [blockRMSDEVM,symRMSDEVM,blockFreqDrifts]...
          = helperEDRModulationTestMeasurements(receivedEDRSymbols,...
          IdealEDRSymbols,edrTestParams)
%helperEDRModulationTestMeasurements Returns EDR modulation accuracy
%and carrier frequency stability test measurements.
%   [BLOCKRMSDEVM,SYMRMSDEVM,BLOCKFREQDRIFTS] = ...
%   helperEDRModulationTestMeasurements(RECIEVEDEDRSYMBOLS,...
%   IDEALEDRSYMBOLS,EDRTESTPARAMS) returns EDR modulation accuracy and
%   carrier frequency stability test measurements based on the received EDR
%   symbols, RECEIVEDEDRSYMBOLS, ideal EDR symbols, IDEALEDRSYMBOLS, and
%   test parameters, EDRTESTPARAMS.
%
%   BLOCKRMSDEVM is a array containing all the RMS DEVM values at block
%   level.
%
%   SYMRMSDEVM is a array containing all the RMS DEVM values at symbol
%   level.
%
%   BLOCKFREQDRIFTS is a array containing all the frequency drifts
%   estimated for each block.

%   Copyright 2020 The MathWorks, Inc.

QsequenceTemp = receivedEDRSymbols.*conj(IdealEDRSymbols);
EsequenceTemp = QsequenceTemp(1:end-1)-QsequenceTemp(2:end);
% Divide entire waveform in to 50 microsecond block
numOfBlocks = floor(length(QsequenceTemp)/edrTestParams.blockLength);
Gsequence = QsequenceTemp(1:numOfBlocks*edrTestParams.blockLength);
Esequence = EsequenceTemp(1:numOfBlocks*edrTestParams.blockLength);
symRMSDEVM = zeros(1,edrTestParams.numDEVMBlocks*edrTestParams.blockLength);
totalCounts = 1;
for iBlock = 1:edrTestParams.numDEVMBlocks
    block1 = Gsequence((edrTestParams.blockLength*(iBlock-1))+1:(edrTestParams.blockLength*iBlock));
    block2 = Esequence((edrTestParams.blockLength*(iBlock-1))+1:(edrTestParams.blockLength*iBlock));
    % Compute sampling frequency error for each block
    samplingFrequencies = (((diff(unwrap(angle(block1))))./(2*pi))*edrTestParams.symbolRate);
    blockFreqDrifts(iBlock) = samplingFrequencies(end)-samplingFrequencies(1);
    % Compute RMS DEVM at block level
    abs1 = sum((abs(block2)).^2);
    abs2 = sum((abs(block1)).^2);
    blockRMSDEVM(iBlock) = sqrt(abs1/abs2);
    % Compute Peak DEVM at block level
    num = (abs(block2)).^2;
    den = abs2/edrTestParams.blockLength;
    devms = sqrt(num./den);
    symRMSDEVM((edrTestParams.blockLength*(iBlock-1))+1:...
        (edrTestParams.blockLength*iBlock)) = devms;
    totalCounts = totalCounts+1;
end
end