Why is my fft peak frequency slightly lower than my known input signal?
4 views (last 30 days)
Show older comments
David Streinger
on 18 Apr 2016
Commented: David Streinger
on 18 Apr 2016
I do an fft of an input signal which I know is exactly one 1kHz, since it comes from a frequency generator.
The peak is at 996, with a measurment time of 10 sec and a sampling frequency of 51000.
measurement1=importdata('765_180.txt');
t=measurement1(:,1);
MIC2=measurement1(:,2);
MIC2_FFT=fft(MIC2);
N = length(measurement1);
figure
grid on
grid minor
subplot(2,1,1)
bin_vals = [0 : N-1]; %adapt to frquency instead of bins
time_new = bin_vals*fs/N; %adapt to frquency instead of bins
N_2 = ceil(N/2); %ceil rounds to next integer-->eliminates the artifacts at the end
plot(time_new(1:N_2), MIC2_FFT_ABS(1:N_2))
%plot(time_new, MIC2_FFT) %this would be with artifacts at freq end
xlabel('Frequency [Hz]')
ylabel('Magnitude');
title('Magnitude spectrum Mic 2 FFT');
Thanks for your help :)
0 Comments
Accepted Answer
Wayne King
on 18 Apr 2016
Hi David, we don't see what fs is in your code above, but in general your code looks like it would generate the expected answer. Once you obtain your t variable above from the first column of the imported data, if you execute
mean(diff(t))
Does that return 1.9608e-05?
You would expect a sampling interval of 1.9608e-05 seconds given the sampling frequency of 51 kHz. I would check to see if that is what you get, otherwise everything else will be off. Again, if fs is specified corrected and the spacing in the t vector is correct, then once you generate your time_new vector if you execute
mean(diff(time_new))
Do you get 0.1? That should be your frequency resolution. Here is an example that produces the expected result, it is essentially your code with a few variables renamed.
fs = 51e3;
dt = 1/fs;
t = 0:dt:10-dt;
% test signal
x = cos(2*pi*1e3*t);
xdft = fft(x);
powerx = abs(xdft(1:Nyq));
N = length(t);
bins = 0:N-1;
freq = bins*fs/N;
Nyq = N/2;
plot(freq(1:Nyq),powerx);
You see in the above, the maximum occurs at bin 1001
[~,idx] = max(powerx);
idx
freq(idx)
Which corresponds to 1000 Hz.
More Answers (1)
Wayne King
on 18 Apr 2016
Hi David, You are using the wrong sampling frequency. The actually sampling frequency is 51200. You can see this by entering
1/mean(diff(t))
Once you have the correct sampling frequency, the tone is exactly where you would expect it.
data = importdata('1kHz_10s_3mics.txt');
fs = 1/mean(diff(data(:,1))); % corrected
N = length(data(:,2));
freq = (0:N-1)*fs/N;
Nyq = N/2;
datadft = fft(data(:,2));
% Power but not scaled properly
powerdata = abs(datadft(1:Nyq));
plot(freq(1:Nyq),powerdata);
[~,idx] = max(powerdata);
freq(idx)
See Also
Categories
Find more on Get Started with Signal Processing Toolbox 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!