%This takes a speaker output (y) and a cancelation speaker(yhat), and listens to it on a mic (Error)
%Tries to minimize error (IE mic should hear nothing) There is an air delay on the y signal.
%So it does properly match y(N-20) = yhat(N) where yhat(N) is a 20 tap delay from y(N)

%Air delays are involved

Error = zeros(1,9000);
mu=.02;

yhat=zeros(1,9000);
AFIR = zeros(1, 256);

%load handel
%y=y(1:9000);

y=.4*(cos(pi/10*[0:10000])+.9*sin(pi/8*[0:10000])+cos(pi/3*[0:10000]+3));

%Add some noise (in input signal)
%y=y+randn(size(y))/10;

for N=400:9000
    yhat(N)=0;
    for i=1:150
        yhat(N) = yhat(N) + AFIR(i)*y(N-i);
    end
    
    %Notice Error is just the mic input (with air delayed signals) in this case
    Mic(N)=y(N-20)+yhat(N);
    Error(N)=Mic(N);   
    
    for i=1:150
        %also notice the sign of mu
        AFIR(i)=AFIR(i)-mu*Error(N)*y(N-i);
    end
    
end
       
figure(1);
plot(yhat);
title('YHat')
figure(2);
plot(y);
title('Y')
figure(3);
plot(Error);
title('Error');