This example shows how to use xPC Target™ as a real-time spectrum analyzer. The example uses the model xpcdspspectrum. To examine the design and implementation of the key block, 'xPC Target 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.
Use 'xpctargetping' to test the host-target connection.
if ~strcmp(xpctargetping, 'success') error(message('xPCTarget:examples:Connection')); end
Open the model xpcdspspectrumxpcdspspectrum. Under the model's configuration parameter xPC Target option settings, the system target file has been set to xpctarget.tlc. Hence, building the model will create an executable image, xpcdspspectrum.dlm, that can be run on a computer booted with the xPC Target kernel.
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 xPC Target build procedure for model: xpcdspspectrum ### Generated code for 'xpcdspspectrum' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for model: xpcdspspectrum ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
Create the MATLAB variable, tg, containing the xPC Target Object. This object contains the information required to communicate with and control the target computer. After starting the model, the spectrum will be displayed on the target computer
tg = xpc; % Create an xPC Target Object tg.start; % Start model execution pause(1); % Wait for scope to be updated xpctargetspy; % 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 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);