function [unmix_sig, unmix] = ica_batch(mix_sig, iter, rate, batch_size)
% function [unmix_sig, unmix] = ica_batch(mix_sig, iter, rate, batch_size)
% takes the mixed signal and uses independ component analysis to 
% compute the unmix matrix and unmix the signal
% batch learning 

[nsrc, ndata] = size(mix_sig);

% this part is for color image only, to center the data in each 
% color channel
mean_r = mean(mix_sig(:, 1:(ndata/3)), 2);
mean_g = mean(mix_sig(:, (ndata/3+1):(2*ndata/3)), 2);
mean_b = mean(mix_sig(:, (2*ndata/3+1):ndata), 2);
centered_sig(:,1:ndata/3)= mix_sig(:, 1:(ndata/3)) - repmat(mean_r,1, ndata/3);
centered_sig(:, (ndata/3+1):2*ndata/3)=mix_sig(:, (ndata/3+1):(2*ndata/3)) - repmat(mean_g,1,ndata/3);
centered_sig(:,(2*ndata/3+1):ndata)=mix_sig(:,(2*ndata/3+1):ndata) - repmat(mean_b, 1, ndata/3);

% scramble mix_sig
perm = randperm(ndata);
scrambled = centered_sig(:, perm);

% start with random guess of W
W = randn(nsrc, nsrc)/10 + eye(nsrc)

batch_index = 1:batch_size:(ndata+1);
n_batch = size(batch_index, 2)
rate = rate;

for i=1:iter,
  i
  for k=1:10,
    k
    for j=1:(n_batch-1),
      i;
      k;
      j;
      x = scrambled(:, batch_index(j):(batch_index(j+1)-1));
      this_batch_size = size(x, 2);
      u = W*x;
      y = 1./(1+ exp(-u));
      % use Amari rule dW = W + (1-2y)*u'*W
      dW = W + (1-2*y)*u'*W ./this_batch_size;
      W = W + rate*dW;
      rate;
    end
    W
  end
  rate = rate*0.8;
end

unmix = W;
unmix_scrambled = W*scrambled;
[temp, index] = sort(perm);
unmix_centered = unmix_scrambled(:, index);
% add in the mean again
%unmix_sig(:,1:(ndata/3))=unmix_centered(:,1:(ndata/3)) + repmat(mean_r, 1, ndata/3);
%unmix_sig(:,(ndata/3+1):(2*ndata/3))=unmix_centered(:,(ndata/3+1):(2*ndata/3)) + repmat(mean_g, 1, ndata/3);
%unmix_sig(:,(2*ndata/3+1):ndata)=unmix_centered(:,(2*ndata/3+1):ndata) + repmat(mean_b, 1, ndata/3);
unmix_sig = unmix_centered;
