function [K, L, A, W] = ed_ica(fn, c, d)

I = imread(fn);
%I=rgb2gray(I);
%figure; imshow(I)
[p,q] = size(I);

J = [];
cc = c - 1;
dd = d - 1;
pq = (p-cc)*(q-dd);

for i=0:cc
    for j=0:dd
 %       if (i==1&j==1) 
 %       else 
            J = [J; reshape(I(1+i:p-cc+i, 1+j:q-dd+j), 1, pq)];
 %       end
    end
end
J=double(J);
figure; plot3( J(1,:), J(2,:), J(3,:),'.')
grid on

%[icasig, A, W] = fastica(J);
A=ones(c*d,c*d)./(c*d*c*d);
figure;
[icasig, A, W] = image_ica(J, 'g','tanh','initGuess',A,'epsilon',0.0091,'maxFinetune',3);

figure; plot3(icasig(1,:), icasig(2,:), icasig(3,:),'.')
grid on

ij = size(icasig, 1);

for i=1:c*d
sigIm =icasig(i,:);
figure; show_vector(sigIm,p-cc, q-dd);
end


pivot = int8((d-1)/2)*c + int8((c+1)/2);
[A1, icasig1] = remove_largest(A, icasig);
% [A1, icasig1] = remove_largest(A1, icasig1);
% [A1, icasig1] = remove_largest(A1, icasig1);
% [A1, icasig1] = remove_largest(A1, icasig1);
% [A1, icasig1] = remove_largest(A1, icasig1);

K = A1(pivot,:)*icasig1;
%K = A(pivot,1:ij-1)*icasig(1:ij-1,:);

figure; show_vector2(K, p-cc, q-dd);

L = A1*icasig1;
%L = A(:,1:ij-1)*icasig(1:ij-1,:);
