-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_E_multtshifts.m
145 lines (119 loc) · 5.43 KB
/
calc_E_multtshifts.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
function [E, grad_E, hess_E] = calc_E_multtshifts(w, d, f, t, fs)
% Computes energy E, its gradient grad_E, and its Hessian hess_E at(w,d)
% using trapezoidal rule to approximate the integrals
%
% INPUTS:
% w: scalar, frequency
% d: 1 x n vector, d(i) = phase shift between 1st and (i+1)th segment
% f: 1 x (n+1) cell array, f{i} = 1 x N_i, samples of segment i
% t: 1 x (n+1) cell array, t{i} = 1 x N_i, times that segment i is sampled at
% fs: scalar, sampling rate
%
% OUTPUTS:
% E: scalar, energy E at (w,d)
% grad_E: (n+1) x 1 vector, gradient of E at (w,d)
% hess_E: (n+1) x (n+1) matrix, Hessian of E at (w,d)
d = [0, d];
n = length(t) - 1; % number of time shifts
% T = zeros(1,n+1); % length in time of segment i, T(i) = T_i
% COMPUTE COS/SIN
cos_wave_shift = {};
sin_wave_shift = {};
for i = 1:n+1
cos_wave_shift{i} = cos(2*pi*(w*(t{i})+d(i))); %cos_wave_shift{i} = cos(2*pi*(w*(t{i}+sum(T))+d(i)));
sin_wave_shift{i} = sin(2*pi*(w*(t{i})+d(i))); %sin_wave_shift{i} = sin(2*pi*(w*(t{i}+sum(T))+d(i)));
% T(i) = length(t{i})/fs;
end
% COMPUTE COS/SIN * F_i
cos_wave_shift_times_fi = cos_wave_shift;
sin_wave_shift_times_fi = sin_wave_shift;
for i = 1:n+1
cos_wave_shift_times_fi{i} = cos_wave_shift_times_fi{i}.*f{i};
sin_wave_shift_times_fi{i} = sin_wave_shift_times_fi{i}.*f{i};
end
% T_cumsum = [0, cumsum(T)]; % T_cumsum(i) = sum_{j=1}^{i-1} T(j)
% COMPUTE COS/SIN * F_i * t (or t^2)
cos_wave_shift_times_fi_times_t = cos_wave_shift_times_fi;
sin_wave_shift_times_fi_times_t = sin_wave_shift_times_fi;
cos_wave_shift_times_fi_times_t2 = cos_wave_shift_times_fi;
sin_wave_shift_times_fi_times_t2 = sin_wave_shift_times_fi;
for i = 1:n+1
% cos_wave_shift_times_fi_times_t{i} = cos_wave_shift_times_fi_times_t{i}.*(t{i} + T_cumsum(i));
% sin_wave_shift_times_fi_times_t{i} = sin_wave_shift_times_fi_times_t{i}.*(t{i} + T_cumsum(i));
%
% cos_wave_shift_times_fi_times_t2{i} = cos_wave_shift_times_fi_times_t{i}.*(t{i} + T_cumsum(i));
% sin_wave_shift_times_fi_times_t2{i} = sin_wave_shift_times_fi_times_t{i}.*(t{i} + T_cumsum(i));
cos_wave_shift_times_fi_times_t{i} = cos_wave_shift_times_fi_times_t{i}.*(t{i});
sin_wave_shift_times_fi_times_t{i} = sin_wave_shift_times_fi_times_t{i}.*(t{i});
cos_wave_shift_times_fi_times_t2{i} = cos_wave_shift_times_fi_times_t{i}.*(t{i});
sin_wave_shift_times_fi_times_t2{i} = sin_wave_shift_times_fi_times_t{i}.*(t{i});
end
% COMPUTE INTEGRALS USING TRAPEZOIDAL RULE
int_cos_wave_shift_times_fi = zeros(1,n+1);
int_sin_wave_shift_times_fi = zeros(1,n+1);
int_cos_wave_shift_times_fi_times_t = zeros(1,n+1);
int_sin_wave_shift_times_fi_times_t = zeros(1,n+1);
int_cos_wave_shift_times_fi_times_t2 = zeros(1,n+1);
int_sin_wave_shift_times_fi_times_t2 = zeros(1,n+1);
for i = 1:n+1
cos_tmp = cos_wave_shift_times_fi{i};
cos_tmp(1) = cos_tmp(1)/2; cos_tmp(end) = cos_tmp(end)/2;
int_cos_wave_shift_times_fi(i) = sum(cos_tmp)/fs;
sin_tmp = sin_wave_shift_times_fi{i};
sin_tmp(1) = sin_tmp(1)/2; sin_tmp(end) = sin_tmp(end)/2;
int_sin_wave_shift_times_fi(i) = sum(sin_tmp)/fs;
% times t
cos_tmp = cos_wave_shift_times_fi_times_t{i};
cos_tmp(1) = cos_tmp(1)/2; cos_tmp(end) = cos_tmp(end)/2;
int_cos_wave_shift_times_fi_times_t(i) = sum(cos_tmp)/fs;
sin_tmp = sin_wave_shift_times_fi_times_t{i};
sin_tmp(1) = sin_tmp(1)/2; sin_tmp(end) = sin_tmp(end)/2;
int_sin_wave_shift_times_fi_times_t(i) = sum(sin_tmp)/fs;
% times t^2
cos_tmp = cos_wave_shift_times_fi_times_t2{i};
cos_tmp(1) = cos_tmp(1)/2; cos_tmp(end) = cos_tmp(end)/2;
int_cos_wave_shift_times_fi_times_t2(i) = sum(cos_tmp)/fs;
sin_tmp = sin_wave_shift_times_fi_times_t2{i};
sin_tmp(1) = sin_tmp(1)/2; sin_tmp(end) = sin_tmp(end)/2;
int_sin_wave_shift_times_fi_times_t2(i) = sum(sin_tmp)/fs;
end
% COMPUTE ENERGY
F_R = sum(int_cos_wave_shift_times_fi);
F_C = -sum(int_sin_wave_shift_times_fi);
E = F_R^2 + F_C^2;
% COMPUTE GRADIENT
grad_F_R = zeros(1,n+1);
grad_F_R(1) = -sum(int_sin_wave_shift_times_fi_times_t)*2*pi;
grad_F_R(2:end) = -int_sin_wave_shift_times_fi(2:end)*2*pi;
grad_F_C = zeros(1,n+1);
grad_F_C(1) = -sum(int_cos_wave_shift_times_fi_times_t)*2*pi;
grad_F_C(2:end) = -int_cos_wave_shift_times_fi(2:end)*2*pi;
grad_E = 2*F_R*grad_F_R + 2*F_C*grad_F_C;
grad_E = grad_E';
% COMPUTE HESSIAN
dwdw_F_R = -sum(int_cos_wave_shift_times_fi_times_t2)*(2*pi)^2;
dwdw_F_C = sum(int_sin_wave_shift_times_fi_times_t2)*(2*pi)^2;
dwdk_F_R = -int_cos_wave_shift_times_fi_times_t(2:end)*(2*pi)^2;
dwdk_F_C = int_sin_wave_shift_times_fi_times_t(2:end)*(2*pi)^2;
dkdk_F_R = -int_cos_wave_shift_times_fi(2:end)*(2*pi)^2;
dkdk_F_C = int_sin_wave_shift_times_fi(2:end)*(2*pi)^2;
hess_E = zeros(n+1,n+1);
for i = 2:n+1
for j = 2:i
if i == j
hess_E(i,j) = 2*grad_F_R(i)^2 + 2*F_R*dkdk_F_R(i-1) ...
+ 2*grad_F_C(i)^2 + 2*F_C*dkdk_F_C(i-1);
else
hess_E(i,j) = 2*grad_F_R(i)*grad_F_R(j) + ...
2*grad_F_C(i)*grad_F_C(j);
end
hess_E(j,i) = hess_E(i,j); % symmetric
end
end
for i = 2:n+1
hess_E(1,i) = 2*grad_F_R(1)*grad_F_R(i) + 2*F_R*dwdk_F_R(i-1) ...
+ 2*grad_F_C(1)*grad_F_C(i) + 2*F_C*dwdk_F_C(i-1);
hess_E(i,1) = hess_E(1,i); % symmetric
end
hess_E(1,1) = 2*grad_F_R(1)^2 + 2*F_R*dwdw_F_R + 2*grad_F_C(1)^2 + 2*F_C*dwdw_F_C;
end