Help with FFT and Dominant Frequency

Hi Everyone,
I'm trying to find dominant frequency using FFT? However, for a vector array which spikes at every 10th iteration and is 0 for everywhere else, I get spikes every 10Hz after I plot FFT data vs. frequency. The pseudo code is
clear all close all clc
%%
fs = 100; % Frequcny of Image Acquisition, 100 Hz T = 1; % Total Time t = 1/fs:1/fs:T; % Time Vector
x = [0,0,0,0,0,0,0,0,0,1,... 0,0,0,0,0,0,0,0,0,0.5,... 0,0,0,0,0,0,0,0,0,.7,... 0,0,0,0,0,0,0,0,0,.8,... 0,0,0,0,0,0,0,0,0,.6,... 0,0,0,0,0,0,0,0,0,.4,... 0,0,0,0,0,0,0,0,0,.8,... 0,0,0,0,0,0,0,0,0,.85,... 0,0,0,0,0,0,0,0,0,1,... 0,0,0,0,0,0,0,0,0,1]; % Image Intensity vector
figure plot(t,x) xlabel('Time, (s)') ylabel ('Image Intensity')
%% Fourier
m = length(x); n = pow2(nextpow2(m)); dim = 2; y = fft(x,n, dim); f = (0:n-1)*(fs/n);
p = y.*conj(y)/n; % figure % plot (f,p) figure plot(f(1:n/2), p(1:n/2)) xlabel('Frequency, (Hz)') ylabel ('P(f)')
Is there something wrong with the code? My understanding is that FFT should only return the dominant frequency, which is 10Hz in this case. Thanks Saad

Answers (1)

Hi Saad,
The fft of a pulse train of uniform spikes is another pulse train of uniform spikes, not just a spike at a single frequency. For example,
n = 200;
y = zeros(1,n);
y(10:10:200) = 1; % exactly periodic
figure(1)
plot(y)
z = fft(y);
figure(2)
plot(1:n,abs(z),1:n,angle(z))
gives another uniform pulse train. [ Technically I suppose the new pulse train z is not uniform, because each pulse is multiplied by a phase factor, but those just have to do with a translation of the original pulse train y. If everything in y is shifted so that the first spike occurs at the beginning of the y array then there are no extra phase factors in z ]
Your input data does not have constant-height pulses so you get the pulse train with some extra, smaller stuff as well.
OK, here comes my rant for the day. Upon running this code,
n = 200;
y = zeros(1,n);
y(10:10:200) = 1;
n1 = pow2(nextpow2(length(x)));
z = fft(y,n1);
figure(2)
plot(1:n1,abs(z),1:n1,angle(z))
then compared to the original code you get a bunch of slop for the fft. In the first case the y waveform is truly periodic, because the pulse spacing of 20 exactly divides the array length of 200. So if another copy of the y array is appended on the right, it looks the same (only twice as long). But power-of-2 appends zeros to the y array, changes its length and messes up the periodicity. I think that the power-of-2 thing is way overvalued and far too often misapplied.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 8 Nov 2017

Answered:

on 9 Nov 2017

Community Treasure Hunt

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

Start Hunting!