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 development computer and started the target computer with target scopes enabled.
Use 'slrtpingtarget' to test the connection between the development and target computers.
if ~strcmp(slrtpingtarget, 'success') error(message('xPCTarget:examples:Connection')); end
Open the model
xpcdspspectrum. 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.mldatx, that can be run on a computer booted with the Simulink Real-Time kernel.
Build the model and download the image, xpcdspspectrum.mldatx, 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. Warning: The target settings indicate a multi-core kernel. To run multi-rate models on multi-core targets consider setting the Solver checkbox "Allow tasks to execute concurrently on target" and build the model again. ### Successful completion of build procedure for model: xpcdspspectrum ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
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
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);