# Combining bins of FFT ouput

I was trying to combine output of a $2n$ point Real FFT to generate custom FFT bins.

For example the FFT generates components at equally spaced frequencies $f_0,f_1,f_2 ...f_{n-1}$

$f_0$ = $0$ Hz
$f_{n-1}$ = SamplingFrequency/2

Instead of generating a linearly distributed bins, I want to create custom bins. For example: How do I create a bin for frequency $(f_0+f_1)/2$. Can simply average $bin_0$ and $bin_1$ ?

-
Hi Jean, it seems like your new to the mathematics part of stack exchange, for some basic information about writing math at this site see e.g. here, here, here and here. You may find that better formatted questions are more likely to receive good answers! –  Tom Oldfield Dec 18 '12 at 22:34
Thanks for pointing out. Formatted! –  Jean Dec 18 '12 at 22:48
If you write out words like "sampling frequency" and "bin", they get interpreted as a juxtaposition of variables names and formatted accordingly (e.g. italicized). You can include text in formulas using the \text{...} command. (Also you forgot to convert several of the indices to subscripts.) –  joriki Dec 18 '12 at 23:07

Certainly nothing keeps you from avergaging bin $0$ and bin $1$; the question is what you're trying to calculate. The FFT calculates a discrete Fourier transform, and you're trying to interpret it as something continuous. That's possible, but it requires a careful interpretation.

One interpretation goes like this: The data you transform represent samples taken over a finite interval with equal spacing. The Fourier coefficients that the FFT computes are the discrete Fourier transform of the data, which you can interpret as the Fourier sum for the unique periodic function that interpolates the sampled values and is band-restricted by the Nyquist frequency. You can view these Fourier coefficients as the coefficients of delta peaks at the corresponding frequencies in a continuous Fourier integral. Now if you restrict the periodic function to a single period, you effectively multiply it by a rectangular window, and in Fourier space that corresponds to convoluting it with the $\operatorname{sinc}$ function. Thus each of the delta peaks gets replaced by a copy of the $\operatorname{sinc}$ function centred at the corresponding frequency. Since these $\operatorname{sinc}$ functions are $1$ at their central frequency and $0$ at the frequencies corresponding to all other coefficients, this gives rise to a continuous function that interpolates between the discrete Fourier coefficients. It's this function that has a well-defined interpretation, namely as the Fourier transform of a single period of the band-limited interpolation of your data.

Thus, to get something meaningful, you'd have to interpolate with $\operatorname{sinc}$ functions, so you'd need a full sum over all the coefficients to get a single interpolated value, not just a combination of the two adjacent coefficients. However, if your data are sufficienly well-behaved, it will often be a good approximation to do linear interpolation instead as you suggested.

-
I am trying to create logarithmically spaced frequency bins instead of the linearly spaced bins that FFT creates by default. –  Jean Dec 19 '12 at 16:38
@Jean: We had all already understood that. If you just want a recipe (with no justification and no guarantees that it is "the right" one), go ahead with your recipe, it's often a good approximation. If you want to get to the bottom of the thing, then make an effort and read joriki's answer, just as he made the effort to write it. –  leonbloy Dec 21 '12 at 19:58
Thanks for pointing out. I missed the last paragraph of his answer! –  Jean Dec 21 '12 at 20:01

Just to add to add to joriki's answer: if what you are trying to do is a "resampling" of the discrete Fourier transform, and if the resampling factor is near one, a linear or cuadratic interpolation is often enough.

If you have a large and-or varying resampling factor, (in particular if you want to do some remapping to a logarithmic scale), where several bins of the original FFT are roughly mapped to a new single bin, a simple average of the original bins can suffice.

But, just as with other general resampling procedures, it's usually better to employ some "window function" to minimize aliasing effects (which amounts to performing a weighted average; the plain average would correspond to a rectangular window). Typically, in linear FFT to MEL-scale (or MEL -like, i.e., logarithmic frequency scale) a varying triangular window (with overlaping) is applied, which is often called a "filter bank"; don't let the name intimidate you, it's just a weighted average. See eg here or here or here.

-