Digital Signal Processing Reference
In-Depth Information
This program is shown in its entirety, since its pieces are shown above. To
summarize what it does, rst it makes an example signal by adding several sinusoids.
Next, it nds the Fourier transform, then prints the frequency resolution. After this,
it plots the magnitudes. It also plots the phases, but rst it zeros-out the phases
that have a tiny corresponding magnitude.
% spectrum.m
%
% Show the spectrum of an example signal.
%
% Set up an example signal
n = 0:99; % number of points
fs = 200; % sampling frequency
Ts = 1/fs; % sampling period
% x is our example signal
x = cos(2*pi*20*n*Ts + pi/4) + ...
3*cos(2*pi*40*n*Ts - 2*pi/5) + ...
2*cos(2*pi*60*n*Ts + pi/8);
% Find the spectrum
X = fft(x);
%m = 0:length(X)-1;
half_m = 0:ceil(length(X)/2);
disp(sprintf(Freq resolution is every %5.2f Hz,...
fs/length(X)));
% Plot magnitudes
subplot(2,1,1);
%stem(m*fs/length(X),abs(X), b);
stem(half_m*fs/length(X),abs(X(half_m+1)), b);
ylabel(magnitude);
xlabel(frequency (Hz));
title(Frequency magnitude response);
% Plot phase angles
subplot(2,1,2);
% The next 3 lines allow us to ignore phases
% that go with very small magnitudes.
tolerance = 0.00001;
Search WWH ::




Custom Search