Tuner Upper Removal Demonstration

From Class Wiki
Revision as of 13:10, 15 December 2013 by Brian.Clark (talk | contribs)
Jump to navigation Jump to search
% 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);