Lets assume we have following: 440 Hz signal, 2048 samples at 44100 sampling rate
If we didn't know what frequency is contained within this signal, we would use fourier transform. By reading the X coordinate of the highest peak when we plot the result (stem), we see it is at value of 21.
Size of the spectrum corresponds to the size of the input signal (2048) which represents all frequencies between 0 half of the sampling rate (22050).
Note: The FFT spectrum is symmetrical (mirrored)
Using this information and accounting the DC component on the 0th index, we are able to compute, that index 21 corresponds to 430.66 Hz, which is roughly the original 440 Hz.
To get the result just as 'fft' function does, rewrite the following equation using cycles
u = index of spectral coefficient
M = sample index
j = Imaginary unit (in MATLAB = 'i')
Use MATLAB function dftmtx
Use 'abs' function when using plot / stem on FFT result - to plot only real part of the FFT result.
DC constant - addition of simple constant to all values in signal. In FFT spectrum it correspons to 0th bin.