Вы находитесь на странице: 1из 11

6/16/2017 ASSIGNMENT-1

ADAPTIVE FILTERS

SUBMITTED BY
ABDULLAH HASAN

SUBMITTED TO
DR. TAHIR ZAIDI

REG NO. 00000119234

MS-83

pg. 0
Question
a) Generate a random signal. The signal should be labeled x[n] and should have 1000 samples.
b) Generate a 4 tap moving average filter.
c) Generate 4 taps cross correlation and auto correlation matrices.
d) Use the LMS equation to calculate the taps of the moving average filter for 3 different values
of µ.
e) Use the RLS equation to calculate the taps of the moving average filter using any appropriate
value for δ and λ.

MATLAB CODE
clc
close all
clear all

%%%%%%%%%%%%%%%%%% Question %%%%%%%%%%%%%%%%%

% Generate a random signal. The signal should be labeled x[n]


% and should have 1000 samples.%

xn= randi(3,1000,1); %generating 1000 random samples b/w [0,3]

%Generate a 4 tap moving average filter.%


M=4;
for i=1:M
z(i) = 0;
for j=0:M-1
z(i) = z(i) + xn(i+j); % Implementing Moving Average Filter
end % summation j=0 to M-1 xn(i+j)
mov_avg(i) = z(i)/M; % 1/M summation j=0 to M-1 xn(i+j)
end

%Generate 4 taps cross correlation matrix.%

for s=1:M
un(s)=xn(s) % Input signal: taking 4 values of random signal
end
dn=mov_avg % Desired Signal: Moving average filter
length_matrix=length(un)+length(dn)-1; % finding the desired taps for
correaltion matrix
cross_cor=fftshift(ifft(fft(un,length_matrix).*conj(fft(dn,length_matr
ix))));
% cross correaltion matrix

%Generate 4 taps auto correlation matrix.%

auto_cor=fftshift(ifft(fft(un,length_matrix).*conj(fft(un,length_matri
x))));
% auto correaltion matrix

pg. 1
%Use the LMS equation to calculate the taps of the moving average
filter
%for three different values of µ.%

Rxx= [auto_cor(1) auto_cor(2) auto_cor(3) auto_cor(4);


auto_cor(2) auto_cor(1) auto_cor(2) auto_cor(3);
auto_cor(3) auto_cor(2) auto_cor(1) auto_cor(2);
auto_cor(4) auto_cor(3) auto_cor(2) auto_cor(1)]
% Auto correlation matrix in sqaure matrix form
eigen_matrix = eig(Rxx) % Finding eigen values
lamda_max=eigen_matrix(1); % Finding lamda Maximum to find the limit
of mu
for p=2:numel(eigen_matrix) % numel returns the total number of
matrix entries
if eigen_matrix(p)>lamda_max % Searching the maximum value
lamda_max=eigen_matrix(p)
end
end
upper_limit_mu=2/lamda_max % Finding the upper limit of mu
lower_limit_mu = 0; % lower limit of mu
MU = (upper_limit_mu-lower_limit_mu).*rand(3,1) + lower_limit_mu;
% Finding three random values of mu within the range

t=[0:3]; % defining x-axis for graphs


w=zeros(1,M); % Initializing taps (w) of the filter
for c=1:3
yn=0;
mu=MU(c) % Assigning different values of mu
for q=1:4
en(q) = dn(q) - w(q)' * un(q) % Finding Error
w(q+1) = w(q) + mu * en(q) * un(q) % Finding moving average
filter taps
end
for q=1:4
yn(q) = sum(w(q)' * un(q)) % Finding Adaptive output
end
figure;
plot(t,yn)
title('Adaptive output For LMS: For three different Values of mu')
end
figure;
plot(t,dn)
title('Desired Signal (LMS)')
figure;
plot(t,un)
title('Input Signal (LMS)')
figure;
plot(t,en)
title('Error (LMS)')

% Use the RLS equation to calculate the taps of the moving average
filter using any appropriate value for ? and ?.

pg. 2
t=[0:3]; % defining x-axis for graphs
w=zeros(1,M); % Initializing taps (w) of the filter
ita=10^4;
I=ones(1,M); %Identity Matrix
P=ita*I; %inverse correaltion matrix

for i=1:4
yn(i) = w(i)' * un(i);
en(i) = dn(i) - yn(i); %a priori estimation error
pi_n(i) = P(i) * un(i);
q = un(i)' * pi_n(i);
v = 1/(1+q);
kn(i) = v * pi_n(i); %Gain Vector
w(i+1) = w(i) + en(i)*kn(i) %tap weights
P(i+1) = P(i) - kn(i)*pi_n(i); %inverse correlation matrix
end
for i=1:4
yd(i) = sum(w(i)' * un(i)) %Adaptive Output
end
figure;
plot(t,dn)
title('Desired Signal (RLS)')
figure;
plot(t,un)
title('Input Signal (RLS)')
figure;
plot(t,en)
title('Error (RLS)')
figure;
plot(t,yd)
title('Adaptive Desired output (RLS)')

pg. 3
GRAPH PLOTS

pg. 4
pg. 5
pg. 6
pg. 7
pg. 8
OUTPUT
un =
3 3 1 3
dn =
2.5000 2.2500 1.7500 1.7500
Rxx =
9.0000 12.0000 15.0000 28.0000
12.0000 9.0000 12.0000 15.0000
15.0000 12.0000 9.0000 12.0000
28.0000 15.0000 12.0000 9.0000
eigen_matrix =
-19.5440
-2.4560
0.8397
57.1603
lamda_max =
-2.4560
lamda_max =
0.8397
lamda_max =
57.1603
upper_limit_mu =
0.0350
mu =
0.0221
en =
2.5000 1.7531 1.4682 0.8074

pg. 9
w=
0 0.1656 0.2818 0.3142 0.3677
yn =
0 0.4969 0.2818 0.9426
mu =
0.0124
en =
2.5000 1.9705 1.5834 1.1911

w=
0 0.0932 0.1666 0.1863 0.2307
yn =
0 0.2795 0.1666 0.5589
mu =
0.0349
en =
2.5000 1.4651 1.3350 0.3654
w=
0 0.2616 0.4150 0.4615 0.4998
yn =
0 0.7849 0.4150 1.3846
w=
0 0.8333 0.7917 0.8421 0.7589
yd =
0 2.5000 0.7917 2.5263

pg. 10

Вам также может понравиться