Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting fft output to hertz

215 views
Skip to first unread message

Anvesha

unread,
Jan 31, 2011, 5:21:04 AM1/31/11
to
hello
I want to convert the output of fft (which is complex number) into frequency in hertz. When I take the fft of an audio signal the output should be the frequency in hertz which should be displayed in the command window. I dont want to plot the graph of frequency but want the values written in the command window. I have tried and tried but still cant it to work. any help will be appreciated.
thanks!

Nasser M. Abbasi

unread,
Jan 31, 2011, 5:37:34 AM1/31/11
to

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


Wayne King

unread,
Jan 31, 2011, 7:40:07 AM1/31/11
to
"Anvesha " <anvesh...@gmail.com> wrote in message <ii62eg$opd$1...@fred.mathworks.com>...

> hello
> I want to convert the output of fft (which is complex number) into frequency in hertz. When I take the fft of an audio signal the output should be the frequency in hertz which should be displayed in the command window. I dont want to plot the graph of frequency but want the values written in the command window. I have tried and tried but still cant it to work. any help will be appreciated.
> thanks!

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

Greg Heath

unread,
Jan 31, 2011, 4:57:34 PM1/31/11
to
On Jan 31, 7:40 am, "Wayne King" <wmkin...@gmail.com> wrote:
> "Anvesha " <anveshaka...@gmail.com> wrote in message <ii62eg$op...@fred.mathworks.com>...

> > hello
> > I want to convert the output of fft (which is complex number) into frequency in hertz. When I take the fft of an audio signal the output should be the frequency   in hertz which should be displayed in the command window. I dont want to plot the graph of frequency but want the values written in the command window. I have tried and tried but still cant it to work. any help will be appreciated.
> > thanks!
>
> 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));

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


Anvesha

unread,
Feb 4, 2011, 2:45:06 AM2/4/11
to
Greg Heath <he...@alumni.brown.edu> wrote in message <20cc7ddc-7dce-4950...@v16g2000vbq.googlegroups.com>...
hello
i am somehow getting some odd output. this is my output:
'the max amplitude of 2.6 occurs at >> '
the input was an audio file with exactly the same code as above.
:(

Anvesha

unread,
Feb 4, 2011, 2:51:03 AM2/4/11
to
"Wayne King" <wmki...@gmail.com> wrote in message <ii6aj7$6vm$1...@fred.mathworks.com>...

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?

Greg Heath

unread,
Feb 4, 2011, 3:05:32 AM2/4/11
to
On Feb 4, 2:51 am, "Anvesha " <anveshaka...@gmail.com> wrote:
> "Wayne King" <wmkin...@gmail.com> wrote in message <ii6aj7$6v...@fred.mathworks.com>...
> > "Anvesha " <anveshaka...@gmail.com> wrote in message <ii62eg$op...@fred.mathworks.com>...

> > > hello
> > > I want to convert the output of fft (which is complex number) into frequency in hertz. When I take the fft of an audio signal the output should be the frequency   in hertz which should be displayed in the command window. I dont want to plot the graph of frequency but want the values written in the command window. I have tried and tried but still cant it to work. any help will be appreciated.
> > > thanks!
>
> > 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
>
> 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. :(.

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 -

TideMan

unread,
Feb 4, 2011, 3:06:04 AM2/4/11
to
On Feb 4, 8:51 pm, "Anvesha " <anveshaka...@gmail.com> wrote:
> "Wayne King" <wmkin...@gmail.com> wrote in message <ii6aj7$6v...@fred.mathworks.com>...
> > "Anvesha " <anveshaka...@gmail.com> wrote in message <ii62eg$op...@fred.mathworks.com>...

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.

Anvesha

unread,
Feb 6, 2011, 7:53:03 AM2/6/11
to
TideMan <mul...@gmail.com> wrote in message <d41c592a-ba95-438e...@y31g2000prd.googlegroups.com>...


Thanks a lot everybody!!! i got where i was going wrong at the basiscs only ! thanks a lot :))

0 new messages