Products & Services Solutions Academia Support User Community Company

Learn more about Stateflow   

Implementing a Spectrum Analyzer Using a Stateflow Chart

What Is a Spectrum Analyzer?

A spectrum analyzer is a tool that measures the frequency response (magnitude and phase angle) of a physical system over a range of frequencies.

A Spectrum Analyzer Model

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 ComponentDescriptionDetails
Sinusoid Generator blockGenerates a complex sinusoidal signal of increasing frequency and supplies this signal to other blocks.

How the Sinusoid Generator Block Works

Complex to Imaginary blockExtracts 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 blockUses 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.

    Note   Typical applications implement the Plant block using a D/A (digital-to-analog) converter on the input signal and an A/D (analog-to-digital) converter on the output signal.

None
Analyzer chartCalculates the frequency response of the second-order system defined by the Plant block.

How the Analyzer Chart Works

Unwrap chartProcesses the phase angle output of the Analyzer chart.

How the Unwrap Chart Works

Running the Spectrum Analyzer Model

Follow these steps to run the model:

  1. Click sf_spectrum_analyzer or type sf_spectrum_analyzer at the MATLAB command prompt.

  2. Double-click the Measured Frequency Response scope.

  3. Select Simulation > Start in the Simulink model window and watch the output in the scope.

  4. 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.

How the Sinusoid Generator Block Works

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:

Stage 1: Definition of Signal Frequency

When the chart awakens, the default transition sets the signal frequency f to fstart and activates state A.

Stage 2: Generation of Complex Signal

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);

Stage 3: Update of Frequency and Complex Signal

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.

Stage 4: Termination of Complex Signal

When the frequency f reaches the value fstop, the state Stopped becomes active. The complex signal terminates and the simulation ends.

How the Analyzer Chart Works

Key features of the Analyzer chart include:

Stage 1: Activation of State A

When the chart awakens, the values of y and yn initialize to zero.

Stage 2: Calculation of Frequency Response

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;

Stage 3: Change Detection of Input Frequency

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.

How the Unwrap Chart Works

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.

  


Related Products & Applications

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