%This takes a speaker output (y) and a cancelation speaker(yhat), and listens to it on a mic (Error)
%Tries to minimize error (Mic should here y(N-20)+yhat(N) where yHat is y delayed 20 taps)

%Instead the filter becomes unstable

%Air delays are involved

Error = zeros(1,5000);
mu=.000005;   % <----- System is unstable for even absurdly small Mu's

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)=y(N-20)-yhat(N);
    if N>430
        for i=1:150
            AFIR(i)=AFIR(i)-mu*Error(N)*y(N-i);
        end
    end
    
end
       
figure(1);
plot(yhat);
title('YHat')
figure(2);
plot(y);
title('Y')
figure(3);
plot(Error);
title('Error');