Main Content

Spectrum Analysis of Signals

This example shows you how to undertake spectral analysis of signals. You can either use recorded data from a file, or receive signals in real time using the RTL-SDR radio, ADALM-PLUTO radio, or USRP™ radio. You can change the radio's center frequency to tune the radio to a band where a signal is present. You can then use the spectrum analyzer to view and make measurements on the received spectrum.

For Simulink® implementation of this example, see Spectrum Analysis of Signals in Simulink.

Required Hardware and Software

By default, this example runs using recorded data from a file. Optionally, you can receive signals over-the-air. For this, you also need one of the following:

Example Code

The receiver asks for user input and initializes variables. Then, it calls the signal source and FM broadcast receiver in a loop. The loop also keeps track of the radio time using the frame duration and lost samples reported by the signal source.

For the option to change default settings, set |cmdlineInput| to 1.

cmdlineInput = false;
if cmdlineInput
%     Request user input from the command-line for application parameters
    userInput = helperSpectralAnalysisUserInput;
%     Set initial parameters
    [SAParams, sigSrc] = helperSpectralAnalysisConfig(userInput);
%     Set initial parameters
    load defaultInputSpecAnalysis.mat
    [SAParams, sigSrc] = helperSpectralAnalysisConfig;


Create spectrumAnalyzer object and configure based on user input

hSpectrum = spectrumAnalyzer(...
    'Name',             'Passband Spectrum',...
    'Title',            'Passband Spectrum', ...
    'Method',           'Welch', ...
    'SpectrumType',     'Power density', ...
    'FrequencySpan',    'Full', ...
    'SampleRate',       SAParams.FrontEndSampleRate, ...
    'SpectralAverages', 50, ...
    'FrequencyOffset',  SAParams.CenterFrequency, ...
    'YLimits',          [-120 10], ...
    'YLabel',           'Magnitude-squared, dB', ...
    'Position',         figposition([50 30 30 40]));

Stream processing

View the spectrum. While the spectrum analyzer is running, you can measure peaks, occupied bandwidth, and other properties of the signal.

% Initialize radio time
radioTime = 0;

% Main loop
while radioTime < userInput.Duration
  % Receive baseband samples (Signal Source)
  if SAParams.isSourceRadio
      if SAParams.isSourcePlutoSDR
          rcv = sigSrc();
          lost = 0;
          late = 1;
      elseif SAParams.isSourceUsrpRadio
          rcv= sigSrc();
          lost = 0;
          [rcv,~,lost,late] = sigSrc();
    rcv = sigSrc();
    lost = 0;
    late = 1;
    rcv = rcv - mean(rcv);  % Remove DC component.
    step(hSpectrum, rcv);

  % Update radio time. If there were lost samples, add those too.
  radioTime = radioTime + SAParams.FrontEndFrameTime + ...

% Release all System objects


In this example, you used System objects to analyze the spectrum of a received signal.