I am not an expert in this, but my guess is that, since sampling frequency (the one used
to sample your analog signal to start with) corresponds to 2 pi on the digital
frequency scale, then, assuming the digital frequency is omega (in radians), then
the corresponding frequency, f, in Hz is
f = Fs/(2*pi) * omega
frequency axis for fft represents omega (in radians) and so the above should
give you what you want. You need to know Fs used to sample the data.
ofcourse, I could be all wrong on this. You can try comp.dsp also.
--Nasser
Hi Anvesha, the DFT bins are spaced at intervals of Fs/N where N is the length of the DFT vector and Fs is the sampling frequency. The first bin is zero frequency. So
Fs = 1e3;
t = linspace(0,1,1e3);
x = cos(2*pi*250*t)+randn(size(t));
xdft = fft(x);
xdft = xdft(1:length(x)/2+1); % only retaining the positive frequencies
freq = 0:Fs/length(x):500; % frequency vector from 0 to the Nyquist
[~,maxindex] = max(abs(xdft));
fprintf('The maximum occurs at %2.1f Hz\n', freq(maxindex));
Alternatively, you can use spectral analysis objects if you have the Signal Processing Toolbox. Those objects construct a frequency vector for you!
hper = spectrum.periodogram;
psdest = psd(hper,x,'Fs',Fs,'NFFT',length(x));
[~,maxindex] = max(abs(psdest.Data));
fprintf('The maximum occurs at %2.1f Hz\n', psdest.Frequencies(maxindex));
Hope that helps,
Wayne
A slightly different perspective:
close all, clear all, clc
Fs = 1e3, N = 1e3
dt = 1/Fs, df = Fs/N
t = linspace(0,(N-1)*dt,N)'; % max(t) = 0.999
f0 = Fs/4 % f0 = (N/4)*df
f0 = 0.5 + Fs/4 % 250.5 between bin example
randn('state',4151941)
x = cos(2*pi*f0*t)+randn(size(t)); % SNR = 0.5/1
% Double-Sided FFT (Bipolar frequencies)
f = linspace(0,(N-1)*df,N)'; % max(f) = Fs-df
absX = abs(fft(x));
PSD = absX.^2/(N*Fs);
Parcevalcheck = df*sum(PSD)-mean(x.^2)
% Single-Sided FFT (Nonnegative frequencies)
f1 = linspace(0,df*N/2,N/2+1)'; % max(f1) = Fs/2
f1 = (0:df:Fs/2)';
absX1 = [absX(1);2*absX(2:N/2);absX(N/2+1)]/N;
% Note the scaling by (1/N)
PSD1 = [PSD(1);2*PSD(2:N/2); PSD(N/2+1)];
Parcevalcheck1 = df*sum(PSD1)-mean(x.^2)
[maxabsX1,maxindex] = max(absX1); %[0.7 251]
fprintf('The maximum amplitude of %2.1f occurs at %2.1f Hz\n',...
absX1(maxindex),f1(maxindex) );
disp('Notice the effect of the noise and finite freqency resolution!')
Hope this helps also.
Greg
hello
thanks a lot for the reply but i'm getting some weird output.
the output is :'the max occurs at >> '
cant make head or tail out of this. :(. i am using an audio file as an input with the same code as above. when i do a plot (fft(x),256), on the x axis i get the frequency. i just want these frequencies to be listed at the command prompt.
am i doing something wrong?
Take a look at the my printf statement which is contined
on the next line using "...".
i am using an audio file as an input with the same code as above. when
i do a plot (fft(x),256)
1.I do not recognize that syntax.
2. fft(x) is complex. You want to plot real,
imag, abs or angle
3. Using my code
plot(f1, absX1)
Hope this helps.
Greg
, on the x axis i get the frequency. i just want these frequencies to
be listed at the command prompt.
> am i doing something wrong?- Hide quoted text -
>
> - Show quoted text -
Huh????
Here is the first sentence of your posting:
"I want to convert the output of fft (which is complex number) into
frequency in hertz."
Then in the last post, you said:
"when i do a plot (fft(x),256), on the x axis i get the frequency."
I thought so...............
You have got completely the wrong end of the stick.
fft(x) is the Fourier transform of x, whence you obtain the PSD.
Frequency is freq = 0:Fs/length(x):500; (as shown by Wayne).
You don't do FFT to get frequencies, you do FFT to get the energy at
various frequencies.
Thanks a lot everybody!!! i got where i was going wrong at the basiscs only ! thanks a lot :))