%% Getting Started with RF Modeling
% Learn how to use the RF Budget Analyzer app to build a simple RF
% receiver, and see how to set up multi-carrier Circuit Envelope
% simulation.
%

% Copyright 2017 The MathWorks, Inc.

%% Build a Cascade (row vector) of RF Elements.
% You can build and analyze an RF cascade by adding elements characterized
% by their data sheet specifications.
%

%%
% You can use the RF Budget Analyser app and drag and drop new elements, or
% you can script the chain elements using  MATLAB commands. If you are not
% familiar with the syntax, you can start with app and generate a MATLAB
% script.
%
% Add elements to your chain in the following order: 
% 
% * Filter specified by an S-parameters Touchstone file 
% * Low Noise Amplifier (LNA)
% * Direct conversion demodulator
% * Baseband amplifier
elements(1) = nport('sawfilterpassive.s2p');
elements(2) = amplifier(                                                ...
    'Name','LNA',                                                       ...
    'Gain',18,                                                          ...
    'NF',3,                                                             ...
    'OIP3',10);
elements(3) = modulator(                                                ...
    'Name','Demod',                                                     ...
    'Gain',10,                                                          ...
    'NF',6.4,                                                           ...
    'OIP3',36,                                                          ...
    'LO',2.45e9,                                                        ...
    'ConverterType','Down');
elements(4) = amplifier(                                                ...
    'Gain',20,                                                          ...
    'NF',11.3,                                                          ...
    'OIP3',42);

%% Inspect RF Budget Using RF Budget Analyzer App
% Construct an rfbudget object. The MATLAB command window dynamically
% displays the budget analysis results.
b = rfbudget(                                                           ...
    'Elements',elements,                                                ...
    'InputFrequency',2.45e9,                                            ...
    'AvailableInputPower',-70,                                          ...
    'SignalBandwidth',8e6)

%%
% Or you can visualize the rf budget object in the app.
show(b)

%% Generate RF Blockset Model
% Use the Export button in the RF Budget Analyzer app to create an RF
% Blockset model or:
exportRFBlockset(b)
save_system(gcs,'model_1')

%%
%
% You can use this model for multi-carrier circuit envelope simulation. 
% The input / output ports and configuration block are set up correctly 
% and you can copy the model for use in any other Simulink testbench.
%
% * The input port specifies a complex powerwave signal centered at
%   2.45GHz.
% * The output ports terminate the cascade and extract the envelope
%   centered at DC (0Hz). The I and Q signals are real baseband signals.
% * The configuration block runs the simulation for a total of 8 simulation
%   frequencies in order to capture the non linearity introduced by the
%   demodulator and amplifiers.
% * The simulation stop time in this case is set equal to 0. This means
%   that the simulation does only a static analysis of the model (harmonic
%   balance).
%

%%
% Observe and understand the model blocks:
%

%%
% 
% * The S-parameter block describing the filter uses rational fitting in
%   order to simulate frequency data in the time domain. Notice that at
%   2.45GHz it introduces a phase rotation of approximately -58 degrees.
% * Both amplifiers specify IP3, but you can also specify IP2.
% * The demodulator includes ideal channel selection filters. Additional
%   impairments can be added such as LO leakage and I/Q imbalance.
%

%%
% Simulate the model to compare the output power values with the RF budget
% analyzer app values. Notice that due to the phase rotation introduced by
% the S-parameter block, the complex input signal is partly downconverted
% on the I and on the Q branch, and thus the output power on the two
% branches is different. For this reason, the gain and other specs of
% direct conversion receivers, are measured at an arbitrary low frequency.
%

%% Generate Measurement Testbench
% Use the Export button in the RF budget analyzer app to create a
% measurement testbench or:
exportTestbench(b)
save_system(gcs,'model_2')

%%
% To measure the gain, noise figure, and OIP3 use the RF Measurement Unit
% dialog box to choose the value you want to verify.
%

%%
% Observe and understand the testbench block:
% 
% * You can measure the output on the I or Q branches.
% * Measurements are done at an arbitrary low frequency
% * Measurements are done in the time domain over an arbitrary signal
%   bandwidth
%

%%
% Run the following simulation: 
%

%%
% 
% * Measure the gain (disable the noise for accurate measurements).
% * Measure the NF. Reduce the baseband bandwidth to 8e3 for narrowband
%   measurements. In this way, the noise figure measurement is not affected 
%   by the filter selectivity.
% * Measure the OIP3. Keep the smaller baseband bandwidth and disable noise
%   for accurate measurements.

%%
% On comparing, you will see that the values of gain, noise figure, and IP3
% match the values in the RF Budget Analyzer app reported in the testbench.
%
