MATLAB: Does values of fft change when length changes

fft

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!

Best 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.