A better method to implement the alpha synapse function in MATLAB

Viewed 374

I have implemented a leaky integrate and fire neuron system that gives output in the form of an alpha function alpha = t/tau * exp(1 - (t/tau)). However, the piece of code that I use to implement it takes up atleast 80% of the runtime (approximately 4s out of the total 5s). During the course of the program this prt gets called at least 30000 times while the computealphasynapseoutput function gets called at least 1.5 million times. So I want to reduce the runtime of this part. I have tried using arrayfun to implement it but that takes up a lot more time than this.

Can anyone suggest a more efficient implementation for this code??

To implement the alpha synapse I used the following piece of code:

% Get the identity of the currently active neurons
idAllActiveNeuron = idAllActiveNeuron > 0;
if any(idAllActiveNeuron) % Only run if atleast one neuron is active
    for iActiveNeuron = find(idAllActiveNeuron).' % run for each active neuron
        %% synapticOutputArray stores the synaptic output for each neuron for each time instant
        %% iIntegration is the time instant
        %% spikeTimesArray is a cell array that is composed of spike times for 
        % each neuron. So if I have 5 neurons and neuron 4 spikes twice 
        % while neuron 5 spikes once, spikeTimesArray would be something 
        % like {[], [], [], [0.0023, 0.0034], [0.0675]}
        %% integrationInstant would be a time value like 0.0810
        %% tau_syn stores the value of tau for each neuron

        synapticOutputArray(iActiveNeuron, iIntegration) = computealphasynapseoutput(spikeTimesArray{iActiveNeuron}, integrationInstant, tau_syn(iActiveNeuron));

    end % iActiveNeuron
end

The function computealphasynapse is implemented as follows:

function synapticOutput = computealphasynapseoutput(firingTime, integrationInstant, tauSyn)
%%COMPUTEALPHASYNAPSEOUTPUT Calculates the synaptic output over all
%previous spikes of the neuron at a particular time instant using the
%alpha synapse function
%
% Usage:
%   synapticOutput = computealphasynapseoutput(firingTime, integrationInstant, tauSyn)
%
% Inputs:
%           firingTime: vector of previous firing times (in seconds)
%   integrationInstant: current integration time instant (in seconds)
%               tauSyn: synaptic time constant of the neuron (in seconds)
%
% Output:
%   synapticOutput: Synaptic output of the neuron at current time summed
%                   over all firing instances
%

% Calculate the time difference of firing from current time
timeDifference = (integrationInstant - firingTime) / tauSyn;
% Calculate and sum the synaptic output for each spike
synapticOutput = sum(timeDifference .* exp(1 - timeDifference));

end % computealphasynapseoutput

Edit:

I have finally awarded the bounty for this question to gnovice for his awesome answer. It helped me to shave off an entire 40 seconds from my simulation time (from 68 to 28 s). I hope it works for people in the future too. I would also like to acknowledge MrAzzaman and qbzenker for taking their time to answer the question and teaching me some cool new approaches. Also, others who commented and helped me. Thanks

3 Answers
Related