Skip to content

Commit cebbfdb

Browse files
committed
LMS2
LMS fn used in the noise cancellation
1 parent 55b8b9e commit cebbfdb

File tree

5 files changed

+147
-25
lines changed

5 files changed

+147
-25
lines changed

LMS2.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function [av_e, av_w, w]=LMS2(L,mu,n)%%u is the input signal & n is the number of iteriation
2+
3+
w=zeros(1,L);
4+
e=zeros(1,L);
5+
for mu=mu
6+
for i2=1:100
7+
f0=1;
8+
f1=300;
9+
t1=2;
10+
t=0:2./9999:2;
11+
S= 0.9929*chirp(t,f0,t1,f1);
12+
L=length(S);
13+
power_of_signal=0.4965;
14+
power_of_noise=0.01*power_of_signal;
15+
v=sqrt(power_of_noise)*randn(1,1000+L);
16+
v=v(1001:1000+L);
17+
d=S+v;
18+
h = fir1(11, 0.25);
19+
% Apply the filter to the input sequence
20+
V2 = filter(h, 1, v);
21+
u=V2;
22+
23+
for i=1:n-1
24+
e(i)=d(i)-w(i).*u(i);
25+
w(i+1)=w(i)+mu.*u(i).*e(i);
26+
end
27+
28+
e_total(:,i2)=e.^2;
29+
W_total(:,i2)=w;
30+
end
31+
av_w = mean(transpose(W_total));
32+
av_e =mean(transpose(e_total));
33+
34+
end
35+
end
36+

SD.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function [Js Ws]=SD(u,d,m,mu)
2+
3+
sgma_d=var(d);
4+
5+
[Ps Rs]=correlation(u,d,m);
6+
7+
Ws=zeros(m,1);
8+
9+
L_max=max(eig(Rs));
10+
mu_max=2./L_max;
11+
12+
for mu=mu
13+
Ws=zeros(m,1);
14+
for i=1:10000
15+
16+
Ws=Ws+mu.*(Ps-(Rs*Ws));
17+
Js(i)=sgma_d-transpose(Ws)*Ps-transpose(Ps)*Ws+transpose(Ws)*Rs*Ws;
18+
19+
end
20+
%j=min(Js);
21+
figure
22+
plot(Js);
23+
title('Steepest descent ')
24+
hold on
25+
end

correlation.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function [P R]=correlation(u,d,m)
3+
% if length(u)>=length(d)
4+
% L=length(d);
5+
% else
6+
L=length(u);
7+
% end
8+
%CALCULATE R MATRIX
9+
r=zeros(1,m);
10+
11+
for n=1:m
12+
t=sum(u(1:L+1-n).*u(n:L))./length(u(1:L+1-n).*u(n:L));
13+
r(n)=t;
14+
end
15+
16+
R=toeplitz(r);
17+
P=zeros(m,1);
18+
for n=1:m
19+
q=sum(u(1:L+1-n).*d(n:L))./length(u(1:L+1-n).*d(n:L));
20+
P(n)=q;
21+
end

noise_cancellation.m

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
1-
% [signal,Fs] = audioread('eric.wav');
2-
% L=length(signal);
3-
% m=100;
4-
% Ts=1/Fs;
5-
% windowLength = round(0.01*Fs);
6-
% overlapLength = round(0.005*Fs);
7-
% S = stft(signal,"Window",hann(windowLength,"periodic"),"OverlapLength",overlapLength,"FrequencyRange","onesided");
8-
% S = abs(S);
91

2+
%-----------------------------------------------------------------------------------------
103
f0=1;
114
f1=300;
125
t1=2;
13-
t=0:2./9999:2
14-
S=2*chirp(t,f0,t1,f1);
6+
t=0:2./9999:2;
7+
S= 0.9929*chirp(t,f0,t1,f1);
158
L=length(S);
9+
figure
10+
plot(S);
11+
1612
% power_of_signal=var(signal);
1713
power_of_signal=var(S);
18-
power_of_noise=0.5*power_of_signal;
19-
v=sqrt(power_of_noise)*randn(1,1000+L);
20-
v=v(1001:1000+L);
21-
b=[1];
14+
power_of_noise=0.01*power_of_signal;
15+
v=sqrt(power_of_noise)*randn(1,5000+L);
16+
v=v(5001:5000+L);
17+
%% Generate input sequence u(n)
18+
% Design the half-band LPF
19+
order = 11;
20+
f_cutoff = 0.25; % normalized cutoff frequency (0.5 = Nyquist frequency)
21+
h = fir1(order, f_cutoff);
22+
% Apply the filter to the input sequence
23+
V2 = filter(h, 1, v);
24+
% n=length(v);
25+
% Plot the results
26+
% subplot(2,1,1);
27+
% plot( v);
28+
% title('Input sequence u(n)');
29+
% xlabel('n');
30+
% ylabel('v(n)');
31+
%
32+
% subplot(2,1,2);
33+
% plot(V2);
34+
% title('Output sequence v(n)');
35+
% xlabel('n');
36+
% ylabel('v(n)');
2237

23-
a=[1 0.5];
24-
V2=filter(b,a,v);
38+
%%
39+
n=4000;
2540
d=S+v;
2641
F=100;
27-
m=11;
28-
% for i=0:Fs/F %from 0 to 480 "480 window"
29-
30-
31-
[j w]= winner_filter(V2,d,m);
32-
%
33-
% y(i:100)=s(i:F*i)-transpose(j);
34-
% end
42+
m=100;
43+
mu=0.1;
44+
[j w]= winner_filter(V2,d,m,n);
45+
figure
46+
plot(j);
47+
title('winner filter');
48+
MMSE_w=min(j)
49+
%%
50+
[Js Ws]=SD(V2,d,m,mu);
3551
%figure
36-
%stem(w);
52+
% plot(Js);
53+
% title('Steepest descent filter');
54+
MMSE_SD=min(Js)
55+
p=var(d)
56+
57+
%%
58+
59+
[av_e, av_w, w]=LMS2(L,mu,10000);
60+
figure
61+
plot(av_e);
62+
title("LMS Learning Curves")

winner_filter.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [j w]= winner_filter(u,d,m,n)
2+
%calculating the variance of the desired signal
3+
sgma_d=var(d);
4+
5+
%CALCULATE R MATRIX
6+
%THE CROSS CORRELATION VECTOR P
7+
8+
[P R]=correlation(u,d,m);
9+
j=zeros(1,n);
10+
for i=1:n
11+
w=inv(R)*P;
12+
j(i)=sgma_d-transpose(P)*inv(R)*P;
13+
end
14+

0 commit comments

Comments
 (0)