-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonline_weighted_DMD_EEG.m
168 lines (137 loc) · 6.27 KB
/
online_weighted_DMD_EEG.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
clc
clearvars
close all
resultdir_all='Results';
if (~exist(resultdir_all,'dir'))
mkdir(resultdir_all);
end
figures_resultdir_all=strcat(resultdir_all,'\Figures');
if (~exist(figures_resultdir_all,'dir'))
mkdir(figures_resultdir_all);
end
mat_resultdir_all=strcat(resultdir_all,'\mat files');
if (~exist(mat_resultdir_all,'dir'))
mkdir(mat_resultdir_all);
end
EEG_Pred_figures=strcat(figures_resultdir_all,'\EEG Prediction');
if (~exist(EEG_Pred_figures,'dir'))
mkdir(EEG_Pred_figures);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Weights
weight_vec=[0.1 0.2 0.4 0.8];weight_str_vec={''}; weight_legends={''};
for w_idx=1:length(weight_vec)
weight_str_vec{w_idx}=strcat('w',num2str(weight_vec(w_idx)*100));
weight_legends{w_idx}=strcat('\rho=',num2str(weight_vec(w_idx)));
end
FS=512;dt=1/FS;initail_pre_event_time=1;
wind=floor(initail_pre_event_time*FS);future_pred_samples=FS/8;
elec_FCz_num=47;
data_event_str={'correcteeg_BPF_110','erroneouseeg_BPF_110'};
event_name={'Correct','Erroneous'};
reg_data_dir=strcat('processed data\');
for event_idx1=1:length(event_name)
eegcarhpfsetfile=strcat(reg_data_dir,data_event_str{event_idx1},'.mat');
for weight_idx1=1:length(weight_vec)
wt=(weight_vec(weight_idx1))^2;
fprintf('loading data for %s events.... \n',event_name{event_idx1});
EEG=matfile(eegcarhpfsetfile);
Data=EEG.ERP;Time=EEG.time;
Datax=Data(:,1:end-1);
Datay=Data(:,2:end);
[n_data,m_data]=size(Datax);
Data_x_wind=Datax(:,1:wind);Data_y_wind=Datay(:,1:wind);
alpha_weight=10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Initial DMD model
cov_A00=alpha_weight*eye(n_data);Ar00=zeros(n_data);
for sample_idx0=1:wind
xu=Data_x_wind(:,sample_idx0); yu=Data_y_wind(:,sample_idx0);
err1=(yu-Ar00*xu);
gamma=1/(wt+ xu'*cov_A00*xu);
Ar01=Ar00 +gamma*err1*xu'*cov_A00;
cov_A01=cov_A00-gamma*(cov_A00*xu)*(xu'*cov_A00');
Ar00=Ar01;
cov_A00=cov_A01;
end
Ar10= Ar00;cov_A0=cov_A00;
err_rms_pred=zeros(length(1:m_data-wind),1);
err_time_pred=zeros(length(1:m_data-wind),1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Online Weighted DMD
for sample_idx=1: m_data-(wind+future_pred_samples)
fprintf('iteration # %d out of %d .... \n',sample_idx,m_data-(wind+future_pred_samples));
xu=Datax(:,sample_idx+wind); yu=Datay(:,sample_idx+wind);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Weighted Online DMD Model Update
err1=(yu-Ar10*xu);
gamma=1/(wt+ xu'*cov_A0*xu);
Ar11=Ar10 +gamma*err1*xu'*cov_A0;
cov_A1=cov_A0-gamma*(cov_A0*xu)*(xu'*cov_A0');
Ar10=Ar11;
cov_A0=cov_A1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% DMD model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=Ar10;[W_telda,D_telda]=eig(A);lambda_telda=diag(D_telda);
omega_telda=log(lambda_telda)/dt;
phi=W_telda;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Future EEG Prediction
test_ind_0=wind+sample_idx+1;test_ind_f=test_ind_0+future_pred_samples-1;
x_ind_0=wind+sample_idx;
x1=Datax(:,x_ind_0);b= pinv(phi)*x1;time=(1:test_ind_f-x_ind_0)./FS;
time_dynamics=zeros(length(b),length(time));
for iter=1:length(time)
time_dynamics(:,iter)=(b.*exp(omega_telda*time(iter)));
end
eeg_hat=real(phi*time_dynamics);eeg_hat=eeg_hat(:,1:length(time));
err_time_pred(sample_idx)=(sample_idx/FS);R_y=max(Datay(elec_FCz_num,test_ind_0:test_ind_f))-min(Datay(elec_FCz_num,test_ind_0:test_ind_f));
err_rms_pred(sample_idx)=(rms(eeg_hat(elec_FCz_num,:)-Datay(elec_FCz_num,test_ind_0:test_ind_f)))/R_y;
end
dynamics_weighted_online.(event_name{event_idx1}).(weight_str_vec{weight_idx1}).rms_future_pred=err_rms_pred(1:sample_idx);
dynamics_weighted_online.(event_name{event_idx1}).(weight_str_vec{weight_idx1}).time_future_pred=err_time_pred(1:sample_idx);
end
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Plotting Predicition Error
close all
fig_num=0;
for event_idx2=1:event_idx1
fig_num=fig_num+1;
H_pred_rms=figure(fig_num);
set(gcf,'PaperPositionMode', 'manual','Position',get(0, 'Screensize'),'PaperOrientation', 'landscape');
for weight_idx2=1:weight_idx1
pred_time= dynamics_weighted_online.(event_name{event_idx2}).(weight_str_vec{weight_idx2}).time_future_pred;
pred_rms= dynamics_weighted_online.(event_name{event_idx2}).(weight_str_vec{weight_idx2}).rms_future_pred;
h_erp=semilogy(pred_time,pred_rms, 'LineWidth',3);
hold on
end
hold off
%%%%Axes
axis_font=30;lgd_font=36;
ax=gca;
ax.YLabel.String = 'Normalized RMS Error';ax.XLabel.String = 'Time(sec)';
ax.FontSize = axis_font;ax.FontWeight = 'bold';
ax.YLabel.FontSize=axis_font; ax.XLabel.FontSize=axis_font;
ax.YLabel.FontWeight='b'; ax.XLabel.FontWeight='b';
ax.YLim=[10^-2,10^2];
ax.XLim=[pred_time(1),pred_time(end)];
%%%%Legends
lgd = legend(weight_legends);
lgd.Box='on';lgd.LineWidth=2;lgd.Location='north';
lgd.FontSize = lgd_font;lgd.FontWeight = 'b';lgd.Orientation='horizontal';
% lgd.NumColumns =3;
grid on
%%%%Title
ax.Title.String = strcat('Online Weighted DMD');
ax.Title.FontWeight = 'bold';ax.Title.FontSize =axis_font;
%%%%%%%%%%%%%%%%%%%
rms_thr_fig=strcat(EEG_Pred_figures,'\',event_name{event_idx2},'_online_weighted_DMD_pred_rms.fig');
rms_thr_png=strcat(EEG_Pred_figures,'\',event_name{event_idx2},'_online_weighted_DMD_pred_rms');
saveas(H_pred_rms,rms_thr_fig);
print(rms_thr_png,'-dpdf','-fillpage')
end
DMD_dyn_file=strcat(mat_resultdir_all,'\','online_weighted_DMD_EEG_Pred.mat');
save(DMD_dyn_file,'dynamics_weighted_online');