Hi experts, I am attempting to use a window function along side an FFT but cannot make sense of the result?

3 views (last 30 days)
Firstly I have created a sine wave g = sin(2*pi*10000*t) + 0.5*sin(2*pi*20000*t) + 1*sin(2*pi*30000*t). When I use the FFT function my is correct seeing a spike at 10k of amplitude 1, a spike at 20k of amplitude 0.5 and a spike at 30k of amplitude 1.
However when I change the sine wave g to g = sin(2*pi*10000*t) + 0.5*sin(2*pi*20000*t) + 1*sin(2*pi*15000*t), the resulting spectrum is not one I can make sense of. (see the attached screenshot.)
Due to this I used the Hamming window function and did the FFT. The result is then showing a spike at 10k with an amplitude of 0.46. I have no idea why. (see attached screenshot)
Can you please explain what is going wrong and if you could point me in the correct direction that would be great. Thanks in advance.
Here is the code I am using to obtain the results:
clear all;
fs = 320000; %setting the sampling frequency
t= 0:1/fs:0.0001; % the 0 is the start time, 1/fs is the sampling frequency and the period of 0.01 sec 1024 points (0 - 1 in steps of 1/fs)
n = 32; %no of samples
g = 0.6*sin(2*pi*20000*t) + sin(2*pi*10000*t) + 0.5*sin(2*pi*15000*t);% creating the sine wave
L=length(g);
w=window(@hamming,L);
figure(1)
plot(w)
fftres = fft(w,n); %using the fft on the sine wave with n number of samples
figure(2);
plot(real(fftres)) % plot the real part of the vector
grid on % turn on the grid for data points and clarity
ylabel('Real') % set label for y axis
title('Raw FFT spectrum') % set label for title
figure(3)
plot(imag(fftres)) % plot the imaginary part of the vector
grid on % turn on the grid for data points and clarity
ylabel('Imaginary') % set label for y axis
xlabel('Index number') % set label for title
%first two elements of spectrum, DC is the first
DCfirst = fftres(1:2); % extract the first and second elements out of the matrix
indexNyquist = n/2+1; % setting the frequency range
NyquistMiddle = fftres(indexNyquist-2:indexNyquist+2); %vicinity of Nyquist, which is in the middle
fftres = fftres(1:indexNyquist); %truncate
fftres = fftres/n; %scale
fftres(2:end) = 2 * fftres(2:end); %compensate for truncating the negative frequencies
%Calculate the magnitude
mag = abs(fftres); %abs for complex numbers returns their magnitude
df = fs/n; %frequency resolution
frequencyAxis = [0:indexNyquist-1]'*df; %values on frequency axis
figure(4);
plot(frequencyAxis, mag) %plot freq plot of fft
grid on % turn on the grid for data points and clarity
xlabel('Frequency [Hz]') % set label for y axis
ylabel('Magnitude [units]') % set label for title
%Position and value of maximum magnitude
[magMax,indexMax] = max(mag); % returns the largest elements of vector mag
freqMax = frequencyAxis(indexMax);
%Display in title
title(sprintf('Max magnitude=%f @ %fHz, \\Deltaf=%fHz', magMax, freqMax,df)) %Plot the spectrum as lines using stem function and removing the markers at %the end of lines
handleStem=stem(frequencyAxis,mag);
set(handleStem(1),'Marker','none')
grid on
xlabel('Frequency [Hz]')
ylabel('Magnitude [units]')
%Display in title
title(sprintf('Max magnitude=%f @ %fHz, \\Deltaf=%fHz ',...
magMax, freqMax,df))

Answers (0)

Community Treasure Hunt

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

Start Hunting!