0
0
SimulinkHow-ToBeginner · 4 min read

How to Use FFT in Simulink: Step-by-Step Guide

In Simulink, use the FFT block from the DSP System Toolbox to compute the Fast Fourier Transform of a signal. Connect your input signal to the FFT block, configure the FFT length, and then use scopes or sinks to visualize the frequency components.
📐

Syntax

The FFT block in Simulink computes the discrete Fourier transform of an input signal. Key parameters include:

  • Input signal: The time-domain signal you want to analyze.
  • FFT length: Number of points for the FFT calculation, usually a power of two.
  • Output: The frequency-domain representation of the input signal.

You can find the FFT block under DSP System Toolbox > Transforms.

matlab
FFT(input_signal, FFT_length)
💻

Example

This example shows how to use the FFT block to analyze a sine wave signal in Simulink.

  • Generate a sine wave using the Sine Wave block.
  • Connect it to the FFT block.
  • Set the FFT length to 256.
  • Use a Complex to Magnitude-Angle block to get magnitude.
  • Display the magnitude using a Scope block.
matlab
% Open a new Simulink model
model = 'fft_example';
new_system(model);
open_system(model);

% Add Sine Wave block
add_block('simulink/Sources/Sine Wave',[model '/Sine Wave']);
set_param([model '/Sine Wave'], 'Frequency', '50', 'SampleTime', '1/1000');

% Add FFT block from DSP System Toolbox
add_block('dspmlti4/Transforms/FFT',[model '/FFT']);
set_param([model '/FFT'], 'FFTLengthSource', 'Property', 'FFTLength', '256');

% Add Complex to Magnitude-Angle block
add_block('dspmlti4/Math Operations/Complex to Magnitude-Angle',[model '/Complex to Mag-Angle']);

% Add Scope block
add_block('simulink/Sinks/Scope',[model '/Scope']);

% Connect blocks
add_line(model, 'Sine Wave/1', 'FFT/1');
add_line(model, 'FFT/1', 'Complex to Mag-Angle/1');
add_line(model, 'Complex to Mag-Angle/1', 'Scope/1');

% Run the simulation
set_param(model, 'StopTime', '0.1');
sim(model);

% The Scope will show the magnitude spectrum of the sine wave signal.
Output
A Simulink Scope window opens displaying the magnitude spectrum with a peak at 50 Hz, showing the sine wave frequency component.
⚠️

Common Pitfalls

  • Incorrect FFT length: Using an FFT length smaller than the signal length can truncate data, while a non-power-of-two length may slow computation.
  • Ignoring sample time: The sample time of the input signal affects frequency resolution; set it correctly.
  • Not converting complex output: The FFT output is complex; use blocks like Complex to Magnitude-Angle to interpret results.
  • Forgetting to set FFT length source: Ensure the FFT length source is set to 'Property' if you want to specify length manually.
matlab
%% Wrong way: FFT length source set to 'Input port' but no input provided
set_param([model '/FFT'], 'FFTLengthSource', 'Input port');

%% Right way: Set FFT length source to 'Property' and specify length
set_param([model '/FFT'], 'FFTLengthSource', 'Property', 'FFTLength', '256');
📊

Quick Reference

Here is a quick summary for using the FFT block in Simulink:

StepDescription
Add FFT blockFrom DSP System Toolbox > Transforms
Set FFT lengthChoose power of two, e.g., 256 or 512
Connect inputFeed your time-domain signal
Convert outputUse Complex to Magnitude-Angle for magnitude
VisualizeUse Scope or other sinks to see results

Key Takeaways

Use the FFT block from DSP System Toolbox to transform signals to frequency domain in Simulink.
Set the FFT length property to a power of two for efficient and accurate results.
Always convert the complex FFT output to magnitude and phase for meaningful analysis.
Match the sample time of your input signal to get correct frequency resolution.
Use scopes or sinks to visualize the frequency spectrum after FFT.