Fast Fourier Transform in MATLAB

In summary, the conversation discusses using MATLAB to explore data numerically and the use of the fft function. The tutorial involves creating data from the sum of two sin functions and applying the fft to verify the frequencies. The n-point DFT is a transformation of n points, with n being the number of time values and frequency values. The fft function defaults to a 251-point transform, but can be modified for different length data. The use of Y .* conj(Y)/n is for calculating the vector of power values rather than their sum, with the division by n accounting for scaling factors. The 1000 in line 6 is the time step from line 1, and the 251 is the time step * end time + 1
  • #1
member 428835
Hi PF!

I'm following a tutorial in MATLAB, shown here

Code:
t = 0:.001:.25;
x = sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t));
Y = fft(y,251);
Pyy = Y.*conj(Y)/251;
f = 1000/251*(0:127);
plot(f,Pyy(1:128))
title('Power spectral density')
xlabel('Frequency (Hz)')

I read the documentation but a few things are still unclear, which I'll list for clarity:
Line 4: why use the 251 part of the transform?
Line 5: Why this rather than, say Norm(Y)?
Line 6: so the 1000 is evidently from line 1, the time step? But where is the 251 coming from; looks like it's the time step * end time + 1.

Thanks!
 
Physics news on Phys.org
  • #2
Heres what Mathworks has to say

https://www.mathworks.com/help/matlab/ref/fft.html
You should be able search this yourself. This page has some code to try. Matlab is a math lab for its users allowing to explore data numerically.

When we did this in school we created our data from the sum of two sin functions and then applied the fft to it to verify that the fft found the frequencies.
 
  • #3
jedishrfu said:
Heres what Mathworks has to say

https://www.mathworks.com/help/matlab/ref/fft.html
You should be able search this yourself. This page has some code to try. Matlab is a math lab for its users allowing to explore data numerically.

When we did this in school we created our data from the sum of two sin functions and then applied the fft to it to verify that the fft found the frequencies.
Yea, I looked at this too. But I could not figure out what was meant by "n-point DFT" and when to specify n.

Also, can you tell me why they plot abs(Y/L) rather than simply abs(Y)?
 
  • #5
joshmccraney said:
But I could not figure out what was meant by "n-point DFT"

A digital Fourier transform (DFT) is a transformation of n points. The equations are a sum from 1 to ##n##. ##n## is the number of points you are transforming. The number of time values. And you will get out the same number of frequency values. If you do a 32-point transform, transforming 32 time samples, you'll get 32 frequency samples.

The fast implementation, the FFT, was originally designed for ##n## being a power of 2. The algorithm has been modified over the years so that's no longer required, but I believe it's still most efficient if ##n## is a power of 2.

As to your original questions:
joshmccraney said:
Line 4: why use the 251 part of the transform?

Since ##t## in this example looks like it has 251 points, that argument is redundant. It will be a 251-point transform by default. But if you changed line 1 to have more or less points, the effect would be to always use a transform of the same length, which will give the same frequency resolution and would allow comparison.

Perhaps the tutorial experimented with changes in line 1 while keeping the rest the same?

joshmccraney said:
Line 5: Why this rather than, say Norm(Y)?

Because norm(Y) would calculate ##\sum_{i=1}^n |Y_i|^2##. This is calculating the vector of power values ##|Y_i|^2##, not their sum.

Edit to add: abs(Y).^2 would have the same effect as Y .* conj(Y). So it's really a stylistic thing. Y . * conj(Y) makes it clear to some people (to me for instance, and apparently the author) exactly the nature of this calculation. No strong reason for it though. Sometimes you just write equations in a certain way as a note to yourself.

The division by ##n## has to do with the scaling factors that the FFT introduces. It's converting the power to some particular units. I don't know the details of what scale factors they're accounting for.

joshmccraney said:
Line 6: so the 1000 is evidently from line 1, the time step? But where is the 251 coming from; looks like it's the time step * end time + 1.

The length of the original vector is ##T = 251/1000## seconds. That is the time-span represented by the original time sample. This causes the frequency spacing, the ##\Delta f## between frequency points, to be ##1/T = (1000/251)## Hz.

And the reason for the (0:127) is that when you DFT a real signal, the upper half is the mirror image of the lower half (well, actually its complex conjugate). This line is plotting 128 points of the magnitude of the FFT, just the lower half. Since it was a 251-point transform, that seems to me to be going a couple of points past the halfway mark.

Try changing it to (0:250) to see what happens. (Of course you need to modify line 7 so you use the same number of points of Pyy).
 
Last edited:
  • Like
Likes member 428835, FactChecker and jedishrfu

FAQ: Fast Fourier Transform in MATLAB

What is Fast Fourier Transform (FFT) in MATLAB?

Fast Fourier Transform (FFT) in MATLAB is a mathematical algorithm used to quickly and efficiently compute the discrete Fourier transform (DFT) of a signal or sequence. It breaks down a signal into its individual frequency components, allowing for analysis and manipulation in the frequency domain.

How is FFT different from regular Fourier Transform?

FFT is a more efficient version of the traditional Fourier Transform, which involves a slower and more computationally intensive calculation. FFT uses a divide-and-conquer approach to break down the signal into smaller sub-signals, making it much faster and more suitable for analyzing large datasets.

What are the advantages of using FFT in MATLAB?

There are several advantages to using FFT in MATLAB, including faster computation times, reduced memory usage, and the ability to easily manipulate and analyze signals in the frequency domain. It is also readily available and well-supported in the MATLAB software.

How do I use FFT in MATLAB?

To use FFT in MATLAB, you first need to import your signal data into the software. Then you can use the built-in function "fft" to compute the FFT of your signal. You can also use other functions, such as "ifft" for inverse FFT and "fftshift" for shifting the frequency components to the center of the spectrum.

What are some common applications of FFT in MATLAB?

FFT in MATLAB has a wide range of applications, including signal processing, image processing, audio processing, and data analysis. It is commonly used in fields such as engineering, physics, and finance for tasks such as filtering, noise reduction, spectrum analysis, and pattern recognition.

Similar threads

Replies
8
Views
836
Replies
7
Views
2K
Replies
9
Views
2K
Replies
2
Views
2K
Replies
1
Views
5K
Back
Top