FFT Zero Padding Issue - Matlab Code Help

  • Context: MATLAB 
  • Thread starter Thread starter palex
  • Start date Start date
  • Tags Tags
    Fft Zero
Click For Summary

Discussion Overview

The discussion revolves around an issue with the Fast Fourier Transform (FFT) in Matlab, specifically regarding the frequency response of a cosine signal when zero padding is applied. Participants explore the implications of sampling rates, frequency scaling, and the interpretation of FFT output.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes a spike at 75 Hz after zero padding a 50 Hz cosine signal and seeks clarification on this phenomenon.
  • Another participant suggests that the spike could be due to DFT aliasing, questioning whether the 50 Hz peak is still present.
  • Concerns are raised about the sampling rate being sufficient according to the Nyquist–Shannon theorem, prompting a request for the participant's sampling rate.
  • It is proposed that the frequency scale may have changed due to the additional sample data, leading to confusion about the identification of the 75 Hz spike.
  • A participant explains that the highest frequency corresponds to the sampling rate and the lowest frequency to the time interval, providing a calculation for the index of the 50 Hz peak.
  • Clarifications are made regarding the relationship between the number of samples and frequency resolution, indicating that the peak remains consistent but its index changes with more samples.
  • One participant humorously notes that Matlab's indexing starts at 1 rather than 0, which may contribute to confusion in interpreting the FFT output.

Areas of Agreement / Disagreement

Participants generally agree on the relationship between sampling, frequency resolution, and FFT output interpretation. However, there remains some uncertainty regarding the implications of zero padding and the interpretation of frequency indices.

Contextual Notes

Limitations include potential misunderstandings about how Matlab scales frequency outputs and the effects of zero padding on frequency representation. The discussion does not resolve whether the interpretation of the frequency response is consistent across different configurations.

Who May Find This Useful

This discussion may be useful for individuals working with FFT in Matlab, particularly those interested in signal processing, frequency analysis, and the effects of sampling and zero padding on frequency representation.

palex
Messages
6
Reaction score
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
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?
 
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!
 
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?
 
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?
 
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.
 
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.
 
Cool... thanks again for your help. It's starting to make sense realizing the initial output are indices rather than Hz.

Regards.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
6
Views
4K
  • · Replies 16 ·
Replies
16
Views
15K
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K