-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictTrainComp.m
152 lines (119 loc) · 3.8 KB
/
dictTrainComp.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
start_spams
clear
clc
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Prepare raw data
% % % % % % % % % % % % % % % % % % % % % % % % % % %
RawInpLoad = load('15814m_ltdbECG_1h.mat');
RawInpLoad = RawInpLoad.val;
n_dl = 128;
epochs = floor(length(RawInpLoad) / n_dl); % 4517
RawInpLoad = RawInpLoad(1:n_dl * epochs);
RawInp = RawInpLoad(1:n_dl*epochs);
RawInp = reshape(RawInp , n_dl, epochs);
crossValidFactor = 0.7;
TrainInp = RawInp(:, 1:floor(epochs*crossValidFactor));
TrainInp = TrainInp - repmat(mean(TrainInp),[size(TrainInp,1),1]);
TrainInp = TrainInp ./ repmat(sqrt(sum(TrainInp.^2)),[size(TrainInp,1),1]);
TestInp = RawInp(:, (size(TrainInp,2)+1):epochs);
TestInp = TestInp - repmat(mean(TestInp),[size(TestInp,1),1]);
TestInp = TestInp ./ repmat(sqrt(sum(TestInp.^2)),[size(TestInp,1),1]);
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Setting parameters for training
% % % % % % % % % % % % % % % % % % % % % % % % % % %
param.K = 1024;
param.lambda = 0.1; % sparsity constraint
param.numThreads = -1;
param.batchsize = 50;
param.verbose = false;
param.batchsize = 50;
param.iter = 50;
param.clean = false;
param.iter_updateD = 1;
% param.verbose = true;
param_cs.lambda = 0.1;
param_cs.mode = 1;
param_cs.numThreads = -1;
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% DL & CS
% % % % % % % % % % % % % % % % % % % % % % % % % % %
m_dl = floor(n_dl / 5);
phi_dl = randn(m_dl,n_dl);
reconSig = cell(1,50);
alpha = cell(1,50);
R1 = cell(1,50);
R2 = cell(1,50);
R3 = cell(1,50);
samplesTrain = size(TrainInp,2);
samplesTest = size(TestInp,2);
rsnr_dl = zeros(1,length(1:floor(samplesTrain /50)));
res_dl = zeros(1,length(1:floor(samplesTrain /50)));
sparsity_dl = zeros(1,length(1:floor(samplesTrain /50)));
prd_dl = zeros(1,length(1:floor(samplesTrain /50)));
for i = 50 : floor(samplesTrain / 50) % adjust iter
res = 0;
x2 = 0;
spar = 0;
prd = 0;
y_dl = [];
xs_dl = [];
x0_dl = [];
xhat_dl = [];
param.iter = i;
epochesD = floor(i * param.batchsize);
X = TrainInp(:,1:epochesD);
D = mexTrainDL(X,param);
alpha{i} = mexLasso(X,D,param);
R1{i} = mean(0.5*sum((X-D*alpha{i}).^2) + param.lambda*sum(abs(alpha{i})));
R2{i} = mean(0.5*sum(X-D*alpha{i}).^2);
R3{i} = mean(param.lambda*sum(abs(alpha{i})));
fprintf('Objective function for i=%d is %f\n', i, R1{i});
psi_dl = D;
A_dl = phi_dl * psi_dl;
for ep = 1:samplesTest
y_dl = phi_dl * TestInp(:,ep);
x0_dl = pinv(A_dl) * y_dl;
xs_dl = l1eq_pd(x0_dl, A_dl, [], y_dl, 5e-7, 50);
% xs_dl = mexLasso(y_dl,A_dl,param_cs);
xhat_dl = psi_dl * xs_dl;
subplot(211)
plot(TestInp(:,ep));
subplot(212)
plot(xhat_dl);
reconSig{i}(:,ep) = {xhat_dl};
res = res + sum(norm(TestInp(:,ep) - xhat_dl).^2);
x2 = x2 + sum(TestInp(:,ep).^2);
spar = spar + length(find(abs(xs_dl)>0.001) );
end
rsnr_dl(i) = 20 * log10(sqrt(x2 / res));
cr_dl = n_dl / m_dl;
sparsity_dl(i) = 1 - spar / samplesTest / length(xs_dl);
prd_dl(i) = sqrt(res / x2);
end
% subplot(211)
% plot(TestInp(:,ep));
% subplot(212)
% plot(xhat_dl);
%
filename = sprintf('./Results/reconProcess_batchsize%d_lambda%d.mat', param.batchsize, param.lambda);
save(filename)
%%
delay = 0.1;
writerObj = VideoWriter('./Results/reconstruction.avi');
writerObj.FrameRate = 5;
open(writerObj);
fig = figure('units','normalized','outerposition',[0 0 1 1]);
plot(TestInp(:,1));
axis([1 n_dl -0.3 0.2]);
hold on
for i = 1 : floor(samplesTrain / 50)
reconSigMat = cell2mat(reconSig{i}(:,20));
h = plot(1:n_dl,reconSigMat);
axis([1 n_dl -0.3 0.2]);
% hold on
frame = getframe(fig);
writeVideo(writerObj,frame);
pause(delay);
delete(h);
end
close(writerObj);