Difference between revisions of "Tuner Upper Removal Demonstration"

From Class Wiki
Jump to: navigation, search
Line 5: Line 5:
 
clear all
 
clear all
 
Totaltime=1;
 
Totaltime=1;
speak('Hit a key and speak the signal.');
+
speak('Hit a key and speak the signal.');
pause;
+
pause;
[st, Fs] = recordsound(Totaltime, 22050, 1);
+
[st, Fs] = recordsound(Totaltime, 22050, 1);
s = st';
+
s = st';
Ls = length(s);
+
Ls = length(s);
% Interference + random noise
+
% Interference + random noise
speak('Hit a key and make the noise!');
+
speak('Hit a key and make the noise!');
pause;
+
pause;
[nt,Fs] = recordsound(Totaltime, 22050, 1);
+
[nt,Fs] = recordsound(Totaltime, 22050, 1);
n = nt';
+
n = nt';
%Sign = 0.01;
+
%Sign = 0.01;
%Dn=20; % Delay of the noise that appears in y.
+
%Dn=20; % Delay of the noise that appears in y.
%n = n(1:Ls) + Sign*randn(Ls,1);
+
%n = n(1:Ls) + Sign*randn(Ls,1);
%an = [0 .01 -.5 1 -.5 .1 .01 0];
+
%an = [0 .01 -.5 1 -.5 .1 .01 0];
an = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
+
an = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
bn = [1];
+
bn = [1];
%sys = tf(an,bn,1/Fs);
+
%sys = tf(an,bn,1/Fs);
%bode(sys);
+
%bode(sys);
%figure(1);
+
%figure(1);
nf = filter(an,bn,n);
+
nf = filter(an,bn,n);
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);
+
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);
y = s + nf;
+
y = s + nf;
Sigx = 0.01;
+
Sigx = 0.01;
bx = [1]; % bx and ax are filtering on n to produce x
+
bx = [1]; % bx and ax are filtering on n to produce x
ax = [1];
+
ax = [1];
Dx = 1; % Delay of n that appears in x
+
Dx = 1; % Delay of n that appears in x
x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
+
x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
%x = n + Sigx*randn(Ls,1);
+
%x = n + Sigx*randn(Ls,1);
speak('Here is the noisy signal.')
+
speak('Here is the noisy signal.')
soundsc(y,Fs);
+
soundsc(y,Fs);
N = 20; % Length of adaptive filter
+
N = 20; % Length of adaptive filter
% LMS algorithm for adaptive noise cancellation
+
% LMS algorithm for adaptive noise cancellation
h = zeros(N,1);
+
h = zeros(N,1);
mu = 1/(10*N*var(x));
+
mu = 1/(10*N*var(x));
%mu = 1.0;
+
%mu = 1.0;
for k=N:Ls xk = x(k:-1:(k-N+1)); nhat(k) = h'*xk; e(k) = - y(k) + nhat(k); h = h - mu*e(k)*xk;%/(xk'*xk);
+
for k=N:Ls xk = x(k:-1:(k-N+1)); nhat(k) = h'*xk; e(k) = - y(k) + nhat(k); h = h - mu*e(k)*xk;%/(xk'*xk);
end
+
end
% The signal estimate is in the vector e
+
% The signal estimate is in the vector e
speak('Here is the cleaned signal.');
+
speak('Here is the cleaned signal.');
soundsc(e,Fs);
+
soundsc(e,Fs);

Revision as of 13:10, 15 December 2013

% Demonstration of LMS algorithm for noise cancellation.
% Rich Kozick, Spring 1997
% Rob Frohne's  modifications for Macintosh 2000. 
% Desired signal 
clear all
Totaltime=1;
speak('Hit a key and speak the signal.'); 
pause;
[st, Fs] = recordsound(Totaltime, 22050, 1);
s = st';
Ls = length(s);  
% Interference + random noise
speak('Hit a key and make the noise!'); 
pause;
[nt,Fs] = recordsound(Totaltime, 22050, 1);
n = nt';
%Sign = 0.01;
%Dn=20;  % Delay of the noise that appears in y.
%n = n(1:Ls) + Sign*randn(Ls,1);
%an = [0 .01 -.5 1 -.5 .1 .01 0];
an = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
bn = [1];
%sys = tf(an,bn,1/Fs);
%bode(sys);
%figure(1);
nf = filter(an,bn,n);
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);  
y = s + nf; 
Sigx = 0.01; 
bx = [1];       % bx and ax are filtering on n to produce x
ax = [1];
Dx = 1;        % Delay of n that appears in x
x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
%x = n + Sigx*randn(Ls,1);  
speak('Here is the noisy signal.')
soundsc(y,Fs);  
N = 20;         % Length of adaptive filter 
% LMS algorithm for adaptive noise cancellation 
h = zeros(N,1);
mu = 1/(10*N*var(x));
%mu = 1.0;
for k=N:Ls     xk = x(k:-1:(k-N+1));     nhat(k) = h'*xk;     e(k) = - y(k) + nhat(k);     h = h - mu*e(k)*xk;%/(xk'*xk);
end 
% The signal estimate is in the vector e
speak('Here is the cleaned signal.');
soundsc(e,Fs);