%% BLE Modulation Characteristics, Carrier Frequency Offset and Drift Test Measurements
% This example shows how to perform Bluetooth® low energy (BLE) radio frequency (RF) physical layer (PHY) transmitter tests specific to modulation characteristics, carrier frequency offset, and drift using Communications Toolbox™ Library for the Bluetooth Protocol. The test measurements compute frequency deviation, carrier frequency offset, and drift values. This example also verifies whether these test measurement values are within the limits specified by the Bluetooth RF-PHY Test Specifications [ 1 ].
%% Objectives of BLE RF-PHY Tests
% The Bluetooth RF-PHY Test Specifications [ 1 ] defined by Bluetooth Special Interest Group (SIG) includes RF-PHY tests for both transmitter and receiver. The objectives of these RF-PHY tests are to:
% Ensure interoperability between all Bluetooth devices.
% Ensure a basic level of system performance for all Bluetooth products.
%  Each test case has a specified test procedure and an expected outcome, which must be met by the implementation under test (IUT).
%% RF-PHY Transmitter Tests
% The main aim of transmitter test measurements is to ensure that the transmitter characteristics are within the specified limits as specified in the test specifications [ 1 ]. This example includes transmitter tests relevant to modulation characteristics, carrier frequency offset, and drift. This table shows various RF-PHY transmitter tests performed in this example.
%
%% Transmitter Test Procedure
% This block diagram summarizes the test procedure for transmitter tests relevant to modulation characteristics, carrier frequency offset, and drift.
%
% Generate test packets and pass them through bleWaveformGenerator to generate BLE test waveforms. The test waveforms required for different test IDs are:
%
%% Check for Support Package Installation
% Check if the 'Communications Toolbox Library for the Bluetooth Protocol' support package is installed.

commSupportPackageCheck('BLUETOOTH');

%% Configure the Test Parameters
% Initialize variables such as transmitter test ID, number of samples per symbol, payload length, and maximum carrier frequency drift. The function,helperBLEModulationTestConfig.m, can be configured to generate test parameters.

txTestID = "RF-PHY/TRM/BV-09-C";
payloadLen = 240;    % Payload length in bytes, must be in the range [37,255]
sps = 32;           % Number of samples per symbol, minimum of 32 samples per
                    % symbol as per the test specifications

% Frequency offset and drift for the tests: RF-PHY/TRM/BV-06-C,
% RF-PHY/TRM/BV-12-C, RF-PHY/TRM/BV-14-C.
maxFreqDrift = 0;     % In Hz, must be in the range [-50e3,50e3]
initFreqOffset = 23000;   % In Hz, must be in the range [-100e3,100e3]
testParams = helperBLEModulationTestConfig(txTestID,sps); % Generate test parameters

%
%% Simulate Transmitter Tests
% To simulate the transmitter tests, perform these steps:
% Generate BLE test packet waveform using  helperBLETestWaveform.
% Add frequency offset, which includes initial frequency offset, and drift to the waveform using comm.PhaseFrequencyOffset.
% Add thermal noise using  comm.ThermalNoise.
% Perform filtering on the noisy waveform using helperModulationTestFilterDesign.
% Perform FM demodulation on the filtered waveform.
% Perform test measurement and display the pass verdict.

testWfmLen = (testParams.nonPDULen+testParams.codingFactor*payloadLen*testParams.bitsPerByte)*sps;
driftRate = maxFreqDrift/length(testWfmLen);% Drift rate
freqDrift = driftRate*(0:1:(length(testWfmLen)-1))';% Frequency drift
freqOffset = freqDrift+initFreqOffset;% Frequency offset and frequency drift
% Create a phase frequency offset System object
pfo = comm.PhaseFrequencyOffset('FrequencyOffset',freqOffset,'SampleRate',testParams.sampleRate);
% Create a thermal noise System object
NF = 12; % Noise figure (dB)
thNoise = comm.ThermalNoise('NoiseMethod','Noise figure',...
                            'SampleRate',testParams.sampleRate,...
                            'NoiseFigure',NF);
filtDesign = helperModulationTestFilterDesign(testParams.phyMode,sps);
filtTestWfm = zeros(testWfmLen,testParams.numOfTestSeqs);
for wfmIdx = 1:testParams.numOfTestSeqs
    % Generate BLE test waveforms
    testWfm = helperBLETestWaveform(testParams.testSeqIds(wfmIdx),...
                                        payloadLen,sps,testParams.phyMode);
    wfmFreqOffset = pfo(testWfm);
    wfmChannel = thNoise(wfmFreqOffset);
    filtTestWfm(:,wfmIdx) =  conv(wfmChannel,filtDesign.Coefficients.','same'); % Perform filtering
end

% The function, helperBLEModulationTestMeasurements.m, performs FM demodulation and computes either frequency deviation, or frequency drift and initial frequency offset based on the provided test case ID.

[waveformDiffFreq,fOut1,fOut2,fOut3] = helperBLEModulationTestMeasurements(filtTestWfm,txTestID,testParams);

% The function, helperBLEModulationTestVerdict.m, verifies whether the measurements are within the specified limits, and displays the verdict on the command window.

helperBLEModulationTestVerdict(waveformDiffFreq,txTestID,testParams,fOut1,fOut2,fOut3)

%
% This example demonstrated the BLE transmitter test measurements specific to modulation characteristics, carrier frequency offset and, drift. The simulation results verify that these computed test measurement values are within the limits specified by Bluetooth RF-PHY Test  Specifications [ 1 ].
%% Appendix
% The helpers used in this example are:
% helperBLETestWaveform.m: Generates BLE test packet waveform
% helperBLEModulationTestConfig.m: Configures BLE transmitter test parameters
% helperBLEModulationTestMeasurements.m: Measures frequency deviation, carrier frequency offset and drift
% helperBLEModulationTestVerdict.m: Validates test measurement values and displays the result
% helperModulationCharacteristicsTest.m: Performs modulation characteristics test
% helperModulationTestFilterDesign.m: Designs channel filter
%% Selected Bibliography
% Bluetooth Special Interest Group (SIG). “Bluetooth RF-PHY Test Specification”, Revision: RF-PHY.TS.5.1.0, Section 4.4. 2018. https://www.bluetooth.com.
% Bluetooth Special Interest Group (SIG). "Bluetooth Core Specification". Version 5.2. https://www.bluetooth.com.
% Copyright 2019-2020 The MathWorks, Inc.