dsp.FFT
Discrete Fourier transform
Description
The dsp.FFT
System object™ computes the discrete Fourier transform (DFT) of an input using fast Fourier
transform (FFT). The object uses one or more of the following fast Fourier transform (FFT)
algorithms depending on the complexity of the input and whether the output is in linear or
bitreversed order:
To compute the DFT of an input:
Create the
dsp.FFT
object and set its properties.Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?
Creation
Description
returns a ft
= dsp.FFTFFT
object, ft
, that computes the DFT of
an ND array. For column vectors or multidimensional arrays, the
FFT
object computes the DFT along the first dimension.
If the input is a row vector, the FFT
object computes a
row of singlesample DFTs and issues a warning.
returns a ft
= dsp.FFT(Name,Value
)FFT
object with each specified property set to
the specified value. Enclose each property name in single quotes. Unspecified properties
have default values.
Properties
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release
function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects.
FFTImplementation
— FFT implementation
Auto
(default)  Radix2
 FFTW
Specify the implementation used for the FFT as one of Auto
,
Radix2
, or FFTW
. When you set this property to
Radix2
, the FFT length must be a power of two.
BitReversedOutput
— Order of output elements relative to input elements
false
(default)  true
Designate order of output channel elements relative to order of input elements. Set
this property to true
to output the frequency indices in bitreversed
order. The default is false
, which corresponds to a linear ordering
of frequency indices.
Normalize
— Divide butterfly outputs by two
false
(default)  true
Set this property to true
if the output of the FFT should be
divided by the FFT length. This option is useful when you want the output of the FFT to
stay in the same amplitude range as its input. This is particularly useful when working
with fixedpoint data types.
The default value of this property is false
with no
scaling.
FFTLengthSource
— Source of FFT length
Auto
(default)  Property
Specify how to determine the FFT length as Auto
or
Property
. When you set this property to Auto
,
the FFT length equals the number of rows of the input signal.
FFTLength
— FFT length
64
(default)  integer
FFT length, specified as an integer greater than or equal to 2.
This property must be a power of two if any of these conditions apply:
The input is a fixedpoint data type.
The BitReversedOutput property is
true
.The FFTImplementation property is
Radix2
.
Dependencies
This property applies when you set the FFTLengthSource
property to Property
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
WrapInput
— Boolean value of wrapping or truncating input
true
(default)  false
Wrap input data when FFT length is shorter than input length. If this property is set to true, modulolength data wrapping occurs before the FFT operation, given FFT length is shorter than the input length. If this property is set to false, truncation of the input data to the FFT length occurs before the FFT operation.
FixedPoint Properties
RoundingMethod
— Rounding method for fixedpoint operations
Floor
(default)  Ceiling
 Convergent
 Nearest
 Round

Simplest
 Zero
Specify the rounding method.
OverflowAction
— Overflow action for fixedpoint operations
Wrap
(default)  Saturate
Specify the overflow action as Wrap
or
Saturate
.
SineTableDataType
— Sine table word and fraction lengths
Same word length as input
(default)  Custom
Specify the sine table data type as Same word length as input
or Custom
.
CustomSineTableDataType
— Sine table word and fraction lengths
numerictype([],16)
(default)  numerictype
Specify the sine table fixedpoint type as an unscaled numerictype
(FixedPoint Designer) object with a Signedness
of
Auto
.
Dependencies
This property applies when you set the SineTableDataType
property to Custom
.
ProductDataType
— Product word and fraction lengths
Full precision
(default)  Same as input
 Custom
Specify the product data type as Full precision
, Same
as input
, or Custom
.
CustomProductDataType
— Product word and fraction lengths
numerictype([],32,30)
(default)  numerictype
Specify the product fixedpoint type as a scaled numerictype
(FixedPoint Designer) object with a Signedness
of
Auto
.
Dependencies
This property applies when you set the ProductDataType
property to Custom
.
AccumulatorDataType
— Accumulator word and fraction lengths
Full precision
(default)  Same as input
 Same as product
 Custom
Specify the accumulator data type as Full precision
,
Same as input
, Same as product
, or
Custom
.
CustomAccumulatorDataType
— Accumulator word and fraction lengths
numerictype([],32,30)
(default)  numerictype
Specify the accumulator fixedpoint type as a scaled numerictype
(FixedPoint Designer) object with a Signedness
of
Auto
.
Dependencies
This property applies when you set the AccumulatorDataType
property to Custom
.
OutputDataType
— Output word and fraction lengths
Full precision
(default)  Same as input
 Custom
Specify the output data type as one of Full precision
,
Same as input
, Custom
.
CustomOutputDataType
— Output word and fraction lengths
numerictype([],16,15)
(default)  numerictype
Specify the output fixedpoint type as a scaled numerictype
(FixedPoint Designer) object with a Signedness
of
Auto
.
Dependencies
This property applies when you set the OutputDataType property to
Custom
.
Usage
Syntax
Input Arguments
x
— Timedomain input signal
vector  matrix  ND array
Timedomain input signal, specified as a vector, matrix, or ND array.
When the FFTLengthSource
property is set to
'Auto'
, the length of x
along the first
dimension must be a positive integer power of two. This length is also the FFT length.
When the FFTLengthSource
property is
'Property'
, the value you specify in FFTLength
property must be a positive integer power of two.
Variablesize input signals are only supported when the
FFTLengthSource
property is set to
'Auto'
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 fi
Complex Number Support: Yes
Output Arguments
y
— Discrete Fourier transform of input signal
vector  matrix  ND array
Discrete Fourier transform of input signal, returned as a vector, matrix, or an
ND array. When FFTLengthSource
property is
set to 'Auto'
, the FFT length is same as the number of rows in the
input signal. When FFTLengthSource
property is set to
'Property'
, the FFT length is specified through the
FFTLength
property.
To support nonpoweroftwo transform lengths with variablesize data, set the
FFTImplementation
property to 'FFTW'
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 fi
Complex Number Support: Yes
Object Functions
To use an object function, specify the
System object as the first input argument. For
example, to release system resources of a System object named obj
, use
this syntax:
release(obj)
Examples
SingleSided Amplitude Spectrum of Signal
Find frequency components of a signal in additive noise.
Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step
syntax. For example, myObject(x) becomes step(myObject,x).
Fs = 800; L = 1000; t = (0:L1)'/Fs; x = sin(2*pi*250*t) + 0.75*cos(2*pi*340*t); y = x + .5*randn(size(x)); % noisy signal ft = dsp.FFT('FFTLengthSource','Property', ... 'FFTLength',1024); Y = ft(y);
Plot the singlesided amplitude spectrum
plot(Fs/2*linspace(0,1,512), 2*abs(Y(1:512)/1024)) title('Singlesided amplitude spectrum of noisy signal y(t)') xlabel('Frequency (Hz)'); ylabel('Y(f)')
Construct a Sinusoidal Signal Using High Energy FFT Coefficients
Compute the FFT of a noisy sinusoidal input signal. The energy of the signal is stored as the magnitude square of the FFT coefficients. Determine the FFT coefficients which occupy 99.99% of the signal energy and reconstruct the timedomain signal by taking the IFFT of these coefficients. Compare the reconstructed signal with the original signal.
Note
:
If you are using R2016a or an earlier release, replace each call to the object with the equivalent step
syntax. For example, obj(x)
becomes step(obj(x))
.
Consider a timedomain signal $\mathit{x}\left[\mathit{n}\right]$, which is defined over the finite time interval $0\le \mathit{n}\le \mathit{N}1$. The energy of the signal $\mathit{x}\left[\mathit{n}\right]$ is given by the following equation:
$${\mathit{E}}_{\mathit{N}}\text{\hspace{0.17em}}=\text{\hspace{0.17em}}{\sum}_{\mathit{n}=0}^{\mathit{N}1}{\left\mathit{x}\left[\mathit{n}\right]\right}^{2}$$
FFT coefficients, $\mathit{X}\left[\mathit{k}\right]$, are considered signal values in the frequency domain. The energy of the signal $\mathit{x}\left[\mathit{n}\right]$ in the frequencydomain is therefore the sum of the squares of the magnitude of the FFT coefficients:
$${\mathit{E}}_{\mathit{N}}\text{\hspace{0.17em}}=\text{\hspace{0.17em}}\frac{1}{\mathit{N}}{\sum}_{\mathit{k}=0}^{\mathit{N}1}{\left\mathit{X}\left[\mathit{k}\right]\right}^{2}$$
According to Parseval's theorem, the total energy of the signal in time or frequencydomain is the same.
$${\mathit{E}}_{\mathit{N}}\text{\hspace{0.17em}}=\text{\hspace{0.17em}}{\sum}_{\mathit{n}=0}^{\mathit{N}1}{\left\mathit{x}\left[\mathit{n}\right]\right}^{2}\text{\hspace{0.17em}}=\text{\hspace{0.17em}}\frac{1}{\mathit{N}}{\sum}_{\mathit{k}=0}^{\mathit{N}1}{\left\mathit{X}\left[\mathit{k}\right]\right}^{2}\text{\hspace{0.17em}}$$
Initialization
Initialize a dsp.SineWave
System object to generate a sine wave sampled at 44.1 kHz and has a frequency of 1000 Hz. Construct a dsp.FFT
and dsp.IFFT
objects to compute the FFT and the IFFT of the input signal.
The 'FFTLengthSource'
property of each of these transform objects is set to 'Auto'
. The FFT length is hence considered as the input frame size. The input frame size in this example is 1020, which is not a power of 2, so select the 'FFTImplementation'
as 'FFTW'
.
L = 1020; Sineobject = dsp.SineWave('SamplesPerFrame',L,... 'PhaseOffset',10,... 'SampleRate',44100,... 'Frequency',1000); ft = dsp.FFT('FFTImplementation','FFTW'); ift = dsp.IFFT('FFTImplementation','FFTW',... 'ConjugateSymmetricInput',true); rng(1);
Streaming
Stream in the noisy input signal. Compute the FFT of each frame and determine the coefficients that constitute 99.99% energy of the signal. Take IFFT of these coefficients to reconstruct the timedomain signal.
numIter = 1000; for Iter = 1:numIter Sinewave1 = Sineobject(); Input = Sinewave1 + 0.01*randn(size(Sinewave1)); FFTCoeff = ft(Input); FFTCoeffMagSq = abs(FFTCoeff).^2; EnergyFreqDomain = (1/L)*sum(FFTCoeffMagSq); [FFTCoeffSorted, ind] = sort(((1/L)*FFTCoeffMagSq),... 1,'descend'); CumFFTCoeffs = cumsum(FFTCoeffSorted); EnergyPercent = (CumFFTCoeffs/EnergyFreqDomain)*100; Vec = find(EnergyPercent > 99.99); FFTCoeffsModified = zeros(L,1); FFTCoeffsModified(ind(1:Vec(1))) = FFTCoeff(ind(1:Vec(1))); ReconstrSignal = ift(FFTCoeffsModified); end
99.99% of the signal energy can be represented by the number of FFT coefficients given by Vec(1)
:
Vec(1)
ans = 296
The signal is reconstructed efficiently using these coefficients. If you compare the last frame of the reconstructed signal with the original timedomain signal, you can see that the difference is very small and the plots match closely.
max(abs(InputReconstrSignal))
ans = 0.0431
plot(Input,'*'); hold on; plot(ReconstrSignal,'o'); hold off;
Algorithms
This object implements the algorithm, inputs, and outputs described on the FFT block reference page. The object properties correspond to the block parameters.
References
[1] FFTW (https://www.fftw.org
)
[2] Frigo, M. and S. G. Johnson, “FFTW: An Adaptive Software Architecture for the FFT,” Proceedings of the International Conference on Acoustics, Speech, and Signal Processing, Vol. 3, 1998, pp. 13811384.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
System Objects in MATLAB Code Generation (MATLAB Coder).
When the following conditions apply, the executable generated from this System object relies on prebuilt dynamic library files (
.dll
files) included with MATLAB^{®}:FFTImplementation
is set to'FFTW'
.FFTImplementation
is set to'Auto'
,FFTLengthSource
is set to'Property'
, andFFTLength
is not a power of two.
Use the
packNGo
function to package the code generated from this System object and all the relevant files in a compressed zip file. Using this zip file, you can relocate, unpack, and rebuild your project in another development environment where MATLAB is not installed. For more details, see How To Run a Generated Executable Outside MATLAB.When the FFT length is a power of two, you can generate standalone C and C++ code from this System object.
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)