# Frequency extracted from MATLAB's $\tt fft$ is not very accurate

As a test I made up a sine wave in MATLAB of this form

y = 5*sin((2 * pi * freq).*x + 1.4) - 6;


where freq is 10 and x varies from $0$ to $1.5$ with a resolution of 1/1000 as shown below

fs = 1000;
x = 0:1/fs: 1.5 - (1/fs);


So I already know the frequency to be able to verify it with fft. After computing the amplitude FFT abs(fft(yy)), I find that the frequency bin with the highest magnitude is $16$. Since I have $1500$ samples which correspond to a sampling frequency of $1000$ then the 16$^\rm{th}$ bin corresponds to

$$\mathrm{\frac{Frequency \ Bin \times Sampling \ Frequency}{Number \ of\ Samples} = \frac{16 \times 1000}{1500} = 10.6667\ Hz}$$

However I know that my frequency I hardcoded is actually $10\ \rm Hz$. This can be repeated with different values and the same inaccurate result keeps occurring. And the smaller the hardcoded frequency the larger the error in the result. Why is this happening?

• The frequency resolution is given by sampling frequency/fft length so in order to get better accuracy increase the fft length. If you can't do that (not enough samples), then you'll have to settle for what you already have. Mar 15 '18 at 9:16
• @dsp_user, This is not true at all! For a single pure tone in a DFT, the frequency (and phase and magnitude) can be found exactly. This is the main topic of my blog articles. Yes, I discovered all the formulas I present. The link to my blog is on my profile page. Mar 15 '18 at 12:55
• @CedronDawg Can you find the frequency exactly even if it is not periodic in the DFT aperture (i.e. if the DFT doesn't consist of just deltas)? Mar 15 '18 at 13:13
• How is this not true? What if the sampling frequency were, say, 4kHz? You woudn't be able to get the exact frequency then, would you (4bin*4000/1500 = 10.67)?. Mar 15 '18 at 13:16
• @dsp_user, Take all the time you want. The math is surprisingly simple, nothing beyond the definitions and ordinary algebra. No calculus or reliance on the continuous case whatsoever. I do use some Linear Algebra later on. The first article is about Euler's equation and the Unit Circle, I recommend starting there. Mar 15 '18 at 14:38

MATLAB uses one based indexing, DFTs are zero based, so you have to shift your index value by one.

The bin index corresponds to the the frequency in units of cycles per frame. Your freq is in units of cycles per second. The sample count is in units of samples per frame and the sampling rate is units of samples per second. The bin index calculation should be:

\begin{align} \mathrm{bin} &= \mathrm{freq} \cdot \frac{N}{f_s}\\ 15 &= 10 \cdot \frac{1500}{1000} \end{align}

Where the units are:

$$\rm \frac{cycles}{frame} = \frac{cycles}{second} \cdot \frac{\displaystyle\frac{samples}{frame}}{\displaystyle\frac{samples}{second}}$$

Bin 15 is in index 16 of MATLAB. Since this is a whole number of cycles per frame, all your other bin values should be zero.

Hope this helps.

Ced

$$\rm \frac{Frequency\ Bin \times Sampling\ Frequency}{Number\ of\ Samples} = \frac{15 \times 1000}{1500} = 10$$
Bin $-15$, aka bin $1485$, will also be non-zero and the complex conjugate of bin $15$. Each should have a magnitude of $2.5$. Your DC bin, aka bin $0$, should have a value of $-6$. The corresponding MATLAB indexes will be $16$, $1486$, and $1$.