# Magnitude Spectrum, different magnitudes same amplitude

When the following code is written,the resulting magnitude spectrum shows magnitude values represented by two peaks is different even though they have the same amplitude. why is that?

a fast reply would be much appreciated

    Fs=250; %sampling frquency of 250Hz
i=0:1/250:4; %1000 data points
x1=sin(2*pi*7*i); %create sinusoidal wave with frequency=7Hz
x2=sin(2*pi*40*i);%create sinusoidal wave with frequency=40Hz
subplot(2,1,1);
plot(x1),xlabel('Sampling Points'),ylabel('Amplitude')
subplot(2,1,2);
plot(x2),xlabel('Sampling Points'),ylabel('Amplitude')

y=x1+x2; %combine two signals to one
figure, psd(y) %spectrum analysis

N=length(y); %legnth of y is attributed to N

%calculate real part of Discrete Fourier Transform
for m=1:N,
sumn=0;
for n=1:N,
sumn=sumn + y(n)*(cos (2*pi*(m-1)*(n-1)/N));
end
dftreal(m)=sumn;
end

%calculate imaginary part of Discrete Fourier Transform
for m=1:N,
sumn=0;
for n=1:N,
sumn=sumn + y(n)*(sin (2*pi*(m-1)*(n-1)/N));
end
dftimag(m)=-sumn;
end

%obtaining magnitude spectrum using the obtained complex DFT values and
%plotting the spectrum
for m=1:N,
dftmag(m)=sqrt(dftreal(m)*dftreal(m)+dftimag(m)*dftimag(m));
end

%obtaining magnitude spectrum using the obtained complex DFT values and
figure, plot(dftmag),title('Magnitude Spectrum 1'),xlabel('Sampling Points'),ylabel('Magnitude')

%obtaining new Magnitude Spectrum based on fft
z=fft(y);
dftmag1=abs(z);

figure, plot(dftmag1),title('Magnitude Spectrum 2'),xlabel('Sampling Points'),ylabel('Magnitude')

%obtaining PSD of dftmag1
figure, psd(dftmag1)


## 2 Answers

To add a bit to the previous answers: the FFT result is "binned", not continuous. A plot of an FFT result often fools people by being smooth for large enough FFT lengths. But the result is really more like a bar chart for a sequence of bins, rather than a line graph.

And the bins are not rectangular in shape (in terms of frequency response). The nearer a spectral frequency peak is to the center of an FFT bin, the more accurate the magnitude result. You can get a list of your FFT's bin centers by multiplying sample_rate/length_of_FFT by integers from zero up to half the length. If a frequency peak is not at the exact center of an FFT result bin, the magnitude gets split among multiple bins, which takes height away from the closest peak bin.

In your example, some of your frequencies were closer to bin centers than others.

The energy is simply spread over several FFT bins because your analysis window length is not an integer multiple of the period of your signals - your harmonics are not exactly aligned with the frequency resolution grid of the FFT. Read about spectral leakage and windowing...

On the plot you have posted, what matters is not the peak amplitude, but the sum of the energy over the entire width of the peak. The first harmonic has a higher peak but it is narrow ; the second harmonic has a lower but wider peak. The total energy of both must be equal.