# matlab fft amplitude is not preserved

• In the first example below, the fft sampling bins are spaced at 1Hz, and then with zero padding bins are spaced at 0.5Hz. Therefore sin(2*pi*202.5*t) part of the signal is very well covered and its amplitude is well preserved in fft as can be seen in first figure (this is an example in mathworks).

• In the second example, similarly fft bin is 1.024 after zero padding, and the vector x consists of signal with two frequencies at 102.4 and 204.8, which are multipliers of bin 1/1.024, so those frequencies are on the fft bins. But the fft magnitudes are not very well preserved as you see in the second figure.

• Is it b/c of decimal places in the bins?

• Near the frequencies 102 & 202.5, why is there plateau in the first figure and why is it cleared up in the second figure
• Is the frequency bin size equal to the window size of the fft?
• Not related to examples below, but if specific frequencies (e.g., between 0.333-10) are of most importance, how can I better fft results specifically in that range? Can I non-uniformly refine fft bins in 0.333-10 range? If I can would there be any drawbacks?

Examples:

``````% First Example
Fs = 1e3;
dt = 0.001;
DFT_bins = 1/(Fs*dt); % bin 1
t = 0:dt:1-dt;
x = cos(2*pi*102.0*t)+sin(2*pi*202.5*t);
xdft = fft(x,2000);
xdft = xdft(1:length(xdft)/2+1);
xdft = xdft/length(x);
xdft(2:end-1) = 2*xdft(2:end-1);
freq = 0:Fs/(2*length(x)):Fs/2;
figure;
plot(freq,abs(xdft));
hold on; plot(freq,ones(2*length(x)/2+1,1),'LineWidth',2);
xlabel('Hz'); ylabel('Amplitude');
``````

``````% Second Example
Lf = 2^nextpow2(length(x));
DFT_bins = 1/(Lf*dt); % bin 1/1.024
x = cos(2*pi*DFT_bins*100*t)+sin(2*pi*DFT_bins*200*t);
xdft = fft(x,Lf);
Len0 = length(xdft);
xdft = xdft(1:Len0/2+1);
xdft = xdft/Len0;
xdft(2:end-1) = 2*xdft(2:end-1);
freq = 0:Fs/(Len0):Fs/2;
figure;
plot(freq,abs(xdft)); hold on; grid minor;
xlabel('Hz'); ylabel('Amplitude');
``````

matlabfft