%% 802.11p Spectral Emission Mask Testing
% This example shows how to perform spectrum emission mask tests for an IEEE® 802.11p™ transmitted waveform.
%% Introduction
% IEEE 802.11p is an approved amendment to the IEEE 802.11™ standard to enable support for wireless access in vehicular environments (WAVE). Using the half-clocked mode with a 10 MHz channel bandwidth, it operates at the 5.85-5.925 GHz bands, for which it defines additional spectral emission masks.
% This example shows how to perform spectral mask measurements on a waveform generated by using WLAN Toolbox™ software. Alternatively, you can perform this measurement on a waveform captured with a spectrum analyzer.
% The example generates a waveform consisting of three 10 MHz IEEE 802.11p packets separated by a 32 microsecond gap. Each packet contains random data and uses 16-QAM. The example oversamples the waveform using a larger IFFT than required for the nominal baseband rate and does not perform spectral filtering, and uses a high-power amplifier (HPA) model, which introduces in-band distortion and spectral regrowth. The example performs spectral emission mask measurement on the upsampled waveform after HPA modeling. This diagram shows the test setup.
%
%
%% Non-HT Packet Configuration
% Set the non-high-throughput (non-HT) transmission parameters by using the wlanNonHTConfig object, specifying 10 MHz bandwidth operation as used by IEEE 802.11p.

cfgNHT = wlanNonHTConfig;          % Create packet configuration
cfgNHT.ChannelBandwidth = 'CBW10'; % 10 MHz
cfgNHT.MCS = 4;                    % Modulation 16QAM, rate-1/2
cfgNHT.PSDULength = 1000;          % PSDU length in bytes

%
%% Waveform Generation
% This section configures and generates a waveform containing three packets with an idle time of 32 microseconds between each packet.
% To model the effect of a HPA on the waveform and view the out-of-band spectral emissions, the waveform must be oversampled. This example generates the waveform using a larger IFFT than required for the nominal baseband rate, resulting in an oversampled waveform. The example does not perform spectral filtering.
% Set random stream for repeatability of results, specify an oversampling factor, and generate random data of the required PSDU length.

s = rng(98765);
osf = 3;
idleTime = 32e-6;
numPackets = 3;
data = randi([0 1],cfgNHT.PSDULength*8*numPackets,1);

% Generate the multi-packet waveform and calculate the nominal baseband sample rate.

genWaveform = wlanWaveformGenerator(data,cfgNHT, ...
                OversamplingFactor=osf, ...
                NumPackets=numPackets,...
                IdleTime=idleTime);
fs = wlanSampleRate(cfgNHT);

%
%% HPA Modeling
% The HPA introduces nonlinear behavior in the form of inband distortion and spectral regrowth. This example simulates the power amplifiers by using the Rapp model in 802.11ac, which introduces AM/AM distortion.
% Model the amplifier by using  comm.MemorylessNonlinearity object, and configure reduced distortion by specifying a back-off, hpaBackoff, such that the amplifier operates below its saturation point. You can increase the backoff to reduce EVM for higher MCS values.

pSaturation = 25; % Saturation power of a power amplifier in dBm 
hpaBackoff = 16; % dB

% Create and configure a memoryless nonlinearity to model the amplifier.

nonLinearity = comm.MemorylessNonlinearity;
nonLinearity.Method = 'Rapp model';
nonLinearity.Smoothness = 3; % p parameter
nonLinearity.LinearGain = -hpaBackoff; % dB
nonLinearity.OutputSaturationLevel = db2mag(pSaturation-30);

% Apply the model to the transmit waveform.

txWaveform = nonLinearity(genWaveform);

%
%% Transmit Spectrum Emission Mask Measurement
% This section performs the spectral emission mask test non-HT Data field.
% The IEEE 802.11p standard classifies stations according to the allowed maximum transmit powers (in mW). For the four different classes of stations, the standard defines four different spectral emission masks, and defines the spectral mask limits relative to the peak power spectral density (PSD). This example measures the spectrum emission mask for a Class A station.

% IEEE Std 802.11-2016 Annex D.2.3, Table D-5: Class A STA
dBrLimits = [-40  -40 -28 -20  -10 0   0  -10 -20 -28 -40 -40];  
fLimits   = [-Inf -15 -10 -5.5 -5 -4.5 4.5 5  5.5  10  15 Inf];

% Extract the non-HT Data field of each packet from the oversampled txWaveform by using the start index of each packet and concatenate the extracted non-HT Data fields in preparation for measurement.

ind = wlanFieldIndices(cfgNHT, OversamplingFactor=osf);
startIdx = ind.NonHTData(1); % Start of non-HT Data
endIdx = ind.NonHTData(2); % End of non-HT Data
idleNSamps = idleTime*fs*osf; % Idle time samples
perPktLength = endIdx+idleNSamps;

idx = zeros(endIdx-startIdx+1, numPackets);
for i = 1:numPackets
    % Start of packet in txWaveform, accounting for the filter delay
    pktOffset = (i-1)*perPktLength;
    % Indices of non-HT Data in txWaveform
    idx(:,i) = pktOffset+(startIdx:endIdx);
end
% Select the Data field for the individual packets
gatedNHTDataTx = txWaveform(idx(:), :);

% The helperSpectralMaskTest function overlays the required spectral mask with the measured PSD and checks that the transmitted PSD levels is within the specified mask levels, displaying the result as a pass or fail.
% Evaluate PSD and test for compliance.

helperSpectralMaskTest(gatedNHTDataTx,fs,osf,dBrLimits,fLimits);

% Restore the default random stream.

rng(s);

%
%% Conclusion and Further Exploration
% This example shows how to measure the transmit spectral mask for Class A Stations at the 5.85-5.925 GHz bands for a 10 MHz channel spacing, and how to ensure that the peak spectral density of the transmitted signal falls within the spectral mask to satisfy regulatory restrictions. You can generate a similar result for a 5 MHz channel spacing.
% The HPA model affects the out-of-band emissions in the spectral mask plot. For different station classes with higher relative dB values, try increasing the backoff for lower emissions.
% For information on other transmitter measurements like modulation accuracy and spectral flatness, and other formats, refer to the following examples:
% 802.11be 4096-QAM 320 MHz Waveform Generation and Analysis
% 802.11ba WUR Waveform Generation and Analysis
% 802.11ad Transmitter Spectral Emission Mask Testing
% 802.11ac Transmitter Modulation Accuracy and Spectral Emission Testing
% Recover and Analyze Packets in 802.11 Waveform
%% Selected Bibliography
% IEEE Std 802.11-2016: IEEE Standard for Information technology - Telecommunications and information exchange between systems - Local and metropolitan area networks - Specific requirements, Part 11: Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications, IEEE, New York, NY, USA, 1999-2016.
% IEEE Std 802.11p-2010: IEEE Standard for Information technology - Telecommunications and information exchange between systems - Local and metropolitan area networks - Specific requirements, Part 11: Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications, Amendment 6: Wireless Access in Vehicular Environments, IEEE, New York, NY, USA, 2010.
% Archambault, Jerry, and Shravan Surineni. "IEEE 802.11 spectral measurements using vector signal analyzers." RF Design 27.6 (2004): 38-49.
% Copyright 2015-2021 The MathWorks, Inc.
%