-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemoSweepDwtPre.m
188 lines (154 loc) · 5.84 KB
/
demoSweepDwtPre.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
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Test sparsity vs time vs M
% Pre-processing data through
% % % % % % % % % % % % % % % % % % % % % % % % % % %
start_spams
clear
clc
load ./Results/sweeplambda_PreDWTbatchsize50.mat
mdivision = 20;
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Prepare raw data
% % % % % % % % % % % % % % % % % % % % % % % % % % %
RawInpLoad = load('15814m_ltdbECG_1h.mat');
RawInpLoad = RawInpLoad.val;
n_dl = 128;
m_dl = 51;
epochs = floor(length(RawInpLoad) / n_dl); % 4517
RawInpLoad = RawInpLoad(1:n_dl * epochs);
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Prepare training and testing data
% % % % % % % % % % % % % % % % % % % % % % % % % % %
batchsize = 50;
atoms = 512;
RawInp = RawInpLoad(1:n_dl*epochs);
RawInp = reshape(RawInp , n_dl, epochs);
crossValidFactor = 0.7;
indexD = randperm(atoms);
initD = RawInp(:, indexD);
initD = initD - repmat(mean(initD),[size(initD,1),1]);
initD = initD ./ repmat(sqrt(sum(initD.^2)),[size(initD,1),1]);
RawInp = RawInp(:,atoms+1:end);
epochs = epochs - atoms;
TrainInp = RawInp(:, 1 : floor(epochs*crossValidFactor));
wt = haarmtx(n_dl);
TrainInpDWT = wt * TrainInp;
TrainInpDWT = TrainInpDWT - repmat(mean(TrainInpDWT),[size(TrainInpDWT,1),1]);
TrainInpDWT = TrainInpDWT ./ repmat(sqrt(sum(TrainInpDWT.^2)),[size(TrainInpDWT,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]);
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Compressive sensing
% % % % % % % % % % % % % % % % % % % % % % % % % % %
samplesTrain = size(TrainInpDWT,2);
samplesTest = size(TestInp,2);
sweepParam = [1e-4, 1e-3, 1e-2, 1e-1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];
rsnr_dl = zeros(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
cr_dl = zeros(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
prd_dl = zeros(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
sparsity_dl = zeros(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% basis = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% R1 = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% R2 = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% alpha = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% spCoeff = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
% reconSig = cell(length(sweepParam),mdivision,length(1:floor(samplesTrain / batchsize)));
%%
% poolobj = gcp('nocreate'); % If no pool, do not create new one.
% if isempty(poolobj)
% poolsize = 0;
% parpool('local',20);
% else
% poolsize = poolobj.NumWorkers;
% end
%%
for k = 1 : length(sweepParam)
for i = 1 : mdivision
m_dl = floor(i * n_dl / mdivision);
phi_dl = randn(m_dl,n_dl);
parfor j = 1 : floor(samplesTrain / batchsize) % adjust iter
param = struct;
param.iter = j;
param.batchsize = batchsize;
param.K = atoms;
param.lambda = sweepParam(k);
param.numThreads = -1;
param.verbose = false;
param.iter_updateD = 1;
param.D = initD;
res = 0;
x2 = 0;
spar = 0;
y_dl = [];
xs_dl = [];
x0_dl = [];
xhat_dl = [];
D = [];
epochesD = floor(j * param.batchsize);
X = TrainInpDWT(:,1:epochesD);
D = mexTrainDL(X,param);
% coef = mexLasso(X,D,param);
% alpha{i,j} = coef;
% R1{i,j} = mean(0.5*sum((X-D*coef).^2) + param.lambda*sum(abs(coef)));
% R2{i,j} = mean(0.5*sum(X-D*alpha{i,j}).^2);
% fprintf('Objective function for i=%d, j=%d is %f', i, j, R1{i,j});
% basis(i, j) = {D};
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, normErr(k,j));
xhat_dl = psi_dl * xs_dl;
% spCoeff{k,i,j}(:,ep) = {xs_dl};
% reconSig{k,i,j}(:,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(k,i,j) = 20 * log10(sqrt(x2 / res));
cr_dl(k,i,j) = n_dl / m_dl;
sparsity_dl(k,i,j) = 1 - spar / samplesTest / length(xs_dl);
prd_dl(k,i,j) = sqrt(res / x2);
end
end
end
delete(poolobj)
filename = sprintf('./Results/sweeplambda_preDWT_m%d_batchsize%d.mat', mdivision, batchsize);
save(filename,'-v7.3');
% subplot(211)
% plot(TestInp(:,ep));
% subplot(212)
% plot(xres_dl);
%% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Plot reconstruction process
% % % % % % % % % % % % % % % % % % % % % % % % % % %
%
% delay = 1;
% epSel = 1000;
% writerObj = VideoWriter('./Results/reconstruction.avi');
% writerObj.FrameRate = 5;
% open(writerObj);
% fig = figure('units','normalized','outerposition',[0 0 1 1]);
% plot(TestInp(:,epSel));
% axis([1 n_dl -0.3 0.3]);
% hold on
%
% for i = 1 : floor(samplesTrain / 50)
% reconSigMat = cell2mat(reconSig{5,i}(:,epSel));
%
% h = plot(1:n_dl,reconSigMat);
% axis([1 n_dl -0.3 0.3]);
% % hold on
% frame = getframe(fig);
% writeVideo(writerObj,frame);
% pause(delay);
% delete(h);
% end
%
% plot(1:n_dl,reconSigMat);
% close(writerObj);
%% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Plot results
% % % % % % % % % % % % % % % % % % % % % % % % % % %