%% Two-Tone Envelope Analysis Using Real Signals
% This example shows how to use the RF Blockset(TM) Circuit Envelope
% library to test intermodulation distortion of an amplifier using
% two-carrier envelope analysis.
%
% Copyright 2011-2017 The MathWorks, Inc.

%%
%

model = 'simrfV2_power_imd';
open_system(model);

%% System Architecture
% The system consists of:
% 
% * A Simulink(R) sinusoidal input with frequency |(f1-f2)/2|, and an
% Inport that assigns the input modulation to the carrier |(f1+f2)/2|. This
% formulation is equivalent to the sum of two sinusoids with frequency |f1|
% and |f2|, according to the sum-product formula for cosines:
%
% $$\cos{2\pi f_1 t}+\cos{2\pi f_2 t}=2\cos{\left(2\pi\frac{f_1+f_2}{2}t
%    \right)}\cos{\left(2\pi\frac{f_1-f_2}{2}t\right)}$$
%
% * An amplifier with specified 0 dB linear gain and -20 dB OIP3. For an
% input signal $u$, the amplifier computes the output $v$ according to the
% polynomial
%
% $$v(t) = 2u(t) - \frac{4}{15}u^3(t)$$
%
% * Two Outport blocks that probe the input and output voltages
% (across shunt resistors) at the carrier frequency.
%
% * A spectrum analyzer
%
% The example model defines variables for block parameters using a callback
% function. To access model callbacks, select *Model* > *Model Settings* >
% *Model Properties* and click the Callbacks tab in the Model Properties
% window.
%

%% Running the Example
% # Type |open_system('simrfV2_power_imd')| at the Command Window prompt.
% # Select *Simulation* > *Run*.
% 
evalc(['sim(''', model, ''',1e-2)']);

%%
% The resulting output power spectrum, labeled 'Output Power', shows
% third-order intermodulation distortion. The Display block shows the power
% of the bandpass waveform, which is half of power of the envelope
% waveform.
%
% The spectrum analyzer is set to probe for the Output Power. On comparing
% values, you will see that the spectrum analyzer matches the output power
% spectrum.
%
% The input drives the nonlinear amplifier into compression, so the linear
% component of the output is attenuated. To simulate the amplifier in a
% linear region:
%
% * Specify |Inf| for the IP3 parameter of the Amplifier block located in
%   the Nonlinearity tab of the dialog, or
% * Reduce the power of the input signal by decreasing the value of the
%   Amplitude parameter of the Sine Wave block.

%% Performing Two-Tone Analysis Using Circuit Envelope Simulation
% This example takes advantage of the properties of real signals--namely,
% the sum-product equivalence of sinusoids. To perform the same experiment
% on a different RF system:
%
% # Choose |f1| and |f2|, the frequencies of the test tones.
% # Use a Simulink Sine source and an Inport block to model modulation with
% frequency |(f1-f2)/2| on a carrier with frequency |(f1+f2)/2|.
% Alternatively, you can use a Sinusoid block from the RF Blockset Circuit
% Envelope library to specify both a modulation and a carrier
% simultaneously.
% # Specify |(f1+f2)/2| as one of your carrier frequencies in the
% Configuration block dialog.
% # Use an Outport block to probe the distorted signal.
% 

%% References
% Cripps, Steve C. _RF Power Amplifiers for Wireless Communications_.
% Artech House, Inc., 2006.
%

bdclose(model)
