-
Notifications
You must be signed in to change notification settings - Fork 6
/
ECG12.m
296 lines (254 loc) · 14.7 KB
/
ECG12.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BRAVEHEART - Open source software for electrocardiographic and vectorcardiographic analysis
% ECG12.m -- ECG Object Class
% Copyright 2016-2024 Hans F. Stabenau and Jonathan W. Waks
%
% Source code/executables: https://github.com/BIVectors/BRAVEHEART
% Contact: braveheart.ecg@gmail.com
%
% BRAVEHEART is free software: you can redistribute it and/or modify it under the terms of the GNU
% General Public License as published by the Free Software Foundation, either version 3 of the License,
% or (at your option) any later version.
%
% BRAVEHEART is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
% without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along with this program.
% If not, see <https://www.gnu.org/licenses/>.
%
% This software is for research purposes only and is not intended to diagnose or treat any disease.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
classdef ECG12
% The ECG12 constructor reads a digitized 12-lead ECG from a file
% If sample values and no metadata are supplied, then the frequency and units must be inferred or otherwise provided
% An instance can return a filtered version of itself and construct a median beat
properties (SetAccess=private)
hz
units
I
II
III
avF
avL
avR
V1
V2
V3
V4
V5
V6
end
methods
function obj = ECG12(varargin)
if nargin == 0; return; end
if nargin == 14
[obj.hz, obj.units, ...
obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = varargin{:};
elseif nargin == 10
[obj.hz, obj.units, ...
obj.I, obj.II, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = varargin{:};
obj.III = -obj.I + obj.II;
obj.avF = obj.II - 0.5*obj.I;
obj.avR = -0.5*obj.I - 0.5*obj.II;
obj.avL = obj.I - 0.5*obj.II;
elseif nargin == 2
[filename, format] = varargin{:};
obj.units='mV';
% ADD NEW ECG FORMATS TO THIS SWITCH STATEMENT
switch format
case 'bidmc_format'
obj.hz=500;
unitspermv=200;
[obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_ecg(filename, unitspermv, format);
case 'muse_xml'
[obj.hz, obj.I, obj.II, obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = ...
load_musexml(filename);
obj.III = -obj.I + obj.II;
obj.avF = obj.II - 0.5*obj.I;
obj.avR = -0.5*obj.I - 0.5*obj.II;
obj.avL = obj.I - 0.5*obj.II;
case 'prucka_format'
obj.hz=997;
unitspermv=1;
[obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_ecg(filename, unitspermv, format);
case 'unformatted'
unitspermv=1;
[obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6, obj.hz] = load_unformatted(filename);
case 'philips_xml'
[obj.hz, obj.I, obj.II, obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = ...
load_philipsxml(filename);
obj.III = -obj.I + obj.II;
obj.avF = obj.II - 0.5*obj.I;
obj.avR = -0.5*obj.I - 0.5*obj.II;
obj.avL = obj.I - 0.5*obj.II;
case 'ISHNE'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_ISHNE(filename);
case 'mrq_ascii'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_mrq(filename);
case 'DICOM'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_dicom(filename);
case 'hl7_xml'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_hl7xml(filename);
case 'generic_csv'
% Pull formatting details out of generic_csv_params.csv
% This way can edit frequency, unitspermv, and
% orientation if running via MATLAB or compiled version
[obj.hz, unitspermv, orientation] = read_generic_csv_params();
[obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_generic_csv(filename, unitspermv, orientation);
case 'cardiosoft_xml'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_cardiosoftxml(filename);
case 'schiller_xml'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_schillerxml(filename);
case 'scp_ecg'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_scpecg(filename);
case 'edf'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_edf(filename);
case 'claris'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_claris(filename);
case 'physionet_csv'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_physionet_csv(filename);
case 'physionet_dat'
[obj.hz, obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, ...
obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6] = load_physionet_dat(filename);
otherwise
error('unknown format %s', format);
end
else
error('ECG12: invalid number of arguments %d', nargin);
end
% Error handeling if file doesn't parse correctly
fn = fieldnames(ECG12());
for i = 3:length(fn)
if isempty(obj.(fn{i}))
error(sprintf('%s seems to be incorrect - could not find ECG data in file',format));
end
end
if isrow(obj.I); obj.I = obj.I'; end
if isrow(obj.II); obj.II = obj.II'; end
if isrow(obj.III); obj.III = obj.III'; end
if isrow(obj.avL); obj.avL = obj.avL'; end
if isrow(obj.avF); obj.avF = obj.avF'; end
if isrow(obj.avR); obj.avR = obj.avR'; end
if isrow(obj.V1); obj.V1 = obj.V1'; end
if isrow(obj.V2); obj.V2 = obj.V2'; end
if isrow(obj.V3); obj.V3 = obj.V3'; end
if isrow(obj.V4); obj.V4 = obj.V4'; end
if isrow(obj.V5); obj.V5 = obj.V5'; end
if isrow(obj.V6); obj.V6 = obj.V6'; end
end
function [obj, highpass_lvl_min] = filter(obj, maxRR_hr, aps)
% maxRR_hr is needed for auto HPF level calculation
% highpass_lvl_min is level chosen by auto (if used)
% Check that aps.wavelet_level_highpass is not > max level = floor(log2(num_samples))
if aps.wavelet_level_highpass > floor(log2(length(obj.I)))
error('Error! Max level of wavelet decomposition for a signal of length %i is %i - you chose %i.', ...
length(obj.I), floor(log2(length(obj.I))), aps.wavelet_level_highpass);
end
[obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6, highpass_lvl_min] = ...
ecgfilter(obj.I, obj.II, obj.III, obj.avR, obj.avF, obj.avL, obj.V1, obj.V2, obj.V3, obj.V4, obj.V5, obj.V6, ...
obj.hz, maxRR_hr, ...
aps.lowpass, aps.wavelet_level_lowpass, aps.wavelet_name_lowpass, ...
aps.highpass, aps.wavelet_level_highpass, aps.wavelet_name_highpass);
if isrow(obj.I); obj.I = obj.I'; end
if isrow(obj.II); obj.II = obj.II'; end
if isrow(obj.III); obj.III = obj.III'; end
if isrow(obj.avL); obj.avL = obj.avL'; end
if isrow(obj.avF); obj.avF = obj.avF'; end
if isrow(obj.avR); obj.avR = obj.avR'; end
if isrow(obj.V1); obj.V1 = obj.V1'; end
if isrow(obj.V2); obj.V2 = obj.V2'; end
if isrow(obj.V3); obj.V3 = obj.V3'; end
if isrow(obj.V4); obj.V4 = obj.V4'; end
if isrow(obj.V5); obj.V5 = obj.V5'; end
if isrow(obj.V6); obj.V6 = obj.V6'; end
end
function c = write(obj, file, format)
[fid, err] = fopen(file, 'w');
if fid == -1; error('opening %s for writing: %s', file, err); end
switch format % Note: doesn't account for differences in gain between input and output formats
case 'bidmc_format' %%% BIDMC txt file
u = 200.0;
c = fprintf(fid, '%d %d %d %d %d %d %d %d %d %d %d %d %d\n', round([ (1:length(obj.I))' ...
u*obj.I u*obj.II u*obj.III u*obj.avR u*obj.avF u*obj.avL ...
u*obj.V1 u*obj.V2 u*obj.V3 u*obj.V4 u*obj.V5 u*obj.V6 ])');
case 'prucka_format'
u = 1;
c = fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f\n', ...
[u*obj.I u*obj.II u*obj.III u*obj.avR u*obj.avF u*obj.avL ...
u*obj.V1 u*obj.V2 u*obj.V3 u*obj.V4 u*obj.V5 u*obj.V6]');
case 'unformatted'
u = 1;
c = fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f\n', ...
[[obj.hz; u*obj.I] [obj.hz; u*obj.II] [obj.hz; u*obj.III] ...
[obj.hz; u*obj.avR] [obj.hz; u*obj.avF] [obj.hz; u*obj.avL] ...
[obj.hz; u*obj.V1] [obj.hz; u*obj.V2] [obj.hz; u*obj.V3] ...
[obj.hz; u*obj.V4] [obj.hz; u*obj.V5] [obj.hz; u*obj.V6]]');
% case 'muse_xml' % Not ready yet
% u = 205; % 4.88 uV resolution
% xml = write_muse_xml_file(obj);
% fprintf(fid, xml);
otherwise
error('ECG12.write(): unknown format %s', format);
end
fclose(fid);
end
function [medbeat_12L, beatsig_ecg_12L] = medianbeat(obj, startb, endb) % not sure how to return or store beatsig atm
[medbeat_I, beatsig_I] = medianbeat(obj.I, startb, endb);
[medbeat_II, beatsig_II] = medianbeat(obj.II, startb, endb);
[medbeat_III, beatsig_III] = medianbeat(obj.III, startb, endb);
[medbeat_avR, beatsig_avR] = medianbeat(obj.avR, startb, endb);
[medbeat_avF, beatsig_avF] = medianbeat(obj.avF, startb, endb);
[medbeat_avL, beatsig_avL] = medianbeat(obj.avL, startb, endb);
[medbeat_V1, beatsig_V1] = medianbeat(obj.V1, startb, endb);
[medbeat_V2, beatsig_V2] = medianbeat(obj.V2, startb, endb);
[medbeat_V3, beatsig_V3] = medianbeat(obj.V3, startb, endb);
[medbeat_V4, beatsig_V4] = medianbeat(obj.V4, startb, endb);
[medbeat_V5, beatsig_V5] = medianbeat(obj.V5, startb, endb);
[medbeat_V6, beatsig_V6] = medianbeat(obj.V6, startb, endb);
medbeat_12L = ECG12(obj.hz, obj.units, medbeat_I, medbeat_II, medbeat_III, ...
medbeat_avR, medbeat_avF, medbeat_avL, ...
medbeat_V1, medbeat_V2, medbeat_V3, medbeat_V4, medbeat_V5, medbeat_V6);
beatsig_ecg_12L = ECG12(obj.hz, obj.units, beatsig_I, beatsig_II, beatsig_III, ...
beatsig_avR, beatsig_avF, beatsig_avL, ...
beatsig_V1, beatsig_V2, beatsig_V3, beatsig_V4, beatsig_V5, beatsig_V6);
end
function [ecg1, ecg2] = split(obj, idx)
ecg1 = ECG12();
ecg2 = ECG12();
ecg1.I = obj.I(1:idx); ecg2.I = obj.I(idx+1:end);
ecg1.II = obj.II(1:idx); ecg2.II = obj.II(idx+1:end);
ecg1.III = obj.III(1:idx); ecg2.III = obj.III(idx+1:end);
ecg1.avF = obj.avF(1:idx); ecg2.avF = obj.avF(idx+1:end);
ecg1.avL = obj.avL(1:idx); ecg2.avL = obj.avL(idx+1:end);
ecg1.avR = obj.avR(1:idx); ecg2.avR = obj.avR(idx+1:end);
ecg1.V1 = obj.V1(1:idx); ecg2.V1 = obj.V1(idx+1:end);
ecg1.V2 = obj.V2(1:idx); ecg2.V2 = obj.V2(idx+1:end);
ecg1.V3 = obj.V3(1:idx); ecg2.V3 = obj.V3(idx+1:end);
ecg1.V4 = obj.V4(1:idx); ecg2.V4 = obj.V4(idx+1:end);
ecg1.V5 = obj.V5(1:idx); ecg2.V5 = obj.V5(idx+1:end);
ecg1.V6 = obj.V6(1:idx); ecg2.V6 = obj.V6(idx+1:end);
ecg1.hz = obj.hz; ecg2.hz = obj.hz;
ecg1.units = obj.units; ecg2.units = obj.units;
end
function t = sample_time(obj); t = 1000/obj.hz; end
function l = length(obj); l = length(obj.I); end
% default copy, destructor, assignment
end
end