% ns_Gon_001.m
% 03 Nov 2016

% Gong001
% Modeling a single neuron with a integrate and fire spiking neuron model
close all
clc
clear all

% Number of time steps
   NT = 500;
% Simulation time [ms]
   tMax = 200;
% time constant  [ms]
   tau = 20;
% threshold voltage  [mV]
   vTH = 1.0;
% refractroy time / delay  [ms]
   tREF = 1;
% time steps / time increment  [ms]
   t  = linspace(0,tMax,NT);
   dt = t(2) - t(1);
   K = dt / tau;
% external input  [mV]
   vEXT = zeros(NT,1);
  % vEXT(10:end) = 2;
    vEXT(50:150)   = 1;
%    vEXT(18:23)   = 1.3;
%    vEXT(26:31)   = 1.5;
%    vEXT(34:39)   = 1.2;
   
   
% TIME EVOLUTION OF MEMBRANE POTENTIAL  v   [mV]  ========================
   v = zeros(NT,1);
   v(1) = -1;
for c = 1 : NT-1
    if v(c) > vTH
       v(c+1) = v(c) - vTH;
    else   
      v(c+1) = v(c) + K *(-v(c) + vEXT(c));
    end 

end

   
% GRAPHICS ===============================================================
figure(1)
   set(gcf,'units','normalized','Position',[0.1 0.2 0.20,0.20]);
   set(gca,'fontsize',12);
   v(v>vTH) = vTH;
  
   xP = t; yP = v;
   plot(t,vEXT,'r','lineWidth',1)
   hold on
   yP = v;
   plot(t,v,'b','lineWidth',2)
   
   tx = 'time  t  [ms]';
   ty = 'v  [mV]';
   xlabel(tx); ylabel(ty);
   
   legend('v_{EXT}','v_m');
   box on
   grid on
   
   