DSPRelated.com
Forums

FFT bins: how do they exactly relate to frequency?

Started by Rick81 June 15, 2009
Hi everybody, 

I am Richard and I am new to this news group, new to dsp and fft.
And of course I have a question...


I have coded an fft and now I'm not sure how the frequency in hz relates
to the bin index and the space between the bins.  

Lets assume I have taken N=1024 samples from an ADC at fs=256khz.
Further I have calculated the fft and its magnitude spectrum containing
512 bins.

What I already know:
The highest frequency I get will be 256khz/2 = 128khz.
The lowest frequency will be ((256khz)^-1 * 1024)^-1 = 250hz.
The spacing between the bins is fs/N=250hz.

Where I am NOT sure:
Is it correct that the first bin represents the frequency range from DC to
250hz, the second the range from 250hz to 500hz and so on until the last
bin which represents 127,75khz to 128khz?? 
Would be logic, because this assumption counts 512 points. But is it
really that way?

By assuming that is correct, the center frequency of the first bin whose
boundaries are DC to 250 Hz is right in the middle at 125hz?? 
for the second one its right between 250Hz and 500hz --> 375hz, and so on.

Is this correct or did I understand something wrong??


Different Thing: Lets assume I use a window for this sampled ADC data. 
If I look at the window function in the frequency domain it has the main
lobe and to left and right its side lobes. 
Now If i make an fft of the windowed data, it is like I have a set of 512
filters, where every bin is in the center of a filters main lobe, or??


Often the frequency response of the windows is normalized to +-pi on the x
axis, where the main lobe is in the middle at zero. Where does this
notation come from, and how can I convert it to frequency? 


Thanks in advance, 
I hope somebody will answer my questions. 

Greets from germany, 
Richard








On 15 Jun, 13:43, "Rick81" <richard.webchec...@gmx.de> wrote:
> Hi everybody, > > I am Richard and I am new to this news group, new to dsp and fft. > And of course I have a question... > > I have coded an fft and now I'm not sure how the frequency in hz relates > to the bin index and the space between the bins. &#4294967295; > > Lets assume I have taken N=1024 samples from an ADC at fs=256khz. > Further I have calculated the fft and its magnitude spectrum containing > 512 bins. > > What I already know: > The highest frequency I get will be 256khz/2 = 128khz.
Nope. That's the Nyquist frequency. The difference is that you must actively filter the (analog) signal such that its bandwidth does not exceed 128 kHz.
> The lowest frequency will be ((256khz)^-1 * 1024)^-1 = 250hz.
The lowest frequency in the (baseband) spectrum is DC.
> The spacing between the bins is fs/N=250hz.
The most commonly used term is 'bin width'. The bins represent the intervals between subsequent 'spectrum lines' or 'Fourier coefficients.'
> Where I am NOT sure: > Is it correct that the first bin represents the frequency range from DC to > 250hz, the second the range from 250hz to 500hz and so on until the last > bin which represents 127,75khz to 128khz?? > Would be logic, because this assumption counts 512 points. But is it > really that way?
For the purposes of a newbie's question: Yes, your understanding is more or less correct. Very oversimplified, but more or less correct. Be aware, though, that the truth is a bit more involved. The term 'sinc interporlation' describes the situation a bit more accurately. Your questions below touch on it.
> By assuming that is correct, the center frequency of the first bin whose > boundaries are DC to 250 Hz is right in the middle at 125hz?? > for the second one its right between 250Hz and 500hz --> 375hz, and so on. > > Is this correct or did I understand something wrong??
Think of the frequency bins as being *centered* at 0 Hz, 250 Hz etc.
> Different Thing: Lets assume I use a window for this sampled ADC data. > If I look at the window function in the frequency domain it has the main > lobe and to left and right its side lobes. > Now If i make an fft of the windowed data, it is like I have a set of 512 > filters, where every bin is in the center of a filters main lobe, or??
This is exactly what sinc-interpolation is all about: Each spctrum coefficient has the top of a sinc's main lobe associated with it. This sinc also has zero crossings at all the other spectrum coefficient locations. This is a standard result, but might be somewhat beyond what a newbie might want to dive into.
> Often the frequency response of the windows is normalized to +-pi on the x > axis, where the main lobe is in the middle at zero. Where does this > notation come from, and how can I convert it to frequency?
Read e.g. Lyons' "Understanding Digital Signal Processing", 3rd edition. Rune
On Jun 15, 7:43 am, "Rick81" <richard.webchec...@gmx.de> wrote:
> Hi everybody, > > I am Richard and I am new to this news group, new to dsp and fft. > And of course I have a question... > > I have coded an fft and now I'm not sure how the frequency in hz relates > to the bin index and the space between the bins. > > Lets assume I have taken N=1024 samples from an ADC at fs=256khz.
N = 1024 Fs = 256e3 dt = 1/Fs % 3.9063e-006 t = dt*(0:N-1); T = N*dt % 0.0040 t = 0:dt:T-dt;
> Further I have calculated the fft and its magnitude spectrum containing > 512 bins.
df = 1/T % 250 f = df*(0:N-1); % N*df = N/T = 1/dt = Fs f = 0:df:Fs-df; X = fft(x); A = abs(X); The double sided spectrum will have N bins. A = A(1:N); A = [A(1),A(2:N/2),A(N/2+1),A(N/2+2:N)]; % For real x, A(N+2-k) = A(k) A = [A(1),A(2:N/2),A(N/2+1),A(N/2:-1:2)]; As = [A(1),A(2:N/2),A(N/2+1)]; fs = df*(0:N/2); The single sided spectrum will have N/2+1 = 513 bins. The first bin A(1) contains DC, the last bin A(N/2+1) contains the Nyquist component.
> What I already know: > The highest frequency I get will be 256khz/2 = 128khz. > The lowest frequency will be ((256khz)^-1 * 1024)^-1 = 250hz. > The spacing between the bins is fs/N=250hz. > > Where I am NOT sure: > Is it correct that the first bin represents the frequency range from DC to > 250hz, the second the range from 250hz to 500hz and so on until the last > bin which represents 127,75khz to 128khz?? > Would be logic, because this assumption counts 512 points. But is it > really that way? > By assuming that is correct, the center frequency of the first bin whose > boundaries are DC to 250 Hz is right in the middle at 125hz?? > for the second one its right between 250Hz and 500hz --> 375hz, and so on. > > Is this correct or did I understand something wrong??
As(1) ~ [0,1/2)*df As(k) ~ [k-3/2,k-1/2)*df, 2<=k<=N/2 As(N/2+1) ~ (N/2-1/2,N/2]*df Note that, in the single sided spectrum, the representative frequency ranges for DC and Nyquist are half as long as the other components. Also note that, for practical work, the Nyquist component should be filtered out before the ADC. Then one has the option to use N/2+1= 513 bins with As(513) = 0 or N/2 = 512 bins with As(513) = []. When reading the literature be cognizant of what representation is being used. Hope this helps. Greg
On Jun 16, 1:54&#4294967295;am, Greg <he...@alumni.brown.edu> wrote:
> On Jun 15, 7:43 am, "Rick81" <richard.webchec...@gmx.de> wrote: > > > Hi everybody, > > > I am Richard and I am new to this news group, new to dsp and fft. > > And of course I have a question... > > > I have coded an fft and now I'm not sure how the frequency in hz relates > > to the bin index and the space between the bins. > > > Lets assume I have taken N=1024 samples from an ADC at fs=256khz. > > N &#4294967295;= 1024 > Fs = 256e3 > dt = 1/Fs &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; % 3.9063e-006 > t &#4294967295;= dt*(0:N-1); > T &#4294967295;= N*dt &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; % 0.0040 > t &#4294967295;= 0:dt:T-dt; > > > Further I have calculated the fft and its magnitude spectrum containing > > 512 bins. > > df = 1/T &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% 250 > f &#4294967295;= df*(0:N-1); > % N*df = N/T = 1/dt = Fs > f = 0:df:Fs-df; > > X = fft(x); > A = abs(X); > > The double sided spectrum will have N bins. > > A = A(1:N); > A = [A(1),A(2:N/2),A(N/2+1),A(N/2+2:N)]; > % For real x, A(N+2-k) = A(k) > A &#4294967295;= [A(1),A(2:N/2),A(N/2+1),A(N/2:-1:2)]; > As = [A(1),A(2:N/2),A(N/2+1)];
Correction: To represent all of the signal energy As = [A(1),sqrt(2)*A(2:N/2),A(N/2+1)]; Hope this helps. Greg
On Jun 15, 8:12&#4294967295;am, Rune Allnor <all...@tele.ntnu.no> wrote:
> On 15 Jun, 13:43, "Rick81" <richard.webchec...@gmx.de> wrote: >
...
> > By assuming that is correct, the center frequency of the first bin whose > > boundaries are DC to 250 Hz is right in the middle at 125hz?? > > for the second one its right between 250Hz and 500hz --> 375hz, and so on. > > > Is this correct or did I understand something wrong?? > > Think of the frequency bins as being *centered* at 0 Hz, 250 Hz etc.
and at -250 Hz (k=N-1), -500 Hz (k=N-2), etc. you *can* think of them as a bin that collects all of the energy from -1/2 bin width to +1/2 bin width from the center, but only under the assumption that the analog signal was heterodyned down from k*Fs/N to DC and passed through a brickwall filter that has gain of 1 from -1/2 to +1/2 bin width (which is +/- Fs/(2N)). that means that you're assuming that the filter has an impulse response of h(t) = (Fs/2N) * sinc((Fs/2N)*t) the signal that comes outa that should be whatever you have in bin #k. r b-j
Rick81 <richard.webchecker@gmx.de> wrote:
 
< I have coded an fft and now I'm not sure how the frequency in hz relates
< to the bin index and the space between the bins.  
(snip)
 
< Is it correct that the first bin represents the frequency range from DC to
< 250hz, the second the range from 250hz to 500hz and so on until the last
< bin which represents 127,75khz to 128khz?? 

The FFT has periodic boundary conditions.  The samples given to the
FFT are assumed to be samples of a periodic function, and the results
are based on that.  The first bin is the DC bin, the average of
all the samples.  Now, consider a 1Hz input to the above.  
Only 1/250th of the signal will be sampled, and the result will 
depend on which part that is.  It might even be zero.

(snip)

-- glen

There are so many smart people on this word!!
Especially Rune, Greg, r b-j and glen  :)

You helped me a lot!! 
Now I know how to scale my frequencies.


Glen, you wrote: 
>The first bin is the DC bin, the average of >all the samples. Now, consider a 1Hz input to the above. >Only 1/250th of the signal will be sampled, and the result will >depend on which part that is. It might even be zero.
Does this mean, that I can throw the dc bin away, because its value is - more or less- random ? Greetings Richard
On 16 Jun, 23:34, "Rick81" <richard.webchec...@gmx.de> wrote:

> Does this mean, that I can throw the dc bin away, because its value is - > more or less- random ?
No. Throw it away if you don't need it. Otherwise, keep it. Rune