%implements a frequency domain adaptive LMS filter, using the overlap save
%method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%restrictions: x & d = column vector
function [y,e,W] = fdaf_lms(x,d,W,N,mu,adapt)
global tic_it;

if (tic_it == 1 && adapt == 1)
    tic
end
%set matrix needed in the computation
g = [[eye(N) zeros(N)];[zeros(N) zeros(N)]];

%step that updates the output of the filter
X = diag(fft(x));
Y = X*W;%W has to be a column vector
y = ifft(Y)';y = y(1,N+1:2*N)';

%step that computes the error of the signal
e = d-y;

if (adapt == 1)
    %step that computes the future filtercoefficients
    E = fft([zeros(N,1);e]);
    grad = g*ifft(X'*E);
    W = W + 2*mu*eye(2*N)*fft(grad);
end

if (tic_it == 1 && adapt == 1)
    toc
    tic_it = 0;
end
