FFT yields two-times the actual frequency
Show older comments
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,

Why is this? What am I doing wrong?
1 Comment
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.
Accepted Answer
More Answers (0)
Categories
Find more on Audio and Video Data 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!