%% Power in Simulink(R) Sources and Signals
% This example shows how to use Input Port and Output Port blocks of the RF
% Blockset(TM) Equivalent Baseband library to convert between dimensionless
% Simulink signals and equivalent-baseband signals.
%
% In general, signals in Simulink are dimensionless, so their amplitudes do
% not correspond to a particular voltage or power. However, in an RF
% system, power is the quantity of interest. When you use blocks from the
% RF Blockset Equivalent Baseband library in a Simulink model, you must
% specify how the software interprets the Simulink signals that exist
% outside the boundaries of the Input Port and Output Port blocks. RF
% Blockset Equivalent Baseband software provides two options to interpret
% the Simulink signal: power wave or voltage. The amplitude of a source in
% Simulink determines the signal power level and affects the signal power
% and power spectrum.
%
% All the models used in this example interpret the Simulink signal as a
% power wave with dimensions of $\mbox{W}^{0.5}$. This means for an RF
% system, the source signal generated by regular Simulink blocks is treated
% as the incident power wave to the RF system, and the RF output signal is
% the transmitted power wave of the RF system. If you choose to interpret
% the Simulink signal as a voltage, you need to modify the models by
% considering the impedance effects when you calculate the powers. For more
% details, see
% <matlab:helpview(fullfile(docroot,'simrf','helptargets.map'),'convert_signal');
% Converting to and from Simulink Signals>.

%   Copyright 2006-2017 The MathWorks, Inc.

%% White Noise Source
% This part of the example shows how to create a complex
% baseband-equivalent <matlab:rfb_power_examples_white_noise; White Noise
% Source>. This type of source is useful, for example, as a stimulus for
% visualizing the frequency response of an RF system.
%
% Use a Random Source block from the DSP System Toolbox(TM) Sources
% sublibrary to create this source. In the Random Source block dialog box,
% set the Complexity parameter to "Complex" and the Variance parameter
% using the following expression:
%
% |power_in_watts|
% 
% ... where |power_in_watts| is the desired noise power in watts.
%
% <<../rfb_power_examples_fig1.JPG>>

%%
% To calculate a signal power in dBm, use an RMS block (from the DSP System
% Toolbox Statistics sublibrary), followed by a dB Conversion block (from
% the DSP System Toolbox Math Functions/Math Operations sublibrary).
%
% In the dB Conversion block dialog box, set *Convert to* to "dBm", *Input
% Signal* to "Amplitude", and *Load resistance (ohms)* to 1.
%
open('rfb_power_examples_white_noise.slx')

%%
% To display the power spectrum of a signal, use the Spectrum Scope block
% (from the DSP System Toolbox Sinks sublibrary). In the Spectrum Scope
% block dialog box ("Scope Properties" tab), set the *Spectrum type:*
% parameter to "Two-sided((-Fs/2...Fs/2])". This is the desired frequency
% range because the complex baseband-equivalent representation translates
% the carrier frequency to zero hertz. The real-world frequencies above and
% below the carrier (i.e. higher and lower sidebands) are represented as
% positive and negative frequencies, respectively.
sim('rfb_power_examples_white_noise')

%%
% In addition, note that the selected Spectrum Scope Window Type can affect
% how the power is distributed amongst the channels closest to the actual
% frequency. For example, if a pure sine wave falls between two channels,
% you may need to sum the power in one or two channels either side of the
% actual frequency to determine the exact total power.

%% Complex Sine Source
% The next model, of a <matlab:rfb_power_examples_cis_wave; Complex Sine
% Wave>, shows how to use power to set the amplitude of a complex sine wave
% source block for an RF system.
%
% Complex sine wave sources are often used in baseband-equivalent Simulink
% models. These sources have the following time-domain output:
%
% |signal(t) = amplitude * (cos(2*pi*f*t+phi)+j*sin(2*pi*f*t+phi))|
%
% The mean square power of the output, |signal|, is |amplitude^2|.
%
% By contrast, the time-domain output of a real sine wave source is:
%
% |signal2(t) = amplitude * sin(2*pi*f*t+phi)|
%
% ... and the mean square power of |signal2| is |amplitude^2/2| i.e. half
% that of a complex sine wave with the same |amplitude|.
open('rfb_power_examples_cis_wave.slx')

%%
% Use the Sine Wave block to create a complex sine source. In the block
% dialog box, set the *Output complexity* parameter to "Complex" and the
% *Amplitude* parameter to:
%
% |sqrt(power_in_watts)|

%%
% By default, the Spectrum Scope displays power spectral density normalized
% to the unit sampling frequency in units of dBW/Hertz. However, for a
% sinusoid we want to display the mean square power in dBm. To do this, set
% the *Spectrum units:* parameter to "dBm" in the "Scope Properties" tab.
% The Spectrum Scope displays the tone as a positive frequency (upper side
% band).
sim('rfb_power_examples_cis_wave')

%% Two-Tone Input to Idealized Baseband Nonlinear Amplifier
% The third model, of a <matlab:rfb_power_examples_two_tone_math_amp;
% Two-Tone Input to an Idealized Baseband Nonlinear Amplifier>, shows how
% the Amplifier block in the RF Blockset Idealized Baseband library affects
% the signal.
%
% In the Amplifier block dialog box set the *IIP3 (dBm)* parameter to
% 20dBm. In the Sine Wave block dialog box, set *Amplitude* to:
%
% |sqrt(10^((power_in_dBm - 30)/10))|
%
% Setting |power_in_dBm = -10| in the model workspace results in -10dBm per
% tone. Note that we must use a Matrix Sum block with the *Sum along*
% parameter set to "Rows" after the source block to sum the two-channel
% output of the source. Without the Row Sum, a two-channel signal would be
% created, all blocks downstream would have two independent channels, and
% no mixing would occur.
open('rfb_power_examples_two_tone_math_amp.slx')


%%
sim('rfb_power_examples_two_tone_math_amp')

%%
% The Spectrum Scope displays the power level in each intermodulation tone.
% The power level of each is:
%
% |-10dBm - 2*(20dBm - -10dBm) = -70dBm|.

%% Two-Tone Input to Equivalent Baseband Nonlinear Amplifier
% Like the third model, the fourth model, of a
% <matlab:rfb_power_examples_two_tone_physical_amp; Two-Tone Input to an
% Equivalent Baseband Nonlinear Amplifier>, shows how an Amplifier block
% affects the signal. However, this time we use the Amplifier block from
% the Equivalent Baseband library of RF Blockset. Unlike the Idealized
% Baseband blocks, the Equivalent Baseband blocks allow you to set the
% Center frequency and impedances. Thus, if you want to model an RF system
% at real RF frequencies, the loading and reflection effects, we recommend
% these physical blocks.

%%
% In this model, we set several parameters to |Z0|:
%
% * *Source impedance* parameter of the Input Port block dialog box
% * *Reference impedance* parameter of the S-Parameters Amplifier block
% dialog box
% * *Load impedance* parameter of the Output Port block dialog box
open('rfb_power_examples_two_tone_physical_amp.slx')

%%
% In the Input Port block, set the *Center frequency (Hz)* parameter to 2e9
% (2GHz). The baseband frequencies of the two-tone complex Simulink signal
% are 200kHz and 300kHz. Thus, in the RF system (the Equivalent Baseband
% blocks connected between the Input Port and Output Port blocks), the real
% RF two-tone frequencies are 2.0002GHz and 2.0003GHz. By default, the
% Spectrum Scope displays in baseband. To display the desired tones at
% 2.0002GHz and 2.0003GHz (-10dBm each) and the intermodulation tones at
% 2.0001GHz and 2.0004GHz (-70dBm each), set the *Frequency display
% offset:* parameter in the "Axis Properties" tab to the value of the
% Center frequency, in this case it is 2e9 (2GHz).
sim('rfb_power_examples_two_tone_physical_amp')

%% Displaying Power Spectrum Within a Cascade of RF Blockset Equivalent Baseband Blocks  
% The Output Port block lets you create a link budget plot for multi-block
% cascades. This feature allows you to visualize the characteristics of the
% cascade non-intrusively. Therefore, it is not usually necessary to tap a
% cascade of RF Blockset Equivalent Baseband blocks. However, it is
% sometimes useful to do so, for example to see the modulated spectrum at
% an intermediate point. The final model, of a
% <matlab:rfb_power_examples_tap_cascade; Tap Cascade of Equivalent
% Baseband Blocks in RF Blockset>, achieves this with a subsystem that
% approximately models a real-world directional coupler. As with its
% real-world counterpart, the tapping is intrusive in that it presents a
% load impedance to the downstream part of the cascade and it drives the
% upstream part with a source impedance.

%%
% Double click on the subsystem "Pseudo 30dB Directional Coupler" to open
% it and see how the model works. The Output Port and Input Port blocks
% correspond to the input and output impedance of the mainline of a
% real-world directional coupler, respectively. However, the phase behavior
% of a real-world directional coupler is not modeled here.
open('rfb_power_examples_tap_cascade.slx')

%%
sim('rfb_power_examples_tap_cascade')
close_system(['rfb_power_examples_tap_cascade/Spectrum',sprintf('\n'),'Scope 2']);

%%
% The first Spectrum Scope shows the intermodulation tones after one
% amplifier. Note that the power is 30dB down because of the
% characteristics of the "Pseudo 30dB Directional Coupler" subsystem. You
% could calibrate this out with a gain block, or even modify the subsystem
% to model a 0dB-loss "active" directional coupler.

%%
open_system(['rfb_power_examples_tap_cascade/Spectrum',sprintf('\n'),'Scope 2']);

%%
% The second Spectrum Scope shows an increased level of intermodulation
% tones after a cascade of two non-linear amplifiers.
bdclose('rfb_power_examples_white_noise');
bdclose('rfb_power_examples_cis_wave');
bdclose('rfb_power_examples_two_tone_math_amp');
bdclose('rfb_power_examples_two_tone_physical_amp');
bdclose('rfb_power_examples_tap_cascade');
