This example shows how to use Simulink® Real-Time™ as a real-time spectrum analyzer. The example uses the model
slrtex_dsp_spectrum. To examine the design and implementation of the key block, 'Spectrum Analyzer', 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 the Simulation Data Inspector and on a Simulink Scope block.
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 development computer and started the target computer.
Use 'slrtpingtarget' to test the connection between the development and target computers.
if ~strcmp(slrtpingtarget,"success") disp('No connection to target computer') else disp('Successful connection to target computer') end
Successful connection to target computer
Open the model
slrtex_dsp_spectrum. 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, slrtex_dsp_spectrum.mldatx, that can be run on a computer booted with the Simulink Real-Time kernel.
Build the model and download the image, slrtex_dsp_spectrum.mldatx, to the target computer.
Configure for a non-Verbose build.
Build and download application.
### Starting Simulink Real-Time build procedure for model: slrtex_dsp_spectrum 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: slrtex_dsp_spectrum ### Created MLDATX ..\slrtex_dsp_spectrum.mldatx
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 in the Simulation Data Inspector.
Create an Simulink Real-Time Object
Start model execution
Wait for SDI to be updated
View spectrum plot
tg = slrt('TargetPC1'); load(tg,'slrtex_dsp_spectrum'); start(tg); pause(1); tg.StopTime = 30; disp('Note: Model will continue to run for 30 seconds. To stop execution, type tg.stop')
Note: Model will continue to run for 30 seconds. To stop execution, type tg.stop
To view the plotted signal data, open the Simulation Data Inspector.
This image shows an example view on the Simulink Scope.
This image shows an example view on the Simulation Data Inspector.
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
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);