www.pudn.com > ica_v0.04.rar > example.m


%
% example.sci
%
% Time-stamp: 
%
% Simple example using the ica program from Matlab.
%
% The ica program is called repeatedly so that the progress
% of the unmixing matrix being learnt can be monitored.
%
% Programmer: Peter Stepien
%             pstepien@sedal.usyd.edu.au
%             CEL, School of Electrical & Information Engineering
%             The University of Sydney
%             SYDNEY NSW 2006 Australia
%
%             COPYRIGHT 1997-2001

% Greeting
fprintf('\nICA Program Example Script (Matlab)\n');
fprintf('------------------------------------------------------------\n\n');

% Clear everything first
clear;

% Generate input data
s=exprnd(1,2,10000).*sign(normrnd(0,1,2,10000));
s=s-mean(s')'*ones(1,10000);
A=[0.7,0.2;-0.3,0.9];
x=A*s;

% Save the input data to file
fd=fopen('ica.in','wb');
fwrite(fd,x,'float64');
fclose(fd);

% Start main loop
fprintf('Running ica ...\n');
W_path=zeros(1,10);
old_w=eye(2,2);
for i=1:10,
  
  % Run the ica program
  if i==1
    unix('ica -c 2 -n 5 -l 0.0001 -nounmix -w');
  else
    unix('ica -c 2 -n 5 -l 0.0001 -nounmix -w -iw');
  end;

  % Read in the resultant unmixing matrix
  fd=fopen('ica_w.out','rb');
  W=fread(fd,[2,2],'float64');
  fclose(fd);
  
  % Calculate the update difference for plotting
  W_path(i)=sqrt(sum(mean((W-old_w).^2)));
  
  % Get ready for the next loop
  old_w=W;
  unix('mv ica_w.out ica_w.in');
  
end;
fprintf('Finished.\n');

% Plot learning path
fprintf('Plot learning path ...\n');
plot(W_path);
title('W Learning Path');

% Print the unmixing results
fprintf('Print unmixing results ...\n\n');
fprintf('The mixing matrix:\n');
disp(A);
fprintf('The resultant unmixing matrix:\n');
disp(W);
fprintf('The product of the mixing and unmixing matrices (normalised):\n');
WA=W*A;
WA(1,:)=WA(1,:)/WA(1,1);
WA(2,:)=WA(2,:)/WA(2,2);
disp(WA);

% Finished
fprintf('------------------------------------------------------------\n');