FFT Zero Padding Issue - Matlab Code Help

  • MATLAB
  • Thread starter palex
  • Start date
  • Tags
    Fft Zero
In summary, the frequency response of your cosine signal is showing a spike at 75 Hz when it should be 50 Hz. The cause of this is unknown, but it can be rectified by increasing the sampling rate.
  • #1
palex
6
0
Hello,
I have a simple cosine f = 50Hz. When I generate Matlab code to produce 1/2 second of this signal and take the FFT, the response correctly shows a spike at 50 Hz. However, when I bracket the signal with 1/2 seconds of zeros on either side, the frequency response is showing a spike at 75 Hz. Does anyone know why this might occur, and how might one rectify it?

Thanks so much!
 
Physics news on Phys.org
  • #2
Welcome to PF, palex! :smile:

I see 3 possibilities:

  1. It's an extra peak caused by aliasing of discretely sampled data (DFT aliasing).
    In this case the peak of 50 Hz should still be there.
    Is it?
  2. Your data is sampled at an insufficient frequency.
    To correctly sample data for an FFT the sample rate has to be at least twice as high as the frequency you are sampling (Nyquist–Shannon sampling theorem).
    What is your sampling rate? Is it the same as before?
  3. Your frequency scale has changed due to the extra sample data, and 75 Hz is actually 50 Hz (the frequency scale depends on the sampling rate and on the number of samples).
    Can you be sure that 75 Hz is actually 75 Hz?
 
  • #3
Thanks for the reply!
The cosine is sampled well above the Nyquist limit.
I think it may have to do with the frequency scale, though I don't quite see it. The Matlab code is:

fs = 0.0001;
t = 0:fs:0.5;
y = cos(50*2*pi.*t);
t = 0:fs:1.5;
y = [zeros(1,0.5/fs), y, zeros(1,0.5/fs)];
stem(abs(fft(y)))


Thanks again!
 
  • #4
The highest frequency you get is your sampling rate (10 kHz).
The lowest frequency you get corresponds to your time interval (originally 2 Hz, now 2/3 Hz).

Originally your 50 Hz peak would be at index 25 (since 25 x 2 Hz = 50 Hz).
Now your 50 Hz peak would be at index 75 (since 75 x 2/3 Hz = 50 Hz).

Does this match your results?
 
  • #5
Yes, this is matching what I am getting. I guess my point of confusion is why these peaks are changing. The dominant frequency is 50 Hz in all three cases. Does Matlab interpret the x-axis scaling in a strange way?
 
  • #6
If you put more samples in, you get a higher resolution in the frequency spectrum.
The peak is not changing - there are more elements in the result (3 times as many).
So the corresponding index of the 50 Hz peak increases with a factor 3.

Matlab has nothing to do with it.
It is how the FFT works.
The index is not the frequency.
You have to divide the index with the duration of the entire time interval to get the corresponding frequency.
 
  • #7
I like Serena said:
If you put more samples in, you get a higher resolution in the frequency spectrum.
The peak is not changing - there are more elements in the result (3 times as many).
So the corresponding index of the 50 Hz peak increases with a factor 3.

Matlab has nothing to do with it.
It is how the FFT works.
The index is not the frequency.
You have to divide the index with the duration of the entire time interval to get the corresponding frequency.

small potatoes but remember that MATLAB adds 1 to the index. they don't know how to count from 0 at The Math Works. so they assign the index 1 to DC with the FFT.
 
  • #8
Cool... thanks again for your help. It's starting to make sense realizing the initial output are indices rather than Hz.

Regards.
 

1. What is zero padding in FFT and why is it necessary?

Zero padding is the process of adding zeros to the end of a signal before performing the FFT (Fast Fourier Transform). It is necessary because it allows for a higher frequency resolution in the FFT output, which can provide more accurate frequency analysis of the signal. It also helps to reduce the effect of spectral leakage, which can occur when the signal does not have an integer number of periods within the FFT window.

2. How does zero padding affect the FFT output?

Zero padding does not change the actual frequency content of the signal, but it does increase the number of frequency bins in the FFT output. This means that the FFT output will have a higher resolution and can provide more detailed information about the frequencies present in the signal. However, it can also introduce artificially high or low frequency components in the FFT output, which should be taken into consideration when interpreting the results.

3. Is there a specific amount of zero padding that should be used?

The amount of zero padding used in FFT depends on the specific application and the desired frequency resolution. It is recommended to use a power of 2 for the zero padding size, such as 256, 512, 1024, etc. However, the optimal amount of zero padding should be determined based on the specific signal and analysis goals.

4. Can zero padding be applied to any type of signal?

Yes, zero padding can be applied to any type of signal, whether it is a continuous or discrete signal. However, it is most commonly used with discrete signals such as digital audio or time series data.

5. Are there any potential drawbacks of using zero padding in FFT?

One potential drawback of zero padding is that it can increase the computational time and memory requirements for the FFT. This can be a concern for large datasets or real-time applications. Additionally, as mentioned earlier, zero padding can introduce artificial frequency components in the FFT output, so it should be used carefully and the results should be interpreted with caution.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
738
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
13K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top