new project on serial correlations
This commit is contained in:
9
projects/project_serialcorrelation/solution/firingrate.m
Normal file
9
projects/project_serialcorrelation/solution/firingrate.m
Normal 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
|
||||
26
projects/project_serialcorrelation/solution/isiserialcorr.m
Normal file
26
projects/project_serialcorrelation/solution/isiserialcorr.m
Normal 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
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
30
projects/project_serialcorrelation/solution/spikeraster.m
Normal file
30
projects/project_serialcorrelation/solution/spikeraster.m
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user