| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Stateflow |
| Contents | Index |
| Learn more about Stateflow |
| On this page… |
|---|
Running the Spectrum Analyzer Model |
A spectrum analyzer is a tool that measures the frequency response (magnitude and phase angle) of a physical system over a range of frequencies.
This Simulink model measures the frequency response of a second-order system driven by a complex sinusoidal signal. A scope displays the measured frequency response as discrete Bode plots.

| Model Component | Description | Details |
|---|---|---|
| Sinusoid Generator block | Generates a complex sinusoidal signal of increasing frequency and supplies this signal to other blocks. | |
| Complex to Imaginary block | Extracts the imaginary part of the complex signal from the Sinusoid Generator block so that a sine wave of increasing frequency can drive the Plant block. | None |
| Plant block | Uses a transfer function to describe a second-order system with a natural frequency of 150 Hz (300π radians per second) and a damping ratio of 0.3. Since the ratio is less than 1, this system is underdamped and contains two complex conjugate poles in the denominator of the transfer function. | None |
| Analyzer chart | Calculates the frequency response of the second-order system defined by the Plant block. | |
| Unwrap chart | Processes the phase angle output of the Analyzer chart. |
Follow these steps to run the model:
Click sf_spectrum_analyzer or type sf_spectrum_analyzer at the MATLAB command prompt.
Double-click the Measured Frequency Response scope.
Select Simulation > Start in the Simulink model window and watch the output in the scope.
In the scope display, right-click and select Autoscale from the context menu.
The scope shows a set of discrete Bode plots.

In the magnitude plot, the sharp peak is the response of the Plant block to a resonant frequency.
In the phase plot, the angle changes from 0 to -π radians (-180 degrees). Each complex pole in the Plant block adds -π/2 radians to the phase angle.
This block is a masked subsystem that contains a Stateflow chart. To access the chart, right-click the Sinusoid Generator block and select Look Under Mask from the context menu.
Key features of the signal generator chart include:
Absolute-time temporal logic for controlling changes in frequency (see Operators for Absolute-Time Temporal Logic)
Embedded MATLAB function that generates a complex signal (see Using Embedded MATLAB Functions in Stateflow Charts)
Transition condition that contains complex operands (see Transition Action Types)

When the chart awakens, the default transition sets the signal frequency f to fstart and activates state A.
Note To set fstart, double-click the Sinusoid Generator block and enter a value (in Hz) in the Initial frequency field. |
While state A is active, the Embedded MATLAB function computey generates the complex signal y based on frequency f and simulation time t.
Code for the function appears below:
function computey(t) yprev = y; y = exp(2*pi*f*t*1j);
If delay seconds have elapsed since activation of state A, the frequency f increases by an amount fstep and the Embedded MATLAB function computey generates a new signal.
Updates occur until the frequency f reaches the value fstop.
Note To set delay, double-click the Sinusoid Generator block and enter a value (in seconds) in the Delay at each frequency field. To set fstep, enter a value (in Hz) in the Step frequency field. |
When the frequency f reaches the value fstop, the state Stopped becomes active. The complex signal terminates and the simulation ends.
Note To set fstop, double-click the Sinusoid Generator block and enter a value (in Hz) in the Stop frequency field. |
Key features of the Analyzer chart include:
Change detection of input frequency (see Using Change Detection in Actions)
Embedded MATLAB function that processes complex data (see Using Embedded MATLAB Functions in Stateflow Charts)
State during action that contains complex operands (see State Action Types)

When the chart awakens, the values of y and yn initialize to zero.
The data y stores the second-order system response to a signal from the Sinusoid Generator block.
The data yn stores an input signal of a given frequency.
For a given frequency, the Embedded MATLAB function computeOutputs finds the magnitude and phase angle of the system response.
Code for the function appears below:
function computeOutputs mag = abs(y)/abs(yn); ang = -angle(y) + pi/2;
The hasChanged operator detects if the input frequency f has changed since the previous time step. If so, the Embedded MATLAB function calculates the magnitude and phase angle for the new frequency.
This chart unwraps the phase angle output of the Analyzer chart. Unwrapping means preventing the phase angle from jumping more than π radians or dropping more than -π radians.

If the phase angle jumps more than π radians, the chart subtracts 2π radians from the angle.
If the phase angle drops more than -π radians, the chart adds 2π radians to the angle.
![]() | Implementing a Frame Synchronization Controller Using a Stateflow Chart | Defining Interfaces to Simulink Models and the MATLAB Workspace | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |