-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindTrains.m
58 lines (55 loc) · 2.04 KB
/
FindTrains.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
%Given a dataset containing a baseline with spike trains, find the starts
%of the spike trains
function trains=FindTrains(data,separation,threshold)
%INPUT: data all the data, column vector or row vector
% separation minimum distance between start of trains (ms)
% MAKE SURE YOU SELECT A NUMBER LARGER THAN
% THE EXPECTED LENGTH OF A TRAIN
% threshold minimum amplitude to count as non-baseline
%
%OUTPUT: trains vector of indices of train starts
%
% ex. FindTrains(sbuf(:,1),1000,1) will return the start of each spike
% train exceeding 1, assuming trains are at most 1000ms long
%
%
%CALLED BY: PlotAll.m, Realign.m
%
%Initialize
ln=length(data); %length of the data
trains=[]; %will contain the index of the start of each train
outtrainindex=-1*separation; %keeps track of the end of previous train. This initialization
%ensures the first train is found
intrain=0; %we assume we start out of a train
%Walk through the data and decide if we are in train or not in train.
%each time we enter a train, record the index.
i=1;
while i<=ln
if ~intrain
%we are not yet in a train
if data(i)>threshold
%Enter a train
intrain=1;
if isempty(trains)
trains=[i];
elseif i-outtrainindex>separation
trains=[trains i];
end
else
%We are still not in a train
i=i+1;
end
else
%We are already in a train, and try to find the end
j=i;
while data(j)>threshold && j<=ln
%We are still in a train
j=j+1;
end
i=j;
outtrainindex=j;
intrain=0;
%We left the train or the file has ended (which also ends the train)
%Next train must be more than separation away from current index
end
end