Fast Fourier Transform in MATLAB
Last Updated :
28 Nov, 2022
Fast Fourier Transform is an algorithm for calculating the Discrete Fourier Transformation of any signal or vector. This is done by decomposing a signal into discrete frequencies. We shall not discuss the mathematical background of the same as it is out of this article’s scope. MATLAB provides a built-in function to calculate the Fast Fourier Transform of a signal, the FFT function.
Syntax:
A = fft(X, n, dim)
Where A stores the Discrete Fourier Transformation (DFT) if signal X, which could be a vector, matrix, or multidimensional array. n specifies the n-point DFT for X, if not specified then, it is for all points in X. ‘dim’ specifies the dimension across which the DFT is to be calculated in N-D arrays.
Let us see this in practice for vectors as they are the more practical way of signal processing. We will compute the DFT of a dummy sinusoidal wave corrupted with some random error.
Example 1:
Matlab
ax1 = axes( 'Position' ,[0.1 0.1 0.45 0.45]);
ax2=axes( 'Position' ,[0.6 0.6 0.35 0.35]);
freq = 23;
afreq = 2*pi*freq;
amp = 0.23;
phase = pi/4;
T = linspace(-pi,pi,100);
signal = amp*sin((afreq * T) + phase);
signal = signal + 0.23*random(size(T));
plot(ax1,T,signal),title(ax1, "Original signal" )
y=fft(signal);
plot(ax2,T,y),title(ax2, "FFT signal" )
|
Output:
FFT of a random sinusoidal signal
Let us take another example where we will compute the DFT of a rectangular pulse using FFT.
Example 2:
Matlab
x=linspace(-2.5,2.5,300);
y=rectpuls(x,1);
k=fft(y);
ax1=axes( 'Position' ,[0.03 0.03 0.3 0.3]);
ax2=axes( 'Position' ,[0.39 0.39 0.5 0.5]);
plot(ax1,x,y)
axis(ax1,[-1 1 -0.1 1.23]),title(ax1, "Rectangular pulse" )
plot(ax2,x,k)
axis(ax2,[-1 1 -.9 .9]),title(ax2, "DFT of rectangular pulse" )
|
Output:
Now, we will Fast Fourier Transform a matrix.
Example 3:
Matlab
mat = exprnd(1,3);
fft_mat_entire = fft(mat);
fft_mat_dim1 = fft(mat,[],1);
fft_mat_dim2 = fft(mat,[],2);
|
Output:
The output will give us Fourier Transform in three cases:
- FFT along the entire matrix.
- FFT along dimension 1
- FFT along dimension 2
Share your thoughts in the comments
Please Login to comment...