#### jenabi

##### Newbie level 4

i need help below is the code that is for dynamically changing the tap length of adaptive filter....but when i run this in matlab i get error saying that d(n:-1:n-L+1) cannot have negative indices...can someone tell me how to debug this...and where am i going wrong

clc;

clear;

delta = 2;

n_bits=15;

t=5000;

x = 0.5*((rand(1,t)>0.5)-.5);

r = 1;

imp = [-4.4 0 -7 -12 -9 -14 -20 -16.5 -20 -22.5 -20];

response = (10.^(imp/10));

quant = round(response*(2^n_bits));

quant = quant/2^n_bits;

channel = [0.4 1 0.2 0 0.06 0.1 0.04 0.01 0.02 0.01 0.005 0.01];

channel = channel.*2;

b = (randn(1,5)).*exp(-(0:4));

c = [1 0 -.3 0 .5 0 -.1 0 0.1];

% Generate noise

snr = 20;

n=1/sqrt(2).*(randn(1,length(x)));

noise = 10^(-snr/20)*n;

noise = 0.25.*noise;

var = 1;

noise_low_SN=0.55*(randn(1,t*r)*sqrt(var));

noise_low_q=round(noise_low_SN*2^n_bits)/2^n_bits;

noise_high_SN=0.01*(randn(1,t*r)*sqrt(var));

noise_high_q=round(noise_high_SN*2^n_bits)/2^n_bits;

noise = [noise_high_q];

% input signal

input = (filter(c,1,x));

%input = (x);

pad = zeros(1,delta);

pad1 = ones(1,4);

input_LMS = round(input*2^n_bits)/2^n_bits;

input_q = round(input*2^n_bits)/2^n_bits; %Quantisation for file output

input_des=round(x*2^n_bits)/2^n_bits;

%adaptive filtering

L = 9; % filter length

P = 3; % sub-filter size

mu = 0.005; % step size ( mu < 1/(L*std(x)^2) )

W = zeros(1,L); % adaptive coefficients are initialised to zero

ASE = 50;

e_array = zeros(1,ASE);

e_sub_array = zeros(1,50);

pad = zeros(1,L);

x_pad = zeros(1,L-3);

x_delay = [pad x];

d = [x_pad, input_LMS];

y = zeros(1,length(x));

e = zeros(1,length(x));

y_sub = zeros(1,length(x));

e_sub = zeros(1,length(x));

coeff = zeros(1,length(x));

for n = L:length(x)

n

L

X = d(n:-1: n-L+1);

y = round((W*X')*2^n_bits)/2^n_bits; % adaptive filter out

X_sub = d(n:-1:n-L+(P+1));

W_sub = W(1:1L-P));

y_sub = round((W_sub*X_sub')*2^n_bits)/2^n_bits; % adaptive sub-filter out

% error signals

e = (round((x_delay - y)*2^n_bits))/2^n_bits;

e_sub = (round((x_delay - y_sub)*2^n_bits))/2^n_bits;

e_array(2:ASE) = e_array(1:ASE-1); % calculation of ASE

e_array(1) = (e)^2;

e_sub_array(2:ASE) = e_sub_array(1:ASE-1);

e_sub_array(1) = (e_sub)^2;

ASE_res = sum(e_array);

ASE_res_sub = sum(e_sub_array);

if (ASE_res <= ASE_res_sub) % evaluate Filter length

L = L + P; % to add or remove P taps

elseif (ASE_res >= ASE_res_sub)

L = L - P;

end

coeff = (round(2*mu*e*2^n_bits)/2^n_bits);

W = W + coeff*X; % LMS update

W=(round(W*2^n_bits)/2^n_bits);

e_sq = e^2;

end;

thanks