Quantcast

Simulink Real-Time

Spectrum Analyzer

This example shows how to use Simulink® Real-Time™ as a real-time spectrum analyzer. The example uses the model xpcdspspectrum. To examine the design and implementation of the key block, 'Spectrum Scope', right-click the block and select 'Mask >> Look Under Mask'.

The example displays the Fast Fourier Transform (FFT) of the input signal using a buffer of 512 samples. The input signal is the sum of two sine waves, one with an amplitude of 0.6 and a frequency of 250 Hz, the other with an amplitude of 0.25 and a frequency of 600 Hz. The resulting spectrum is displayed in a scope of type 'Target' on the target computer monitor.

The example also shows how you can use MATLAB™ language to change the amplitude and frequency of the input sine waves while the application is running.

To run the example, you must have installed DSP System Toolbox™ on your host computer and booted the target computer with target scopes enabled.

Check Connection Between Host and Target Computer

Use 'slrtpingtarget' to test the host-target connection.

if ~strcmp(slrtpingtarget, 'success')
  error(message('xPCTarget:examples:Connection'));
end

Open, Build, and Download Model to the Target Computer

Open the model xpcdspspectrumxpcdspspectrum. Under the model's configuration parameter Simulink Real-Time option settings, the system target file has been set to slrt.tlc. Hence, building the model will create an executable image, xpcdspspectrum.dlm, that can be run on a computer booted with the Simulink Real-Time kernel.

open_system('xpcdspspectrum');

Build the model and download the image, xpcdspspectrum.dlm, to the target computer.

set_param('xpcdspspectrum','RTWVerbose','off'); % Configure for a non-Verbose build.
rtwbuild('xpcdspspectrum');                     % Build and download application.
### Starting Simulink Real-Time build procedure for model: xpcdspspectrum
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: xpcdspspectrum
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model and Plot Spectrum Data

Create the MATLAB® variable, tg, containing the Simulink Real-Time target object. This object allows you to communicate with and control the target computer. After starting the model, the spectrum will be displayed on the target computer screen

tg = slrt;                              % Create an Simulink Real-Time Object
tg.start;                               % Start model execution
pause(1);                               % Wait for scope to be updated
tg.viewTargetScreen;                    % Get spectrum plot
tg.StopTime = 600;
disp('Note: Model will continue to run for 600 seconds. To stop execution, type tg.stop')
Note: Model will continue to run for 600 seconds. To stop execution, type tg.stop

Changing Signal Characteristics

You can change the amplitude and frequency of the sine wave generators while the application is running. To do this, first call getparamid with the target object, the block name, and the parameter name to get the parameter object. Then, call setparam with the target object, the parameter object, and the new value.

s1amp = getparamid(tg, 'Sine 1', 'Amplitude');

setparam(tg, s1amp, 0.3);

By repeated use of the getparamid and setparam commands. you can monitor and vary the input signals in real time.

s1fre = getparamid(tg, 'Sine 1', 'Frequency');

setparam(tg, s1fre, 300);

s2amp = getparamid(tg, 'Sine 2', 'Amplitude');

setparam(tg, s2amp, 0.55);

s2fre = getparamid(tg, 'Sine 2', 'Frequency');

setparam(tg, s2fre, 500);