%clear all;
%close all;
%clc;

%[Dksvd,g,err]=newnew;
 

seg=0;

for N=1:960

 
 s1 = wavread('male_src_1.wav');
 %s1 = s1(1:2:end); % Downsample from 48.0kHz to 8.0kHz
 s2 = wavread('male_src_2.wav');
 %s2 = s2(1:2:end); % Downsample from 44.1kHz to 8.8kHz
 s3 = wavread('male_src_3.wav');
 %s3 = s3(1:2:end); % Downsample from 48.0kHz to 8.0kHz
 s4 = wavread('male_src_4.wav');
 %s4 = s4(1:2:end); % Downsample from 48.0kHz to 8.0kHz   
 

 s1 = s1(seg+1:seg+64); s2 = s2(seg+1:seg+64); s3 = s3(seg+1:seg+64); s4 = s4(seg+1:seg+64);
 
 
 mixing = [0.3420, 0.6428, 0.7934, 0.9239; ...
           0.9397, 0.7660, 0.6088, 0.3827]; 
 
 data.signal = [s1, s2, s3, s4];      
 


 mixture = [s1 s2 s3 s4] * mixing';
 
 m  = length(s1);
 
 
 
 data.M = opMatrix(kron(mixing,speye(m,m)), ...
              'Mixing matrix: kron(mixing,speye)');
          
 
 
 
 B = Dksvd;

 M=OPTOMATRIX(data.M,1);
 
 A = M * B;
 
 
 data.b = mixture(:);
 
 %data = completeOps(data);
 
 opts = spgSetParms('optTol',1e-4);
 tau = pi;    % Initial one-norm of solution
 sigma = 0;  % Go for a basis pursuit solution
 y = spgl1(A, data.b,tau, sigma, [], opts);    
 
 %A=kron(mixing,speye(m,m))
 
 
 %w=randn(256,64);
 %Dksvd=w*Dksvd;
 
    
 f = B*y;
 %es1(seg+1:seg+512)=f(1:512);
 %es2(seg+1:seg+512)=f(513:1024);
 %es3(seg+1:seg+512)=f(1025:1536);
 %es4(seg+1:seg+512)=f(1537:2048); 
 
 es1(seg+1:seg+64)=f(1:64);
 es2(seg+1:seg+64)=f(65:128);
 es3(seg+1:seg+64)=f(129:192);
 es4(seg+1:seg+64)=f(193:256);
 
 seg=seg+64;
 
 
end



z1= wavread('male_inst_sim_1');
z2= wavread('male_inst_sim_2');
z3= wavread('male_inst_sim_3');
z4= wavread('male_inst_sim_4');
z1=z1';
z2=z2';
z3=z3';
z4=z4';

a1=z1(1,1:61440);
%a1 = a1(1:2:end); 
b1=z1(2,1:61440);
%b1 = b1(1:2:end);

a2=z2(1,1:61440);
%a2 = a2(1:2:end); 
b2=z2(2,1:61440);
%b2 = b2(1:2:end);

a3=z3(1,1:61440);
%a3 = a3(1:2:end); 
b3=z3(2,1:61440);
%b3 = b3(1:2:end);

a4=z4(1,1:61440);
%a4 = a4(1:2:end); 
b4=z4(2,1:61440);
%b4 = b4(1:2:end);


Y(1,:,1)=a1;
Y(2,:,1)=b1;

Y(1,:,2)=a2;
Y(2,:,2)=b2;

Y(1,:,3)=a3;
Y(2,:,3)=b3;

Y(1,:,4)=a4;
Y(2,:,4)=b4;

se1(1,:)=0.3420*es1;
se1(2,:)=0.9397*es1;

se2(1,:)=0.6428*es2;
se2(2,:)=0.7660*es2;

se3(1,:)=0.7934*es3;
se3(2,:)=0.6088*es3;

se4(1,:)=0.9239*es4;
se4(2,:)=0.3827*es4;


[s_true,e_spat,e_interf,e_artif]=bss_decomp_mtifilt(se1,Y,1,512);
[SDR,ISR,SIR,SAR]=bss_mcrit(s_true,e_spat,e_interf,e_artif);
result1=[SDR,ISR,SIR,SAR];

[s_true,e_spat,e_interf,e_artif]=bss_decomp_mtifilt(se2,Y,2,512);
[SDR,ISR,SIR,SAR]=bss_mcrit(s_true,e_spat,e_interf,e_artif);
result2=[SDR,ISR,SIR,SAR];

[s_true,e_spat,e_interf,e_artif]=bss_decomp_mtifilt(se3,Y,3,512);
[SDR,ISR,SIR,SAR]=bss_mcrit(s_true,e_spat,e_interf,e_artif);
result3=[SDR,ISR,SIR,SAR];

[s_true,e_spat,e_interf,e_artif]=bss_decomp_mtifilt(se4,Y,4,512);
[SDR,ISR,SIR,SAR]=bss_mcrit(s_true,e_spat,e_interf,e_artif);
result4=[SDR,ISR,SIR,SAR];


%wavwrite(g1,'mask1.wav');
wavwrite(mixture,'mixture.wav');
wavwrite(es1,'sparse.wav');