DSPRelated.com
Code

Function to perform Adaptive filtering

Senthilkumar December 28, 2011 Coded in Scilab

This function performs echo cancellation using NLMS adaptive filter

function[y,M] = adapt_filt(xlms,B,h,delta,l,l1)
    //x = the signal from the speaker directly
    //B = the signal thorugh the echo path
    //h = impulse response of adaptive filter
    //l = length of signal 'x'
    //l1 = length of adaptive filter order
    for k = 1:150
        for n = 1:l
            xcap = xlms((n+l1-1):-1:(n+l1-1)-l1+1);
            yout(n) = h*xcap;
            e(n) = B(n)- yout(n);
            xnorm = 0.001+(xcap*xcap');
            h = h+((delta*e(n))*(xcap'));
        end
        eold = 0.0;
        for i = 1:l
            MSE = eold+(e(i)^2);
            eold = MSE;
        end
        if MSE <= 0.0001 then
            break;
        end
    end
    y =  zeros(1,length(e));
    M = zeros(1,length(h));
    y  = e;
    M = h;
endfunction