function [time, rate] = instantaneous_rate(spikes, dt, t_max)
% Firing rate as the inverse of the interspike interval.
%
% [time, rate] = instantaneous_rate(spikes, dt, t_max)
%
% Arguments:
%   spikes: vector containing the times of the spikes.
%   dt    : the temporal resolutions of the recording.
%   t_max : the duration of the trial.
%
% Returns:
%   the vector representing time and a vector containing the rate.

    time = 0:dt:t_max-dt;
    rate = zeros(size(time));

    isis = diff([0 spikes]);
    inst_rate = 1 ./ isis;
    spike_indices = [1 round(spikes ./ dt)];

    for i = 2:length(spike_indices)
        rate(spike_indices(i - 1):spike_indices(i)) = inst_rate(i - 1);
    end
end
