FFT yields two-times the actual frequency

7 views (last 30 days)
I have the following code that records the audio through mic, then computes and plots its FFT.
clear all
close all
recorder = audiorecorder(44100,16,1);
recordblocking(recorder,2);
xtmessage = getaudiodata(recorder);
xaxis = zeros(88200,1);
%frequncy axis
for i=1:88200
xaxis(i)=i-44101;
end
xfmessage=fft(xtmessage);
xfmessage=fftshift(xfmessage);
figure(2)
plot(xaxis,abs(xfmessage)/length(xfmessage),'r') %normalize the amplitude
Now, I generate sound using online tone generators in a quiet room with no background noise. However, the FFT plot always shows the 2x the frequency of the tone I generate. For instance, I make 440Hz sound, FFT shows impulses at +-880Hz, I make 2kHz sound, FFT shows impulses at +-4kHz. Following screenshot illustrates my problem,
Adsız.png
Why is this? What am I doing wrong?
  1 Comment
dpb
dpb on 28 Nov 2019
Using the wrong sample rate in the frequency calc, apparently. The FFT has no inkling what the actual sample frequency is; it's all dependent upon you calculating the correct dF for the actual sample rate.
Fs = YourSampleFrequency; % Hz
Y = fft(y); % y is sampled waveform
P2 = abs(Y/L); % 2-side PSD
P1 = P2(1:L/2+1); % Only positive frequency (ignore if want keep both +/-)
P1(2:end-1)=2*P1(2:end-1); % Scale one-sided (ditto above if two-sided)
L=SizeOfFFT;
f = Fs*(0:(L/2))/L;
Above from Example in FFT doc for context.
Looks like you forgot Nyquist and didn't divide Fmax by two, maybe...altho I didn't try to read your code in great detail.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Nov 2019
The problem appears to be the way the frequency axis (‘xaxis’) is computed. The limits should be ±Fn, where ‘Fn’ is the Nyquist frequency.
Example —
Fs = 44100; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
xaxis = linspace(-1, 1, size(xfmessage,1))*Fn; % Assumes ‘xfmessage’ Is A Column Vector
That should produce the correct result.
  2 Comments
Yavuz Selim Tozlu
Yavuz Selim Tozlu on 28 Nov 2019
Edited: Yavuz Selim Tozlu on 28 Nov 2019
You were right, I should have scaled the x-axis by 1/2 to fit the criteria.
Star Strider
Star Strider on 28 Nov 2019
Thank you.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!