function [input_signal, error_signal, desired_signal, filter_output, impulse, filter_current,
mse, db, db_avg]=LMS(filter_size, step_size, input_file, iterations)
% Function to perform the LMS algorithm on an input file.
% Inputs: Filter order, step size, input wav file, number of iterations.
% Outputs: Input signal, error estimation signal (echo cancelled), desired signal (echoed
signal), adaptive filter output, real impulse response
% Estimation of impulse response, mean sqaure error, attenuation (dB), average
attenuation.
%Read in the input file
input_signal = wavread(input_file);
% Create the impulse response for the desired signal
impulse=zeros(filter_size,1);
for (i=1:5)
impulse(((i-1)*filter_size/5)+1)=1/i;
end
% Convolve the impulse with the input signal to generate the desired signal
desired_signal = conv(input_signal, impulse);
% initialise adaptive filter impulse and input vector to zero vector of length specified at
command line
filter_current = zeros(filter_size,1);
input_vector = zeros(filter_size, 1);
% Loop for number of iterations specified in command line.
for i=1:iterations
i
input_vector(1)=input_signal(i); % insert new sample at beginning of input
vector.
filter_output(i)=dot(filter_current, input_vector); %Caluculate adaptive filter
output
error= desired_signal(i)-filter_output(i) % Calculate estimation error
filter_current = filter_current + 2*step_size*error*input_vector; % Update filter
taps by LMS recursion
% Shift values in input vector along.
for j=filter_size:-1:2
input_vector(j)=input_vector(j-1);
end
error_signal(i)=error; % store estimation error
cost(i)=error*error; % calculate instantaneous cost square error
end
% Find moving average of error squared.
for i=1:iterations-100
mse(i)=mean(cost(i:i+100));
i
end
%find moving average of db attenuation (averaged to smooth output).
for i=1:iterations-2500
db(i)=-
20*log10(mean(abs(desired_signal(i:i+2500)))��./mean(abs(error_signal(i:i+2500))));
i
end
%find total average db attenuation
db_avg=mean(db);
JonSco
- 粉丝: 89
- 资源: 1万+
- 1
- 2
- 3
前往页