MATLAB FFT Zero Padding Issue - Matlab Code Help

  • Thread starter Thread starter palex
  • Start date Start date
  • Tags Tags
    Fft Zero
AI Thread Summary
The discussion revolves around a MATLAB code issue where a cosine signal at 50 Hz shows an unexpected spike at 75 Hz after zero-padding. The participants identify potential causes, including aliasing, insufficient sampling frequency, and changes in frequency scale due to added samples. It is clarified that the peak frequency remains at 50 Hz, but its index changes due to the increased number of samples from zero-padding. Understanding that MATLAB's FFT output provides indices rather than direct frequency values helps clarify the confusion regarding the frequency response. The conversation concludes with a better grasp of how FFT indexing works in MATLAB.
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
Views
2K
Replies
5
Views
2K
Replies
4
Views
4K
Replies
16
Views
14K
Replies
4
Views
3K
Replies
10
Views
3K
Back
Top