-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunAnalyze.m
213 lines (194 loc) · 6.08 KB
/
RunAnalyze.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function Analysis = RunAnalyze(FileName, PlotSubject)
% Analysis = RunAnalyze(FileName, PlotSubject)
% (1) loads the .smr file 'FileName',
% (2) examines two voltage traces (presumably from a dynamic clamp
% experiment) and calculates various properties of each trace
% and their mutual interactions, and
% (3) categorizes the results
%
% INPUT PARAMETERS:
% -FileName is the name (with complete path) of the .smr file
% OPTIONAL:
% -PlotSubject should be set to true[false] to produce[suppress]
% plots of waveforms/analysis. Alternatively, it can be set
% to a string to aid it titling plots (e.g. 'Exp #71')
% PlotVar defaults to false
% OUTPUT PARAMETERS:
% -Analysis.Cat: integer value that codes the category type.
% type 'help CategorizeDynamic' for info.
% -Analysis.CatString: string that describes the category
% -Analysis.Cell0: structure with information about cell 0
% returned from AnalyzeWaveform.m. It should be
% straightforward, but type 'help AnalyzeWaveform3' for info.
% -Analysis.Cell1: structure with information about cell 1
% -Analysis.SlowWave: structure with information about
% slow-wave behaviour of the combined system
% -SlowWave.Freq is the frequency of the dominant slow-wave
% component (in Hz)
% -SlowWave.Sigma is a (very crude) measure of the importance
% of the slow-wave frequency in the power spectrum
% -Analysis.HalfCenter: structure with information about
% half-center oscillations
% -HalfCenter.Freq is the half-center frequency (in Hz)
% -HalfCenter.Phase is the phase that cell 1 lags behind cell0
% -HalfCenter.PhaseSigma is the standard deviation of the phases
% -HalfCenter.ExclusionFact is the tendancy of bursts to
% avoid each other. 0 is random, 1 is maximum, and -1 is minimum.
%
% If a feature is not detected, relevant frequencies are set to
% zero, and relevant lists are empty
StabilizationTime = 10; %s
LowThresh = -1; %Threshold for spike derivatives, mv/ms
HighThresh = 2; % Currently these are ignored!
if(nargin < 2)
PlotSubject = false;
elseif(~ischar(PlotSubject))
if(PlotSubject)
if(ispc)
Slash = '\';
else
Slash = '/';
end
Ind = strfind(FileName, Slash);
Ind = Ind(end) + 1;
PlotSubject = FileName(Ind:end);
end
end
[t, v0, v1] = GetData(FileName);
if(DoPlot(PlotSubject))
if(ischar(PlotSubject) & length(PlotSubject) > 0)
TitleStr = [PlotSubject, ' Waveforms'];
else
TitleStr = 'Waveforms';
end
h = NamedFigure(TitleStr);
set(h, 'WindowStyle', 'docked');
hold off;
plot(t / 1000, v0, 'b-');
hold on;
plot(t / 1000, v1, 'r-');
xlabel('Time (s)', 'FontSize', 18);
ylabel('Voltage (mV)', 'FontSize', 18);
title(RealUnderscores(TitleStr), 'FontSize', 18);
hold off;
end
Analysis = AnalyzeDynamic(t, v0, v1, StabilizationTime, ...
LowThresh, HighThresh, PlotSubject);
[Cat, CatString] = CategorizeDynamic(Analysis);
disp(sprintf('System is %s', CatString))
Analysis.Cat = Cat;
Analysis.CatString = CatString;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t, v0, v1] = GetData(FileName)
if(length(strfind(lower(FileName), '.abf')) > 0)
[t, v0, v1] = GetCleanAbfData(FileName);
elseif(length(strfind(lower(FileName), '.smr')) > 0)
[t, v0, v1] = GetCleanSmrData(FileName);
elseif(length(strfind(lower(FileName), '.dat')) > 0)
fid = fopen(FileName);
if(fid < 0)
error(['Couldn''t open ', FileName])
end
try
NumT = fscanf(fid, '%d', 1);
NumCol = 3;
Mat = fscanf(fid, '%g', [NumCol, NumT]);
fclose(fid);
t = Mat(1,:);
v0 = Mat(2,:);
v1 = Mat(3,:);
catch
ErrStruct = lasterr;
disp(ErrStruct.message)
error(['Error reading ', FileName])
end
elseif(length(strfind(lower(FileName), '.mat')) > 0)
load(FileName, 't', 'v0', 'v1');
else
ErrStr = sprintf(['Error opening ', FileName, ...
'\nFiles must be of type .abf .smr .dat or .mat']);
error(ErrStr);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t, v0, v1] = GetCleanAbfData(FileName)
AbfS = LoadAbf(FileName);
t = AbfS.Time * 1000; %convert to ms
FNames = fieldnames(AbfS.Units);
Current = [];
Voltage = [];
for n = 1:length(FNames)
Unit = AbfS.Units.(FNames{n});
if(strcmp(Unit, 'mV'))
Voltage = [Voltage, n];
elseif(strcmp(Unit, 'nA'))
Current = [Current, n];
end
end
if(length(Voltage == 2) ~= 2)
for n = 1:length(FNames)
disp(FNames)
end
error(['Incorrect number of voltage traces in ', FileName]);
end
v0 = AbfS.Data.(FNames{Voltage(1)});
v1 = AbfS.Data.(FNames{Voltage(2)});
[t, v0, v1] = CleanAndSmooth(t, v0, v1);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t, v0, v1] = GetCleanSmrData(FileName)
[t, v_arr, ChanNames] = load_smr(FileName);
%Get index of voltage channels
Ind = [];
NameInd = strfind(ChanNames, '1V');
for n = 1:length(NameInd)
if(length(NameInd{n}) > 0)
Ind = [Ind, n];
end
end
if(length(Ind) ~= 2)
ChanNames
error(['Incorrect number of voltage traces in ', FileName]);
end
if(size(v_arr, 1) ~= length(t))
v_arr = v_arr';
end
v0 = v_arr(:,Ind(1));
v1 = v_arr(:,Ind(2));
%t = t * 1000;
while(t(2) - t(1) < .01) %this is a kludge!
t = t * 1000;
end
%This step necessary with one-electrode set-up:
[t, v0, v1] = CleanAndSmooth(t, v0, v1);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t, v0, v1] = CleanAndSmooth(t, v0, v1)
%First remove DCC noise by interpolating to the DCC frequency
[t, v0, v1, DCC_Info] = CleanDCC(t, v0, v1);
if(~isfinite(DCC_Info.DCC_Freq))
disp('Warning: weird DCC signal!')
DCC_Info
end
disp(sprintf('DCC freq = %g kHz', DCC_Info.DCC_Freq))
%Next low-pass filter the waveform
SmoothTime = .5; %ms
if(t(2) - t(1) < SmoothTime)
n = 2;
while(t(n) - t(1) < SmoothTime)
n = n + 2;
end
[B,A] = butter(2, 2 / n,'low');
v0 = filtfilt(B, A, v0);
v1 = filtfilt(B, A, v1);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PlotVar = DoPlot(PlotSubject)
if(ischar(PlotSubject))
PlotVar = true;
else
PlotVar = PlotSubject;
end
return