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?
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} &amp;= \mathrm{freq} \cdot \frac{N}{f_s}\\
15 &amp;= 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



Follow up

Your calculation should have been:

$$\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$.