%implements the delay modelling filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%toggle plot function on/off
enable_p = 0;

display('Callibration started')
%load global variabels
global Wd,global white_hrtf;global Nd;global mud;global fs;global calibrated;

%construct first filtercoefficient vector
Wd = [zeros(2*Nd,1)];

%first values of output and error
output_c = [];error_c =[];

%read-out the input samples
K = floor((length(white_hrtf(1:96000,1))-2*Nd)/Nd);%number of read operations
for k = 0:1:K
    if (k == 0)
        x = [zeros(Nd,1);white_hrtf(1:Nd,2)];
        d = white_hrtf(1:Nd,1);
    else
        x = white_hrtf((k*Nd-Nd)+1:(k*Nd+Nd),2);
        d = white_hrtf((k*Nd+1):(k*Nd+Nd),1);
    end
    
    [out,err,Wd] = fdaf_lms(x,d,Wd,Nd,mud,1);
    output_c = [output_c,out'];
    error_c = [error_c,err'];
end

global calibrated;
calibrated = 1;
display('Callibration finished');

%plot error and output if required
if (enable_p == 1)
    figure;length(error_c)
    subplot(2,1,1);plot([0:1/fs:(length(error_c)-1)/fs],error_c,'r');
    title('Error');
    hold on;
    subplot(2,1,2);plot([0:1/fs:(length(error_c)-1)/fs],output_c);
    title('Output');
end