%% Instantaneous rate
clear 
clc
close all

load 'lifoustim.mat'
t_max = 30;
rates = zeros(length(spikes), 30/dt);

for i = 1:length(spikes)
    [t, rates(i,:)] = instantaneous_rate(spikes{i}, dt, t_max);
end


f = figure();
set(f, 'paperunits', 'centimeters')
set(f, 'papersize', [10, 10])
set(f, 'paperposition', [0 0 10 10])
hold on
plot(t, rates(1,:), 'displayname', 'trial 1')
plot(t, mean(rates,1), 'displayname', 'average rate')

xlabel('time [s]', 'fontsize', 10)
ylabel('firing rate [Hz]','fontsize', 10)
legend('show')
box('off')

saveas(gcf, 'instantaneous_rate', 'pdf')

%% Binning Method
clear 
clc
close all

load 'lifoustim.mat'
t_max = 30;
rates = zeros(length(spikes), 30/dt);

for i = 1:length(spikes)
    [t, rates(i,:)] = binned_rate(spikes{i}, 0.05, dt, t_max);
end


f = figure();
set(f, 'paperunits', 'centimeters')
set(f, 'papersize', [10, 10])
set(f, 'paperposition', [0 0 10 10])
hold on
plot(t, rates(1,:), 'displayname', 'trial 1')
plot(t, mean(rates,1), 'displayname', 'average rate')

xlabel('time [s]', 'fontsize', 10)
ylabel('firing rate [Hz]','fontsize', 10)
legend('show')
box('off')

saveas(gcf, 'binned_rate', 'pdf')


%% Convolution Method
clear 
clc
close all

load 'lifoustim.mat'
t_max = 30;
rates = zeros(length(spikes), 30/dt);

for i = 1:length(spikes)
    [t, rates(i,:)] = convolution_rate(spikes{i}, 0.05, dt, t_max);
end


f = figure();
set(f, 'paperunits', 'centimeters', 'papersize', [10, 10], 'paperposition', [0 0 10 10])
hold on
plot(t, rates(1,:), 'displayname', 'trial 1')
plot(t, mean(rates,1), 'displayname', 'average rate')

xlabel('time [s]', 'fontsize', 10)
ylabel('firing rate [Hz]','fontsize', 10)
legend('show')
box('off')

saveas(gcf, 'convolved_rate', 'pdf')

