%% Top-Down Design of an RF Receiver
% 
% This example designs an RF receiver for a ZigBee(R)-like application
% using a top-down methodology. It verifies the BER of an impairment-free
% design, then analyzes BER performance after the addition of impairment
% models.  The example uses the |RF Budget Analyzer| App to rank the
% elements contributing to the noise and nonlinearity budget. 
%%
% Design specifications:
%
% * Data rate = 250 kbps
% * OQPSK modulation with half sine pulse shaping, as specified in IEEE(R)
% 802.15.4 for the physical layer of ZigBee
% * Direct sequence spread spectrum with chip rate = 2 Mchips/s
% * Sensitivity specification = -100 dBm
% * Bit Error Rate (BER) specification = 1e-4
% * Analog to digital converter (ADC) with 10 bits and 0 dBm saturation
% power
%
% To create fully standard-compliant ZigBee waveforms, you can use the
% <matlab:web('https://www.mathworks.com/matlabcentral/fileexchange/62845-communications-toolbox-library-for-the-zigbee-protocol','-browser') Communications Toolbox Library for the ZigBee Protocol> Add-on.

% Copyright 2018-2020 The MathWorks, Inc.

%%
% This example guides you through the following steps:
%
% * Develop the baseband transmitter model for waveform generation
% * Determine SNR specification to achieve the 1e-4 BER from a link-level
% idealized baseband model
% * Derive RF subsystem specifications from equivalent-baseband model of RF
% receiver and ADC
% * Derive direct conversion specifications from circuit envelope model of
% RF receiver
% * Perform multi-carrier simulation including interfering signals and
% derive the specifications of the DC offset compensation algorithm
%
 
%% Design and Verify Baseband Transmitter
%
% To evaluate the performance of the RF receiver design, it is necessary
% and sufficient to use a signal spectrally representative of an 802.15.4
% waveform.
%
% The
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignTx.slx') baseband transmitter model>
% creates and illustrates a spectrally representative ZigBee waveform
% in the spectral and constellation domains.  This model and all the
% subsequent models use callbacks to create MATLAB workspace variables that
% parameterize the systems.
%
model = 'TopDownRFReceiverDesignTx';
open_system(model);
sim(model);
 
%% Determine Receiver SNR Requirement
%
% To design the receiver, first determine the SNR needed to 
% achieve the specified BER less than 1e-4. calculated in the 
% simulation bandwidth of 4 MHz. 
% Run the
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignLink.slx') link-level model>
% to simulate the receiver processing required to achieve the target BER.
%
% Computing the BER accurately requires alignment of the transmit and
% receive signals. The simulation must compensate for a two-sample delay of
% the received signal  compared to the transmitted signal.  Also, to ensure
% correct chip-to-symbol-to-bit mapping, the simulation must  align the
% chips  to frame boundaries at the input to the Chips to Symbol block on a
% frame boundary. Accounting for the receive signal delay and the frame
% boundary alignment requires addition of a *Delay* block set  to a 32-2=30
% delay on the receiver branch before recovering the received symbols.
 
bdclose(model);
model = 'TopDownRFReceiverDesignLink';
open_system(model);
sim(model, 'StopTime', '0.2');
 
%% 
% The model achieves a 1e-4 BER at an SNR of -2.7 dB, which can be verified
% by collecting 100 bit errors.
%
% In the
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignLink.slx')
% link-level model>, the AWGN block accounts for the overall channel and RF
% receiver SNR budget.
 
%% Add ADC and Determine Receiver Total Gain and Noise Figure (NF)
%
% This section uses traditional heuristic derivations to determine the
% high-level specifications of the RF receiver and ADC.
%
% * B = 4 MHz = simulation bandwidth = simulation sampling frequency
% * kT = 174 dBm/Hz = thermal noise floor power
% * Sensitivity = -100 dBm = receiver sensitivity
% * SNR = -2.7 dB
% * Noise power in simulation bandwidth = Pn = sensitivity-SNR = -100 dBm - (-2.7 dB) = -97.3 dBm

% * Pn = kT + 10*log10(B) + NF = -97.3 dBm
%
% Solving for the receiver noise figure (NF):
%
% NF = -97.3 dBm + 174 dBm/Hz -10*log10(4e6 Hz) = 10.7 dB

%
% Derive the receiver gain using the ADC specifications and dynamic range.
%
% * ADC Number of bits = Nbits = 10
% * ADC Saturation power = Psat = 0 dBm (50 Ohm normalization)
% * ADC Sampling frequency = Fadc = 2.6 MHz
% * ADC Dynamic Range = 6 * Nbits + 1.8 = 61.8 dB
% * Noise power in ADC bandwidth = PNadc = Pn + 10log10(Fadc/Fs) = -99.2 dBm
% * Assuming a 0.1 dB contribution to SNR, Quantization noise = PNadc - 16 dB
% * Receiver Gain = (Psat - Dynamic Range) - PNadc + 16 dB = (0 dBm - 61.8 dB) - (-99.2 dBm) + 16 dB = 53.4 dB
%
%%
% Simulating an <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignIdealBaseband.slx') idealized baseband model>
% of the RF Receiver, verify the preliminary RF receiver specifications (NF
% = 10.7 dB and receiver gain = 53.4 dB). This can be done by collecting
% 100 errors.
%
% The spectrum analyzer shows that the received spectrum with the ADC is
% roughly identical in shape to the spectrum of the previous section,
% without the ADC.
 
bdclose(model);
model = 'TopDownRFReceiverDesignIdealBaseband';
open_system(model);
sim(model, 'StopTime', '0.3');
 
%% Refine Architectural Description of RF Receiver
%
% In this section the RF receiver, and its noise figure and gain budget
% specifications, are modelled by using four discrete subcomponents with
% these characteristics:
%
% * SAW Filter:      Noise Figure = 2.5 dB,    Gain = -3 dB
% * LNA:             Noise Figure = 6   dB,    Gain = 22 dB
% * Passive Mixer:   Noise Figure = 10  dB,    Gain = -5 dB
% * VGA:             Noise Figure = 14  dB,    Gain = 40 dB
%
% The SAW filter performance is derived from a Touchstone file that
% specifies S-parameters characteristics. You can verify the gain by
% visualizing the S21 parameter in the X-Y plane at the operating frequency
% of 2.45 GHz. You can verify the noise figure by visualizing the NF
% parameter in the X-Y plane at the operating frequency of 2.45 GHz.
% Typically, an LNA with low noise and high gain follows the SAW filter,
% which greatly reduces the impact of the noise figure of the components
% after the LNA. Also, the passive mixer is specified with a high IP2.
% Similar to the SAW filter, you can verify the mixer gain by visualizing
% the S21 parameter in the X-Y plane over a user-specified frequency range
% of [2e9 3e9].
%
% An
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignEquivalentBaseband.slx') equivalent baseband model>
% simulates the refined RF receiver.
%
% Run the simulation and verify the RF receiver link budget 
% by using the output port visualization pane. The total noise figure and 
% gain across the four stages has been divided according to the 
% following budget:
%
% * Component NF (dB) = [2.5, 6, 10, 14] 
% * Component noise factor F (linear) = 10^(NF/10) = [1.78 3.98 10.0 25.1]
% * Power gain (dB) = [-3, 22, -5, 40] = 54 dB > 53.4 dB
% * Voltage gain VG (linear) = 10^(Power gain/20) = [0.71 12.59 0.56 100.0]
% * System noise factor Fsys (linear) = $$1 + [F(1) - 1] + {{[F(2) - 1]} \over {VG(1)}} + {{[F(3) - 1]} \over {VG(1) \times VG(2)}} + {{[F(4) - 1]} \over {VG(1) \times VG(2) \times VG(3)}} = 11.8$
% * System noise figure NFsys (dB) = 10*log10(Fsys) = 10.7 dB
%
% With this model you can verify that a BER < 1e-4 corresponds to a
% Chip Error Rate (ChER) around 7%.  By computing ChER, you can run the
% subsequent models for less time and still collect accurate BER
% statistics.
 
bdclose(model);
model = 'TopDownRFReceiverDesignEquivalentBaseband';
open_system(model);
sim(model, 'StopTime', '0.3');
 
%% Use Circuit Envelope to Simulate Additional RF Impairments
%
% Using the circuit envelope modeling approach, continue refining 
% the RF receiver architecture by adding more realistic impairments.
%
% The
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignCircuitEnvelope.slx') circuit envelope model>
% of the RF Receiver differs from the equivalent baseband model as it:
% 
% * Replaces the equivalent baseband mixer with a quadrature modulator,
% consisting of parameterizable I and Q mixers and phase shifter block, and
% an LO with impairments
% * Uses broadband impedances (50 ohm) to explicitly model the power
% transfer between blocks
% 
% Comparing spectra, power measurements, and ChER to the
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignEquivalentBaseband.slx')
% equivalent baseband model>, there are no significant performance
% differences. However, with the
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignCircuitEnvelope')
% circuit envelope model>, you can include even order nonlinearity effects,
% I/Q imbalance, and specifications of colored noise distributions for each
% of the components.
 
bdclose(model);
model = 'TopDownRFReceiverDesignCircuitEnvelope';
open_system(model);
sim(model);
 
%% 
% You can manually build the circuit envelope model of the RF Receiver by
% using blocks from the
% <matlab:open_system('simrfV2libtop') Circuit Envelope library>, or
% it can be automatically generated using the RF Budget Analyzer App.
%
% The RF Budget Analyzer App 
%
% * Uses Friis equations to determine the noise, gain, and nonlinearity
% budget of an RF chain
% * Allows you to explore the receiver design 
% space and determine how to break down the specifications across the
% elements of the chain 
% * Helps you determine which element has the largest contribution to the
% noise and nonlinearity budget
% * Can generate an RF receiver model with which you can perform
% multi-carrier simulation and further modify.
%
rfBudgetAnalyzer('TopDownRFReceiverDesign.mat')
 
%% Add Wideband Interference, LO Leakage, and DC Offset Cancellation
%
% This section modifies the
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignCircuitEnvelope.slx')
% circuit envelope model> to create this
% <matlab:openExample('comm_simrf/TopDownRFReceiverDesignExample','supportingFile','TopDownRFReceiverDesignCircuitEnvelopeWithInterferer')
% circuit envelope with interferer model>. The circuit envelope with
% interferer model includes a wideband interfering signal and these
% impairments:
%
% * LO-RF isolation of 90 dB in the quadrature demodulator
% * OIP2 equal to 70 dBm in the quadrature demodulator
% * WCDMA-like blocker of -30 dBm at 2500 MHz
%
% This simulation models a non-standard-compliant interfering signal that
% has power and spectral distribution characteristics realistic for a
% WCDMA signal.
%
% The design requires a DC offset compensation algorithm to achieve the
% desired ChER due to the DC offset that results from the LO leakage and
% the nonlinearity in the demodulator caused by the high out-of-band
% interfering signal power.  In this case you include a very selective
% filter, that introduces a long latency with corresponding computation
% delay increases in the ChER measurement block.
%
% The spectrum centered at 0 Hz shows the DC offset compensation reducing
% the DC offset.  As you run the model, note that the DC offset is
% eventually completely removed.

bdclose(model);
model = 'TopDownRFReceiverDesignCircuitEnvelopeWithInterferer';
open_system(model);
sim(model, 'StopTime', '5e-3');
 
%% Conclusion
%
% Following a top-down design methodology, RF receiver components
% specifications were derived. Impairment, interferer, and RF receiver
% subcomponent models were iteratively refined to increase fidelity and
% validated at each stage to confirm overall system performance goals were
% achieved.
%
bdclose(model);
clear model;
