%% Bluetooth BR RF-PHY Transmitter Tests for Modulation Characteristics, Carrier Frequency Offset, and Drift
% This example shows how to perform Bluetooth® basic rate (BR) radio frequency (RF) physical layer (PHY) transmitter tests specific to modulation characteristics, carrier frequency offset, and drift using the 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 Bluetooth RF-PHY Tests
% The Bluetooth RF-PHY Test Specifications [1] defined by the Bluetooth Special Interest Group (SIG) include RF-PHY tests for transmitters and receivers. 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. The expected outcome must be met by the implementation under test (IUT).
%% RF-PHY Transmitter Tests
% The main goal of the transmitter test measurements is to ensure that the transmitter characteristics are within the limits specified by the Bluetooth RF-PHY 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.
%
%% RF-PHY Transmitter Test Procedure
% This block diagram summarizes the test procedure for transmitter tests relevant to modulation characteristics, carrier frequency offset, and drift.
%
% Generate DH/DM packets and pass through the bluetoothWaveformGenerator.m function to generate Bluetooth test waveforms. This table shows the test waveforms and packet type(s) required for different test IDs:
%
%% Check for Support Package Installation
% Check if the 'Communications Toolbox Library for the Bluetooth Protocol' support package is installed.

commSupportPackageCheck('BLUETOOTH');

%
%% Configure Simulation Parameters
% You can change the txTestID, packetType, initFreqOffset, maxFreqDrift and sps parameters based on the transmitter test, packet type, initial frequency offset, maximum frequency drift, and samples per symbol, respectively.

txTestID = 'RF/TRM/CA/BV-07-C';
packetType = 'DH1';             % Select packet type as per transmitter test case ID
initFreqOffset =0; % Initial frequency offset in Hz
maxFreqDrift =0;   % Maximum frequency drift in Hz, [-25e3, 25e3] for one slot packet
                                               % [-40e3, 40e3] for three and five slot packets
sps = 32;                                      % Minimum of 4 samples per symbol as per test specifications

%
%% Generate Test Parameters
% Test parameters are generated based on transmitter test, packet type, initial frequency offset, maximum frequency drift and samples per symbol. To generate payload, packet duration, and waveform configuration parameters, use helperBRModulationTestPacketConfig.m function. To design channel filter based on the sample rate, use helperModulationTestFilterDesign.m function. To add frequency offset and thermal noise, create and configurecomm.PhaseFrequencyOffset and comm.ThermalNoise System objects, respectively.

[payload,packetDuration,cfg] = helperBRModulationTestPacketConfig(txTestID,packetType,sps);
filtDesign = helperModulationTestFilterDesign('BR',sps); % Design channel filter
driftRate = maxFreqDrift/(packetDuration*sps);           % Drift rate
freqDrift = driftRate*(0:1:(packetDuration*sps-1))';     % Frequency drift for the packet
freqOffset = freqDrift + initFreqOffset;                 % Frequency offset, includes initial frequency offset and drift

% Create a phase frequency offset System object
sampleRate = sps*1e6;                                    % Sample rate in Hz
pfo = comm.PhaseFrequencyOffset('FrequencyOffset',freqOffset,'SampleRate',sampleRate);

% Create a thermal noise System object
NF = 12;                                                 % Noise figure (dB)
thNoise = comm.ThermalNoise('NoiseMethod','Noise figure', ...
                            'SampleRate',sampleRate, ...
                            'NoiseFigure',NF);

%
%% Simulate Transmitter Tests
% To simulate the transmitter tests, follow these steps:
% Generate a Bluetooth BR waveform for the selected packet type and waveform configuration.
% Add frequency offset, which includes initial frequency offset and drift to the waveform.
% Add noise to the waveform.
% Perform filtering on the noisy waveform.
% Perform FM demodulation on the filtered waveform.
% Perform test measurements and display the pass verdict.

filtWaveform = zeros(packetDuration*sps,size(payload,2)); % Initialization
for i = 1:size(payload,2)
    txWaveform = bluetoothWaveformGenerator(payload(:,i),cfg);
    txWaveformValid = txWaveform(1:packetDuration*sps);
    wfmFreqOffset = pfo(txWaveformValid);
    wfmChannel = thNoise(wfmFreqOffset);
    filtWaveform(:,i) = conv(wfmChannel,filtDesign.Coefficients.','same');
end

%
% Based on the transmitter test, the helperBRModulationTestMeasurements.m function performs FM demodulation and returns these values:
% RF/TRM/CA/BV-07-C: Returns the frequency deviations and center frequencies for the two test sequences, freq1, freq2, respectively and maximum frequency deviation for the second test sequence, freq3.
% RF/TRM/CA/BV-08-C: Returns the initial frequency offset, freq1.
% RF/TRM/CA/BV-09-C: Returns the initial frequency offset and frequency drift, freq1 and freq2, respectively.

[waveform,freq1,freq2,freq3] = helperBRModulationTestMeasurements(filtWaveform,txTestID,sps,packetType);

%
% The helperBRModulationTestVerdict.m function verifies whether the test measurements are within the specified limits and displays the verdict.

helperBRModulationTestVerdict(waveform,txTestID,sps,freq1,freq2,freq3)

%
% This example demonstrates the Bluetooth BR 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 example uses these helpers:
% helperBRModulationTestPacketConfig.m: Configures Bluetooth transmitter test parameters
% helperModulationTestFilterDesign.m: Designs channel filter
% helperBRModulationTestMeasurements.m: Measures frequency deviation, carrier frequency offset and drift
% helperModulationCharacteristicsTest.m: Performs modulation characteristics test
% helperBRModulationTestVerdict.m: Validates test measurement values and displays the result
% helperBluetoothPacketDuration.m: Returns the packet duration
%% Selected Bibliography
% Bluetooth Special Interest Group (SIG). “Bluetooth RF-PHY Test Specification”, v1.2/2.0/2.0, EDR/2.1/2.1, EDR/3.0/3.0, HS (), RF.TS/3.0.H.1, Section 4.5. 2009. https://www.bluetooth.com.
% Bluetooth Special Interest Group (SIG). "Core System Package [BR/EDR Controller Volume]". Bluetooth Core Specification. Version 5.2, Volume 2. https://www.bluetooth.com.
% Copyright 2020 The MathWorks, Inc.