Contents

Home Work 1 - Band Pass Recursive Filtering of Signal

% Programmer: Aaron Klapheck
% Date: 3-Mar-09
clear, clc, home
fprintf('The date and time: %s \n \n', datestr(now))
The date and time: 03-Mar-2009 11:42:38 
 

The Original Signals

t = 0:.01:1; % Sample time.
x = 5*sin(10*t) + sin(60*t) + .5*sin(200*t) + .5*sin(300*t); % Signal with noise
f_s = 100; % Hz. Sample rate. Given by (c-a)/b, where t = a:b:c.
           % Must be at least twice the signal's frequency: 2*(300/(2*pi))
f_nyquist = f_s/2;  % By definition

% give a top signal frequency:
% sample frequency => 2*(signal frequency)

plot(t, x)

The Theory of Recursive Filtering.

% Recursive Equation:
% y[n] = a0*x[n] + a1*x[n-1] + a2*x[n-2] + ... + b1*y[n-1] + b2*y[n-2] ...

% Note: Usually less than 24 terms used (12 a's and 12 b's).


% Band Pass Filter
% Equation:
% y[n] = a0*x[n] + a1*x[n-1] + a2*x[n-2] + b1*y[n-1] + b2*y[n-2]

Band Pass Filtering of a Signal x(t).

% Band pass filter equation:
% y[n] = a0*x[n] + a1*x[n-1] + b1*y[n-1]

% Get constant coefficiants
BW = .002; % The band width     Need to edit!!!
fp = .1; % The pass frequency   Need to edit!!!
R = 1 - 3*BW;
K = (1 - 2*R*cos(2*pi*fp) + R^4)/(2-2*cos(2*pi*fp));

a0 = 1-K;
a1 = 2*(K-R)*cos(2*pi*fp);
a2 = R^2-K;
b1 = 2*R*cos(2*pi*fp);
b2 = -R^2;

M = 7; % Numbr of points to average.
len_x = length(x);


y(M) = 1/M*(x(1) + x(2) + x(3) + x(4) + x(5) + x(6) + x(7));
y(M+1) = y(M) + 1/M*(x(8)-x(1));

for i = (M+2):len_x-M
    y(i) = a0*x(i) + a1*x(i-1) + a2*x(i-2) + b1*y(i-1) + b2*y(i-2);
end

% get rid of the fist few data points which are zero.
y = y(M:len_x-M);

t_sampled = t(M:len_x-M);

% Make sure the length of y matches the lenght of t.
len_t = length(t_sampled)
len_y = length(y)

plot(t_sampled, y)
len_t =

    88


len_y =

    88

Compairson of Sampled Signal to Filtered Signal

plot(t_sampled, y, t, x), xlabel('Time'), ylabel('Amplitude'), ...
    legend('Filtered Signal','Sampled Signal','Location','North'), ...
    title('Compairson of Sampled Signal to Filtered Signal')

Perform an FFT of both the original and the filtered signal

% Decide which exponant of 2 to use to make the fft go faster.
% This involves changing the sample frequecy to a larger number.
% This change of the sample frequency will change the look of the fft plot
% but the peaks which tell us where the natural frequencies are present will
% not change depending on fft_freq. To test this out set fft_freq equal to
% 100 or any other similar value and view the graph before and after.
exp = log(f_s)./log(2);
fft_freq = 2^(ceil(exp));


x_fft = fft(x, fft_freq);
x_fft = abs(x_fft);     % Eliminate immaginary componant
x_fft = x_fft(1:(fft_freq/2)); % Plot from 0 to .5*f_s

frequency_vector = f_nyquist*linspace(0,1,(fft_freq/2));


y_fft = fft(y, fft_freq);
y_fft = abs(y_fft);     % Eliminate immaginary componant
y_fft = y_fft(1:(fft_freq/2));

plot(frequency_vector, x_fft, frequency_vector, y_fft), ...
    xlabel('Frequency'), ylabel('Frequency Content'), ...
    title('Frequency Content in the Original and Filtered Signal'), ...
    legend('Original Signal', 'Filtered Signal')


% Notice that the low frequecies were filted out of the original signal
% leacving only the two high frequencies in the filtered signal. The high
% frequencies in this case represent the noise which has an average value
% of 0.