I don't know why I am having such a hard time with this math, but I am. I am doing phase-sensitive detection of a few sensors, which are triggered by a modulated light source. The modulation frequency f0 changes by up to several Hz over the course of the day based on ambient conditions, and is typically around 1370-1390 Hz. I'm doing an FFT to extract the sensor amplitudes at f0. With my system, I'm limited to an upper sampling rate of 10 kHz for up to 2 seconds, but no longer.

Given that I can dynamically change f0, sample rate, and window length, what is the formula for making sure that f0 is in the center of a bin?


The concept you need is to have a whole number of cycles within your sample frame. The cycle count will be the bin index.

For the math, consider carrying units:

$$ \frac{Cycles}{Frame} = \frac{Cycles}{Second} \cdot \frac{Seconds}{Frame} $$


$$ \frac{Seconds}{Frame} = \frac{ \frac{Samples}{Frame} }{ \frac{Samples}{Second} } $$

Solving for your sampling Rate:

$$ \frac{Samples}{Second} = \frac{ \frac{Samples}{Frame} }{ \frac{Seconds}{Frame} } $$

$$ \frac{Samples}{Second} = \frac{ \frac{Samples}{Frame} \cdot \frac{Cycles}{Second} }{ \frac{Cycles}{Frame} } $$

If you have a pure tone, there isn't a need to center it on a bin to find your parameters, although it is much easier to do it that way.

To find the frequency, you can use the formulas in these blog articles:

Once you have the frequency, you can calculate the amplitude and phase with the formulas in this article:

You should also realize that you don't need to call a full FFT. You simply need to calculate the two or three bin values you are interested in which is generally a lot quicker.

  • $\begingroup$ Thanks for the comprehensive answer! Can you elaborate a bit on your last point about not needing a full FFT? $\endgroup$
    – Ben S.
    Feb 22 '19 at 19:24
  • $\begingroup$ @BenS., Sure, that's why I added the question reference in my comment above. A DFT bin calculation is a dot product from a mathematical point of view. The source code in my answer there contains a complete implementation of a single bin calculation. If you have a whole number of cycles in the bin you only need to calculate that one bin value. $\endgroup$ Feb 22 '19 at 19:49
  • $\begingroup$ This is very helpful, thank you. I'm somewhat new to DSP but it's answers like yours that help out a lot. My window has a whole number of samples in it, but I'm still seeing some spread in the peak at my modulation frequency, and it's always off by 1 Hz in my spectrum output. I've scoped my modulation clock as well as my ADC logic lines to make sure everything is ticking at the correct rates, and I've got no problems there. Do you have any advice for this? $\endgroup$
    – Ben S.
    Feb 22 '19 at 21:26
  • $\begingroup$ @BenS., Can you send me some sample data, and perhaps your code, to my email: cedron at exede dot net. $\endgroup$ Feb 22 '19 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.