Jump to content

User:Cxw/How to use the DFT, with Matlab examples

From Wikipedia, the free encyclopedia
DFT quick reference
What is it? DFT element matlab example and comments
How often do you want to sample? sampling frequency
fsample=100;
sample at 100Hz
psample=1/fsample;
sample period
For how long do you want to sample? time range
tmax=10;
run from 0 to 10 sec. inclusive
How many samples does that give you?
nsamples=tmax/psample+1;
+1 because we include t=0 and t=tmax.
nsamples=2^ceil(log2(nsamples));
round up to a power of 2 in length so FFT will work.
times=(0:psample:(nsamples-1)*psample)';
Create a column vector of sample times.
How far apart are each of the frequency-domain result points?
delf=fsample/nsamples;
frequencies=(0:delf:(nsamples-1)*delf)';
Column vector of result frequencies
What signal do you want to sample? input
x=sin(2*pi*10*times)+sin(2*pi*3*times);
Make a 10Hz sine wave plus a 3Hz sine wave
What are the results? Fourier transform fft_x=fft(x, length(x));
What frequencies does the signal have? fft_x_mag=abs(fft_x);
What phase relationships? fft_x_phase=unwrap(angle(fft_x));
How do you view the results?
plot(frequencies, fft_x_mag);
Or, to match the amplitude of the magnitude peak to the amplitude of the sine wave,
plot(frequencies, (2/nsamples)*fft_x_mag);
What about the power spectrum?
fft_x_power=fft_x_mag.^2;
plot(frequencies, fft_x_power);

Notes

[edit]

References

[edit]

Lyons, Richard G. Understanding digital signal processing. Upper Saddle River: Prentice Hall PTR, 2001. ISBN 0-201-63467-8. Chapter 3 discusses the DFT.