a b/featurebased-approach/subfunctions/residualfeats.m
1
function feats = residualfeats(signal,fs,qrs)
2
% This function derives features out of QRS cancelled ECG signals, aiming at the atrial information.
3
%
4
% --
5
% ECG classification from single-lead segments using Deep Convolutional Neural 
6
% Networks and Feature-Based Approaches - December 2017
7
% 
8
% Released under the GNU General Public License
9
%
10
% Copyright (C) 2017  Fernando Andreotti, Oliver Carr
11
% University of Oxford, Insitute of Biomedical Engineering, CIBIM Lab - Oxford 2017
12
% fernando.andreotti@eng.ox.ac.uk
13
%
14
% 
15
% For more information visit: https://github.com/fernandoandreotti/cinc-challenge2017
16
% 
17
% Referencing this work
18
%
19
% Andreotti, F., Carr, O., Pimentel, M.A.F., Mahdi, A., & De Vos, M. (2017). 
20
% Comparing Feature Based Classifiers and Convolutional Neural Networks to Detect 
21
% Arrhythmia from Short Segments of ECG. In Computing in Cardiology. Rennes (France).
22
%
23
% Last updated : December 2017
24
% 
25
% This program is free software: you can redistribute it and/or modify
26
% it under the terms of the GNU General Public License as published by
27
% the Free Software Foundation, either version 3 of the License, or
28
% (at your option) any later version.
29
% 
30
% This program is distributed in the hope that it will be useful,
31
% but WITHOUT ANY WARRANTY; without even the implied warranty of
32
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
% GNU General Public License for more details.
34
% 
35
% You should have received a copy of the GNU General Public License
36
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
37
38
feats = zeros(1,5);
39
signal = signal';
40
41
if length(qrs) > 5
42
    residual = FECGSYN_ts_extraction(qrs,signal,'TS-CERUTTI',0,5,2,fs);
43
else
44
    return
45
end
46
47
try
48
    % Cross-correlation
49
    R=corrcoef(signal,residual);
50
    feats(1) = 1 - R(1,2);
51
    
52
    % Spectral coherence
53
    [Pmf,f]=mscohere(signal,residual,[],[],1024,fs);
54
    feats(2)=1-mean(Pmf(f>3&f<30));
55
    
56
    feats(3) = psqi(residual',fs);
57
    
58
    % Fundamental frequency on spectrum against rest
59
    feats(4)=mpsqi1(residual',fs);
60
    
61
    % Test for randomness
62
    [h,p,R] = white_test(residual', 1, rand(1e3,1));
63
    feats(5) = 1-p;
64
catch
65
    disp('Residuals failed!')
66
end
67
68
end
69
70
function [h, p_value, R] = white_test(x, maxlag, alpha)
71
% tests against the null hyptothesis of whiteness
72
% see http://dsp.stackexchange.com/questions/7678/determining-the-whiteness-of-noise for details
73
% demo:
74
% % white:
75
% >> [h,p,R]=white_test(((filter([1], [1], rand(1e3,1)))))
76
% >> h = 0, p =1, R = 455
77
% % non white:
78
% >> [h,p,R]=white_test(((filter([1 .3], [.4 0.3], rand(1e3,1)))))
79
% >> h = 1, p = 0, R = 2e3
80
%
81
% Copyright (c) 2013, Hanan Shteingart
82
% All rights reserved.
83
%
84
% Redistribution and use in source and binary forms, with or without
85
% modification, are permitted provided that the following conditions are
86
% met:
87
%
88
% * Redistributions of source code must retain the above copyright
89
% notice, this list of conditions and the following disclaimer.
90
% * Redistributions in binary form must reproduce the above copyright
91
% notice, this list of conditions and the following disclaimer in
92
% the documentation and/or other materials provided with the distribution
93
%
94
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
95
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
98
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
99
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
100
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
101
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
102
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
103
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
104
% POSSIBILITY OF SUCH DAMAGE.
105
106
N = length(x);
107
x = x-mean(x);
108
if ~exist('m','var')
109
    maxlag = N-1;
110
end
111
if ~exist('alpha','var')
112
    alpha = 0.05;
113
end
114
[r, lag] = xcorr(x, maxlag, 'biased');
115
R = N/r(lag==0)^2*sum(r(lag > 0).^2);
116
p_value = 1-chi2cdf(R, maxlag);
117
T = chi2inv(1-alpha, maxlag);
118
h =  R > T;
119
end