Modelica.Math.FastFourierTransform

Library of functions for the Fast Fourier Transform (FFT)

Information

This package provides functions to compute the Fast Fourier Transform (FFT).

For an example see Examples.RealFFT1 where the following signal is computed during simulation

y = 5 + 3*sin(2*pi*2) + 1.5*cos(2*pi*3)

the continuous-time signal y is sampled and the FFT is computed with a call to realFFT(f_max=4, f_resolution=0.2), resulting in:

References

Mark Borgerding (2010):
KissFFT, version 1.3.0. http://sourceforge.net/projects/kissfft/.
 
James W. Cooley, John W. Tukey (1965):
An algorithm for the machine calculation of complex Fourier series. Math. Comput. 19: 297-301. doi:10.2307/2003354.
 
Martin R. Kuhn, Martin Otter, Tim Giese (2015):
Model Based Specifications in Aircraft Systems Design. Modelica 2015 Conference, Versailles, France, pp. 491-500, Sept.23-25, 2015. Download from: http://www.ep.liu.se/ecp/118/053/ecp15118491.pdf

Extends from Modelica.Icons.Package (Icon for standard packages).

Package Content

Name Description
Modelica.Math.FastFourierTransform.Examples Examples Examples demonstrating the usage of the Math.FastFourierTransform functions
Modelica.Math.FastFourierTransform.realFFTinfo realFFTinfo Print information about real FFT for given f_max and f_resolution
Modelica.Math.FastFourierTransform.realFFTsamplePoints realFFTsamplePoints Return number of sample points for a real FFT
Modelica.Math.FastFourierTransform.realFFT realFFT Return amplitude and phase vectors for a real FFT
Modelica.Math.FastFourierTransform.realFFTwriteToFile realFFTwriteToFile Write real FFT computation to file
Modelica.Math.FastFourierTransform.Internal Internal Internal library that should not be used directly by a user

Modelica.Math.FastFourierTransform.realFFTinfo Modelica.Math.FastFourierTransform.realFFTinfo

Print information about real FFT for given f_max and f_resolution

Information

Syntax

realFFTinfo(f_max, f_resolution, f_max_factor=5);

Description

From the maximum interested frequency f_max (in [Hz]) and the frequency resolution f_resolution (in [Hz]) the function computes the key FFT data as used by the FFT blocks and prints them to the output window.

Example

realFFTinfo(f_max=170, f_resolution=0.3)

results in the following output:

... Real FFT properties
 Desired:
    f_max         = 170 Hz
    f_resolution  = 0.3 Hz
    f_max_factor  = 5
 Calculated:
    Number of sample points    = 5760 (= 2^7*3^2*5^1)
    Sampling frequency         = 1728 Hz (= 0.3*5760)
    Sampling period            = 0.000578704 s (= 1/1728)
    Maximum FFT frequency      = 864 Hz (= 0.3*5760/2; f={0,0.3,0.6,...,864} Hz)
    Number of frequency points = 2881 (= 5760/2+1)
    Simulation time            = 3.33275 s

See also

realFFTsamplePoints, realFFT, realFFTwriteToFile

Extends from Modelica.Icons.Function (Icon for functions).

Inputs

NameDescription
f_maxMaximum frequency [Hz]
f_resolutionFrequency resolution [Hz]
f_max_factorMaximum FFT frequency >= f_max*f_max_factor (sample frequency = 2*Maximum FFT Frequency)

Modelica.Math.FastFourierTransform.realFFTsamplePoints Modelica.Math.FastFourierTransform.realFFTsamplePoints

Return number of sample points for a real FFT

Information

Syntax

ns = realFFTsamplePoints(f_max, f_resolution, f_max_factor=5);

Description

From the maximum interested frequency f_max (in [Hz]) and the frequency resolution f_resolution (in [Hz]) the function computes the number of sample points ns that is as small as possible and fulfills the following criteria:

Note, in the original publication about the efficient computation of FFT (Cooley and Tukey, 1965), the number of sample points must be 2^a. However, all newer FFT algorithms do not have this strong restriction and especially not the open source software KissFFT from Mark Borgerding used in this function

References

Mark Borgerding (2010):
KissFFT, version 1.3.0. http://sourceforge.net/projects/kissfft/.
 
James W. Cooley, John W. Tukey (1965):
An algorithm for the machine calculation of complex Fourier series. Math. Comput. 19: 297-301. doi:10.2307/2003354.
 
Martin R. Kuhn, Martin Otter, Tim Giese (2015):
Model Based Specifications in Aircraft Systems Design. Modelica 2015 Conference, Versailles, France, pp. 491-500, Sept.23-25, 2015. Download from: http://www.ep.liu.se/ecp/118/053/ecp15118491.pdf

Example

ns = realFFTinfo(f_max=170, f_resolution=0.3)

results in the following output:

ns = 5760

See also

realFFTinfo, realFFT, realFFTwriteToFile

Extends from Modelica.Icons.Function (Icon for functions).

Inputs

NameDescription
f_maxMaximum frequency of interest [Hz]
f_resolutionFrequency resolution [Hz]
f_max_factorMaximum FFT frequency >= f_max*f_max_factor (sample frequency = 2*Maximum FFT Frequency)

Outputs

NameDescription
nsNumber of sample points that can be expressed as ns = 2^i*3^j*5^k and ns is even

Modelica.Math.FastFourierTransform.realFFT Modelica.Math.FastFourierTransform.realFFT

Return amplitude and phase vectors for a real FFT

Information

Syntax

(info, amplitudes, phases) = realFFT(u);

Description

The input argument of this function is a Real vector u. size(u,1) must be even. An efficient computation is performed, if size(u,1) = 2^a*3^b*5^c (a,b,c Integer ≥ 0). An appropriate length of vector u can be computed with function realFFTsamplePoints. Function realFFT computes a real FFT (Fast Fourier Transform) of u and returns the result in form of the outputs amplitudes and phases. Argument info provides additional information:

info = 0: Successful FFT computation.
info = 1: size(u,1) is not even.
info = 3: Another error.

Note, in the original publication about the efficient computation of FFT (Cooley and Tukey, 1965), the number of sample points must be 2^a. However, all newer FFT algorithms do not have this strong restriction and especially not the open source software KissFFT from Mark Borgerding used in this function.

The function returns the FFT such that amplitudes[1] is the mean value of u (= sum(u)/size(u,1)), and amplitudes[i] is the amplitude of a sine-function at the i-th frequency.

References

Mark Borgerding (2010):
KissFFT, version 1.3.0. http://sourceforge.net/projects/kissfft/.
 
James W. Cooley, John W. Tukey (1965):
An algorithm for the machine calculation of complex Fourier series. Math. Comput. 19: 297-301. doi:10.2307/2003354.
 
Martin R. Kuhn, Martin Otter, Tim Giese (2015):
Model Based Specifications in Aircraft Systems Design. Modelica 2015 Conference, Versailles, France, pp. 491-500, Sept.23-25, 2015. Download from: http://www.ep.liu.se/ecp/118/053/ecp15118491.pdf

Example

(info, A) = realFFT({0,0.1,0.2,0.4,0.5, 0.6})

See also Examples.RealFFT1 which is a complete example where an FFT is computed during simulation and stored on file.

See also

realFFTinfo, realFFTsamplePoints, realFFTwriteToFile

Extends from Modelica.Icons.Function (Icon for functions).

Inputs

NameDescription
u[:]Signal for which FFT shall be computed (size(nu,1) MUST be EVEN and should be an integer multiple of 2,3,5, that is size(nu,1) = 2^a*3^b*5^c, with a,b,c Integer >= 0)
nfiNumber of frequency points that shall be returned in amplitudes and phases (typically: nfi = max(1,min(integer(ceil(f_max/f_resolution))+1,nf))); the maximal possible value is nfi=div(size(u,1),2)+1)

Outputs

NameDescription
infoInformation flag (0: FFT computed, 1: nu is not even, 3: another error)
amplitudes[nfi]Amplitudes of FFT
phases[nfi]Phases of FFT in [deg]

Modelica.Math.FastFourierTransform.realFFTwriteToFile Modelica.Math.FastFourierTransform.realFFTwriteToFile

Write real FFT computation to file

Information

Syntax

success = realFFTwriteToFile(t_computed, fileName, f_max, amplitudes, phases, format);

Description

This functions stores the result of an FFT computation on file, so that it can be easily plotted. amplitudes and phases are the vectors that hold the amplitudes and phases values of an FFT computation. If the size of the phases vector is zero, no phases will be stored on file. Otherwise, phases must have the same dimension as the amplitudes vector. The frequency vector f is constructed within the function from the dimension of the amplitudes vector and the information that amplitudes[end] is at frequency f_max. The format argument defines the file format (for details see writeRealMatrix). Argument t_computed is the actual time instant when the FFT was computed. It is used in the print message after the result was stored on file.

The matrix on file has the following structure:

Example

See detailed example model: Examples.RealFFT1.

See also

realFFTinfo, realFFTsamplePoints, realFFT

Extends from Modelica.Icons.Function (Icon for functions).

Inputs

NameDescription
t_computedTime instant at which the FFT was computed
fileNameFile where FFT shall be stored (if it exists, it is deleted and then re-created)
f_maxMaximum frequency [Hz]
amplitudes[:]Amplitudes of FFT
phases[:]Phases of FFT (either provide no argument, or a vector with the same length as amplitudes)
formatMATLAB MAT-file version: "4" -> v4, "6" -> v6, "7" -> v7

Outputs

NameDescription
success= true, if successful
Automatically generated Thu Oct 1 16:08:16 2020.