Improved point processes. Added index.

This commit is contained in:
2015-11-21 23:09:07 +01:00
parent 788d6aa8ab
commit a945268bee
12 changed files with 271 additions and 157 deletions

View File

@@ -1,5 +1,5 @@
function [counts, bins] = counthist(spikes, w)
% computes count histogram and compare with Poisson distribution
% Compute and plot histogram of spike counts.
%
% [counts, bins] = counthist(spikes, w)
%
@@ -19,36 +19,25 @@ function [counts, bins] = counthist(spikes, w)
times = spikes{k};
% alternative 1: count the number of spikes in each window:
% for tk = 0:w:tmax-w
% nn = sum( ( times >= tk ) & ( times < tk+w ) );
% %nn = length( find( ( times >= tk ) & ( times < tk+w ) ) );
% n = [ n nn ];
% nn = sum((times >= tk) & (times < tk+w));
% %nn = length(find((times >= tk) & (times < tk+w)));
% n = [n nn];
% end
% alternative 2: use the hist function to do that!
% alternative 2: use the hist() function to do that!
tbins = 0.5*w:w:tmax-0.5*w;
nn = hist(times, tbins);
n = [ n nn ];
% the rate of the spikes:
rate = (length(times)-1)/(times(end) - times(1));
r = [ r rate ];
n = [n nn];
end
% histogram of spike counts:
maxn = max( n );
[counts, bins ] = hist( n, 0:1:maxn+10 );
maxn = max(n);
[counts, bins] = hist(n, 0:1:maxn+10);
% normalize to probabilities:
counts = counts / sum( counts );
counts = counts / sum(counts);
% plot:
if nargout == 0
bar( bins, counts );
hold on;
% Poisson distribution:
rate = mean( r );
x = 0:1:maxn+10;
a = rate*w;
y = a.^x.*exp(-a)./factorial(x);
plot( x, y, 'r', 'LineWidth', 3 );
hold off;
xlabel( 'counts k' );
ylabel( 'P(k)' );
end

View File

@@ -0,0 +1,55 @@
function [counts, bins] = counthist(spikes, w)
% computes count histogram and compare with Poisson distribution
%
% [counts, bins] = counthist(spikes, w)
%
% Arguments:
% spikes: a cell array of vectors of spike times in seconds
% w: observation window duration in seconds for computing the counts
%
% Returns:
% counts: the histogram of counts normalized to probabilities
% bins: the bin centers for the histogram
% collect spike counts:
tmax = spikes{1}(end);
n = [];
r = [];
for k = 1:length(spikes)
times = spikes{k};
% alternative 1: count the number of spikes in each window:
% for tk = 0:w:tmax-w
% nn = sum( ( times >= tk ) & ( times < tk+w ) );
% %nn = length( find( ( times >= tk ) & ( times < tk+w ) ) );
% n = [ n nn ];
% end
% alternative 2: use the hist function to do that!
tbins = 0.5*w:w:tmax-0.5*w;
nn = hist(times, tbins);
n = [ n nn ];
% the rate of the spikes:
rate = (length(times)-1)/(times(end) - times(1));
r = [ r rate ];
end
% histogram of spike counts:
maxn = max( n );
[counts, bins ] = hist( n, 0:1:maxn+10 );
% normalize to probabilities:
counts = counts / sum( counts );
% plot:
if nargout == 0
bar( bins, counts );
hold on;
% Poisson distribution:
rate = mean( r );
x = 0:1:maxn+10;
a = rate*w;
y = a.^x.*exp(-a)./factorial(x);
plot( x, y, 'r', 'LineWidth', 3 );
hold off;
xlabel( 'counts k' );
ylabel( 'P(k)' );
end
end

View File

@@ -0,0 +1,24 @@
function spikes = hompoissonspikes(rate, trials, tmax)
% Generate spike times of a homogeneous poisson process
% using the exponential interspike interval distribution.
%
% spikes = hompoissonspikes(rate, trials, tmax)
%
% Arguments:
% rate: the rate of the Poisson process in Hertz
% trials: number of trials that should be generated
% tmax: the duration of each trial in seconds
%
% Returns:
% spikes: a cell array of vectors of spike times in seconds
spikes = cell(trials, 1);
mu = 1.0/rate;
nintervals = 2*round(tmax/mu);
for k=1:trials
% exponential random numbers:
intervals = random('Exponential', nintervals, 1);
times = cumsum(intervals);
spikes{k} = times(times<=tmax);
end
end

View File

@@ -9,14 +9,14 @@ function [pdf, centers] = isi_hist(isis, binwidth)
%
% Returns:
% pdf: vector with probability density of interspike intervals in Hz
% centers: vector with corresponding centers of interspikeintervalls in seconds
% centers: vector with centers of interspikeintervalls in seconds
if nargin < 2
% compute good binwidth:
nperbin = 200; % average number of data points per bin
bins = length( isis )/nperbin; % number of bins
nperbin = 200; % average number of data points per bin
bins = length( isis )/nperbin; % number of bins
binwidth = max( isis )/bins;
if binwidth < 5e-4 % half a millisecond
if binwidth < 5e-4 % half a millisecond
binwidth = 5e-4;
end
end

View File

@@ -9,7 +9,7 @@ function isivec = isis( spikes )
for k = 1:length(spikes)
difftimes = diff( spikes{k} );
% difftimes(:) ensures a column vector
% regardless of the type of vector spikes{k}
% regardless of the type of vector in spikes{k}
isivec = [ isivec; difftimes(:) ];
end
end

View File

@@ -1,34 +1,35 @@
function isicorr = isiserialcorr(isis, maxlag)
function isicorr = isiserialcorr(isivec, maxlag)
% serial correlation of interspike intervals
%
% isicorr = isiserialcorr(isis, maxlag)
% isicorr = isiserialcorr(isivec, maxlag)
%
% Arguments:
% isis: vector of interspike intervals in seconds
% isivec: vector of interspike intervals in seconds
% maxlag: the maximum lag in seconds
%
% Returns:
% isicorr: vector with the serial correlations for lag 0 to maxlag
lags = 0:maxlag;
isicorr = zeros( size( lags ) );
isicorr = zeros(size(lags));
for k = 1:length(lags)
lag = lags(k);
if length( isis ) > lag+10 % ensure "enough" data
if length(isivec) > lag+10 % ensure "enough" data
% NOTE: the arguments to corr must be column vectors!
% We insure this in the isis() function that generats the isis.
isicorr(k) = corr( isis(1:end-lag), isis(lag+1:end) );
% We insure this in the isis() function that
% generates the isivec.
isicorr(k) = corr(isivec(1:end-lag), isivec(lag+1:end));
end
end
if nargout == 0
% plot:
plot( lags, isicorr, '-b' );
plot(lags, isicorr, '-b');
hold on;
scatter( lags, isicorr, 100.0, 'b', 'filled' );
scatter(lags, isicorr, 100.0, 'b', 'filled');
hold off;
xlabel( 'Lag k' )
ylabel( '\rho_k')
xlabel('Lag k')
ylabel('\rho_k')
end
end

View File

@@ -7,22 +7,28 @@ function plot_isi_hist(isis, binwidth)
% isis: vector of interspike intervals in seconds
% binwidth: optional width in seconds to be used for the isi bins
% compute normalized histogram:
if nargin < 2
[pdf, centers] = isi_hist(isis);
else
[pdf, centers] = isi_hist(isis, binwidth);
end
bar(1000.0*centers, nelements); % milliseconds on x-axis
% plot:
bar(1000.0*centers, pdf); % milliseconds on x-axis
xlabel('ISI [ms]')
ylabel('p(ISI) [1/s]')
% annotation:
misi = mean(isis);
sdisi = std(isis);
disi = sdisi^2.0/2.0/misi^3;
text(0.95, 0.8, sprintf('mean=%.1f ms', 1000.0*misi), 'Units', 'normalized', 'HorizontalAlignment', 'right')
text(0.95, 0.7, sprintf('std=%.1f ms', 1000.0*sdisi), 'Units', 'normalized', 'HorizontalAlignment', 'right')
text(0.95, 0.6, sprintf('CV=%.2f', sdisi/misi), 'Units', 'normalized', 'HorizontalAlignment', 'right')
%text(0.5, 0.3, sprintf('D=%.1f Hz', disi), 'Units', 'normalized')
text(0.95, 0.8, sprintf('mean=%.1f ms', 1000.0*misi), ...
'Units', 'normalized', 'HorizontalAlignment', 'right')
text(0.95, 0.7, sprintf('std=%.1f ms', 1000.0*sdisi), ...
'Units', 'normalized', 'HorizontalAlignment', 'right')
text(0.95, 0.6, sprintf('CV=%.2f', sdisi/misi), ...
'Units', 'normalized', 'HorizontalAlignment', 'right')
%text(0.95, 0.5, sprintf('D=%.1f Hz', disi), ...
% 'Units', 'normalized', 'HorizontalAlignment', 'right')
end