%% Modeling RF Front End in Radar System Simulation
% In a radar system, the RF front end often plays an important role in
% defining the system performance. For example, since the RF front end is
% the first section in the receiver chain, the design of its low noise
% amplifier is critical to achieving the desired signal-to-noise ratio
% (SNR). This example shows how to incorporate RF front end behavior into
% an existing radar system design.
% 
% This example requires Phased Array System Toolbox(TM).

%   Copyright 2017 The MathWorks, Inc.

%% Available Example Implementations
% This example includes two Simulink(R) models:
%
% * Monostatic Radar with One Target: <matlab:simrfV2_monostatic_radar
% simrfV2_monostatic_radar.slx>
% * FMCW Radar Range and Speed Estimation: <matlab:simrfV2_fmcw_radar
% simrfV2_fmcw_radar.slx>
%

%% Introduction
% Several examples, such as 
% <matlab:web(fullfile(docroot,'phased/examples/end-to-end-monostatic-radar.html'))
% End-to-End Monostatic Radar> and 
% <matlab:web(fullfile(docroot,'phased/examples/automotive-adaptive-cruise-control-using-fmcw-and-mfsk-technology.html'))
% Automotive Adaptive Cruise Control Using FMCW and MFSK Technology>, have
% shown that one can build end-to-end radar systems in Simulink using
% Phased Array System Toolbox. In many cases, once the system model is
% built, the next step would add more fidelity in different subsystems and
% components. A popular candidate for such a component is the RF front end.
% One advantage of modeling the system in Simulink is the capability of
% performing multidomain simulations.
%
% The following sections show two examples of incorporating RF Blockset(TM)
% modeling capability in radar systems built with Phased Array System
% Toolbox.
%

%% Monostatic Radar with One Target
% The first model is adapted from example
% <matlab:web(fullfile(docroot,'phased/examples/modeling-mutual-coupling-in-large-arrays-using-embedded-element-pattern.html'))
% Modeling Mutual Coupling in Large Arrays Using Embedded Element Pattern>,
% which simulates a monostatic pulse radar with one target. From the
% diagram itself, the model below looks identical to the model shown in
% that example.
%

model = 'simrfV2_monostatic_radar';
open_system(model);

%%
% When the model is executed, the resulting plot is also the same.
%

sim(model);

%% 
% However, a deeper look at the transmitter subsystem shows that the
% transmitter now employs two RF Blockset amplifiers.
%

open_system([model '/Transmitter']);

%%
% Similar changes are also implemented on the receiver side.
%

open_system([model '/Receiver Noise']);

%%
% With these changes, the model is capable of simulating RF behaviors. For
% example, the simulation result shown above assumes a perfect power
% amplifier. In real applications, the amplifier suffers from many
% nonlinearities. If one sets the IP3 of the transmitter to 70 dB and runs
% the simulation again, the peak corresponding to the target is no longer
% as dominant. This gives the engineer some knowledge regarding the
% system's performance under different situations.
%

set_param([model '/Transmitter/PA'],'IP3','70');
sim(model);

%%
bdclose(model);
clear model;

%% FMCW Radar Range and Speed Estimation
% The second example is adapted from
% <matlab:web(fullfile(docroot,'phased/examples/automotive-adaptive-cruise-control-using-fmcw-and-mfsk-technology.html'))
% Automotive Adaptive Cruise Control Using FMCW Technology>.
% However, this model uses a triangle sweep waveform instead, so the system
% can estimate range and speed simultaneously. At the top level, the model
% is similar to what gets built from Phased Array System Toolbox. Once
% executed, the model shows the estimated range and speed values that
% match the distance and relative speed of the target car.
%

model = 'simrfV2_fmcw_radar';
open_system(model);
sim(model);

%%
% Similar to the first example, the transmitter and receiver subsystems are
% now built with RF Blockset blocks.
%
% The following figure shows the transmitter subsystem.
%

open_system([model '/Radar Transmitter']);

%%
% The following figure shows the receiver subsystem.
%

open_system([model '/Radar Receiver']);

%%
% In a continuous wave radar system, part of the transmitted waveform is
% used as a reference to dechirp the received target echo. From the
% diagrams above, one can see that the transmitted waveform is sent to the
% receiver via a coupler and the dechirping is performed via an I/Q mixer.
% Therefore, by adjusting parameters in those RF components, higher
% simulation fidelity can be achieved.
%
bdclose(model);
clear model;

%% Summary
% This example shows two radar models that were originally built with Phased
% Array System Toolbox and later incorporated RF models from RF Blockset.
% The simulation fidelity is greatly improved by combining the two products
% together.
