function [x2,w1,w2,w3] = optimum_noise_canceler_multi_ref(x, noise, N)
% implement the whole block of fig 7.1 (a) in Page 140 of Akay's book
% s(n) = x(n) + v(n);

c = xcorr(noise(:,1),noise(:,1),N,'biased');
c(1:N)=[];
R11=toeplitz(c(1:N)');

c = xcorr(noise(:,1),noise(:,2),N,'biased');
c(1:N)=[];
R12=toeplitz(c(1:N)');

c = xcorr(noise(:,1),noise(:,3),N,'biased');
c(1:N)=[];
R13=toeplitz(c(1:N)');

c = xcorr(noise(:,2),noise(:,1),N,'biased');
c(1:N)=[];
R21=toeplitz(c(1:N)');

c = xcorr(noise(:,2),noise(:,2),N,'biased');
c(1:N)=[];
R22=toeplitz(c(1:N)');

c = xcorr(noise(:,2),noise(:,3),N,'biased');
c(1:N)=[];
R23=toeplitz(c(1:N)');

c = xcorr(noise(:,3),noise(:,1),N,'biased');
c(1:N)=[];
R31=toeplitz(c(1:N)');

c = xcorr(noise(:,3),noise(:,2),N,'biased');
c(1:N)=[];
R32=toeplitz(c(1:N)');

c = xcorr(noise(:,3),noise(:,3),N,'biased');
c(1:N)=[];
R33=toeplitz(c(1:N)');

RRR = [R11 R12 R13;
       R21 R22 R23;
       R31 R32 R33];
       
p = xcorr(x,noise(:,1),N,'biased');
p(1:N)=[];
p1 = p(1:N);

p = xcorr(x,noise(:,2),N,'biased');
p(1:N)=[];
p2 = p(1:N);

p = xcorr(x,noise(:,3),N,'biased');
p(1:N)=[];
p3 = p(1:N);

ppp = [p1;p2;p3];

www = RRR\ppp;

w1 = www(1:N);
w2 = www(N+1:2*N);
w3 = www(2*N+1:3*N);

v_hat1 = filter(w1,1,noise(:,1));
v_hat2 = filter(w2,1,noise(:,2));
v_hat3 = filter(w3,1,noise(:,3));

x2 = x - v_hat1 - v_hat2 - v_hat3;

