%% Modeling and Testing an 802.11ax RF Transmitter
% This example shows how to characterize the impact of RF
% impairments in an 802.11ax transmitter. The example generates a
% baseband IEEE(R) 802.11ax(TM) waveform by using WLAN Toolbox(TM) and models the RF
% transmitter by using RF Blockset(TM).
%
% Copyright 2020-2021 The MathWorks, Inc.

%% Introduction
% This example characterizes the impact of RF impairments such as in-phase
% and quadrature (IQ) imbalance, phase noise, and power amplifier (PA)
% nonlinearities on the transmission of an 802.11ax waveform. To evaluate
% the impact of these impairments, the example performs these measurements:
%
% * Error vector magnitude (EVM): vector difference at a given time between
% the ideal (transmitted) signal and the measured (received) signal
% * Spectral mask: test that ensures a transmission in one channel does not
% cause substantial interference in adjacent channels
% * Occupied bandwidth: bandwidth that contains 99% of the total integrated 
% power of the signal, centered on the assigned channel frequency
% * Channel power: filtered mean power centered on the assigned channel
% frequency
% * Complementary cumulative distribution function (CCDF): probability that
% the signal's instantaneous power is at a specified level above its average
% power
% * Peak-to-average power ratio (PAPR): relation between the peak power of
% the signal and its average power
%
% The example works on a packet-by-packet basis and uses a Simulink model
% to perform these steps:
%
% # Generate the baseband 802.11ax waveform by using WLAN Toolbox features.
% # Oversample and filter the waveform by using an FIR Interpolation block.
% # Import the baseband waveform into the RF Transmitter Subsystem block
% implemented by using RF Blockset blocks. The model uses an RF intermediate
% frequency to carry the baseband information in RF Blockset.
% # Model the effects of upconverting the waveform to the carrier frequency
% by using the RF Transmitter Subsystem block. This block models the
% impairments introduced by an RF transmitter using RF Blockset blocks.
% # Calculate the occupied bandwidth and channel power and depict the
% spectral mask by using the Spectrum Analyzer block.
% # Compute the CCDF and PAPR by using the CCDF and PAPR block.
% # Downsample and filter the waveform by using an FIR Decimation block.
% # Extract the data symbols and measure the EVM by demodulating the
% baseband waveform.
%
% The Simulink model uses WLAN Toolbox and DSP System Toolbox(TM) features to
% process the baseband signal (steps 1, 2, and 5-8) and uses RF Blockset
% blocks to model the RF transmitter (steps 3 and 4). This model supports
% Normal and Accelerator simulation modes.

%% Simulink Model Structure
% The model contains three main parts:
%
% * Baseband Waveform Generation: generates the baseband 802.11ax waveforms
% * RF Transmission: models the effects of upconverting the waveform to the
% carrier frequency
% * Baseband Waveform Reception: performs the RF measurements and
% calculates EVM by demodulating the baseband waveform
%
modelName = 'HERFTransmitterModel';
open_system(modelName);

%% Baseband Waveform Generation
% The WLAN 802.11ax block generates standard-compliant high-efficiency
% single-user (HE SU) waveforms, according to IEEE P802.11ax/D7.0. You can
% generate this block using the
% *<docid:wlan_ref#mw_app_wlanWaveformGenerator WLAN Waveform Generator>*
% app. You can access the waveform configuration parameters in the user
% data of the block. This example uses the
% *InitFcn* in the *Model callbacks* to store the structure available in
% the user data in a Base Workspace variable, |HEInfo|. For more
% information about this block, see
% <docid:wlan_ref#mw_76233439-402d-4386-8065-b0ebd5c66fbe Waveform From Wireless Waveform Generator App>.
%
% <<../heRFTransmitterWaveformMask.png>>
%
% To display the effect of the high-power amplifier (HPA) on the
% out-of-band spectral emissions, the FIR Interpolation block oversamples
% and filters the waveform. At the output of the RF Transmitter Subsystem
% block, the FIR Decimation block downsamples the waveform back to its
% original sampling rate. The Multirate Parameters block provides an
% interface to configure the parameters of the FIR Interpolation and
% Decimation blocks.
%
% <<../heRFTransmitterResampleFIRMask.png>>
%

%% Specifying Simulation Time
% Set the *Stop Time* value in the Simulink model to the time required to
% transmit and obtain the EVM results and constellation diagram of, at
% least, one packet. Considering the waveform configuration selected in the
% WLAN 802.11ax block, the packet transmission time in this example is
% 304.4 microseconds. As the filters in the FIR Interpolation and
% Decimation blocks introduce a delay, you can use the
% *IdleTime* parameter in the Mask Editor of the WLAN 802.11ax block to
% compensate for the delay.

%% RF Transmission
% The RF Transmitter Subsystem block is based on a superheterodyne transmitter
% architecture. This architecture models the effects of upconverting the
% waveform to the carrier frequency by characterizing these RF
% components:
%
% * IQ modulator consisting of mixers, a phase shifter, and a local oscillator
% * Bandpass filter
% * Power amplifier
%
% In addition to these components, this RF Transmitter Subsystem block also includes a
% variable gain amplifier (VGA) to control the input back-off (IBO) level
% of the HPA.
%
set_param(modelName,'Open','off');
RFTransmitterBlock = [modelName '/RF Transmitter'];
set_param(RFTransmitterBlock,'Open','on');

%%
% Use an Input Buffer block to send one sample at a time to the RF
% Transmitter Subsystem block.
%
% The Inport block inside the RF Transmitter Subsystem block converts the Simulink
% complex baseband waveform into the RF Blockset Circuit Envelope
% simulation environment. The *Carrier frequencies* parameter of the Inport
% block specifies the center frequency of the carrier in the RF Blockset
% domain. The Outport block converts the RF Blockset signal back into
% Simulink complex baseband.
%
% You can configure the RF Transmitter components by using the RF
% Transmitter Subsystem block mask. 
%
% <<../heRFTransmitterMask.png>>
%
% The RF Transmitter Subsystem block models typical impairments, including:
%
% * I/Q imbalance as a result of gain or phase mismatches between the
% parallel sections of the transmitter chain dealing with the IQ signal paths
% * Phase noise as an effect directly related to the thermal noise
% within the active devices of the oscillator
% * PA nonlinearities due to DC power limitation when the
% amplifier works in the saturation region
%
% Adapt the power level of the baseband waveform to the RF configuration by
% adding a Gain Control block after the Input Buffer block.
%
% As the current RF Transmitter Subsystem block configuration sends one
% sample at a time, the Output Buffer block (after the RF Transmitter
% Subsystem block) collects all samples within an HE SU packet before
% sending the samples onto the Demodulation and EVM calculation block.

%% Baseband Waveform Reception 
% The Demodulation and EVM calculation block recovers and plots the HE Data
% symbols in the Constellation Diagram block by performing frequency and packet
% offset corrections, channel estimation, pilot phase tracking, OFDM
% demodulation, and equalization. This block also performs these EVM
% measurements:
%
% * EVM per subcarrier (dB): EVM averaged over the allocated HE Data symbols within
% a subcarrier
% * EVM per OFDM symbol (dB)
% * Overall EVM (dB and %): EVM averaged over all transmitted HE Data
% symbols
%
% The Spectrum Analyzer block depicts the spectral mask according to IEEE
% P802.11ax/D7.0 Section 27.3.19.1. A second Spectrum Analyzer block,
% called CCDF and PAPR, connected at the input of the HPA block depicts the
% CCDF and PAPR measurements. The Power Meter block measures the RF
% waveform channel power, which is displayed in the Output Power (dBm)
% block.

%% Effect of Power Amplifier Nonlinearities
% To characterize the impact of HPA nonlinearities in the EVM evaluation,
% you can measure the amplitude-to-amplitude modulation (AM/AM) of the HPA.
% The AM/AM refers to the output power levels in terms of the input power
% levels. The helper function |hePlotHPACurve| displays the AM/AM
% characteristic of the HPA selected for this model.
%
hePlotHPACurve();
figHPA = gcf;

%%
% P1dB is the power at 1 dB compression point and is normally used as a
% reference when selecting the IBO level of the HPA. You can see the HPA
% impact on the RF Transmitter Subsystem block by analyzing the EVM results for
% different operating points of the HPA. For example, compare the case when
% IBO = 11 dB, corresponding to HPA operating in the linear region, with
% the case when IBO = 3 dB, corresponding to HPA operating in saturation.
% The gain of the VGA controls the IBO level. To keep a VGA linear behavior
% using the default parameters, select gain values lower than 15 dB.
%
% * _Linear HPA (IBO = 11 dB)_. To operate at an IBO level of 11 dB, set
% the *Available power gain* parameter of the VGA block to 5 dB. To
% calculate the EVM and plot the constellation diagram, run the simulation
% to capture one packet (Stop Time equal to 304.4 us
% for the default configuration).
%
set_param(RFTransmitterBlock,'vgaGain','5');
sim(modelName);

%%
% According to IEEE P802.11ax/D7.0 Table 27-49, the allowed relative
% constellation error (EVM) in an HE SU PPDU when the dual carrier
% modulation, or *DCM* parameter in the WLAN 802.11ax block Mask Editor, is
% equal to |false| and the Modulation/coding, or
% *MCS* parameter in the WLAN 802.11ax block Mask Editor, is equal to 3
% (16-QAM, 1/2) is -16 dB. As the overall EVM, around -41 dB, is lower than
% -16 dB, this architecture falls within the requirements of IEEE
% P802.11ax/D7.0.
%
% * _Nonlinear HPA (IBO = 3 dB)_. To operate at an IBO level of 3 dB, set
% the *Available power gain* parameter of the VGA block to 13 dB.
%
set_param(RFTransmitterBlock,'vgaGain','13');
sim(modelName);

% Restore to default parameters
set_param(RFTransmitterBlock,'vgaGain','5');

%%
% Compared to the previous case, the constellation diagram is more
% distorted. In terms of measurements, the overall EVM, around -28 dB, is
% still lower than -16 dB, so it also falls within the requirements of IEEE
% P802.11ax/D7.0.

%% Summary and Further Exploration
% This example demonstrates how to model and test the transmission of an
% 802.11ax waveform. The RF Transmitter Subsystem block consists of a
% bandpass filter, amplifiers and an IQ modulator. The example highlights
% the effect of HPA nonlinearities on the performance of the RF Transmitter
% Subsystem block. You can explore the impact of altering other impairments
% as well. For example:
%
% * Increase I/Q imbalance by using the *I/Q gain mismatch (dB)* and *I/Q
% phase mismatch (Deg)* parameters on the *IQ Modulator* tab of the RF
% Transmitter Subsystem block.
% * Increase the phase noise by using *Phase noise offset (Hz)* and *Phase
% noise level (dBc/Hz)* parameters on the *IQ Modulator* tab of the RF
% Transmitter Subsystem block.
%
% The RF Transmitter Subsystem block is configured to work with the current
% HE waveform parameters selected in the WLAN 802.11ax block and with the
% RF carrier centered at 5950 MHz. This carrier is within the IEEE 802.11
% HE STA frequency bands (between 1 GHz and 7.125 GHz, according to IEEE
% P802.11ax/D7.0). If you modify the *Center frequency (MHz)* parameter of
% the RF Transmitter Subsystem block or the waveform configuration of the
% WLAN 802.11ax block, check if you need to update the parameters of the RF
% Transmitter components and the FIR filters as these parameters are set to
% work with the current example configuration. For instance, a change in
% the carrier frequency requires revising the *Passband frequencies* and
% *Stopband frequencies* parameters of the Bandpass Filter block inside the
% RF Transmitter. If you increase the waveform bandwidth, check if you need
% to update the *Impulse response duration* and *Phase noise frequency offset (Hz)*
% parameters of the <docid:simrf_ref#bvlqsg1-1 IQ Modulator> block. The
% phase noise offset determines the lower limit of the impulse response
% duration. If the phase noise frequency offset resolution is high for
% a given impulse response duration, a warning message appears, specifying
% the minimum duration suitable for the required resolution.
% 
% You can use this example as the basis for testing HE waveforms for
% different RF configurations. You can replace the RF Transmitter Subsystem
% block by another RF subsystem and then configure the model accordingly.
%
% To use a different HE SU waveform, open the *WLAN Waveform
% Generator* app, select the HE SU configuration, and export a new block. For more
% information on how to generate and use this block, see
% <docid:wlan_ug#example-GenerateWWInSimulinkUsingAppGenBlockExample Generate Wireless Waveform in Simulink Using App-Generated Block>.

%% References
% # IEEE P802.11ax(TM)/D7.0 Draft 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: Enhancements for High Efficiency WLAN.

