-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSTProfileCount.m
126 lines (100 loc) · 4.09 KB
/
STProfileCount.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function [vBinnedCounts] = STProfileCount(stTrain, tTimeWindow, strLevel)
% STProfileCount - FUNCTION Bin spikes by a time window, and return the counts
% $Id: STProfileCount.m 3987 2006-05-09 13:38:38Z dylan $
%
% Usage: [vBinnedCounts] = STProfileCount(stTrain, tTimeWindow)
% [vBinnedCounts] = STProfileCount(stTrain, tTimeWindow, strLevel)
%
% 'stTrain' is a spike train containing either an instance or a mapping.
% 'tTimeWindow' specifies the duration in seconds of the time bins that spikes
% will be lumped into. 'strLevel' optionally specifies whether a spike train
% instance or mapping will be used, and must be one of {instance, mapping}.
% STProfileCount will calculate the number of spikes falling into each time
% bin, and return the results in 'vBinnedCounts'. The format of
% 'tBinnedCounts' is [time_stamp count]. 'time_stamp' is a real value
% representing the median time of each bin. 'count' is the number of spikes
% falling into each bin.
%
% See STProfileFrequency for calculating binned instantaneous frequencies.
% See STProfileCountAddresses for binning spikes from multiplexed mappings.
% Note that STProfileCount will count spikes irrespective of individual
% addresses.
% Author: Dylan Muir <dylan@ini.phys.ethz.ch>
% Created: 29th April, 2004
% Copyright (c) 2004, 2005 Dylan Richard Muir
% -- Check arguments
if (nargin > 3)
disp('--- STProfileCount: Extra arguments ignored');
end
if (nargin < 2)
disp('*** STProfileCount: Incorrect usage');
help STProfileCount;
return;
end
% - Test for a zero-duration spike train
if (STIsZeroDuration(stTrain))
warning('SpikeToolbox:ZeroDuration', 'STProfileCount: Zero-duration spike train.');
vBinnedCounts = [0 0];
return;
end
% -- Which spike train level should we try to count?
if (exist('strLevel', 'var'))
% - The user wants to tell us
switch lower(strLevel)
case {'mapping', 'm'}
if (~isfield(stTrain, 'mapping'))
disp('*** STProfileCount: To count mappings, a mapping must exist in the spike train');
else
vBinnedCounts = STProfileCountNode(stTrain.mapping, tTimeWindow, true);
end
case {'instance', 'i'}
if (~isfield(stTrain, 'instance'))
disp('*** STProfileCount: To count instances, an instance must exist in the spike train');
else
vBinnedCounts = STProfileCountNode(stTrain.instance, tTimeWindow, false);
end
otherwise
SingleLinePrintf('*** STProfileCount: Unknown train level [%s].\n', strLevel);
disp(' strLevel must be one of {mapping, instance}');
end
else
% - We should try to work it out ourselves
% - Try mappings first
if (isfield(stTrain, 'mapping'))
vBinnedCounts = STProfileCountNode(stTrain.mapping, tTimeWindow, true);
elseif (isfield(stTrain, 'instance'))
vBinnedCounts = STProfileCountNode(stTrain.instance, tTimeWindow, false);
else
disp('*** STProfileCount: The spike train must contain either an instance or a mapping');
end
end
% --- FUNCTION STProfileCountNode
function [vBinnedCounts] = STProfileCountNode(stNode, tTimeWindow, bIsMapping)
% - Extract spike lists
if (stNode.bChunkedMode)
spikeList = stNode.spikeList;
else
spikeList = {stNode.spikeList};
end
vBinnedCounts = [];
for (nWindowIndex = 1:ceil(stNode.tDuration / tTimeWindow))
% - Calculate time window
tWindowMin = (nWindowIndex-1) * tTimeWindow;
tWindowMax = nWindowIndex * tTimeWindow;
nNumSpikes = 0;
for (nChunkIndex = 1:length(spikeList))
% - Get spike list
sList = spikeList{nChunkIndex}(:, 1);
if (bIsMapping)
% - Convert to real time signature format
sList = sList .* stNode.fTemporalResolution;
end
% - Apply time window
windowedSpikes = (sList >= tWindowMin) & (sList <= tWindowMax);
nNumSpikes = nNumSpikes + sum(windowedSpikes);
end
% - Append to spike count list
vBinnedCounts = [vBinnedCounts;...
[(nWindowIndex-1) * tTimeWindow + (tTimeWindow/2), nNumSpikes] ];
end
% --- END of STProfileCount ---