%% Modeling and Testing an NR RF Receiver with LTE Interference
% The example shows how to characterize the impact of radio frequency (RF)
% impairments in the RF reception of a new radio (NR) waveform when
% coexisting with a long-term evolution (LTE) interference. The baseband
% waveforms are generated using 5G Toolbox(TM) and LTE Toolbox(TM), and the
% RF receiver is modeled using RF Blockset(TM).
%
% Copyright 2019-2020 The MathWorks, Inc.

%% Introduction
% This example characterizes the impact of LTE interference in the RF
% reception of an NR waveform. To evaluate the impact of the interference,
% 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.
% * Adjacent channel leakage ratio (ACLR): measure of the amount of power
% leaking into adjacent channels. It is defined as the ratio of the
% filtered mean power centered on the assigned channel frequency to the
% filtered mean power centered on an adjacent channel frequency.
% * 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 of a
% signal's instantaneous power to be a level specified above its average
% power.
%
% The impact of the receiver RF impairments such as in-phase and quadrature
% (IQ) imbalance, phase noise, and power amplifier (PA) nonlinearities are
% also considered.
%
% The example works on a subframe by subframe basis. For each subframe, the
% workflow consists of these steps:
%
% # Generate the baseband NR waveform using 5G Toolbox.
% # Generate the baseband LTE waveform (interference) using LTE Toolbox.
% # Upconvert both waveforms to their carrier frequencies using RF Blockset.
% # Use an RF receiver to downconvert the waveform centered at the NR
% carrier to baseband frequency.
% # Calculate the ACLR/ACPR, occupied bandwidth, channel power, and CCDF
% using the Spectrum Analyzer block.
% # Demodulate the NR baseband waveform to measure EVM.
%
% This example uses a Simulink model to perform these operations. Baseband
% signal processing (steps 1, 2 and 6) uses MATLAB(R) function blocks, whereas
% the RF receiver modeling (steps 3 and 4) uses RF Blockset. This model only
% supports Normal and Accelerator simulation modes.

%% Simulink Model Structure
% The model contains three main parts:
%
% * Baseband Generation: generates the baseband NR and LTE waveforms.
% * RF Reception: the upconverted waveforms pass through an RF receiver
% which downconverts the waveform centered at the NR carrier.
% * NR Baseband Reception and Measurements: performs the RF measurements
% and demodulates the baseband waveform to calculate the EVM.
%
modelName = 'NewRadioRFReceiverWithLTEInterferenceModel';
open_system(modelName);

%% Baseband Generation
% The NR-TM Transmission block transmits standard-compliant 5G NR test
% model (NR-TM) waveforms for frequency range 1 (FR1) and frequency range 2
% (FR2) [ <#12 1> ]. For the NR-TM waveform generation, you can specify the
% NR-TM name, the channel bandwidth, the subcarrier spacing (SCS), the
% duplexing mode, the cell identity, and the TS 38.141 version using the
% Waveform Parameters block.
% 
% Similarly, the LTE-TM Transmission block transmits standard-compliant
% LTE-TM waveforms [ <#12 2> ]. You can also specify the TM name, the
% channel bandwidth, the duplexing mode and the cell identity. This model
% resamples, if necessary, the LTE waveform to match the sampling rate of
% the NR waveform. The Waveform Parameter block does not accept an LTE
% bandwidth wider than the NR bandwidth.
%
% Additionally, the Waveform Parameters block provides the option to enable
% or disable the ACLR test. When the ACLR measurement is enabled, the
% waveform is oversampled to visualize the spectral regrowth. The Waveform
% Parameters block also includes a parameter called *Interferer Gain* which
% allows control the linear gain of the LTE interference. To cancel the
% transmission of the LTE interference, set the *Interferer Gain* parameter
% to 0. The Interferer Gain block is connected between the Baseband
% Generation and the RF Reception stages.
% 
% <<../RFReceiverWithLTEInterferenceWaveformParameters.png>>
%
% For more information on how to generate NR-TMs and LTE-TMs, see
% <docid:5g_examples#mw_67a90eda-30fe-4572-bc66-91b382be5218 5G NR-TM and FRC Waveform Generation>
% and <docid:lte_examples#example-DownlinkTestModelGenerationExample LTE Downlink Test Model (E-TM) Waveform Generation>,
% respectively.
%
% As the example works on a subframe by subframe basis, the NR-TM and
% LTE-TM Transmission blocks generate 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, both
% blocks transmit the same frame cyclically. The Subframe Counter block
% stores the number of the currently transmitted subframe. If the
% simulation time is longer than a frame period, the Subframe Counter block
% resets to 0.

%% RF Reception
% The RF Receiver block is based on a superheterodyne receiver
% architecture. This architecture applies passband filtering and amplification
% and downconverts the received waveform to an intermediate frequency.
% The RF components of this superheterodyne receiver are:
%
% * Bandpass filters
% * Low noise and power amplifiers
% * IQ demodulator consisting of mixers, phase shifter, and local oscillator
%
set_param(modelName,'Open','off');
set_param([modelName '/RF Receiver'],'Open','on');

%%
% To send both waveforms to the RF Receiver, insert a Vector Concatenate
% block between the Baseband Reception and the RF Reception stages. The
% Vector Concatenate block concatenates both waveforms horizontally, one
% column per waveform. Then, the Inport block inside the RF Receiver
% converts the two concatenated complex baseband waveforms into RF signals
% considering the center frequencies chosen in the *Carrier frequencies*
% parameter of this block (every frequency selected in *Carrier
% frequencies* is assigned to a different concatenated waveform). The
% Outport block converts the RF signal back into complex baseband.
%
% Due to the fact that the RF Receiver accepts a maximum of 1024 samples
% per subframe, the Input Buffer, before the RF Receiver block, reduces the
% number of samples sent to the RF Receiver. In the current configuration,
% the Input Buffer sends one sample at a time, resulting in the RF Receiver
% being sample-based. To configure the RF Receiver for frame-based
% reception, update the model:
%
% * Set the output buffer size parameter of the Input
% Buffer to a value less than or equal to 1024 samples. Make sure that this
% value is a factor of the number of samples per
% subframe, which depends on the selected NR-TM configuration. 
% * Inside the RF Receiver, set the Samples per frame parameter of the
% RF Configuration block to the output buffer size value of the Input Buffer.
%
% Before sending the samples onto the Decode Subframe block, the Output
% Buffer (after the RF Receiver) buffers all samples within a subframe and
% the ADC block digitizes the signal. You can modify the
% ADC block parameters using its mask.
%
% <<../RFReceiverWithLTEInterferenceADCMask.png>>
%
% The Delay 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 first information received during
% a subframe period. 
%
% You can configure the RF Receiver components using the RF
% Receiver block mask. 
%
% <<../RFReceiverWithLTEInterferenceRFReceiverMask.png>>
%
% The RF Receiver block exhibits typical impairments, including:
%
% * I/Q imbalance as a result of gain or phase mismatches between the
% parallel sections of the receiver chain dealing with the IQ signal paths.
% * Phase noise as a secondary 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 saturation region.

%% NR Baseband Reception and Measurements 
% The Decode Subframe block performs OFDM demodulation of the received
% subframe, channel estimation, and equalization to recover and plot the PDSCH
% symbols in the Constellation Diagram. This block also averages the EVM over 
% time and frequency and plots these values:
%
% * EVM per OFDM symbol: EVM average over each OFDM symbol.
% * EVM per slot: EVM average over the allocated PDSCH symbols within a slot.
% * EVM per subcarrier: EVM average over the allocated PDSCH symbols within
% a subcarrier.
% * Overall EVM: EVM average over all the allocated PDSCH symbols transmitted.
% 
% According to TS 38.141-1 [ <#12 1> ], not all PDSCH symbols are
% considered for the EVM evaluation. Using the RNTI, the helper function
% |hListTargetPDSCHs| selects the target PDSCH symbols to analyze.
%
% The Spectrum Analyzer block provides frequency-domain measurements such
% as ACLR (referred to as ACPR), occupied bandwidth, channel power, and
% CCDF. To visualize the spectral regrowth, the ACLR test oversamples the
% waveform. The oversampling factor depends on the waveform configuration
% and should be set such that the generated signal is capable of
% representing first and second adjacent channels. The ACLR evaluation
% follows the specifications in TS 38.141-1 [ <#12 1> ].

%% Model Performance
% To characterize the impact of the LTE interference on the NR reception
% you can compare the EVM and ACLR results for two different cases: 1)
% there is no interference, for example only the NR waveform; and 2) there
% is interference, you transmit both waveforms, NR and LTE.
%
% * _Without LTE interference (Interferer gain = 0)_. To eliminate the LTE
% interference, set the *Interferer gain* parameter of the Waveform
% Parameters block to 0. To simulate a whole frame, run the simulation long
% enough to capture 10 subframes (10 ms). During simulation, the model
% displays the EVM and ACLR measurements and the constellation diagram.
% These are the results when transmitting 4 subframes.
%
set_param([modelName '/Waveform Parameters'],'InterfererGain','0');
sim(modelName);

%%
% According to TS 38.104 [ <#12 3> ], the minimum required ACLR for
% conducted measurements is 45 dB and the maximum required EVM when the
% constellation is 256-QAM is 3.5%. As the ACLR values, around 51 dB, are
% higher than 45 dB, and the overall EVM, around 1.3%, is lower
% than 3.5%, both measurements fall within the requirements.
%
% * _With LTE interference (Interferer gain = 1)_. To activate the LTE
% interference, set the *Interferer gain* parameter of the Waveform
% Parameters block to any available value different from 0. For example,
% choose value 1.
%
set_param([modelName '/Waveform Parameters'],'InterfererGain','1');
sim(modelName);
slmsgviewer.DeleteInstance();

%%
% Compared to the previous case, the constellation diagram is more
% distorted and the spectral regrowth is higher. In terms of the
% measurements, the ACLR values, around 47.90 dB, and the overall EVM,
% around 2.151%, still fall within the requirements of TS 38.104 [ <#12 3>
% ].
%
% The RF Receiver is configured to work with the default values of the
% Waveform Parameters block and with the NR and LTE carriers centered at
% 2140 MHz and 2150 MHz, respectively. These carriers are within the NR
% operating band n1 [ <#12 4> ] and the E-UTRA operating band 1 [ <#12 5>
% ]. If you change the carrier frequency or the values in the Waveform
% Parameters block, you may need to update the parameters of the RF
% Receiver components as these parameters have been selected to work for
% the default configuration of the example. For more information, see the
% Summary and Further Exploration section of this example.

%% Summary and Further Exploration
% This example demonstrates how to model and test the reception of an NR
% waveform when coexisting with an LTE waveform. The RF receiver consists
% of bandpass filters, amplifiers and an IQ demodulator. To evaluate the
% impact of the LTE interference, the example modifies the gain of the LTE
% waveform and performs ACLR and EVM measurements. You can explore the
% impact of altering the RF 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 Demodulator* tab of the RF
% Receiver block.
% * Increase the phase noise by using *Phase noise offset (Hz)* and *Phase
% noise level (dBc/Hz)* parameters on the *IQ Demodulator* tab of the RF
% Receiver block.
% * Reduce the input back-off of the two amplifiers (PA I and PA Q)
% increasing the *Gain (dB)* parameter on the *LNA* tab of the RF Receiver
% block.
%  
% Additionally, you can check the occupied bandwidth, the channel power,
% and the CCDF measurements by using the Spectrum Analyzer block.
%
% If you change the carrier frequencies or the values in the Waveform
% Parameters block, you may need to update the parameters of the RF
% Receiver components as these parameters have been selected to work for
% the default configuration of the example. For instance, a change in the
% carrier frequencies requires revising the bandwidth of the filters. If
% you select an NR bandwidth wider than 20MHz, you may need to update 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 the coexistence between NR-TM
% and LTE-TM waveforms for different RF configurations. You can try
% replacing the RF Receiver block by another RF subsystem of your choice
% and configuring the model accordingly.

%% Appendix
% This example uses these helper functions:
%
% * <matlab:edit('hNRReferenceWaveformGenerator.m') hNRReferenceWaveformGenerator.m>
% * <matlab:edit('hNRDownlinkWaveformGenerator.m') hNRDownlinkWaveformGenerator.m>
% * <matlab:edit('hOFDMInfo.m') hOFDMInfo.m>
% * <matlab:edit('hOFDMModulate.m') hOFDMModulate.m>
% * <matlab:edit('hOFDMDemodulate.m') hOFDMDemodulate.m>
% * <matlab:edit('hPDSCHResources.m') hPDSCHResources.m>
% * <matlab:edit('hSSBurstInfo.m') hSSBurstInfo.m>
% * <matlab:edit('hVectorDataSource.m') hVectorDataSource.m>
% * <matlab:edit('hListTargetPDSCHs.m') hListTargetPDSCHs.m>
% * <matlab:edit('hSlotResources.m') hSlotResources.m>
% * <matlab:edit('hACLRMeasurementNR.m') hACLRMeasurementNR.m>
% * <matlab:edit('hACLRParametersNR.m') hACLRParametersNR.m>
% * <matlab:edit('hInitializeNRTMModel.m') hInitializeNRTMModel.m>
% * <matlab:edit('hInitializeLTETMModel.m') hInitializeLTETMModel.m>
% * <matlab:edit('hDecodeSubframe.m') hDecodeSubframe.m>

%% Selected Bibliography
% # 3GPP TS 38.141-1. "NR; Base Station (BS) conformance testing Part 1:
% Conducted conformance testing." _3rd Generation Partnership Project;
% Technical Specification Group Radio Access Network._
% # 3GPP TS 36.141 "E-UTRA; Base Station (BS) conformance testing" _3rd Generation
% Partnership Project; Technical Specification Group Radio Access Network._
% # 3GPP TS 38.104. "NR; Base Station (BS) radio transmission and reception." 
% _3rd Generation Partnership Project; Technical Specification Group Radio
% Access Network._
% # 3GPP TS 38.101-1. "NR; User Equipment (UE) 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._
