Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Lms Code

This document performs noise reduction on a speech signal using an adaptive filter. It reads in a noisy speech signal, performs adaptive filtering to reduce the noise, and outputs the filtered signal. It analyzes the performance by calculating metrics like mean squared error, mean absolute error, and signal-to-noise ratios of the input and output signals. Memory usage statistics are also reported.

Uploaded by

saisree
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lms Code

This document performs noise reduction on a speech signal using an adaptive filter. It reads in a noisy speech signal, performs adaptive filtering to reduce the noise, and outputs the filtered signal. It analyzes the performance by calculating metrics like mean squared error, mean absolute error, and signal-to-noise ratios of the input and output signals. Memory usage statistics are also reported.

Uploaded by

saisree
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

clc;

close all;
clear all;
%N=input('length of sequence N = ');
%t=[0:N-1];
%w0=0.001; phi=0.1;
%d=sin(2*pi*[1:N]*w0+phi);
%x=d+randn(1,N)*0.5;
[d fs]=audioread('RecdSpeech.m4a');
N = length(d);
%sound(d,47000);
x = awgn(d,20);%%Speech+Noise = x
%plot(X);
%sound(x,47000);
w=zeros(1,N);
mu=input('mu = ');
tic
for i=1:N
e(i) = d(i) - w(i)' * x(i);
w(i+1) = w(i) + mu * e(i) * x(i);
end
for i=1:N
yd(i) = (sum(w(i)' * x(i)))/10;
end
subplot(221),plot(d),ylabel('Desired Signal'),
subplot(222),plot(x),ylabel('Input Signal+Noise'),
subplot(223),plot(e),ylabel('Error'),
subplot(224),plot(yd),ylabel('Adaptive Desired output');
%sound(x,fs);
%pause(5);
sound(yd,fs);
toc

%%
% *Cross Correlation of Signals*
d = d(:,1);
x = x(:,1);
yd = transpose(yd);

[Rx1, lags1] = xcorr(d,x, 'coeff');


[Rx2, lags2] = xcorr(d,yd, 'coeff');
delay1 = lags1/fs;
delay2 = lags2/fs;
Rx3 = crosscorr(d,x);
Rx4 = crosscorr(d,yd);
delay3 = -20:1:20;

figure(2)

subplot(221),plot(delay1, Rx1, 'r');


grid on
xlim([-max(delay1) max(delay1)]);
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10);
xlabel('Delay'),ylabel('Cross Correlation coefficient'),title('X Corr of the Input
and Noisy Signal');

subplot(223),plot(delay2, Rx2, 'r');


grid on
xlim([-max(delay2) max(delay2)]);
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10);
xlabel('Delay'),ylabel('Cross Correlation coefficient'),title('X corr of Input and
Denoised signal');

subplot(222),plot(delay3,Rx3,'r');
grid on
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10);
xlabel('Delay'),ylabel('Cross Correlation coefficient'),title('X Corr of the Input
and Noisy Signal');

subplot(224),plot(delay3,Rx4,'r');
grid on
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10);
xlabel('Delay'),ylabel('Cross Correlation coefficient'),title('X Corr of the Input
and Denoised Signal');

%%
% *%% CCF Coeff*

cc = corrcoef(d,x);
cross_core = cc(1,2);
fprintf('CC Coeff of Input = %f\n',cross_core);

cc1 = corrcoef(d,yd);
cross_core1 = cc1(1,2);
fprintf('CC Coeff of Output = %f\n',cross_core1);

%%
% *%%Time taken = 2.007945/2.001476 sec*

%% Calculatingg MSE
ESqrAvg = 0;
for i=1:N
ESqrAvg = ESqrAvg + (e(i)*e(i));
end
% MSE = e(N-1)*e(N-1);
MSE = ESqrAvg/N;
fprintf('MSE = %f\n',MSE);
%Last MSE value got = 0.0031

%%
% *%%Mean Absolute Error*

MAE = 0;
for i = 1:N
MAE = MAE + abs(d(i)-yd(i));
end
MAE = MAE/N;
fprintf('MAE = %f\n',MAE);
%%
% *%%Plotting filter coefficient plot*
figure(3)
plot(w);

%% Calculating SNR of Output


% x = transpose(x);
% d = transpose(d);
% SNRydx = snr(yd,x);
% SNRdx = snr(d,x);
% SNRdyd = snr(d,yd);
% SNRydd = snr(yd,d);
% SNRxd = snr(x,d);
% SNRxyd = snr(x,yd);

% SNR1 = snr(x,fs);
% SNR2 = snr(d,fs);
% SNR3 = snr(yd,fs);

% [r1,noisepow1] = snr(x); %r value same as SNR123 values above


% [r2,noisepow2] = snr(d);
% [r3,noisepow3] = snr(yd);

% mxm1 = max(abs(x));
% mxm2 = max(abs(d));
% mxm3 = max(abs(yd));
% SNR1 = (10*log10((mxm1*mxm1)/MSE));
% SNR2 = (10*log10((mxm2*mxm2)/MSE));
% SNR3 = (10*log10((mxm3*mxm3)/MSE));

Num1 = 0; Den1 = 0;
for i = 1:N
Num1 = Num1 + d(i)^2;
end
for i = 1:N
Den1 = Den1 + (x(i)-d(i))^2;
end
SNRIp = 20*log10(sqrt(Num1)/sqrt(Den1));

Num2 = 0; Den2 = 0;
for i = 1:N
Num2 = Num2 + d(i)^2;
end
for i = 1:N
Den2 = Den2 + (yd(i)-d(i))^2;
end
SNROp = 20*log10(sqrt(Num2)/sqrt(Den2));

%%
% *%%Memory Required*

[user,sys] = memory;
a = user.MaxPossibleArrayBytes;
b = user.MemUsedMATLAB;
c = sys.SystemMemory.Available;
d = sys.PhysicalMemory.Available;
e = sys.PhysicalMemory.Total;
f = e-d;
fprintf('Maximum Possible Array Bytes = %.2f MB\n',a/1000000);
fprintf('Memory Used in MATLAB = %.2f MB\n',b/1000000);
fprintf('Available System Memory = %.2f MB\n',c/1000000);
fprintf('Total Physical Memory = %.2f MB\n',e/1000000);
fprintf('Available Physical Memory = %.2f MB\n',d/1000000);
fprintf('Physical Memory used = %.2f MB\n',f/1000000);

You might also like