matlab code for adaptive filter using 1 tap

close all;clear all
PI = 4.0 * atan(1.0);

%pure signal---->sine wave
x=fopen('pure_signal.dat','w');
for n=0:1:(8000/200)*10
rad = 2.0*PI*n*(200/8000);
sinea(n + 1) = sin(rad);

end
fprintf(x,'%f\n',sinea);
fclose(x);


load pure_signal.dat;

% Generate the random noise
y = fopen('random_noise.dat','w');
yout = randn(1,length(pure_signal));
fprintf(y,'%f\n',yout);
fclose(y);
n = filter([ 000000.5 ],1,yout); % Generate the corruption noise
d = sinea + n;
z = fopen('corrupted_signal.dat','w');
fprintf(z,'%f\n',d);


%adaptive filter
fs = 8000; % Sampling rate
t = 0:1:length(pure_signal)-1; % Create the index array
t = t/fs; % Convert indices to time instants

mu = 0.01; % Initialize the step size
w = 0.0 ; % Initialize the adaptive filter coefficients
% Adaptive filtering using the LMS algorithm
for i = 1:1:length(t)
y(i) = w*yout(i);
e(i) = d(i)-y(i);
w = w + mu*e(i)*yout(i);
end




% Plot signals and spectra
subplot(4,1,1), plot(t,pure_signal);grid;ylabel('Orig. speech');
subplot(4,1,2),plot(t,d);grid;ylabel('Corrupt. speech')
subplot(4,1,3),plot(t,yout);grid;ylabel('Ref. noise');
subplot(4,1,4),plot(t,e);grid;ylabel('Clean speech');
xlabel('Number of samples');

abc = fopen('adaptive_filter.dat','w');
fprintf(abc,'%f\n',e);


fclose(z);
fclose(abc);

No comments:

Post a Comment