-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrain.m
137 lines (88 loc) · 4.59 KB
/
train.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
function train
%% data loading and pre-processing
load('Training_dataset.mat');
% summary(Labels)
% segment samples
[Signals,Labels] = segSampl(Signals,Labels);
% splitting into classes
afibX = Signals(Labels=='A');
afibY = Labels(Labels=='A');
normalX = Signals(Labels=='N');
normalY = Labels(Labels=='N');
OtX = Signals(Labels=='O');
OtY = Labels(Labels=='O');
NsX = Signals(Labels=='~');
NsY = Labels(Labels=='~');
% 80-20 Train-test split
[trainIndA,~,testIndA] = dividerand(717,0.8,0.0,0.2);
[trainIndN,~,testIndN] = dividerand(4929,0.8,0.0,0.2);
[trainIndO,~,testIndO] = dividerand(2597,0.8,0.0,0.2);
[trainIndNs,~,testIndNs] = dividerand(151,0.8,0.0,0.2);
XTrainA = afibX(trainIndA);
YTrainA = afibY(trainIndA);
XTrainN = normalX(trainIndN);
YTrainN = normalY(trainIndN);
XTestA = afibX(testIndA);
YTestA = afibY(testIndA);
XTestN = normalX(testIndN);
YTestN = normalY(testIndN);
XTrainO = OtX(trainIndO);
YTrainO = OtY(trainIndO);
XTestO = OtX(testIndO);
YTestO = OtY(testIndO);
XTrainNs = NsX(trainIndNs);
YTrainNs = NsY(trainIndNs);
XTestNs = NsX(testIndNs);
YTestNs = NsY(testIndNs);
%% data augmentation
XTrain = [repmat(XTrainA(1:560),7,1); repmat(XTrainO(1:1960),2,1); repmat(XTrainNs(1:112),35,1); XTrainN(1:3920)];
YTrain = [repmat(YTrainA(1:560),7,1); repmat(YTrainO(1:1960),2,1); repmat(YTrainNs(1:112),35,1); YTrainN(1:3920)];
XTest = [repmat(XTestA(1:140),7,1); repmat(XTestO(1:490),2,1); repmat(XTestNs(1:28),35,1); XTestN(1:980)];
YTest = [repmat(YTestA(1:140),7,1);repmat(YTestO(1:490),2,1); repmat(YTestNs(1:28),35,1); YTestN(1:980)];
%% feature extraction - wavelet, rr, spectralprop
% wavelet decomposition
count = 1;
wavedecTrain1 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest1 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
count = count+1;
wavedecTrain2 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest2 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
count = count+1;
wavedecTrain3 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest3 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
count = count+1;
wavedecTrain4 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest4 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
count = count+1;
wavedecTrain5 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest5 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
count = count+1;
wavedecTrain6 = cellfun(@(x)waveletdecomposition(x,count),XTrain,'UniformOutput',false);
wavedecTest6 = cellfun(@(x)waveletdecomposition(x,count),XTest,'UniformOutput',false);
%RR interval features
count = 1;
RRTrain1 = cellfun(@(x)RRfeatures(x,count),XTrain,'UniformOutput',false);
RRTest1 = cellfun(@(x)RRfeatures(x,count),XTest,'UniformOutput',false);
count = count+1;
RRTrain2 = cellfun(@(x)RRfeatures(x,count),XTrain,'UniformOutput',false);
RRTest2 = cellfun(@(x)RRfeatures(x,count),XTest,'UniformOutput',false);
count = count+1;
RRTrain3 = cellfun(@(x)RRfeatures(x,count),XTrain,'UniformOutput',false);
RRTest3 = cellfun(@(x)RRfeatures(x,count),XTest,'UniformOutput',false);
%spectral features
count = 1;
spectTrain1 = cellfun(@(x)spectfeatures(x,count),XTrain,'UniformOutput',false);
spectTest1 = cellfun(@(x)spectfeatures(x,count),XTest,'UniformOutput',false);
count = count+1;
spectTrain2 = cellfun(@(x)spectfeatures(x,count),XTrain,'UniformOutput',false);
spectTest2 = cellfun(@(x)spectfeatures(x,count),XTest,'UniformOutput',false);
%concatenate features
XTrain2 = cellfun(@(x,y,z,w,a,b,c,d,m,n,k)[x;y;z;w;a;b;c;d;m;n;k],wavedecTrain1,wavedecTrain2,wavedecTrain3,wavedecTrain4,wavedecTrain5,wavedecTrain6,RRTrain1,RRTrain2,RRTrain3,spectTrain1,spectTrain2,'UniformOutput',false);
XTest2 = cellfun(@(x,y,z,w,a,b,c,d,m,n,k)[x;y;z;w;a;b;c;d;m;n;k],wavedecTest1,wavedecTest2,wavedecTest3,wavedecTest4,wavedecTest5,wavedecTest6,RRTest1,RRTest2,RRTest3,spectTest1,spectTest2,'UniformOutput',false);
%% standardization
XTrain_SD = cellfun(@(x)normalize(x,'zscore'),XTrain2,'UniformOutput',false);
XTest_SD = cellfun(@(x)normalize(x,'zscore'),XTest2,'UniformOutput',false);
%% training and validation of deep learning models
lstm_model(XTrain,YTrain,XTest_SD,YTest,150,128) %lstm
bilstm_model(XTrain_SD,YTrain,XTest_SD,YTest,200,128) %bilstm
gru_model(XTrain,YTrain,XTest_SD,YTest,200,128) %gru