clc;clear all;close all;
[x,Fs,nbits]=WAVREAD('housle.wav');
%sound(x,Fs);
channels=1;
winms=20; %20ms okno
stepms = 10;
winpts = ceil(winms*Fs/1000);
steppts = ceil(stepms*Fs/1000);
N=512;
P=256;

q=ceil((size(x,1))/N)-1;
matrix=zeros(N,q);
energy=zeros(q,1);
y=zeros(q*N,channels);
a0=0.9;
a1=0.9;
delay0=128;
delay1=256;
L1=10;
L2=502;
L3=N;
n=1;
vyst=1;
aa=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1];
bits=[1 1 1 0 1 0 1 0 1 1 1 1 0 0 0 1];
%--- tvorba mikrosegmentų ---
%matrix = ramce(x,N,P);
%[q,r]= size(matrix);
%okno = hamming(q)*ones(1,r);
%matrix = matrix./vyst;
%matrix = matrix.*okno;
kk=3;


for i=1:q;
 for j = 1:N
  matrix(j,i) = x(j+N*(i-1),1);
  energy(i)=energy(i)+ matrix(j,i)^2;
 end
   energy(i)=energy(i)/N;
end



one_mixer=zeros(16,512);
zero_mixer=zeros(16,512);
%zero_mixer=zeros(512*length(bits));



for j=1:16;
  for i = 1:N
one_mixer(j,i)=mixer_signal(1/512,i,bits,j,L1,L2,L3);
zero_mixer(j,i)=1-mixer_signal(1/512,i,bits,j,L1,L2,L3);
end
%plot(1+N*(j-1):j*N,one_mixer(j,:));
%hold on;
%plot(1+N*(j-1):j*N,zero_mixer(j,:),'o');
%hold on;
%pause;
end

   
  for i=10:size(matrix,2) %sloupce
       a=a1;
       n=aa(n);
       
       if(bits(n)==1)
       delay=delay1;
       else delay=delay0;
       end
       
    for j=1:N %radky
    matrix(j,i) = matrix(j,i) + a*x(j+N*(i-1)-delay0,1)*zero_mixer(n,j)+a*x(j+N*(i-1)-delay1,1)*one_mixer(n,j);
    end
   
   n=n+1;
end
 %for k=1:2 %channel 
 for i=1:size(matrix,2) %sloupce
 for j=1:size(matrix,1) %radky
 y(j+N*(i-1),1)=matrix(j,i)*vyst;
 end
 end
 %end
%y(1:length(x),2)=x(1:length(x),2);
wavwrite(y,Fs,'output.wav');
plot(y);
sound(y,Fs);
