-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictDraw2.m
109 lines (89 loc) · 3.19 KB
/
dictDraw2.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
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Test signal dimension
% % % % % % % % % % % % % % % % % % % % % % % % % % %
clear
clc
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Prepare raw data
% % % % % % % % % % % % % % % % % % % % % % % % % % %
RawInpLoad = load('15814m_ltdbECG_1h.mat');
RawInpLoad = RawInpLoad.val;
n_dl = 102;
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 = RawInp;
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 = RawInp(:, (size(TrainInp,2)+1):size(TrainInp,2)*2);
% 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 = 512;
param.lambda = 10; % sparsity constraint
param.numThreads = -1;
param.batchsize = 400;
param.verbose = false;
param.iter = 10;
param.clean = false;
param2 = param;
% param.verbose = true;
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% Prepare training and testing data
% % % % % % % % % % % % % % % % % % % % % % % % % % %
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]);
basis = cell(1,floor(size(TrainInp,2) / param.batchsize));
X = TrainInp(:,1:param.batchsize);
[D,model] = mexTrainDL(X,param);
basis{1} = {D};
for i = 2 : floor(size(TrainInp,2) / param.batchsize)
X = TrainInp(:,(i-1)*param.batchsize+1:i*param.batchsize);
param2 = param;
param2.D = D;
[D,model] = mexTrainDL(X,param2,model);
basis{i} = {D};
coef = mexLasso(X,D,param2);
alpha = coef;
R = mean(0.5*sum((X-D*coef).^2) + param2.lambda*sum(abs(coef)));
end
%%
delay = 0.01;
numOfAtoms = 10;
randAtoms = randperm(param2.K);
writerObj = VideoWriter('./Results/DictEvo.avi');
writerObj.FrameRate = 5;
open(writerObj);
fig = figure('units','normalized','outerposition',[0 0 1 1 ]);
%%
% for i = 1 : param.iter
for i = 1 : floor(size(TrainInp,2) / param2.batchsize)
for j = 1 : numOfAtoms
h = subplot(2,5,j);
cla(h);
matBasis = cell2mat(basis{i});
plot(matBasis(:,randAtoms(j)));
title(['Atom: ',num2str(randAtoms(j))]);
xlabel('Time Samples')
ylabel('Normalized Amplitude');
axis([0 n_dl -0.5 0.5]);
frame = getframe(fig);
writeVideo(writerObj,frame);
pause(delay);
end
end
close(writerObj);