# FFT for a single frequency

I was looking for a more efficient way of finding the magnitude and phase of a signal at a certain frequency without performing an FFT because it produces more information than I need and I came across this comment on the Matlab message board:

If I am sampling at 1500Hz and my signal is 100 points and I am looking for the phase and magnitude at 15Hz, would this be suitable method for obtaining the information I need? Also, why does the OP suggest windowing the signal as well?

• Using Goertzel algorithm in matlab: goertzel(x, 1:N) * 2 / N Jun 23 '17 at 9:50

It's certainly calculating the right thing. Though instead of

sum(x.*(cos(1000*2*pi*t)-i*sin(2*pi*1000*t)))*2/N;


you might try

sum(x.*exp(-i*2*pi*1000*t))*2/N;


If you need to do something similar, but in-line (not in a batch), you might want to look at the Goertzel algorithm. As the Wikipedia link says:

.. provides a means for efficient evaluation of individual terms of the Discrete Fourier Transform

And there's nothing particularly "Fast" about this way of doing it, so it's really just calculating one DFT bin.

• Also, I dont think OP would need to window $x[n]$ for the one bin DFT correct? I do not know why it was recommended to him in that other forum... Apr 11 '13 at 18:38
• +1 for the Goertzel algorithm, that is the best solution here Apr 11 '13 at 19:08
• Can you think of any reason why an FFT and the Goertzel algorithm would produce the same result while the cos/sin summation formula would produce a different result? Apr 12 '13 at 13:19
• Just the usual: off-by-one indexing, out by some scale term. What error are you seeing?
– Peter K.
Apr 12 '13 at 13:24
• FFT and Goertzel are producing 874.4778 as the result. Summation is producing 13.0451. Seems to be an issue of scaling but the factor doesn't seem normal. Apr 12 '13 at 13:26

An $N$-point DFT for single bin $k$ can be computed as:

k = 3;
N = 10;
x = [0:N-1];
X = sum(x.*exp(-i*2*pi*k*[0:N-1]/N));


Where the bin frequency is given by $k*fs/N$

If you wish to do this regularly overtime as in a STDFT, you can use the sliding DFT or sliding Goertzel (cheaper) . The sliding Goertzel is essentially a comb filter (with a delay set to $N$ samples) followed by a second order IIR. You need only compute the complex spectral coefficient every $hop$ samples.

This paper also describes how to perform frequency domain windowing by convolving adjacent bins with the DFT of the window. For example, a hanning windowed version of the $k$th bin can be obtained by

$$-0.25X_{k-1} + 0.5X_{k} - 0.25 X_{k+1}$$ In , there is some discussion of implementation, precision and other considerations when using the sliding Goertzel.

Hope that helps.

 Eric Jacobsen and Richard Lyons. The Sliding DFT. IEEE Signal Processing Magazine, Vol. 20, No. 2. (March 2003), pp. 74-80.

• Is there any use to windowing the data before hand for a single-point DFT though? Apr 11 '13 at 22:10
• @Mohammad Well you can view it as taking an N-point DFT for all N bins, and only looking at one bin. The question is then, is windowing important when taking a DFT? Well this depends on the application. No window = a rectangular window, which has a different frequency response to say a hanning window. In terms of magnitude response, different windows have different main lobe and side lobes characteristics. I imagine this is what you care about. See ccrma.stanford.edu/~jos/sasp/Spectrum_Analysis_Windows.html
– Dom
Apr 12 '13 at 9:56
• @Dom I am familiar with the windowing process - what I am asking about is why one would care to window the original data signal, when you only care about 1 DFT bin. I dont think there are any advantages to that. The whole point of windowing is how it affects magnitude ratios among bins. Here though, we care about one bin only. Apr 12 '13 at 14:36
• @Mohammad I think about each bin as a bandpass filter. So windowing alters the response of this filter to frequencies other than the one that it is tuned to (since we are smoothing the 'unwindowed' response with the DFT of the window). To make it clear, compare the magnitude response (20*log10(abs(X)) of a single bin as a function of different input frequencies (non-integer k values in the above code) for both a windowed and 'unwindowed' input sinusoid.
– Dom
Apr 13 '13 at 14:17