image thumbnail

Kohonen Self Organizing Feature Maps (SOFM) for Simulink.

by

 

25 Apr 2012 (Updated )

A implementation of the Kohonen Self Organizing Feature Maps (SOFM) algorithm for Simulink.

weights2DView(t,x,u,flag,maxVector,grid,updateFig)
function [sys,x0,str,ts] = weights2DView(t,x,u,flag,maxVector,grid,updateFig)

switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
   [sys,x0,str,ts]=mdlInitializeSizes(grid)
  
  %%%%%%%%%%  
  % Update %
  %%%%%%%%%%
  case 2,                                               
   sys = mdlUpdate(t,x,u,maxVector,grid,updateFig);
    
  %%%%%%%%%%
  % Output %
  %%%%%%%%%%
 % case 3,                                               
   % sys = mdlOutputs(t,x,u);    

  %%%%%%%%%%%%%
  % Terminate %
  %%%%%%%%%%%%%
  case 9,                                               
    sys = [];
    %close(gcf);
    
      
  %%%%%%%%%%%%%%%%%%%
  % Unhandled flags %
  %%%%%%%%%%%%%%%%%%%
  case { 1, 3, 4 }
    sys=[];

  otherwise
    error(['unhandled flag = ',num2str(flag)]);
end

%end

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(grid)

sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 1;
sizes.NumOutputs     = 0;
sizes.NumInputs      = grid(1)*grid(2)*2 + 3;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

x0  = 0;
str = [];
ts  = [-1 0]; % Inherited sample time

% end mdlInitializeSizes

%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function sys = mdlUpdate(t,x,u,maxVector,grid,updateFig)
sys = [];    
n = grid(1)*grid(2);
if (mod(u(n*2+3),updateFig) == 0)
   figure(1);

     plot(reshape(u(1:n)+j*u(n+1:2*n),grid(2),grid(1)), 'b.-');
 
     hold on
     plot(reshape(u(1:n)+j*u(n+1:2*n),grid(2),grid(1)).', 'r.-');
     axis(maxVector);
     title(['n = ' num2str(u(n*2+3)) ' mu(n) = ' num2str(u(n*2+1)) ' sigma^2(n) = ' num2str(u(n*2+2))]);
     axis(maxVector);
xlabel('w_x');
ylabel('w_y');
     hold off
 end
%end mdlUpdate

%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
%function sys = mdlOutputs(t,x,u)
%sys = x;

%end mdlOutputs

Contact us