function [ V, t, n_spikes ] = IF( I, C, V0, Vth, Vreset, N )
%Simulate an IF neuron.
%INPUTS:
%  I = input current.
%  C = capacitance.
%  V0 = intial voltage.
%  Vth = threshold voltage
%  Vreset = voltage reset value.
%  N = # steps to simulate.
%OUTPUT:
%  V = voltage of IF model.
%  t = time axis.
%  n_spikes = # spikes detected.

dt=0.01;                         %Set the timestep.

n_spikes = 0;                   %The # of spikes detected.

V = zeros(N,1);
V(1)=V0;                        %Set the initial condition.

for k=1:N-1                     %Compute V, step-by-step.
    V(k+1) = V(k) + dt*I/C;
    if V(k+1) > Vth             %When we cross threshold,
        V(k+1) = Vreset;        %...set V to reset,
        n_spikes = n_spikes+1;  %...and add 1 to spike counter.
    end
end

t = (1:length(V))*dt;

end

