1
+ function [zef ] = zef_KF(zef , q_value )
2
+ % Optimal q_value as parameter
3
+ %% Initial parameters
4
+ snr_val = zef .inv_snr ;
5
+ pm_val = zef .inv_prior_over_measurement_db ;
6
+ amplitude_db = zef .inv_amplitude_db ;
7
+ pm_val = pm_val - amplitude_db ;
8
+ std_lhood = 10 ^(-snr_val / 20 );
9
+ sampling_freq = zef .inv_sampling_frequency ;
10
+ high_pass = zef .inv_low_cut_frequency ;
11
+ low_pass = zef .inv_high_cut_frequency ;
12
+ number_of_frames = zef .number_of_frames ;
13
+ source_direction_mode = zef .source_direction_mode ;
14
+ source_directions = zef .source_directions ;
15
+ source_positions = zef .source_positions ;
16
+ time_step = zef .inv_time_3 ;
17
+
18
+ %% Reconstruction identifiers
19
+ reconstruction_information.tag = ' Kalman' ;
20
+ reconstruction_information.inv_time_1 = zef .inv_time_1 ;
21
+ reconstruction_information.inv_time_2 = zef .inv_time_2 ;
22
+ reconstruction_information.inv_time_3 = zef .inv_time_3 ;
23
+ reconstruction_information.sampling_freq = zef .inv_sampling_frequency ;
24
+ reconstruction_information.low_pass = zef .inv_high_cut_frequency ;
25
+ reconstruction_information.high_pass = zef .inv_low_cut_frequency ;
26
+ reconstruction_information.number_of_frames = zef .number_of_frames ;
27
+ reconstruction_information.source_direction_mode = zef .source_direction_mode ;
28
+ reconstruction_information.source_directions = zef .source_directions ;
29
+ reconstruction_information.snr_val = zef .inv_snr ;
30
+ reconstruction_information.pm_val = zef .inv_prior_over_measurement_db ;
31
+
32
+ %%
33
+ [L ,n_interp , procFile ] = zef_processLeadfields(zef );
34
+
35
+ % get ellipse filteres full measurement data. f_data: "sensors" x "time points"
36
+ [f_data ] = zef_getFilteredData(zef );
37
+ timeSteps = arrayfun(@(x ) zef_getTimeStep(f_data , x , zef ), 1 : number_of_frames , ' UniformOutput' , false );
38
+
39
+ z_inverse_results = cell(0 );
40
+ %% CALCULATION STARTS HERE
41
+ % m_0 = prior mean
42
+ m = zeros(size(L ,2 ), 1 );
43
+
44
+ % [theta0] = zef_find_gaussian_prior(snr_val-pm_val,L,size(L,2),zef.normalize_data,0);
45
+
46
+ % Transition matrix is Identity matrix
47
+ % P = eye(size(L,2)) * theta0;
48
+
49
+ P = (1 - std_lhood ^ 2 )*10 .^(-(snr_val - pm_val )/10 )*diag(mean(var(f_data(: ,1 : zef .kf_number_of_noise_steps ),0 ,2 ))./repelem(sum(reshape(sum(L .^ 2 ),3 ,[])),3 ));
50
+
51
+ A = eye(size(L ,2 ));
52
+ q_given_flag = false ;
53
+ % If q_value given in the function call
54
+ if nargin > 1
55
+ if max(size(q_value )) == 1
56
+ Q = q_value * eye(size(L ,2 ));
57
+ elseif min(size(q_value )) == 1
58
+ Q = diiag(q_value(: ));
59
+ else
60
+ Q = q_value ;
61
+ end
62
+ q_given_flag = true ;
63
+ else
64
+ zef_init_gaussian_prior_options ;
65
+ evolution_prior_db = zef .inv_evolution_prior ;
66
+ q_value = find_evolution_prior(L , f_data , std_lhood , evolution_prior_db , zef .kf_evolution_prior_mode );
67
+ if zef .kf_evolution_prior_mode <= 2
68
+ % time-dependent models
69
+ Q = q_value ;
70
+ else
71
+ q_given_flag = true ;
72
+ if zef .kf_evolution_prior_mode == 3
73
+ % full matrix model
74
+ Q = q_value ;
75
+ else
76
+ % iid models
77
+ Q = q_value * eye(size(L ,2 ));
78
+ end
79
+ end
80
+ end
81
+ reconstruction_information.Q = q_value ;
82
+ clear q_value
83
+ % std_lhood
84
+ R = std_lhood ^ 2 * eye(size(L ,1 ));
85
+
86
+ Q_Store = cell(0 );
87
+
88
+ %% KALMAN FILTER
89
+ filter_type = zef .filter_type ;
90
+ smoothing = zef .kf_smoothing ;
91
+ if filter_type == 1
92
+ [P_store , z_inverse ] = kalman_filter(m ,P ,A ,Q ,L ,R ,timeSteps , number_of_frames , smoothing ,q_given_flag );
93
+ elseif filter_type == 2
94
+ n_ensembles = zef .number_of_ensembles ;
95
+ z_inverse = EnKF(m ,A ,P ,Q ,L ,R ,timeSteps ,number_of_frames , n_ensembles ,q_given_flag );
96
+ elseif filter_type == 3
97
+ [P_store , z_inverse ] = kalman_filter_sLORETA(m ,P ,A ,Q ,L ,R ,timeSteps , number_of_frames , smoothing ,q_given_flag );
98
+ elseif filter_type == 4
99
+ [P_store , z_inverse ] = kalman_filter(m ,P ,A ,Q ,L ,R ,timeSteps , number_of_frames , smoothing ,q_given_flag );
100
+ P_old = eye(size(L ,2 )) * theta0 ;
101
+ H = L * sqrtm(P_old );
102
+ W = inv(sqrtm(diag(diag(H ' *inv(H * H ' + R )*H ))));
103
+ z_inverse = cellfun(@(x ) W * x , z_inverse , ' UniformOutput' , false );
104
+
105
+ end
106
+
107
+ %% RTS SMOOTHING
108
+
109
+ if (smoothing == 2 )
110
+ [~ , m_s_store , ~ ] = RTS_smoother(P_store , z_inverse , A , Q , number_of_frames );
111
+ z_inverse = m_s_store ;
112
+ end
113
+
114
+ %% POSTPROCESSING
115
+ [z ] = zef_postProcessInverse(z_inverse , procFile );
116
+ % normalize the reconstruction so that the highest value is equal to 1
117
+ [z ] = zef_normalizeInverseReconstruction(z );
118
+ %% CALCULATION ENDS HERE
119
+ zef.reconstruction_information = reconstruction_information ;
120
+ zef.reconstruction = z ;
121
+
122
+ end
0 commit comments