Take the 2-minute tour ×
Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. It's 100% free, no registration required.

I am trying to sample a sine wave and plot it's frequency components, but I am having problems implementing it.The result of taking 65536 samples of one cycle of a sine wave with max amplitude 1 and a frequency 100 can be seen below.Where the Y-axis this the magnitude of the complex Fourier sum, and the x-axis is the sample number.How can I see what frequency the sine wave has from this plot?, I would like to have a plot of freq vs Magnitude and not sample Number vs magnitude.

I was expecting one big spike of amplitude 1 or 2 spikes of amplitude 0.5 each, but I seem to be getting a large amplitude, I don't know what i might be doing wrong.

FFT of sin(2*pi*f*t)

Matlab Code:

Amp = 1;
freq = 100;
dt = 2 * pi /65536;
index = 1;
for t = 0:dt:2*pi
   sine(index) = Amp * sin(2*pi*freq*t);
   sampleNumber(index) = index;
   index = index + 1;
end

transform = fft(sine);
magTransform = abs(transform);
plot(sampleNumber,magTransform);
share|improve this question
1  
FFT implementations often have a gain proportional to $N$ or $\sqrt{N}$ in them. Your result isn't unreasonable. –  Jason R Feb 24 at 14:22
    
Dividing the magnitude by $N$ gives two spikes with magnitudes of around 0.425 each (which is still lower than the 0.5 expected).How do I change the x-axis to frequency from the sample number? –  KillaKem Feb 24 at 14:34

4 Answers 4

up vote 4 down vote accepted

It looks like you're getting your frequency in Hertz confused with in radians/sec since you have the factor of $2\pi$ in both your sampling period dt and your signal. I rewrote a bit of your code to clarify what I think you really want.

Amp = 1;
freqHz = 12000;
fsHz = 65536;
dt = 1/fs;
index = 1;
sine = [];
sampleNumber = [];
for t = 0:dt:1-dt
   sine(index) = Amp * sin(2*pi*freqHz*t);
   sampleNumber(index) = index;
   index = index + 1;
end

% alternative to the above loop
% sine = Amp*sin(2*pi*freq*(0:dt:1-dt));

N = 65536;
transform = fft(sine,N)/N;
magTransform = abs(transform);

faxis = linspace(-fsHz/2,fsHz/2,N);
plot(faxis/1000,fftshift(magTransform));
axis([-40 40 0 0.6])
xlabel('Frequency (KHz)')

If your sampling frequency is 65536 samples/second, and you want for example a tone at 12 KHz, you can create it as shown. So here your sample period is 1/65536 seconds.

Your expectation of getting two spikes with an amplitude of 0.5 each was correct -- just your generated tone was not.

As for scaling the x-axis to be in Hertz, just create a vector with the same number of points as your FFT result and with a linear increment from $-fs/2$ to $+fs/2$. Note also the fftshift I used in the plot. That's because the output of Matlab's FFT function goes linearly from 0 to fs. I find it easier to visualize having DC centered but either way is fine. Without the fftshift the faxis vector would go from 0 to fs.

FFT of a 12 KHz tone

share|improve this answer

Some FFTs require dividing by 1/N to represent magnitude "naturally" (which is non-energy preserving). To label the X axis requires knowing the sample rate (Fs). If known, then f_x = bin_index * Fs / N, up to N/2, then mirrored for negative frequencies. If the frequency of a spectral peak (your input sinewave) isn't exactly periodic in the FFT length (e.g. an integer number of cycles), then the magnitude of the closest FFT result bin will be smaller, and you will need to interpolate between bins to find a closer estimate to the peak magnitude (parabolic or windowed-Sinc kernel interpolations are common).

share|improve this answer

To add som formulas to the answer of hotpaw2:

With the FFT you compute a representation of your signal as

$$x(t)=\sum_{k=0}^{N-1} \hat x_k e^{2\pi i\,f_k\,t}$$

where $f_k=\frac{k}{N}f_s$ for $k=0,1,...,N/2-1$ and $f_k=\frac{k-N}{N}f_s$ for $k=N/2,...,N-1$, assuming $N$ even.

Now the FFT requires that the samples be taken with sampling step $\tau=1/f_s$, $x_n=x(n\tau)$, and the FFT of the sample array $(x_n)_n$ gives the scaled amplitude array $(N\hat x_k)_k$, since $\sum_{k=0}^{N-1}1=N$. The rescaling is usually left out of the FFT implementations to be dealt with by the user of the FFT library.

share|improve this answer

FFT provides method of computing DFT this you already know. now consider a signal x(n) and its DFT X(k). if your signal consist of N(65536 in your case) samples then X(k) will provide values at discrete frequencies of 2*pi*k/N .In fact the above DFT X(k) means X(2*pi*k/N). so if you are finding X(1) then it means you are finding DFT coefficient at discrete frequency of 2*pi*1/N and similary ,X(2) means coefficient for 2*pi*2/N and hence so on .Each coefficient shows contribution of that frequency in that signal if its large then it means that frequency constitute major part of signal. so for plotting fft with respect to frequency replace sample axis with frequency axis having points 2*pi*k/N where k=0 to 65535.FT never provides any information regarding time .it just provide frequency information of the signal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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