# FFT yields two-times the actual frequency

2 ビュー (過去 30 日間)
Yavuz Selim Tozlu 2019 年 11 月 28 日
Commented: Star Strider 2019 年 11 月 28 日
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 件のコメント

dpb 2019 年 11 月 28 日
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.

サインイン to comment.

### 採用された回答

Star Strider 2019 年 11 月 28 日
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 件のコメント

Yavuz Selim Tozlu 2019 年 11 月 28 日
You were right, I should have scaled the x-axis by 1/2 to fit the criteria.
Star Strider 2019 年 11 月 28 日
Thank you.