Why does values of fft change when length changes?

Hi, I've got the following code-
x =[1,2,3,2];
y = [2,2,2,0,-4,4,4];
xlen = length(x);
ylen = length(y);
resultlength = xlen+ylen-1;
x1 = [x zeros(1,resultlength-xlen)];
y1 = [y zeros(1,resultlength-ylen)];
X = fft(x); Y=fft(y);
X1 = fft(x1); Y1=fft(y1);
X2 = fft(x,512); Y2 = fft(y,512);
% X2 = X2(1:10); Y2 = Y2(1:10);
freq1 = linspace(0,100,resultlength);
freq2 = linspace(0,100,512);
plot(freq1,abs(X1),'ro'); hold;
plot(freq2,abs(X2),'bo');
Shouldn't the blue plot and the red plot follow each other? Why are there values in the red plot that are not in the blue plot?
Thanks!

 Accepted Answer

You have zero-padded both of your ‘x’ signals, extending ‘x1’ from 4 to 10 by adding 6 zeros at the end, and ‘x2’ by zero-padding it out to a length of 512. The ‘energy’ in the signal are in the non-zero-padded data (zeros within the data are of course permitted). It is necessary to ‘normalise’ the fft by dividing the results by the length of the original (non-zero-padded) signal.
Compare the plots for these two normalised results:
X1 = fft(x1)/xlen;
X2 = fft(x,512)/xlen;
They come very close to overlapping. The blue curve has increased frequency resolution, so appears more continuous.

5 Comments

Hi, I tried to normalise it, but this is the image I get. The red circles still do not match the blue ones. Is there something I'm still doing wrong?
I read the help section of FFT, and came up with the following code-
x =[1,2,3,2];
xlen = length(x);
resultlength=10;
x1 = [x zeros(1,resultlength-xlen)];
X1 = fft(x1);
X2 = fft(x,512);
P1 = abs(X1/xlen);
P2 = abs(X2/xlen);
P3=P1(1:xlen/2+1); P3(2:end-1)=2*P3(2:end-1);
P4=P2(1:xlen/2+1); P4(2:end-1)=2*P4(2:end-1);
figure;plot((0:1/(length(P3)-1):1)*10,P3,'ro');hold on;
plot((0:1/(length(P4)-1):1)*10,P4,'bo');
However, here are the results I get- P3 =
2.0000 3.3069 0.8693
and
P4 =
2.0000 3.9997 1.9994
Why does zero padding change the values of the fourier transform so much? What is the mathematical justification behind this?
Thanks in advance!
My pleasure!
What you see in that plot is as good as you can get. The red circles will not match the blue circles because the frequency resolution of the blue circles is significantly greater than for the red circles.
Zero-padding the fft increases the frequency resolution by increasing the number of points at which the Fourier transform is evaluated. (It is evaluated at the same number of points as the argument vector.) The more you zero-pad ‘x’, the closer the red and blue circles will be. I invite you to explore that on your own.
EDIT I also saw your other Question. The multiplication by 2 in a one-sided fft results from the total energy of the signal being equally divided into the positive-frequency half and negative-frequency half of the fft output. To plot a one-sided fft, multiplying the amplitudes by 2 normalises the amplitudes to be the same as the amplitudes in the original time-domain signal. (Note that the amplitudes may not be exactly equal because of the frequency ‘leakage’ involved in taking the Fourier transform of a sampled signal.)
Hey thank you so much! I realised this after tinkering with the resultlength value. Is there any documentation about how Matlab implements its fft function? I think this might help me.
So does this mean that zero padding improves the accuracy of the fft, i.e. fft is a closer approximation to the analytical Fourier Transform with more zero padding?
Once again, thanks for your help!
As always, my pleasure!
See the references at the end of the fft documentation page for information on the MATLAB implementation. With respect to the Fourier transform itself, see Fourier Transforms (link). All good digital signal processing textbooks discuss the algorithm involved in calculating the Fast Fourier Transform in detail.
‘So does this mean that zero padding improves the accuracy of the fft, i.e. fft is a closer approximation to the analytical Fourier Transform with more zero padding?’
Yes! I could not have stated it better.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!