-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpikeDetector.m
76 lines (69 loc) · 2.28 KB
/
SpikeDetector.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
% Spike detector
% --- Takes sampling rate (Hz), threshold (number of standard deviations),
% voltage data, and pos/neg phase detection.
% Outputs array of event times and amplitudes, event count, voltage
% threshold and spike rate.
function [events, c, thresh] = SpikeDetector(rate, Nsig, volt, isNeg)
% Parameters
t = 1e-3; % Approx. duration of a spike (s)
n = rate*t; % Number of samples spanning a spike
% Calculate median, sigma, threshold; return threshold
sig = std(volt);
if isNeg == 1
thresh = median(volt) - Nsig*sig;
if Nsig > 1.5
threshOff = median(volt) - (Nsig/3)*sig;
else
threshOff = thresh;
end
else
thresh = median(volt) + Nsig*sig;
if Nsig > 1.5
threshOff = median(volt) + (Nsig/3)*sig;
else
threshOff = thresh;
end
end
% Event detection
c = 0; % Initialize event count
k = 0; % Initialize switch index
if isNeg == 1
for i=1:length(volt)-n
% If voltage is below detection threshold, add event to events
% list, increment event count, and change switch index to 1
if k == 0 && volt(i) <= thresh
c = c+1;
k = 1;
e = volt(i:i+n);
[M, index] = min(e);
events(c, 1) = M; % Max amplitude of event
events(c, 2) = (index+i-2)/rate; % Time of event (s)
events(c, 3) = index+i-1; % Index of event
% If switch index is on and voltage exceeds return threshold,
% change switch index back to 0
elseif k == 1 && volt(i) >= threshOff
k = 0;
end
end
else
for i=1:length(volt)-n
% If voltage is above detection threshold, add event to events
% list, increment event count, and change switch index to 1
if k == 0 && volt(i) >= thresh
c = c+1;
k = k+1;
e = volt(i:i+n);
[M, index] = max(e);
events(c, 1) = M; % Max amplitude of event
events(c, 2) = (index+i-2)/rate; % Time of event (s)
events(c, 3) = index+i-1; % Index of event
% If switch index is on and voltage is below return threshold,
% change switch index back to 0
elseif k == 1 && volt(i) <= threshOff
k = 0;
end
end
end
if c == 0
events = zeros(1, 3);
end