Tuner Upper Removal Demonstration: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
% Demonstration of LMS algorithm for noise cancellation.
% Demonstration of LMS algorithm for noise cancellation. Tuner upper problem.
% Rich Kozick, Spring 1997
% Rich Kozick, Spring 1997
% Rob Frohne's modifications for Macintosh 2000.
% Rob Frohne's modifications for Macintosh 2000
% and Linux 2006 or so.
% Desired signal

clear all

Totaltime=1;
clear all
speak('Hit a key and speak the signal.');
Fs=8000; % 8 Khz sampling for Linux.
pause;
T0 = 2; % 2 seconds
[st, Fs] = recordsound(Totaltime, 22050, 1);

s = st';
system("espeak 'After hitting enter in the command window, speak the signal.'");
Ls = length(s);
st = record(T0,Fs);
% Interference + random noise
%st=wavread('Hello.wav');
speak('Hit a key and make the noise!');
st=st(:,1);
pause;

[nt,Fs] = recordsound(Totaltime, 22050, 1);
system("espeak 'Here is the signal.'")
n = nt';
soundsc(st);
%Sign = 0.01;

%Dn=20; % Delay of the noise that appears in y.
Ls = length(st);
%n = n(1:Ls) + Sign*randn(Ls,1);
T0=length(st)/Fs;
%an = [0 .01 -.5 1 -.5 .1 .01 0];
% Make the tuner upper noise or even several at the same time.
an = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
t=0:1/Fs:T0-1/Fs;
bn = [1];
n = 10*(sin(2*pi*100*pi*t) + cos(2*pi*600*t) + 1.5*sin(2*pi*850*t));
%sys = tf(an,bn,1/Fs);

%bode(sys);
% Add him or them to the desired signal.
%figure(1);
nf = filter(an,bn,n);
x = st' + n;

%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);
system("espeak 'Here is the noisy signal.'")
y = s + nf;
soundsc(x,Fs);
Sigx = 0.01;

bx = [1]; % bx and ax are filtering on n to produce x
N = 64; % Length of adaptive filter
ax = [1];

Dx = 1; % Delay of n that appears in x
% LMS algorithm for adaptive noise cancellation
x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);

%x = n + Sigx*randn(Ls,1);
h = ones(N,1);
speak('Here is the noisy signal.')
mu = 0.1;
soundsc(y,Fs);
for k=N:Ls
N = 20; % Length of adaptive filter
xk = x(k:-1:(k-N+1));
% LMS algorithm for adaptive noise cancellation
y(k) = h'*xk';
h = zeros(N,1);
mu = 1/(10*N*var(x));
e(k) = y(k);
h = h - 2*mu*e(k)*xk'/(xk*xk');
%mu = 1.0;
end
for k=N:Ls

xk = x(k:-1:(k-N+1));
% The signal estimate is in the vector e
nhat(k) = h'*xk;
system("espeak 'Here is a scaled version of the tail of the cleaned signal.'");
e(k) = - y(k) + nhat(k);
skip =1000;
h = h - mu*e(k)*xk;%/(xk'*xk);
soundsc(e(skip:length(e)),Fs);
end
% The signal estimate is in the vector e
speak('Here is the cleaned signal.');
soundsc(e,Fs);

Revision as of 14:43, 6 December 2015

% Demonstration of LMS algorithm for noise cancellation.  Tuner upper problem.

% Rich Kozick, Spring 1997 % Rob Frohne's modifications for Macintosh 2000 % and Linux 2006 or so.


clear all Fs=8000; % 8 Khz sampling for Linux. T0 = 2;  % 2 seconds

system("espeak 'After hitting enter in the command window, speak the signal.'"); st = record(T0,Fs); %st=wavread('Hello.wav'); st=st(:,1);

system("espeak 'Here is the signal.'") soundsc(st);

Ls = length(st); T0=length(st)/Fs; % Make the tuner upper noise or even several at the same time. t=0:1/Fs:T0-1/Fs; n = 10*(sin(2*pi*100*pi*t) + cos(2*pi*600*t) + 1.5*sin(2*pi*850*t));

% Add him or them to the desired signal. x = st' + n;

system("espeak 'Here is the noisy signal.'") soundsc(x,Fs);

N = 64;  % Length of adaptive filter

% LMS algorithm for adaptive noise cancellation

h = ones(N,1); mu = 0.1; for k=N:Ls

 xk = x(k:-1:(k-N+1));
 y(k) = h'*xk';
 e(k) = y(k);
 h = h - 2*mu*e(k)*xk'/(xk*xk');

end

% The signal estimate is in the vector e system("espeak 'Here is a scaled version of the tail of the cleaned signal.'"); skip =1000; soundsc(e(skip:length(e)),Fs);