Friday, January 22, 2010

The Compute Spectrum Function

This function uses the FFT algorithm to compute the spectrum of an input with a number of points:

function [X,f]=ComputeSpectrum(x,fs,N);
% ComputeSpectrum Compute the Spectrum
%
% x input signal
% fs Sampling frequency of the input signal
% N number of points to evaluate spectrum (integer number of 2)
%
% X Absolute value of the spectrum (capital X for spectrum)
% f Frequency axis of the spectrum
%
% This function uses the FFT algorithm to compute the spectrum of an
% input signal with a number of points (N) N-point FFT where N must be
% a power of 2 (e.g. 2^10)
%
%Example: Compute the spectrum of sinusoid at 3Hz, fs=36Hz
%
% x=cos(2*pi*3*[0:12*3]*1/(12*3)); 12 times max freq
% [X,f]=ComputeSpectrum(x,12*3,2^12);
%
%Revision:0.0.1 jeastham
%
X=abs(fft(x,N));
f=(fs/N)*[0:N-1]; %make the axis freq
X=X(1:end/2);
f=f(1:end/2);

No comments: