├── README.md ├── convm.m ├── lms.m └── proj.m /README.md: -------------------------------------------------------------------------------- 1 | # Sound-source-localization 2 | Developing an algorithm using MATLAB to detect the unknown location(coordinates) of a sound source in a closed room using a series of microphones. 3 | -------------------------------------------------------------------------------- /convm.m: -------------------------------------------------------------------------------- 1 | function X = convm(x,p) 2 | 3 | %CONVM Generates a convolution matrix 4 | 5 | %----- 6 | 7 | %USAGE X = convm(x,p) 8 | 9 | % 10 | 11 | % Given a vector x of lenght N, an N+p-1 by p convolution 12 | 13 | % matrix of the following form is generated 14 | 15 | % 16 | 17 | % | x(0) 0 0 ... 0 | 18 | 19 | % | x(1) x(0) 0 ... 0 | 20 | 21 | % | x(2) x(1) x(0) ... 0 | 22 | 23 | % X = | . . . . | 24 | 25 | % | . . . . | 26 | 27 | % | . . . . | 28 | 29 | % | x(N) x(N-1) x(N-2) ... x(N-p+1) | 30 | 31 | % | 0 x(N) x(N-1) ... x(N-p+2) | 32 | 33 | % | . . . . | 34 | 35 | % | . . . . | 36 | 37 | % | 0 0 0 ... x(N) | 38 | 39 | % 40 | 41 | % 42 | 43 | %--------------------------------------------------------------- 44 | 45 | % copyright 1996, by M.H. Hayes. For use with the book 46 | 47 | % "Statistical Digital Signal Processing and Modeling" 48 | 49 | % (John Wiley & Sons, 1996). 50 | 51 | %--------------------------------------------------------------- 52 | 53 | 54 | 55 | N = length(x)+2*p-2; 56 | 57 | x = x(:); 58 | 59 | xpad = [zeros(p-1,1);x;zeros(p-1,1)]; 60 | 61 | for i=1:p 62 | 63 | X(:,i)=xpad(p-i+1:N-i+1); 64 | 65 | end; 66 | 67 | -------------------------------------------------------------------------------- /lms.m: -------------------------------------------------------------------------------- 1 | function [A,E] = lms(x,d,mu,nord,a0) 2 | 3 | %LMS Adaptive filtering using the Widrow-Hoff LMS algorithm. 4 | 5 | %--- 6 | 7 | %USAGE [A,E] = lms(x,d,mu,nord,a0) 8 | 9 | % 10 | 11 | % x : input data to the adaptive filter. 12 | 13 | % d : desired output 14 | 15 | % mu : adaptive filtering update (step-size) parameter 16 | 17 | % nord : number of filter coefficients 18 | 19 | % a0 : (optional) initial guess for FIR filter 20 | 21 | % coefficients - a row vector. If a0 is omitted 22 | 23 | % then a0=0 is assumed. 24 | 25 | % 26 | 27 | % The output matrix A contains filter coefficients. 28 | 29 | % - The n'th row contains the filter coefficients at time n 30 | 31 | % - The m'th column contains the m'th filter coeff vs. time. 32 | 33 | % - The output vector E contains the error sequence versus time. 34 | 35 | % 36 | 37 | % see also NLMS and RLS 38 | 39 | % 40 | 41 | %--------------------------------------------------------------- 42 | 43 | % copyright 1996, by M.H. Hayes. For use with the book 44 | 45 | % "Statistical Digital Signal Processing and Modeling" 46 | 47 | % (John Wiley & Sons, 1996). 48 | 49 | %--------------------------------------------------------------- 50 | 51 | 52 | 53 | X=convm(x,nord); 54 | 55 | [M,N] = size(X); 56 | 57 | if nargin < 5, a0 = zeros(1,N); end 58 | 59 | a0 = a0(:).'; 60 | 61 | E(1) = d(1) - a0*X(1,:).'; 62 | 63 | A(1,:) = a0 + mu*E(1)*conj(X(1,:)); 64 | 65 | if M>1 66 | 67 | for k=2:M-nord+1; 68 | 69 | y(k) = A(k-1,:)*X(k,:).'; 70 | 71 | E(k) = d(k) - y(k); 72 | 73 | A(k,:) = A(k-1,:) + mu*E(k)*conj(X(k,:)); 74 | 75 | end; 76 | A = A(k,:); 77 | end; 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /proj.m: -------------------------------------------------------------------------------- 1 | clc; % clear command window 2 | clear all; % clear workspace 3 | close all; % close figure window 4 | X_source = input('Enter the x-location of source'); % x-axis of source location, I took '8' 5 | Y_source = input('Enter the y-location of source'); % y-axis of source location, I took '7' 6 | r = input('Enter number of mics'); % Number of total microphones, I took '6' 7 | d = input('Enter distance between mics'); % seperation between any two microphones, I took '1' 8 | x_axis = 0:d:r-1; % setting up x-axis location of source 9 | y_axis = [0 0 0 0 0 0]; % setting up y-axis location of source 10 | soundsiga = randn(1,5000); % input sound signal generation 11 | c = 343; % speed of sound in air 12 | sam_freq = input('Enter the sampling frequency'); % sampling frequency, I took '2000' 13 | ref_mic_dis = sqrt((X_source-x_axis(1))^2+(Y_source-y_axis(1))^2)/c; % distance from source to reference microphone 14 | mic1 = sqrt((X_source-x_axis(2))^2+(Y_source-y_axis(2))^2)/c; % distance from source to microphone 1 15 | mic2 = sqrt((X_source-x_axis(3))^2+(Y_source-y_axis(3))^2)/c; % distance from source to microphone 2 16 | mic3 = sqrt((X_source-x_axis(4))^2+(Y_source-y_axis(4))^2)/c; % distance from source to microphone 3 17 | mic4 = sqrt((X_source-x_axis(5))^2+(Y_source-y_axis(5))^2)/c; % distance from source to microphone 4 18 | mic5 = sqrt((X_source-x_axis(6))^2+(Y_source-y_axis(6))^2)/c; % distance from source to microphone 5 19 | 20 | del_ref_mic = [mic1-ref_mic_dis mic2-ref_mic_dis mic3-ref_mic_dis mic4-ref_mic_dis mic5-ref_mic_dis]*sam_freq % time difference between reference microphone other microphones 21 | 22 | ord = input('Enter order of the filter'); % order of the filter, I took '151' 23 | t = -200:200; % to generate sinc signal 24 | sinc1 = sinc(t+200-del_ref_mic(1)-ord/2); % delayed signal for microphone 1 25 | sinc2 = sinc(t+200-del_ref_mic(2)-ord/2); % delayed signal for microphone 2 26 | sinc3 = sinc(t+200-del_ref_mic(3)-ord/2); % delayed signal for microphone 3 27 | sinc4 = sinc(t+200-del_ref_mic(4)-ord/2); % delayed signal for microphone 4 28 | sinc5 = sinc(t+200-del_ref_mic(5)-ord/2); % delayed signal for microphone 5 29 | 30 | fds1 = filter(sinc1,1,soundsiga); % delaying sound signal for 1st microphone 31 | fds2 = filter(sinc2,1,soundsiga); % delaying sound signal for 2nd microphone 32 | fds3 = filter(sinc3,1,soundsiga); % delaying sound signal for 3rd microphone 33 | fds4 = filter(sinc4,1,soundsiga); % delaying sound signal for 4th microphone 34 | fds5 = filter(sinc5,1,soundsiga); % delaying sound signal for 5th microphone 35 | 36 | 37 | %% 38 | 39 | [A1, E1] = lms(soundsiga,fds1,.01,ord); % passing signal of microphone 1 through adaptive filter 40 | [A2, E2] = lms(soundsiga,fds2,.01,ord); % passing signal of microphone 2 through adaptive filter 41 | [A3, E3] = lms(soundsiga,fds3,.01,ord); % passing signal of microphone 3 through adaptive filter 42 | [A4, E4] = lms(soundsiga,fds4,.01,ord); % passing signal of microphone 4 through adaptive filter 43 | [A5, E5] = lms(soundsiga,fds5,.01,ord); % passing signal of microphone 5 through adaptive filter 44 | figure; % opening a new figure window 45 | hold on; % holding the plot of required figure in figure window 46 | plot(A1,'r'); % to plot output of adaptive filter for microphone 1 47 | plot(A2,'c'); % to plot output of adaptive filter for microphone 2 48 | plot(A3,'y'); % to plot output of adaptive filter for microphone 3 49 | plot(A4,'m'); % to plot output of adaptive filter for microphone 4 50 | plot(A5,'m'); % to plot output of adaptive filter for microphone 5 51 | title('output of Adaptive filter'); % giving title for the figure 52 | xlabel('No.of samples---->'); % giving title for x-label 53 | ylabel('Amplitude---->'); % giving title for y-label 54 | [Phi1,ome1] = phasez(A1,1); % phi and omega values of output matrix (microphone 1) 55 | [phi2,ome2] = phasez(A2,1); % phi and omega values of output matrix (microphone 2) 56 | [phi3,ome3] = phasez(A3,1); % phi and omega values of output matrix (microphone 3) 57 | [phi4,ome4] = phasez(A4,1); % phi and omega values of output matrix (microphone 4) 58 | [phi5,ome5] = phasez(A5,1); % phi and omega values of output matrix (microphone 5) 59 | 60 | figure % opening a new figure window 61 | hold on; % holding the plot of required figure in figure window 62 | plot(ome1/pi,Phi1,'r'); % plotting phase of output signal from filter (microphone 1) 63 | plot(ome2/pi,phi2,'c'); % plotting phase of output signal from filter (microphone 2) 64 | plot(ome3/pi,phi3,'y'); % plotting phase of output signal from filter (microphone 3) 65 | plot(ome4/pi,phi4,'m'); % plotting phase of output signal from filter (microphone 4) 66 | plot(ome5/pi,phi5,'m'); % plotting phase of output signal from filter (microphone 5) 67 | title('Phase of output signal from filter'); % giving title for the figure 68 | xlabel('Frequency---->'); % giving title for x-label 69 | ylabel('Phase(/omega)---->'); % giving title for y-label 70 | 71 | la1 = ome1\Phi1; % delayes estimated from slope of signal (microphone 1) 72 | la2 = ome2\phi2; % delayes estimated from slope of signal (microphone 2) 73 | la3 = ome3\phi3; % delayes estimated from slope of signal (microphone 3) 74 | la4 = ome4\phi4; % delayes estimated from slope of signal (microphone 4) 75 | la5 = ome4\phi5; % delayes estimated from slope of signal (microphone 5) 76 | 77 | delay_slope = [la1 la2 la3 la4 la5]; % delay from obtained slope 78 | final_delay = -(delay_slope+ord/2) % removing the extra added length of filter 79 | 80 | 81 | %% 82 | syms xn yn; % Taking xn and yn as symbols 83 | z1 = (sqrt((xn-x_axis(2))^2+yn^2)-sqrt(xn^2+yn^2))*sam_freq/c; % equation of delay between reference microphone to other microphone 84 | z2 = (sqrt((xn-x_axis(3))^2+yn^2)-sqrt(xn^2+yn^2))*sam_freq/c; 85 | z3 = (sqrt((xn-x_axis(4))^2+yn^2)-sqrt(xn^2+yn^2))*sam_freq/c; 86 | z4 = (sqrt((xn-x_axis(5))^2+yn^2)-sqrt(xn^2+yn^2))*sam_freq/c; 87 | z5 = (sqrt((xn-x_axis(6))^2+yn^2)-sqrt(xn^2+yn^2))*sam_freq/c; 88 | zd1 = (z1-final_delay(1))^2; % difference between estimated and ideal delays (microphone 1) 89 | zd2 = (z2-final_delay(2))^2; % difference between estimated and ideal delays (microphone 2) 90 | zd3 = (z3-final_delay(3))^2; % difference between estimated and ideal delays (microphone 3) 91 | zd4 = (z4-final_delay(4))^2; % difference between estimated and ideal delays (microphone 4) 92 | zd5 = (z5-final_delay(5))^2; % difference between estimated and ideal delays (microphone 5) 93 | zd = zd1+zd2+zd3+zd4; 94 | %% 95 | 96 | x = diff(zd,xn); % Partial difference of sum of total difference in delays with respect to xn 97 | y = diff(zd,yn); % Partial difference of sum of total difference in delays with respect to yn 98 | X = matlabFunction(x); % generating function from symbol xn 99 | Y = matlabFunction(y); % generating function from symbol yn 100 | mue = .015; % initiating step size 101 | loc_x = 1; % initial estimated x-axis location of sound source 102 | loc_y = 1; % initial estimated y-axis location of sound source 103 | for n = 1:50000 104 | loc_x = loc_x-mue*X(loc_x,loc_y); % steepest descent algorithm of x-axis 105 | loc_y = loc_y-mue*Y(loc_x,loc_y); % steepest descent algorithm of y-axis 106 | end 107 | display(loc_x); 108 | display(loc_y); --------------------------------------------------------------------------------