Friday, January 22, 2010

DSP Tutorial #5: Frequency Analysis


This example computes the spectrum of a complex signal using the ComputeSpectrum function.

Couple important things to notice:
- Our range of frequencies on the x-axis is Fs/2
- As Fs increases, the resolution in the frequency domain is much better
- As our FFT bins (2^N) increases, the more bins we have (we can still resolve the freq content provided we meet the sample theorem, we have more bins - basically a finer plot)
- As our windows length increases, the better resolution we have


close all;
clear all;
fs=500;
Ts=1/fs;
n=0:1*fs;
tn=n*Ts;
x=5+cos(2*pi*200*tn)+10+cos(2*pi*100*tn);
% Time Domain Plot
figure('Color',[1 1 1]);
plot(tn,x);box off;
title(['Time Domain Signal']);
[X,f]=ComputeSpectrum(x,fs,2^14);
figure('Color',[1 1 1]);
plot(f,20*log(X));
title(['Freq Domain']);
axis('tight');

2 comments:

claypipkin said...

I'm getting the following error after copying and pasting your code:

"
Error in DSP5 (line 15)
[X,f]=ComputeSpectrum(x,fs,2^14);
"

Any thoughts as to why matlab doesn't like this? I have the signal analysis toolbox installed.

Me said...

It looks like the ComputeSpectrum function is an existing function, just not one Matlab supports. I found documentation for it here.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundMixer.html#computeSpectrum()
However, it just takes the FFT of the signal. Matlab does this with functions like FFT, and freq. Here's the MatLab help page:
http://www.mathworks.com/help/matlab/examples/using-fft.html
Good luck!