-
Notifications
You must be signed in to change notification settings - Fork 58
/
Main_HRV_Analysis.m
329 lines (293 loc) · 13.2 KB
/
Main_HRV_Analysis.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function [HRVout, ResultsFileName ] = Main_HRV_Analysis(InputSig,t,InputFormat,HRVparams,subID,ann,sqi,varargin)
% ====== HRV Toolbox for PhysioNet Cardiovascular Signal Toolbox =========
%
% Main_HRV_Analysis(InputSig,t,InputFormat,HRVparams,subID,ann,sqi,varargin)
% OVERVIEW:
% Main "HRV Toolbox for PhysioNet Cardiovascular Signal Toolbox"
% Configured to accept RR intervals as well as raw data as input file
%
% INPUT:
% InputSig - (seconds) Vector containing RR intervals data
% or ECG/PPG waveform
% t - (seconds) Time of the rr interval data or
% leave empty for ECG/PPG input
% InputFormat - String that specifiy if the input vector is:
% 'RRIntervals' for RR interval data
% 'ECGWaveform' for ECG waveform
% 'PPGWaveform' for PPG signal
% HRVparams - struct of settings for hrv_toolbox analysis that can
% be obtained using InitializeHRVparams.m function
% HRVparams = InitializeHRVparams();
%
%
% OPTIONAL INPUTS:
% subID - (optional) string to identify current subject
% ann - (optional) annotations of the RR data at each point
% indicating the type of the beat
% sqi - (optional) Signal Quality Index; Requires a
% matrix with at least two columns. Column 1
% should be timestamps of each sqi measure, and
% Column 2 should be SQI on a scale from 0 to 1.
% Use InputSig, Type pairs for additional signals such as ABP
% or PPG signal. The input signal must be a vector containing
% signal waveform and the Type: 'ABP' and\or 'PPG'.
%
%
% OUTPUS:
% results - HRV time and frequency domain metrics as well
% as AC and DC, SDANN and SDNNi
% ResultsFileName - Name of the file containing the results
%
% NOTE: before running this script review and modifiy the parameters
% in "initialize_HRVparams.m" file accordingly with the specific
% of the new project (see the readme.txt file for further details)
%
% EXAMPLES
% - rr interval input
% Main_HRV_Analysis(RR,t,'RRIntervals',HRVparams)
% - ECG waveform input
% Main_HRV_Analysis(ECGsig,[],'ECGWaveform',HRVparams,'101')
% - ECG waveform and also ABP and PPG waveforms
% Main_HRV_Analysis(ECGsig,[],'ECGWaveform',HRVparams,[],[],[], abpSig,
% 'ABP', ppgSig, 'PPG')
%
% DEPENDENCIES & LIBRARIES:
% HRV Toolbox for PhysioNet Cardiovascular Signal Toolbox
% https://github.com/cliffordlab/PhysioNet-Cardiovascular-Signal-Toolbox
%
% REFERENCE:
% Vest et al. "An Open Source Benchmarked HRV Toolbox for Cardiovascular
% Waveform and Interval Analysis" Physiological Measurement (In Press), 2018.
%
% REPO:
% https://github.com/cliffordlab/PhysioNet-Cardiovascular-Signal-Toolbox
% ORIGINAL SOURCE AND AUTHORS:
% This script written by Giulia Da Poian
% Dependent scripts written by various authors
% (see functions for details)
% COPYRIGHT (C) 2018
% LICENSE:
% This software is offered freely and without warranty under
% the GNU (v3 or later) public license. See license file for
% more information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
if nargin < 4
error('Wrong number of input arguments')
end
if nargin < 5
subID = '0000';
end
if nargin < 6
ann = [];
end
if nargin < 7
sqi = [];
end
if length(varargin) == 1 || length(varargin) == 3
error('Incomplete Signal-Type pair')
elseif length(varargin) == 2
extraSigType = varargin(2);
extraSig = varargin{1};
elseif length(varargin) == 4
extraSigType = [varargin(2) varargin(4)];
extraSig = [varargin{1} varargin{3}];
end
if isa(subID,'cell'); subID = string(subID); end
error_flag = [];
% Start HRV analysis
try
switch InputFormat
case 'ECGWaveform'
% Convert ECG waveform in rr intervals
[t, rr, jqrs_ann, SQIvalue , tSQI] = ConvertRawDataToRRIntervals(InputSig, HRVparams, subID);
sqi = [tSQI', SQIvalue'];
case 'PPGWaveform'
[rr,t,sqi] = Analyze_ABP_PPG_Waveforms(InputSig,{'PPG'},HRVparams,[],subID);
case 'RRIntervals'
rr = InputSig;
if isempty(t)
fprintf('\n*** Error!***\nFor input type "RRIntervals" must provide \nvetcor with time of the rr intervals \n')
fprintf('***HRV analyis will not be perfomed!***\n')
fprintf('\n');
error_flag = 'Input Error: missing vetcor with time stamps of the rr interval';
end
otherwise
fprintf('\n*** Wrong Input Type!***\nThis function accepts: ECGWaveform, PPGWaveform or RRIntervals! \n')
fprintf('***No HRV analyis will be perfomed!***\n')
fprintf('\n');
error_flag = 'Input Error: wrong Input Type';
end
if t(end) < HRVparams.windowlength
fprintf('\n*** Warning!***\nThe signal is shorter than the analysis window length \n')
fprintf('***HRV analyis can fail!***\n')
fprintf('\n');
error_flag = 'Warning: input signal is shorter than the analysis window length';
end
% 1. Preprocess Data, AF detection, create Windows Indexes
error_flag = 'Data Preprocessing or AF detection failure';
[NN, tNN, tWin, AFWindows,out] = PreparDataForHRVAnlysis(rr,t,ann,sqi,HRVparams,subID);
error_flag = []; % clean error flag since preprocessing done
HRVout = [tWin' (tWin+HRVparams.windowlength)'];
HRVtitle = {'t_start' 't_end'};
% 3. Calculate time domain HRV metrics - Using HRV Toolbox for PhysioNet
% Cardiovascular Signal Toolbox Toolbox Functions
if HRVparams.timedomain.on
error_flag = 'Time Domain Analysis failure';
TimeMetrics = EvalTimeDomainHRVstats(NN,tNN,sqi,HRVparams,tWin);
% Export results
HRVout = [HRVout cell2mat(struct2cell(TimeMetrics))'];
HRVtitle = [HRVtitle fieldnames(TimeMetrics)'];
error_flag = []; % clean error flag since time domain analysis done
end
% 4. Frequency domain metrics (LF HF TotPow)
if HRVparams.freq.on
error_flag = 'Frequency Domain Analysis failure';
FreqMetrics = EvalFrequencyDomainHRVstats(NN,tNN,sqi,HRVparams,tWin);
% Export results
HRVout = [HRVout cell2mat(struct2cell(FreqMetrics))'];
HRVtitle = [HRVtitle fieldnames(FreqMetrics)'];
error_flag = []; % clean error flag since frequency domain analysis done
end
% 5. PRSA, AC and DC values
if HRVparams.prsa.on
error_flag = 'PRSA Analysis failure';
[ac,dc,~] = prsa(NN, tNN, HRVparams, sqi, tWin );
% Export results
HRVout = [HRVout, ac(:), dc(:)];
HRVtitle = [HRVtitle {'ac' 'dc'}];
error_flag = []; % clean error flag since PRSA analysis done
end
% 6.Poincare Features
if HRVparams.poincare.on
error_flag = 'Poincare Analysis failure';
[SD1, SD2, SD12Ratio] = EvalPoincareOnWindows(NN, tNN, HRVparams, tWin, sqi);
% Export results
HRVout = [HRVout, SD1(:),SD2(:),SD12Ratio(:)];
HRVtitle = [HRVtitle {'SD1', 'SD2', 'SD1SD2'}];
error_flag = []; % clean error flag since Poincare analysis done
end
% 7.Entropy Features
if HRVparams.Entropy.on
error_flag = 'Entropy Analysis failure';
m = HRVparams.Entropy.patternLength;
r = HRVparams.Entropy.RadiusOfSimilarity;
[SampEn, ApEn] = EvalEntropyMetrics(NN, tNN, m ,r, HRVparams, tWin, sqi);
% Export results
HRVout = [HRVout, SampEn(:),ApEn(:)];
HRVtitle = [HRVtitle {'SampEn', 'ApEn'}];
error_flag = []; % clean error flag since Entropy analysis done
end
% Generates Output - Never comment out
error_flag = 'Failure during output file generation';
ResultsFileName.HRV = SaveHRVoutput(subID,tWin,HRVout,HRVtitle, [],HRVparams, tNN, NN);
error_flag = []; % clean error flag
% 8. Multiscale Entropy (MSE)
if HRVparams.MSE.on
try
mse = EvalMSE(out.NN_gapFilled,out.tNN_gapFilled,sqi,HRVparams,out.tWinMSE);
catch
mse = NaN;
fid = fopen([HRVparams.writedata filesep 'AnalysisError.txt','a']);
fprintf(fid, 'MSE analysis error for subject %s \n',subID );
fclose(fid);
end
% Save Results for MSE
Scales = 1:HRVparams.MSE.maxCoarseGrainings;
HRVout = [Scales' mse];
for i=1:length(out.tWinMSE)
Windows{i} = strcat('t_', num2str(tWin(i)));
end
HRVtitle = {'Scales' Windows{:}};
ResultsFileName.MSE = SaveHRVoutput(subID,[],HRVout,HRVtitle, 'MSE', HRVparams, tNN, NN);
end
% 9. DetrendedFluctuation Analysis (DFA)
if HRVparams.DFA.on
try
[alpha1, alpha2] = EvalDFA(out.NN_gapFilled,out.tNN_gapFilled,sqi,HRVparams,out.tWinDFA);
% Save Results for DFA
HRVout = [out.tWinDFA' alpha1 alpha2];
HRVtitle = {'t_win' 'alpha1' 'alpha2'};
ResultsFileName.DFA = SaveHRVoutput(subID,[],HRVout,HRVtitle, 'DFA', HRVparams, tNN, NN);
catch
fid = fopen([HRVparams.writedata filesep 'AnalysisError.txt'],'a');
fprintf(fid, 'DFA analysis error for subject %s \n',subID );
fclose(fid);
end
end
% 10. Heart Rate Turbulence Analysis (HRT)
if HRVparams.HRT.on
try
% Create analysis windows from original rr intervals
tWinHRT = CreateWindowRRintervals(t, rr, HRVparams,'HRT');
[TO, TS, nPVCs] = Eval_HRT(rr,t,ann,sqi, HRVparams, tWinHRT);
% Save Results for DFA
HRVout = [tWinHRT' TO TS nPVCs];
HRVtitle = {'t_win' 'TO' 'TS' 'nPVCs'};
ResultsFileName.HRT = SaveHRVoutput(subID,[],HRVout,HRVtitle, 'HRT', HRVparams, t, rr);
catch
fid = fopen([HRVparams.writedata filesep 'AnalysisError.txt'],'a');
fprintf(fid, 'HRT analysis error for subject %s \n',subID );
fclose(fid);
end
end
% 11. Analyze additional waveform signals (ABP, PPG or both)
if ~isempty(varargin)
try
fprintf('Analyizing %s \n', extraSigType{:});
Analyze_ABP_PPG_Waveforms(extraSig,extraSigType,HRVparams,jqrs_ann,subID);
catch
fid = fopen([HRVparams.writedata filesep 'AnalysisError.txt'],'a');
fprintf(fid, 'ABP/PPG analysis error for subject %s \n',subID );
fclose(fid);
end
end
% 12. Some statistics on %ages windows removed (poor quality and AF)
% save on file
RemovedWindowsStats(tWin,AFWindows,HRVparams,subID);
fprintf('HRV Analysis completed for subject ID %s \n',subID);
fid = fopen([HRVparams.writedata filesep 'FileSuccessfullyAnalyzed.txt'],'a');
fprintf(fid, '%s \n',subID );
fclose(fid);
catch
% Write subjectID on log file
fid = fopen(strcat(HRVparams.writedata,filesep,'AnalysisError.txt'),'a');
HRVout = NaN;
ResultsFileName = '';
fprintf(fid, 'Basic HRV Analysis faild for subject: %s, %s \n', subID, error_flag);
fclose(fid);
end % end of HRV analysis
end %== function ================================================================
%
function [NN, tNN, tWin,AFWindows,out] = PreparDataForHRVAnlysis(rr,t,annotations,sqi,HRVparams,subjectID)
out = []; % Struct used to save DFA and MSE preprocessed data
% Exclude undesiderable data from RR series (i.e., arrhytmia, low SQI, ectopy, artefact, noise)
[NN, tNN] = RRIntervalPreprocess(rr,t,annotations, HRVparams);
tWin = CreateWindowRRintervals(tNN, NN, HRVparams); % Create Windows for Time and Frequency domain
% Create Windows for MSE and DFA and preprocess
if HRVparams.MSE.on || HRVparams.DFA.on
% Additional pre-processing to deal with missing data for MSE and DFA analysis
[out.NN_gapFilled, out.tNN_gapFilled] = RR_Preprocessing_for_MSE_DFA( NN, tNN );
end
if HRVparams.MSE.on
out.tWinMSE = CreateWindowRRintervals(out.tNN_gapFilled, out.NN_gapFilled, HRVparams,'mse');
end
if HRVparams.DFA.on
out.tWinDFA = CreateWindowRRintervals(out.tNN_gapFilled, out.NN_gapFilled, HRVparams,'dfa');
end
% 2. Atrial Fibrillation Detection
if HRVparams.af.on
[AFtest, AfAnalysisWindows] = PerformAFdetection(subjectID,t,rr,sqi,HRVparams);
fprintf('AF analysis completed for subject %s \n', subjectID);
% Remove RRAnalysisWindows contating AF segments
[tWin, AFWindows]= RemoveAFsegments(tWin,AfAnalysisWindows, AFtest,HRVparams);
if HRVparams.MSE.on
out.tWinMSE = RemoveAFsegments(out.tWinMSE,AfAnalysisWindows, AFtest,HRVparams);
end
if HRVparams.DFA.on
out.tWinDFA = RemoveAFsegments(out.tWinDFA,AfAnalysisWindows, AFtest,HRVparams);
end
else
AFWindows = [];
end
end