Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fft/amplitude/frequency grid definiton problems

Subject: fft/amplitude/frequency grid definiton problems

From: Xngel

Date: 2 Feb, 2003 20:22:25

Message: 1 of 5

Hello, I'm dealing with what I think is probably a straightforward
issue where I'm missing some fundamental idea...


I'm trying to write a short routine to find the fundamental frequency
in a digitally sampled piece of audio. I have the frequency grid
defined as


f=Fs*(length(data))/2)/length(data)


where Fs is the sampling rate


and the amplitude values as


datafft=(sqrt(abs(fft(data)))


I'm then finding max(datafft) and then finding the index value of the
maximum, and telling MATLAB to return the frequency (from f) that has
the same index value as the largest fft value.


The problem here is that f is half of the size of datafft, I'm
probably not defining the frequencies correctly. I know this is
probably a somewhat moronic question, but any help would be greatly
appreciated!! Thanks much :)

Subject: fft/amplitude/frequency grid definiton problems

From: Jeroen Boschma

Date: 3 Feb, 2003 09:15:05

Message: 2 of 5



Xngel wrote:
>
> Hello, I'm dealing with what I think is probably a straightforward
> issue where I'm missing some fundamental idea...
>
> I'm trying to write a short routine to find the fundamental frequency
> in a digitally sampled piece of audio. I have the frequency grid
> defined as
>
> f=Fs*(length(data))/2)/length(data)

'f' is here a scalar, equal to 0.5*Fs ...

If you want an array corresponding with the FFT up to Fs/2:

   f = Fs*(0:(length(data)/2))/(length(data)+1);

>
> where Fs is the sampling rate
>
> and the amplitude values as
>
> datafft=(sqrt(abs(fft(data)))

Nope, the complex amplitude is:

    datafft=fft(data);

The absolute amplitude is:

   datafft=abs(fft(data));

If you need the data only to find the maximum, the power may do also
which is much faster computed:

   datafft = fft(data);
   datafft = datafft.*conj(datafft);

If you only need the frequency where the maximum is, it not necessary to
make a frequency-array (waste of memory):

   datafft = fft(data); % complex
amplitude
   datafft = datafft(1:length(datafft)/2); % up to Fs/2
   datafft = datafft.*conj(datafft); % power
   max_indices = find(datafft == max(datafft)); % indices with
max. pow
   f_max = Fs*max_indices/(2*length(datafft)+1); % frequencies

Now 'f_max' is an array with frequencies where the maximum amplitude is
present.

Greetz,

   Jeroen

>
> I'm then finding max(datafft) and then finding the index value of the
> maximum, and telling MATLAB to return the frequency (from f) that has
> the same index value as the largest fft value.
>
> The problem here is that f is half of the size of datafft, I'm
> probably not defining the frequencies correctly. I know this is
> probably a somewhat moronic question, but any help would be greatly
> appreciated!! Thanks much :)

Subject: fft/amplitude/frequency grid definiton problems

From: heath@ll.mit.edu (Greg Heath)

Date: 3 Feb, 2003 13:19:11

Message: 3 of 5

Xngel <Xngel34@hotmail.com> wrote in message
news:<eeb8816.-1@WebX.raydaftYaTP>...
> Hello, I'm dealing with what I think is probably a straightforward
> issue where I'm missing some fundamental idea...
> I'm trying to write a short routine to find the fundamental frequency
> in a digitally sampled piece of audio. I have the frequency grid
> defined as
>
> f=Fs*(length(data))/2)/length(data)
> where Fs is the sampling rate

dt = 1/Fs
t = (0:N-1)*dt
df = 1/(N*dt)
f = (0:N-1)*df
 
> and the amplitude values as
> datafft=(sqrt(abs(fft(data)))

datafft= abs(fft(data)) % I use PSD = abs(dt*fft(data)).^2
 
> I'm then finding max(datafft) and then finding the index value of the
> maximum, and telling MATLAB to return the frequency (from f) that has
> the same index value as the largest fft value.

Q = fix((N+2)/2)
[Fmax Imax] = max(datafft(1:Q))
I = find(data(1:Q)==Fmxax)

> The problem here is that f is half of the size of datafft, I'm
> probably not defining the frequencies correctly. I know this is
> probably a somewhat moronic question, but any help would be greatly
> appreciated!! Thanks much :)

Hope this helps.

Greg

Subject: fft/amplitude/frequency grid definiton problems

From: Jenn

Date: 3 Feb, 2003 19:40:31

Message: 4 of 5

Greg Heath wrote:
>
>
> Xngel <Xngel34@hotmail.com> wrote in message
> news:<eeb8816.-1@WebX.raydaftYaTP>...
>> Hello, I'm dealing with what I think is probably a
straightforward
>> issue where I'm missing some fundamental idea...
>> I'm trying to write a short routine to find the fundamental
frequency
>> in a digitally sampled piece of audio. I have the frequency grid
>> defined as
>>
>> f=Fs*(length(data))/2)/length(data)
>> where Fs is the sampling rate
>
> dt = 1/Fs
> t = (0:N-1)*dt
> df = 1/(N*dt)
> f = (0:N-1)*df
>
>> and the amplitude values as
>> datafft=(sqrt(abs(fft(data)))
>
> datafft= abs(fft(data)) % I use PSD = abs(dt*fft(data)).^2
>
>> I'm then finding max(datafft) and then finding the index value
of the
>> maximum, and telling MATLAB to return the frequency (from f)
that has
>> the same index value as the largest fft value.
>
> Q = fix((N+2)/2)
> [Fmax Imax] = max(datafft(1:Q))
> I = find(data(1:Q)==Fmxax)
>
>> The problem here is that f is half of the size of datafft, I'm
>> probably not defining the frequencies correctly. I know this is
>> probably a somewhat moronic question, but any help would be
greatly
>> appreciated!! Thanks much :)
>
> Hope this helps.
>
> Greg
>


Thanks for your time, both of you were a huge help, MATLAB is
starting to make more and more sense to me.


-Jenn

Subject: fft/amplitude/frequency grid definiton problems

From: Jeroen Boschma

Date: 4 Feb, 2003 08:43:02

Message: 5 of 5

I was a bit hasty with my reply, I messed up some indices ;( See below.

1) f = Fs*(0:(length(data)/2))/(length(data)+1);

      MUST BE:

     f = Fs*(0:(length(data)/2 - 1))/length(data);

2) f_max = Fs*max_indices/(2*length(datafft)+1);

      MUST BE

     f_max = Fs*max_indices/(2*length(datafft));

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us