new project on serial correlations

This commit is contained in:
2017-01-22 22:47:50 +01:00
parent 1e5c588c31
commit a932ca022f
12 changed files with 706 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
function rate = firingrate(spikes, tmin, tmax)
% mean firing rate between tmin and tmax.
rates = zeros(length(spikes), 1);
for i = 1:length(spikes)
times= spikes{i};
rates(i) = length(times((times>=tmin)&(times<=tmax)))/(tmax-tmin);
end
rate = mean(rates);
end

View File

@@ -0,0 +1,26 @@
function isicorr = isiserialcorr(spikes, maxlag)
% serial correlation of interspike intervals
%
% isicorr = isiserialcorr(spikes, maxlag)
%
% Arguments:
% spikes: spike times in seconds
% maxlag: the maximum lag
%
% Returns:
% isicorr: vector with the serial correlations for lag 0 to maxlag
isivec = [];
for k = 1:length(spikes)
times = spikes{k};
isivec = [isivec; diff(times(:))];
end
lags = 0:maxlag;
isicorr = zeros(size(lags));
for k = 1:length(lags)
lag = lags(k);
if length(isivec) > lag+10 % ensure "enough" data
isicorr(k) = corr(isivec(1:end-lag), isivec(lag+1:end));
end
end

View File

@@ -0,0 +1,46 @@
function [isicorr, lowerbound, upperbound] = isiserialcorrbootstrap(spikes, maxlag)
% serial correlation of interspike intervals
%
% isicorr = isiserialcorrbootstrap(spikes, maxlag)
%
% Arguments:
% spikes: spike times in seconds
% maxlag: the maximum lag
%
% Returns:
% isicorr: vector with the serial correlations for lag 0 to maxlag
isivec = [];
for k = 1:length(spikes)
times = spikes{k};
isivec = [isivec; diff(times(:))];
end
lags = 0:maxlag;
isicorr = zeros(size(lags));
for k = 1:length(lags)
lag = lags(k);
if length(isivec) > lag+10 % ensure "enough" data
isicorr(k) = corr(isivec(1:end-lag), isivec(lag+1:end));
end
end
repeats = 1000;
isicorrshuffled = zeros(repeats, length(lags));
for i = 1:repeats
isishuffled = isivec(randperm(length(isivec)));
for k = 1:length(lags)
lag = lags(k);
if length(isivec) > lag+10 % ensure "enough" data
isicorrshuffled(i, k) = corr(isishuffled(1:end-lag), isishuffled(lag+1:end));
end
end
end
bounds = prctile(isicorrshuffled, [1.0 99], 1);
lowerbound = bounds(1, :);
upperbound = bounds(2, :);
end

View File

@@ -0,0 +1,64 @@
%% load data:
data = load('baselinespikes.mat');
spikes = data.spikes;
cells = data.cells;
%% print raster:
maxn = length(spikes);
if maxn > 5
maxn = 5;
end
figure()
for k = 1:maxn
subplot(maxn, 1, k);
spikeraster(spikes(k), 0.0, 1.0)
title(cells{k})
end
%% firing rates:
rates = zeros(length(spikes), 1);
for k = 1:length(spikes)
rates(k) = firingrate(spikes(k), 0.0, 9.0);
end
figure();
subplot(1, 2, 1);
boxplot(rates);
subplot(1, 2, 2);
hist(rates, 20)
%% serial correlations:
maxlag = 10;
lags = 0:maxlag;
corrs = zeros(length(spikes), 1);
figure();
for k = 1:length(spikes)
isicorrs = isiserialcorr(spikes(k), maxlag);
corrs(k) = isicorrs(2);
plot(lags, isicorrs);
hold on;
end
hold off;
figure();
plot(rates, corrs, 'o');
ylim([-0.7 0])
%% bootstrap serial correlations:
maxlag = 10;
lags = 0:maxlag;
figure();
for k = 1:maxn
[isicorr, lowerbound, upperbound] = isiserialcorrbootstrap(spikes(k), maxlag);
subplot(maxn, 1, k);
plot(lags, isicorr, 'b', 'linewidth', 2)
hold on;
plot(lags, lowerbound, 'r', 'linewidth', 1)
plot(lags, upperbound, 'r', 'linewidth', 1)
hold off;
end

View File

@@ -0,0 +1,30 @@
function spikeraster(spikes, tmin, tmax)
% Display a spike raster of the spike times given in spikes.
%
% spikeraster(spikes, tmax)
% spikes: a cell array of vectors of spike times in seconds
% tmin: plot spike raster starting at tmin seconds
% tmax: plot spike raster upto tmax seconds
ntrials = length(spikes);
for k = 1:ntrials
times = spikes{k};
times = times((times>=tmin) & (times<=tmax));
if tmax < 1.5
times = 1000.0*times; % conversion to ms
end
for i = 1:length( times )
line([times(i) times(i)],[k-0.4 k+0.4], 'Color', 'k');
end
end
if (tmax-tmin) < 1.5
xlabel('Time [ms]');
xlim([1000.0*tmin 1000.0*tmax]);
else
xlabel('Time [s]');
xlim([tmin tmax]);
end
ylabel('Trials');
ylim([0.3 ntrials+0.7 ]);
end