function [mem,Atp,Btp,EPSPsno] = EPSP(At,Bt,T,deltaT,PreTrain,s,Tau1Ep,Tau2ep,V,prevE,nmdaCont,nmda_norm,nmda_s)
%governs the behavior of NMDA-mediated EPSPs in the model

%s = 1/1000; %this is set in the GUI
%Tau1Ep = 50; %this is set in the GUI
%Tau2ep = 5; %this is set in the GUI

%As described in the paper this is the normalising factor for EPSP
norm = 0.6968; 
%Find all pre-synaptic events
indexs = find(((PreTrain) == T+deltaT));
%convert the time to the correct unit
T= T/10;
deltaT = deltaT/10;

%calculate the decay since the last last time point.
Atp = (exp(-(deltaT)/Tau1Ep)*At);
Btp = (exp(-(deltaT)/Tau2ep)*Bt);
%add this to the array
EPSPsno(1) = (s/norm)*(Atp - Btp);
%loop through the pre synaptic events
for i = 1:length(indexs)
    %as long as something has happened
    if isempty(indexs) == false
        %calculate the contribution of the presynaptic events
        Atp = Atp + exp(-((T+deltaT)-(PreTrain(indexs(i))/10))/Tau1Ep);
        Btp = Btp + exp(-((T+deltaT)-(PreTrain(indexs(i))/10))/Tau2ep);
        %add this to the array
        EPSPsno(length(EPSPsno)+1) = (s/norm)*(Atp - Btp) ;
    end
end
%calculate the membrane voltage based on the NMDA contribution and the
%above and return this.
mem = ((V/-65)*((s/norm)* (Atp - Btp)))+(nmdaCont);
