function x = simplecell(data,varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Cell: 2D odd-symmetric DOG filter
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (nargin==1)
% only got an input vector, set default parameters
% kernel amplitudes
C = 1;
E = 1;
% kernel variances
u = 1/8^2;
v = 1/8^2;
% kernel seperation
S = 5;
elseif (nargin==2)
% got a parameter vector
params = varargin{1};
C = params(1);
E = params(1);
u = 1/params(2)^2;
v = 1/params(2)^2;
S = params(3);
else
error('wrong number of parameters, need 1 or 2')
end
% set default network parameters
A = 0; % decay rate
B = 1; % node activation upper bound
D = 1; % node activation lower bound
% fetch input vector
theta = data;
% set network size and clipping parameter
n = size(theta,2);
ks = 6*sqrt(1/u);
% kernel functions
C_ki = @(k,i) C*exp(-u*(k-i-S).^2);
E_ki = @(k,i) E*exp(-v*(k-i+S).^2);
% DOG functions
function s = F_i (theta,i)
s = 0;
for k = i-ks:i+ks % truncating the kernels at +- ks from i
s = s + theta(mod(k-1,n)+1) * (B*C_ki(k,i) - D*E_ki(k,i));
end
end
function s = G_i (theta,i)
s = 0;
for k = i-ks:i+ks
s = s + theta(mod(k-1,n)+1) * (C_ki(k,i) + E_ki(k,i));
end
end
% final distance-dependent shunting equation, solved at equilibrium
x_i = @(theta,I,i) (F_i(theta,i)*I)/(A + G_i(theta,i)*I);
% compute total intensities
I = sum(theta,2);
% pre-build x
x = zeros(size(theta,1),n);
% normalize theta and run the network
for r = 1:size(theta,1)
theta(r,:) = theta(r,:)/I(r);
for i = 1:n
x(r,i) = x_i(theta(r,:), I(r), i);
end
end
end