%% Modeling and Testing an LTE RF Transmitter
% This example shows how to characterize the impact of radio frequency (RF)
% impairments in an LTE transmitter. The example generates the baseband LTE
% waveform, which consists of an E-UTRA test model (E-TM), by using LTE
% Toolbox(TM) and models the RF transmitter by using RF Blockset(TM).
%
% Copyright 2020 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 in the transmission of an LTE 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. The
% example performs EVM measurements according to the specifications in TS
% 36.104, Annex E [ <#12 2> ].
% * 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 subframe-by-subframe basis. For each subframe, the
% workflow consists of these steps:
%
% # Generate the baseband E-TM waveform using LTE Toolbox.
% # Import the baseband waveform as an RF signal into the RF Transmitter
% block implemented using RF Blockset. The model uses an RF intermediate
% frequency to carry the baseband information in RF Blockset.
% # Upconvert the waveform to the carrier frequency by using an RF
% transmitter. You can accurately model the impairments introduced by an
% actual RF transmitter by using the RF components available in RF
% Blockset.
% # Calculate the occupied bandwidth, channel power, CCDF and PAPR by using
% the Spectrum Analyzer block.
% # Measure the EVM by demodulating the baseband waveform.
%
% This example performs these operations by using a Simulink(R) model in
% which MATLAB(R) Function blocks carry out the baseband signal processing
% (steps 1 and 5) and RF Blockset performs the RF transmitter modeling
% (steps 2 and 3). This model supports |Normal| and |Accelerator|
% simulation modes.

%% Simulink Model Structure
% The model contains three main parts:
%
% * Baseband Waveform Generation: generates the baseband E-TM waveforms
% * RF Transmission: upconverts the waveform to the carrier frequency
% * Baseband Reception and Measurements: performs the RF measurements
% and calculates EVM by demodulating the baseband waveform
%
modelName = 'RFLTETransmitterModel';
open_system(modelName);

%% Baseband Generation
% The E-TM Waveform block generates standards-compliant E-TM waveforms [
% <#12 1> ]. For the waveform generation, you can specify the TM name,
% channel bandwidth, duplexing mode, and cell identity in the E-TM Waveform
% block.
% 
% <<../RFTransmitterWaveformMaskLTE.png>>
%
% For more information on how to generate E-TMs, see
% <docid:lte_ug#example-DownlinkTestModelGenerationExample LTE Downlink Test Model (E-TM) Waveform Generation>.
%
% As the example works on a subframe-by-subframe basis, the E-TM Waveform
% block generates one subframe at a time. Transmitting ten subframes,
% corresponding to one frame in the case of FDD duplexing mode, lasts 10
% ms. If the simulation time is longer than 10 ms, this block transmits the
% same frame cyclically. The Subframe Counter block stores the number of
% the currently transmitted subframes. If the simulation time is longer than
% a frame period, the Subframe Counter block resets to 0 for every new
% frame simulated.

%% RF Transmission
% The RF Transmitter block is based on a superheterodyne transmitter
% architecture. This architecture upconverts the received waveform to the
% carrier frequency and applies passband filtering and amplification. The
% RF components of this superheterodyne transmitter are:
%
% * IQ modulator consisting of mixers, a phase shifter, and a local oscillator
% * Bandpass filter
% * Power amplifier
%
% In addition to these components, this RF Transmitter block also includes a
% variable gain amplifier (VGA) to control the input back-off (IBO) level
% of the high power amplifier (HPA).
%
set_param(modelName,'Open','off');
set_param([modelName '/RF Transmitter'],'Open','on');

%%
% The Inport block inside the RF Transmitter converts the complex baseband
% waveform into an RF signal. You can vary the center frequency of this RF
% signal by modifying the *Intermediate frequency (MHz):* parameter of the
% RF Transmitter block mask (in this example, the default frequency is 70
% MHz). The Outport block converts the RF signal back into complex
% baseband.
%
% You can use an Input Buffer before the RF Transmitter block
% to reduce the number of samples sent to the RF Transmitter. For
% simplicity, the Input Buffer in the current configuration sends one
% sample at a time, resulting in the RF Transmitter being sample-based,
% i.e. it consumes one sample at a time.
%
% Before sending the samples onto the Decode Subframe block, the Output
% Buffer (after the RF Transmitter) collects all samples within a subframe.
%
% The Delay block, which is connected to the Subframe Counter block,
% accounts for buffer-induced delays. As the duration of the delay is
% equivalent to the transmission of a subframe, the Decode Subframe block
% does not demodulate the samples received during the first subframe period.
%
% You can configure the RF Transmitter components by using the RF
% Transmitter block mask. 
%
% <<../RFTransmitterMaskLTE.png>>
%
% The RF Transmitter block exhibits 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

%% Baseband Reception and Measurements 
% The Decode Subframe block recovers and plots the PDSCH symbols in the
% Constellation Diagram by performing OFDM demodulation of the received
% subframe, channel estimation, and equalization. This block also performs
% EVM measurements according to the specifications in TS 36.104, Annex E [
% <#12 2> ]. These specifications suggest measuring the EVM at two
% locations in time (low and high), where the low and high locations
% correspond to the alignment of the FFT window within the start and end of
% the cyclic prefix, respectively. For more information regarding how to
% measure EVM, see <docid:lte_ug#example-PDSCHEVMMeasurementExample PDSCH
% Error Vector Magnitude (EVM) Measurement>. 
%
% This example carries out EVM measurements averaged over the allocated
% PDSCH symbols within a subcarrier, resource block, OFDM symbol, subframe,
% frame, and overall grid. The EVM per subframe is measured for the high
% and low locations in time while the other results only depict the highest
% EVM of both locations.
%
% The Spectrum Analyzer block provides additional measurements such
% as occupied bandwidth, channel power, CCDF and PAPR.

%% Effect of Power Amplifier
% To characterize the impact of the HPA 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 |hPlotHPACurveLTE| displays the AM/AM characteristic of
% the HPA selected for this model.
%
hPlotHPACurveLTE();
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 by analyzing the EVM results for different
% operating points of the HPA. For example, compare the case when IBO = 12
% dB, corresponding to HPA operating in the linear region, with the case
% when IBO = 6 dB, corresponding to HPA starting to operate in the
% nonlinear region. The gain of the VGA controls the IBO level. To keep a
% VGA linear behavior, select gain values lower than 20 dB.
% 
%
% * _Linear HPA (IBO = 12 dB)_. To operate at an IBO level of 12 dB, set
% the *Available power gain* parameter of the VGA block to 0 dB. To
% simulate a whole frame, run the simulation long enough to capture 10
% subframes (Stop Time equal to 10 ms). During simulation, the model
% displays the spectrum and the constellation diagram.
%
set_param([modelName '/RF Transmitter'],'vgaGain','0');
sim(modelName);

%%
% According to TS 36.104 [ <#12 2> ], the maximum EVM when the
% constellation is 64-QAM is 8%. As the overall EVM is around 1.5%, this
% architecture falls within the requirements of TS 36.104 [ <#12 2> ].
%
% * _Nonlinear HPA (IBO = 6 dB)_. To operate at an IBO level of 6 dB, set
% the *Available power gain* parameter of the VGA block to 6 dB.
%
set_param([modelName '/RF Transmitter'],'vgaGain','6');
sim(modelName);
slmsgviewer.DeleteInstance();

% Restore to default parameters
set_param([modelName '/RF Transmitter'],'vgaGain','0');

%%
% Compared to the previous case, the constellation diagram is more
% distorted. In terms of measurements, the overall EVM, around 2.8%, still
% falls within the requirements of TS 36.104 [ <#12 2> ]. 
%
% If you want to push the HPA to operate further in the nonlinear region,
% you will need to oversample the signal (around 5 times its baseband
% bandwidth) so that the simulation bandwidth is large enough to capture
% in-band spectral regrowth.

%% Summary and Further Exploration
% This example demonstrates how to model and test the transmission of an
% LTE waveform. The RF Transmitter 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. 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 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 block.
%  
% Additionally, you can check the CCDF and PAPR measurements by using the
% Spectrum Analyzer window: select the CCDF Measurements button on the
% toolbar.
%
% The RF Transmitter is configured to work with the default values of the E-TM
% Waveform block and with the LTE carrier centered at 2140 MHz. This
% carrier is within the E-UTRA operating band 1 [ <#12 3> ]. If you change
% the carrier frequency or the values in the E-TM Waveform block, you may
% need to update the parameters of the RF Transmitter components, as these
% parameters have been selected to work for the default configuration of
% the example. For instance, a change in the carrier frequency requires
% revising the bandwidth of the filters. Modifying the waveform bandwidth
% may require updating the *Impulse response duration* and *Phase noise
% frequency offset (Hz)* parameters of the IQ Modulator block. The phase
% noise offset determines the lower limit of the impulse response duration.
% If the phase noise frequency offset resolution is too high for a given
% impulse response duration, a warning message appears, specifying the
% minimum duration suitable for the required resolution. For more
% information, see <docid:simrf_ref#bvlqsg1-1 IQ Modulator>.
% 
% This example could be the basis for testing E-TM waveforms for
% different RF configurations. You can replace the RF Transmitter block
% by another RF subsystem of your choice and configuring the model
% accordingly.

%% References
% # 3GPP TS 36.141 "E-UTRA; Base Station (BS) conformance testing" _3rd Generation
% Partnership Project; Technical Specification Group Radio Access Network._
% # 3GPP TS 36.104 "E-UTRA; Base Station (BS) radio transmission and reception" _3rd Generation
% Partnership Project; Technical Specification Group Radio Access Network._
% # 3GPP TS 36.101. "E-UTRA; User Equipment (UE) radio transmission and reception." 
% _3rd Generation Partnership Project; Technical Specification Group Radio
% Access Network._
