-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetThreshold.m
137 lines (135 loc) · 6.01 KB
/
getThreshold.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
127
128
129
130
131
132
133
134
135
136
137
function Thresh = getThreshold(Data,spkidx,previdx,rez,type,threshparams)
switch type
case 1
% change in dV/dt (Atherton and Bevan 2005)
% take the dV/dt of the voltage trace, and take the SD of the dV/dt for 50 ms before the AP
% The threshold is reached once the dV/dt reaches: mean(dV/dt)
% +2*SD(dV/dt) in units of V/s (or mV/ms)
previdx = max(spkidx - round(50/rez),previdx);
[~, mini] = min(Data(previdx:spkidx));
stidx = previdx + mini-1;
dVdt = diff(Data(stidx:spkidx))/rez;
grr=0;
Thresh=[];
%while isempty(Thresh)
Thresh = find(dVdt>=(mean(dVdt)+(threshparams(1)-grr)*std(dVdt)),1,'first')+ stidx - 1;
% grr = grr + .1;
%end
% if grr>0
% disp(['For Thresh #1, used a std multiplier of ' num2str(2-grr)])
% end
case 2
% dV/dt threshold (Cooper et al. 2003; Metz et al. 2005)
% set a cutoff point of the dV/dt, such as 30 mV/ms, as the AP threshold (Fig. 2A, dotted line)
previdx = max(spkidx - round(50/rez),previdx);
[~, mini] = min(Data(previdx:spkidx));
stidx = previdx + mini-1;
dVdt = diff(Data(stidx:spkidx))/rez;
Thresh=[];
%while isempty(Thresh)
cutoff = threshparams(2);
tmpT=find(dVdt>=cutoff,1,'first');
if isempty(tmpT)
Thresh=[];
elseif tmpT==1
Thresh = tmpT+ stidx - 1;
else
% dVdt is in between two points, so check which side cut-off is
% closer to
if cutoff-dVdt(tmpT-1)<dVdt(tmpT)-cutoff
Thresh = tmpT+ stidx - 2;
else
Thresh = tmpT+ stidx - 1;
end
end
% grr = grr + 1;
%end
% if grr>0
% disp(['For Thresh #2, used a cutoff of ' num2str(cutoff)])
% end
case 3
% maximum of second derivative (Mainen et al. 1995)
% take the maximum of the second derivative of the voltage trace with respect to time (Fig. 2A, gray line)
previdx = max(spkidx - round(50/rez),previdx);
[~, mini] = min(Data(previdx:spkidx));
stidx = previdx + mini-1;
dV2d2t = diff(diff(Data(stidx:spkidx)));
[~, maxi] = max(dV2d2t);
Thresh = maxi + stidx;
% case 1
% Thresh = find(diff(Data(previdx:spkidx))>=.5*max(diff(Data(previdx:spkidx))),1,'first') + previdx - 2; % .22
% case 2
% [~, mini] = min(Data(previdx:spkidx));
% b = find(Data(previdx+mini+10:spkidx)<=-15,1,'last') + mini + previdx + 9;
% newmini = round((mini + previdx + 9 + b)/2);
% mydiff = Data(newmini:spkidx); %diff(Data(newmini:spkidx));
% mysz = 3;
% for z = (mysz+1):length(mydiff)-mysz
% laterpart(z-mysz) = mean(mydiff(z:(z+mysz-1)));
% earlypart(z-mysz) = mean(mydiff((z-mysz):(z-1)));
% mydiff(z-mysz) = mean(mydiff(z:(z+mysz-1))) - mean(mydiff((z-mysz):(z-1)));
% end
% [~, tmp] = max(mydiff);
% Thresh = tmp + newmini + mysz; %9;
% case 3
% [~, mini] = min(Data(previdx:spkidx));
% b = find(Data(previdx+mini+10:spkidx)<=-15,1,'last') + mini + previdx + 9;
% newmini = round((mini + previdx + 9 + b)/2);
% mydiff = Data(newmini:spkidx); %diff(Data(newmini:spkidx));
% mysz = 3;
% for z = (mysz+1):length(mydiff)-mysz
% laterpart(z-mysz) = mean(mydiff(z:(z+mysz-1)));
% earlypart(z-mysz) = mean(mydiff((z-mysz):(z-1)));
% mydiff(z-mysz) = mean(mydiff(z:(z+mysz-1))) - mean(mydiff((z-mysz):(z-1)));
% end
% tmp = find(mydiff>.5*max(mydiff),1,'first');
% Thresh = tmp + newmini + mysz; %9;
% case 4
% [~, mini] = min(Data(previdx:spkidx));
% [~, maxi] = max(diff(diff(Data((mini+previdx):spkidx))));
% Thresh = maxi + previdx + mini - 1;
% case 5
% [~, mini] = min(Data(previdx:spkidx));
% [~, maxi] = max(diff(diff(Data((mini+previdx):spkidx))));
% Thresh = maxi + previdx + mini - 2;
% case 6
% [~, mini] = min(Data(previdx:spkidx));
% b = find(Data(previdx+mini+10:spkidx)<=-15,1,'last') + mini + previdx + 9;
% newmini = round((mini + previdx + 9 + b)/2);
% diffvec = diff(Data(newmini:b));
% meandiff = cummean(diffvec);
% Thresh = [];
% w=0;
% while isempty(Thresh)
% Thresh = find([diffvec(6:end-1)>(4-w)*meandiff(5:end-2) & diffvec(7:end)>(4-w)*meandiff(5:end-2)],1,'first') + newmini-1+5;
% w = w + .5;
% end
% case 7
% [~, mini] = min(Data(previdx:spkidx));
% b = find(Data(previdx+mini+10:spkidx)<=-15,1,'last') + mini + previdx + 9;
% newmini = round((mini + previdx + 9 + b)/2);
% diffvec = diff(Data(newmini:b));
% meandiff = diffvec; %cummean(diffvec);
% Thresh = [];
% w=0;
% while isempty(Thresh)
% Thresh = find([diffvec(6:end-1)>(7-w)*meandiff(5:end-2) & diffvec(7:end)>(7-w)*meandiff(5:end-2)],1,'first') + newmini-1+5;
% w = w + .5;
% end
% %if isempty(Thresh), Thresh = spkidx; end %find((diffvec(6:end)-meandiff(5:end-1))./meandiff(5:end-1)>2.7,1,'first') + newmini-1+5; end
% case 8
% [~, mini] = min(Data(previdx:spkidx));
% b = find(Data(previdx+mini+10:spkidx)<=-15,1,'last') + mini + previdx + 9;
% newmini = round((mini + previdx + 9 + b)/2);
% diffvec = diff(Data(newmini:b));
% meandiff = diffvec; %cummean(diffvec);
% Thresh = [];
% w=0;
% while isempty(Thresh)
% Thresh = find([(4-w)*meandiff(5:end-2)<diffvec(6:end-1) & (4-w)*meandiff(5:end-2)<diffvec(7:end)],1,'last') + newmini-1+5;
% w = w + .5;
% end
end
if Data(Thresh)>-15
Thresh
end