DSPRelated.com

Speech Noise cancellation - scilab code

Senthilkumar December 28, 20116 comments Coded in Scilab
//Caption: Speech Noise Cancellation using LMS Adaptive Filter

clc;
//Reading a speech signal
[x,Fs,bits]=wavread("E:\4.wav");
order = 40;  // Adaptive filter order
x = x';
N = length(x);
t = 1:N;
//Plot the speech signal
figure(1)
subplot(2,1,1)
plot(t,x)
title('Noise free Speech Signal')
//Generation of noise signal
noise = 0.1*rand(1,length(x));
//Adding noise with speech signal
for i = 1:length(noise)
    primary(i)= x(i)+noise(i);
end
//Plot the noisy speech signal
subplot(2,1,2)
plot(t,primary)
title('primary = speech+noise (input 1)')
//Reference noise generation
for i = 1:length(noise)
    ref(i)= noise(i)+0.025*rand(10);
end
//Plot the reference noise
figure(2)
subplot(2,1,1)
plot(t,ref)
title('reference noise (input 2)')
//Adaptive filter coefficients initialized to zeros
w = zeros(order,1);
Average_Power = pow_1(x,N)
mu = 1/(10*order*Average_Power); //Adaptive filter step size
//Speech noise cancellation
for k = 1:110
    for i =1:N-order-1
        buffer = ref(i:i+order-1); //current order points of reference
        desired(i) = primary(i)-buffer'*w; // dot product the reference & filter
        w = w+(buffer.*mu*desired(i)); //update filter coefficients
    end
end
//Plot the Adaptive Filter output
subplot(2,1,2)
plot([1:length(desired)],desired)
title('Denoised Speech Signal at Adaptive Filter Output')
//Calculation of Mean Squarred Error between the original speech signal and
//Adaptive filter output
for i =1:N-order-1
    err(i) = x(i)-desired(i);
    square_error(i)= err(i)*err(i);
end
MSE = (sum(square_error))/(N-order-1);
MSE_dB = 20*log10(MSE);
//Playing the original speech signal
sound(x,Fs,16)
//Delay between playing sound signals
for i = 1:1000
    j = 1;
end
/////////////////////////////////
//Playing Noisy Speech Signal
sound(primary,Fs,16)
//Delay between playing sound signals
for i = 1:1000
    j = 1;
end
/////////////////////////////////
//Playing denoised speech signal (Adaptive Filter Output)
sound(desired,Fs,16)

speech signal-sampling rate conversion

Senthilkumar December 28, 20111 comment Coded in Scilab
//Caption: write a program for decimation and interpolation of given
//Speech Signal [ Multirate Signal Processing]

clear;
clc;
[x,Fs,bits]=wavread("E:\4.wav");
n = length(x)
DECIMATION_OF_X = x(1:2:length(x));
INTERPOLATION_OF_X = zeros(1,2*length(x));
INTERPOLATION_OF_X(1:2:2*length(x)) = x;
subplot(3,1,1)
plot([1:n],x)
xtitle("ORIGINAL Speech SIGNAL");
subplot(3,1,2)
plot([1:ceil((n/2))],DECIMATION_OF_X)
xtitle("DECIMATION BY A FACTOR OF 2")
subplot(3,1,3)
plot([1:2*n],INTERPOLATION_OF_X)
xtitle("INTERPOLATION BY A FACTOR OF 2")

scilab program to speech signal information

Senthilkumar December 28, 20111 comment Coded in Scilab
//Caption: Reading a Speech Signal & 
//[1]. Displaying its sampling rate
//[2]. Number of bits used per speech sample
//[3]. Total Time duration of the speech signal in seconds
clear;
clc;
[y,Fs,bits]=wavread("E:\4.wav");
a = gca();
plot2d(y);
a.x_location = 'origin';
title('Speech signal with Sampling Rate = 8 KHz, No. of Samples = 8360')
disp(Fs,'Sampling Rate in Hz Fs = ');
disp(bits,'Number of bits used per speech sample b =');
N = length(y);
T = N/Fs;
disp(N,'Total Number of Samples N =')
disp(T,'Duration of speech signal in seconds T=')
//Result
//Sampling Rate in Hz Fs =    
//     8000.  
//Number of bits used per speech sample b =   
//    16.  
//Total Number of Samples N =   
//    8360.  
//Duration of speech signal in seconds T=   
//    1.045

sound play command in scilab

Senthilkumar December 28, 2011 Coded in Scilab
//Caption: Reading a Speech Signal & 
//Play the signal
clear;
clc;
[y,Fs,bits_y]=wavread("E:\4.wav");
sound(y,Fs,16)

Plot the Low pass and High pass filtered speech signal

Senthilkumar December 28, 2011 Coded in Scilab
//Caption: Program to Plot the frequency reponse of 
//[1]. Low Pass Filter 
//[2]. High Pass Filter

clear;
clc;
[x,Fs,bits]=wavread("E:\4.wav");
n = length(x)
//Low Pass Filter of Length = 19, Wc = 0.5, Hamming Window
[wft_LPF,wfm_LPF,fr_LPF]=wfir('lp',18,[0.25,0],'hm',[0,0])
//High Pass Filter of Length = 19, Wc = 0.5, Hamming Window
[wft_HPF,wfm_HPF,fr_HPF]=wfir('hp',18,[0.25,0],'hm',[0,0])

subplot(3,1,1)
plot([1:n],x)
xtitle("ORIGINAL Speech SIGNAL");
subplot(3,1,2)
a = gca();
plot(2*fr_LPF,wfm_LPF,'g')
poly1= a.children.children(1);
poly1.thickness =3;
xtitle("Frequency Response of Low Pass Filter N = 19, Wc =0.5, Hamming Window");
xgrid(1)
subplot(3,1,3)
b = gca();
plot(2*fr_HPF,wfm_HPF,'r')
poly1= b.children.children(1);
poly1.thickness =3;
xtitle("Frequency Response of High Pass Filter N= 19, Wc =0.5, Hamming Window");
xgrid(1)

Function used to calculate average power

Senthilkumar December 28, 2011 Coded in Scilab
function[y]= pow_1(x,N)
    xold = 0.0;
    for n =1:N
        sumx = xold+(x(n)^2);
        xold = sumx;
    end
    y = sumx/N;
endfunction

Function to perform Adaptive filtering

Senthilkumar December 28, 2011 Coded in Scilab
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

Full band Echo canceller

Senthilkumar December 28, 2011 Coded in Scilab
//Caption : FULL Band Echo Cnacellation using NLMS Adaptive Filter

clc;
//Reading a speech signal
[x,Fs,bits]=wavread("E:\4.wav");
order = 40;  // Adaptive filter order
x = x';
N = length(x);  //length of speech signal
//Delay introduced in echo path
delay = 100;   
//Echo at same speaker
xdelayed = zeros(1,N+delay);
for i = delay+1:N+delay
    xdelayed(i) = x(i-delay);
end
//Initialize the adaptive filter coefficients to zero
hcap = zeros(1,order);
//To avoid negative values generated during convolution
for i = 1:order-1
    xlms(i) = 0.0;
end
for i = order:N+order-1
    xlms(i) = x(i-order+1);
end
Power_X = pow_1(x,N);  //Average power of speech signal
//Calculation of step size of adaptive filter
delta = 1/(10*order*Power_X);
[x_out,Adapt_Filter_IR] = adapt_filt(xlms,xdelayed,hcap,delta,N,order)
figure(1)
subplot(3,1,1)
plot([1:N],x)
title('Speech Signal Generated by some Speaker A')
sound(x,Fs,16)
subplot(3,1,2)
plot([1:length(xdelayed)],xdelayed)
title('Echo signal of speaker A received by speaker A')
sound(xdelayed,Fs,16)
subplot(3,1,3)
plot([1:length(x_out)],x_out)
title('Echo signal at speaker A after using NLMS Adaptive filter Echo Canceller')
figure(2)
plot2d3('gnn',[1:length(Adapt_Filter_IR)],Adapt_Filter_IR)
title('Adaptive Filter (Echo Canceller) Impulse response')

Chaning the bit depth in speech samples

Senthilkumar December 28, 2011 Coded in Scilab
//Caption: Reading a Speech Signal & 
//[1]. Write it in another file
//[2]. Changing the bit depth from 16 bits to 8 bits
clear;
clc;
[y,Fs,bits_y]=wavread("E:\4.wav");
wavwrite(y,Fs,8,"E:\4_8bit.wav");
[x,Fs,bits_x]=wavread("E:\4_8bit.wav");
Ny = length(y); //Number of samples in y (4.wav)
Nx = length(x); //Number of samples in x (4_8bit.wav)
Memory_y = Ny*bits_y;  //memory requirement for 4.wav in bits
Memory_x = Nx*bits_x;  //memory requirement for 4_8bit.wav in bits
disp(Memory_y,'memory requirement for 4.wav in bits =')
disp(Memory_x,'memory requirement for 4_8bit.wav in bits =')
//Result
//memory requirement for 4.wav in bits =   
//     133760.  
// 
//memory requirement for 4_8bit.wav in bits =   
//     66880.

Differential Phase Shift Keying -

Senthilkumar March 24, 2011 Coded in Scilab
//Generation of Differential Phase shift keying signal
clc;
bk = [1,0,1,1,0,1,1,1];//input digital sequence
for i = 1:length(bk)
  if(bk(i)==1)
    bk_not(i) =~1;
  else
    bk_not(i)= 1;
  end
end
dk_1(1) = 1&bk(1);  //initial value of differential encoded sequence
dk_1_not(1)=0&bk_not(1);
dk(1) = xor(dk_1(1),dk_1_not(1))//first bit of dpsk encoder
for i=2:length(bk)
  dk_1(i) = dk(i-1);
  dk_1_not(i) = ~dk(i-1);
  dk(i) = xor((dk_1(i)&bk(i)),(dk_1_not(i)&bk_not(i)));
end
for i =1:length(dk)
  if(dk(i)==1)
    dk_radians(i)=0;
  elseif(dk(i)==0)
    dk_radians(i)=%pi;
  end
end
disp(bk,'(bk)')
bk_not = bk_not';
disp(bk_not,'(bk_not)')
dk = dk';
disp(dk,'Differentially encoded sequence (dk)')
dk_radians = dk_radians';
disp(dk_radians,'Transmitted phase in radians')