exercises for sta and reconstruction

This commit is contained in:
2015-11-20 12:30:12 +01:00
parent 139c825030
commit 5eaa3ce2b4
8 changed files with 210 additions and 54 deletions

View File

@@ -0,0 +1,19 @@
function [time, rate] = binned_rate(spike_times, bin_width, dt, t_max)
% Calculates the firing rate with the binning method. The hist funciton is
% used to count the number of spikes in each bin.
% Arguments:
% spike_times, vector containing the times of the spikes.
% bin_width, the width of the bins in seconds.
% dt, the temporal resolution.
% t_max, the tiral duration.
%
% Returns two vectors containing the time and the rate.
time = 0:dt:t_max-dt;
bins = 0:bin_width:t_max;
rate = zeros(size(time));
h = hist(spike_times, bins) ./ bin_width;
for i = 2:length(bins)
rate(round(bins(i - 1) / dt) + 1:round(bins(i) / dt)) = h(i);
end

View File

@@ -0,0 +1,24 @@
function [time, rate] = convolution_rate(spike_times, sigma, dt, t_max)
% Calculates the firing rate with the convolution method.
% Arguments:
% spike_times, a vector containing the spike times.
% sigma, the standard deviation of the Gaussian kernel in seconds.
% dt, the temporal resolution in seconds.
% t_max, the trial duration in seconds.
%
% Returns two vectors containing the time and the rate.
time = 0:dt:t_max - dt;
rate = zeros(size(time));
spike_indices = round(spike_times / dt);
rate(spike_indices) = 1;
kernel = gauss_kernel(sigma, dt);
rate = conv(rate, kernel, 'same');
end
function y = gauss_kernel(s, step)
x = -4 * s:step:4 * s;
y = exp(-0.5 .* (x ./ s) .^ 2) ./ sqrt(2 * pi) / s;
end

View File

@@ -0,0 +1,22 @@
function [time, rate] = instantaneous_rate(spike_times, dt, t_max)
% Function calculates the firing rate as the inverse of the interspike
% interval.
% Arguments:
% spike_times, 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 spike_times]);
inst_rate = 1 ./ isis;
spike_indices = [1 round(spike_times ./ dt)];
for i = 2:length(spike_indices)
rate(spike_indices(i - 1):spike_indices(i)) = inst_rate(i - 1);
end

View File

@@ -0,0 +1,19 @@
function s_est = reconstructStimulus(spike_times, sta, stim_duration, dt)
% Function estimates the stimulus from the Spike-Triggered-Average
% (sta).
% Arguments:
% spike_times, a vector containing the spike times in seconds.
% sta, a vector containing the spike-triggered-average.
% stim_duration, the total duration of the stimulus.
% dt, the sampling interval given in seconds.
%
% Returns:
% the estimated stimulus.
s_est = zeros(round(stim_duration / dt), 1);
binary_spikes = zeros(size(s_est));
binary_spikes(round(spike_times ./ dt)) = 1;
s_est = conv(binary_spikes, sta, 'same');

View File

@@ -0,0 +1,32 @@
function [sta, std_sta, valid_spikes] = spikeTriggeredAverage(stimulus, spike_times, count, sampling_rate)
% Function estimates the Spike-Triggered-Average (sta).
%
% Arguments:
% stimulus, a vector containing stimulus intensities
% as a function of time.
% spike_times, a vector containing the spike times
% in seconds.
% count, the number of datapoints that are taken around
% the spike times.
% sampling_rate, the sampling rate of the stimulus.
%
% Returns:
% the sta, a vector containing the staandard deviation and
% the number of spikes taken into account.
snippets = zeros(numel(spike_times), 2*count);
valid_spikes = 1;
for i = 1:numel(spike_times)
t = spike_times(i);
index = round(t*sampling_rate);
if index <= count || (index + count) > length(stimulus)
continue
end
snippets(valid_spikes,:) = stimulus(index-count:index+count-1);
valid_spikes = valid_spikes + 1;
end
snippets(valid_spikes:end,:) = [];
sta = mean(snippets, 1);
std_sta = std(snippets,[],1);

Binary file not shown.