-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.m
207 lines (164 loc) · 5.65 KB
/
code.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
%% Read in data
data = textread('ptx 200nM 5.2.2.txt');
Time = data(:,1);
Voltage = data(:,2);
%% Figure out length & number of traces
prompt = 'How many data points are in one trace? ';
tracelength = 2000; %length of one trace
numtraces = length(data)/tracelength; %number of traces
%% Bring baseline of data plot to zero
origtlmatrix = tracelength*ones(numtraces,1); %Creates a numtraces x 1 matrix of tracelength
origtraces = mat2cell(data, origtlmatrix, 2); %Creates a numtraces x 1 with a tracelength x 2 matrix (each trace) within each cell
Bdata = Baseline(data, numtraces, origtraces); %Calls function Baseline
% figure(1)
% for i=0:numtraces-1
% plot(Time(2000*i+1:2000*(i+1)), Voltage(2000*i+1:2000*(i+1)), 'b-')
% hold on
% end
% altVoltage = Bdata(:,2);
% for i=0:numtraces-1
% plot(Time(2000*i+1:2000*(i+1)), altVoltage(2000*i+1:2000*(i+1)), 'r-')
% hold on
% end
% %plot(Time, Bdata(:,2), 'r-')
% legend('Original', 'Baselined')
%% Convert data to matrix of all the traces
tlmatrix = tracelength*ones(numtraces,1); %Creates a numtraces x 1 matrix of tracelength
traces = mat2cell(Bdata, tlmatrix, 2); %Creates a numtraces x 1 with a tracelength x 2 matrix (each trace) within each cell
%% Smooth every trace
windowWidth = 4; % Whatever you want.
kernel = ones(windowWidth,1) / windowWidth;
% figure(2)
fltrace =[];
for i=1:numtraces
onetrace = traces{i,1};
filtrace = filter(kernel,1, onetrace(:,2)); % Implement moving average filter on each trace
filtraces = horzcat(onetrace(:,1), filtrace);
fltrace = [fltrace;filtraces];
onetrace = [];
end
% plot(fltrace(:,1), fltrace(:,2))
% hold on
%
% tlmatrix = tracelength*ones(numtraces,1); %Creates a numtraces x 1 matrix of tracelength
% filteredtraces = mat2cell(fltrace, tlmatrix, 2); %Creates a numtraces x 1 with a tracelength x 2 matrix (each trace) within each cell
%
% meanfilteredtrace = Mean(tracelength, numtraces, filteredtraces);
%
% plot(meanfilteredtrace(:,1), meanfilteredtrace(:,2), 'r-', 'LineWidth', 3)
%
% legend ('All Smoothed Traces', 'Smoothed Mean Trace')
%% Find mean of all traces
meantraces = Mean(tracelength, numtraces, traces); %Call function 'Mean'
% figure(3)
% plot(meantraces(:,1), meantraces(:,2), 'r.', 'MarkerSize', 20)
% hold on
% plot (Time, Voltage, 'b.')
% legend ('Mean Trace', 'Original Data')
xmean = meantraces(:,1);
ymean = meantraces(:,2);
meanmins = islocalmin(meantraces(:,2)); % Find local minimas
meanmaxs = islocalmax(meantraces(:,2)); % Finds local maximas
%% Median
% medtraces = Median(tracelength, numtraces, traces); %Call function 'Median'
%
% figure(4)
% plot(medtraces(:,1), medtraces(:,2), 'g.','MarkerSize', 20)
% hold on
% plot (meantraces(:,1), meantraces(:,2), 'r.','MarkerSize', 20)
% hold on
% plot (Time, Voltage, 'b.')
%
% legend ('Median Trace', 'Mean Trace', 'All Traces')
%% Find the beginning/end of artifact & of 1st peak
grad = gradient(meantraces(:,2)); % Calulates gradient of mean trace
% Finds beginning and end of stimulus artifact through gradient analysis
Xartifact = find(abs(grad) > 10);
Xartifactbeg = Xartifact(1);
Xartifact2 = find(abs(grad(Xartifactbeg:tracelength)) < 0.2);
Xartifactend = Xartifact2(1) +Xartifactbeg-1;
next = Xartifactend+1;
Xpeak = find((abs(grad(next:tracelength))) >= 0.2); % Beginning of first peak occurs at first point after artifact where gradient is larger than 0.2
Xpeakbeg = Xpeak(1)+ Xartifactend;
Xpeakmax = find(meanmaxs(Xpeakbeg:2000)); % End of first peak occurs at point after beginning of peak where there exists a local maximum
Xpeakend = Xpeakmax(1) + Xpeakbeg - 1;
%% Find the beginning/end of artifact & of 1st peak (filtered)
gradfilt = gradient(meanfilteredtrace(:,2));
Xartifactf = find(abs(gradfilt) > 10);
Xartifactbegf = Xartifactf(1);
Xartifact2f = find(abs(gradfilt(Xartifactbegf:tracelength)) < 0.2);
Xartifactendf = Xartifact2f(1) +Xartifactbegf-1;
nextf = Xartifactendf+1;
Xpeakf = find((abs(gradfilt(nextf:tracelength))) >= 0.2);
Xpeakbegf = Xpeakf(1)+ Xartifactendf;
meanmaxsf = islocalmax(meanfilteredtrace(:,2));
%% Voltage data for each trace
% z=[];
% qe=[];
% for i=1:numtraces
% z = (traces{i,1} (:,2));
% qe = horzcat(qe, z);
% end
%% Outliers
% nn = [];
% outliers = [];
%
% for j = 1:tracelength
% for i=1:numtraces
% y = traces{i,1} (j,:);
% d = y(:,2);
% nn = horzcat(nn,d);
% end
%
% TF = isoutlier(nn);
% nn = [];
% outliers = [outliers;TF];
% end
%% Find minima of each trace (Not filtered)
% minimas = Minimum(numtraces, traces, Xpeakbeg, Xpeakend);
%
% figure(5)
% plot(minimas(:,1),minimas(:,2), '.', 'MarkerSize', 20)
% grid on
% hold on
% Amplitude = mean(minimas);
%
% plot(Amplitude(:,1), Amplitude(:,2) , 'r.', 'MarkerSize', 20)
%% while loop to find beginning of peak
for i = Xpeakbeg:-1:1
if ymean(i-1) < ymean(i)
break
end
end
beg = i
Xpeakmaxf = find(meanmaxsf((Xpeakbeg+1):2000));
Xpeakendf = Xpeakmaxf(1) + Xpeakbeg
%% minimas with inverse loop method
% minimasW = Minimum(numtraces, traces, i, Xpeakendf);
%
% figure(6)
% plot(minimasW(:,1),minimasW(:,2), '.', 'MarkerSize', 20)
% grid on
% hold on
% AmplitudeW = mean(minimasW);
%
% plot(AmplitudeW(:,1), AmplitudeW(:,2) , 'r.', 'MarkerSize', 20)
%% Histograms of NCV & Amplitude
%
% figure(7)
% hi = histogram(minimasW(:,1))
% figure(8)
% h = histogram(minimasW(:,1), 'BinWidth', 0.00005)
%
% figure(9)
% AmplitudeHistogram = histogram(minimasW(:,2), 'BinWidth', 0.25)
%% Integral of first peak
xtest = xmean(beg:Xpeakendf);
ytest = ymean(beg:Xpeakendf);
figure(10)
plot(xmean, ymean)
axis([xmean(beg-50) xmean(Xpeakendf+50) -30 30])
hold on
area(xtest, ytest)
Area = trapz(xtest, abs(ytest))
%% Phase Difference