xue@1
|
1 //---------------------------------------------------------------------------
|
xue@1
|
2
|
xue@1
|
3 #include <stddef.h>
|
Chris@2
|
4 #include "sinest.h"
|
xue@1
|
5 #include "fft.h"
|
xue@1
|
6 #include "opt.h"
|
Chris@2
|
7 #include "sinsyn.h"
|
xue@1
|
8 #include "splines.h"
|
Chris@2
|
9 #include "windowfunctions.h"
|
xue@1
|
10
|
xue@1
|
11 //---------------------------------------------------------------------------
|
xue@1
|
12 /*
|
xue@1
|
13 function dsincd_unn: derivative of unnormalized discrete sinc function
|
xue@1
|
14
|
xue@1
|
15 In: x, scale N
|
xue@1
|
16
|
xue@1
|
17 Returns the derivative of sincd_unn(x, N)
|
xue@1
|
18 */
|
xue@1
|
19 double dsincd_unn(double x, int N)
|
xue@1
|
20 {
|
xue@1
|
21 double r=0;
|
xue@1
|
22 double omg=M_PI*x;
|
xue@1
|
23 double domg=omg/N;
|
xue@1
|
24 if (fabs(x)>1e-6)
|
xue@1
|
25 {
|
xue@1
|
26 r=M_PI*(cos(omg)-sin(omg)*cos(domg)/sin(domg)/N)/sin(domg);
|
xue@1
|
27 }
|
xue@1
|
28 else
|
xue@1
|
29 {
|
xue@1
|
30 if (domg!=0)
|
xue@1
|
31 {
|
xue@1
|
32 double sindomg=sin(domg);
|
xue@1
|
33 r=-omg*omg*omg*(1-1.0/(1.0*N*N))/3*M_PI/N/sindomg/sindomg;
|
xue@1
|
34 }
|
xue@1
|
35 else
|
xue@1
|
36 r=0;
|
xue@1
|
37 }
|
xue@1
|
38 return r;
|
xue@1
|
39 }//dsincd_unn
|
xue@1
|
40
|
xue@1
|
41 /*
|
xue@1
|
42 function ddsincd_unn: 2nd-order derivative of unnormalized discrete sinc function
|
xue@1
|
43
|
xue@1
|
44 In: x, scale (equivalently, window size) N
|
xue@1
|
45
|
xue@1
|
46 Returns the 2nd-order derivative of sincd_unn(x, N)
|
xue@1
|
47 */
|
xue@1
|
48 double ddsincd_unn(double x, int N)
|
xue@1
|
49 {
|
xue@1
|
50 double r=0;
|
xue@1
|
51 double omg=M_PI*x;
|
xue@1
|
52 double domg=omg/N;
|
xue@1
|
53 double PI2=M_PI*M_PI;
|
xue@1
|
54 double NN=1.0/N/N-1;
|
xue@1
|
55 if (domg==0)
|
xue@1
|
56 {
|
xue@1
|
57 r=PI2*N*NN/3;
|
xue@1
|
58 }
|
xue@1
|
59 else
|
xue@1
|
60 {
|
xue@1
|
61 if (fabs(x)>1e-5)
|
xue@1
|
62 {
|
xue@1
|
63 r=sin(domg)*cos(omg)-sin(omg)*cos(domg)/N;
|
xue@1
|
64 }
|
xue@1
|
65 else
|
xue@1
|
66 {
|
xue@1
|
67 r=omg*omg*omg/N*NN/3;
|
xue@1
|
68 }
|
xue@1
|
69 double ss=sin(omg)*NN;
|
xue@1
|
70 r=-2.0/N*cos(domg)*r/sin(domg)/sin(domg)+ss;
|
xue@1
|
71 r=r*PI2/sin(domg);
|
xue@1
|
72 }
|
xue@1
|
73 return r;
|
xue@1
|
74 }//ddsincd_unn
|
xue@1
|
75
|
xue@1
|
76 //---------------------------------------------------------------------------
|
xue@1
|
77 /*
|
xue@1
|
78 function Window: calculates the cosine-family-windowed spectrum of a complex sinusoid on [0:N-1] at
|
xue@1
|
79 frequency f bins with zero central phase.
|
xue@1
|
80
|
xue@1
|
81 In: f: frequency, in bins
|
xue@1
|
82 N: window size
|
xue@1
|
83 M, c[]: cosine-family window decomposition coefficients
|
xue@1
|
84 Out: x[0...K2-K1] containing the spectrum at bins K1, ..., K2.
|
xue@1
|
85
|
xue@1
|
86 Returns pointer to x. x is created anew if x=0 is specified on start.
|
xue@1
|
87 */
|
xue@1
|
88 cdouble* Window(cdouble* x, double f, int N, int M, double* c, int K1, int K2)
|
xue@1
|
89 {
|
xue@1
|
90 if (K1<0) K1=0;
|
xue@1
|
91 if (K2>N/2-1) K2=N/2-1;
|
xue@1
|
92
|
xue@1
|
93 if (!x) x=new cdouble[K2-K1+1];
|
xue@1
|
94 memset(x, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
95
|
xue@1
|
96 for (int l=K1-M; l<=K2+M; l++)
|
xue@1
|
97 {
|
xue@1
|
98 double ang=(f-l)*M_PI;
|
xue@1
|
99 double omg=ang/N;
|
xue@1
|
100 long double si, co, sinn=sin(ang);
|
xue@1
|
101 si=sin(omg), co=cos(omg);
|
xue@1
|
102 double sa=(ang==0)?N:(sinn/si);
|
xue@1
|
103 double saco=sa*co;
|
xue@1
|
104
|
xue@1
|
105 int k1=l-M, k2=l+M;
|
xue@1
|
106 if (k1<K1) k1=K1;
|
xue@1
|
107 if (k2>K2) k2=K2;
|
xue@1
|
108
|
xue@1
|
109 for (int k=k1; k<=k2; k++)
|
xue@1
|
110 {
|
xue@1
|
111 int m=k-l, kt=k-K1;
|
xue@1
|
112 if (m<0) m=-m;
|
xue@1
|
113 if (k%2)
|
xue@1
|
114 {
|
xue@1
|
115 x[kt].x-=c[m]*saco;
|
xue@1
|
116 x[kt].y+=c[m]*sinn;
|
xue@1
|
117 }
|
xue@1
|
118 else
|
xue@1
|
119 {
|
xue@1
|
120 x[kt].x+=c[m]*saco;
|
xue@1
|
121 x[kt].y-=c[m]*sinn;
|
xue@1
|
122 }
|
xue@1
|
123 }
|
xue@1
|
124 }
|
xue@1
|
125 return x;
|
xue@1
|
126 }//Window
|
xue@1
|
127
|
xue@1
|
128 /*
|
xue@1
|
129 function dWindow: calculates the cosine-family-windowed spectrum and its derivative of a complex
|
xue@1
|
130 sinusoid on [0:N-1] at frequency f bins with zero central phase.
|
xue@1
|
131
|
xue@1
|
132 In: f: frequency, in bins
|
xue@1
|
133 N: window size
|
xue@1
|
134 M, c[]: cosine-family window decomposition coefficients
|
xue@1
|
135 Out: x[0...K2-K1] containing the spectrum at bins K1, ..., K2,
|
xue@1
|
136 dx[0...K2-K1] containing the derivative spectrum at bins K1, ..., K2
|
xue@1
|
137
|
xue@1
|
138 No return value.
|
xue@1
|
139 */
|
xue@1
|
140 void dWindow(cdouble* dx, cdouble* x, double f, int N, int M, double* c, int K1, int K2)
|
xue@1
|
141 {
|
xue@1
|
142 if (K1<0) K1=0;
|
xue@1
|
143 if (K2>N/2-1) K2=N/2-1;
|
xue@1
|
144 memset(x, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
145 memset(dx, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
146
|
xue@1
|
147 for (int l=K1-M; l<=K2+M; l++)
|
xue@1
|
148 {
|
xue@1
|
149 double ang=(f-l), Omg=ang*M_PI, omg=Omg/N;
|
xue@1
|
150 long double si, co, sinn=sin(Omg), cosn=cos(Omg);
|
xue@1
|
151 si=sin(omg), co=cos(omg);
|
xue@1
|
152 double sa=(ang==0)?N:(sinn/si), dsa=dsincd_unn(ang, N);
|
xue@1
|
153 double saco=sa*co, dsaco=dsa*co, sinnpi_n=sinn*M_PI/N, cosnpi=cosn*M_PI;
|
xue@1
|
154
|
xue@1
|
155 int k1=l-M, k2=l+M;
|
xue@1
|
156 if (k1<K1) k1=K1;
|
xue@1
|
157 if (k2>K2) k2=K2;
|
xue@1
|
158
|
xue@1
|
159 for (int k=k1; k<=k2; k++)
|
xue@1
|
160 {
|
xue@1
|
161 int m=k-l, kt=k-K1;
|
xue@1
|
162 if (m<0) m=-m;
|
xue@1
|
163 if (k%2)
|
xue@1
|
164 {
|
xue@1
|
165 x[kt].x-=c[m]*saco;
|
xue@1
|
166 x[kt].y+=c[m]*sinn;
|
xue@1
|
167 dx[kt].x-=c[m]*(-sinnpi_n+dsaco);
|
xue@1
|
168 dx[kt].y+=c[m]*cosnpi;
|
xue@1
|
169 }
|
xue@1
|
170 else
|
xue@1
|
171 {
|
xue@1
|
172 x[kt].x+=c[m]*saco;
|
xue@1
|
173 x[kt].y-=c[m]*sinn;
|
xue@1
|
174 dx[kt].x+=c[m]*(-sinnpi_n+dsaco);
|
xue@1
|
175 dx[kt].y-=c[m]*cosnpi;
|
xue@1
|
176 }
|
xue@1
|
177 }
|
xue@1
|
178 }
|
xue@1
|
179 }//dWindow
|
xue@1
|
180
|
xue@1
|
181 /*
|
xue@1
|
182 function ddWindow: calculates the cosine-family-windowed spectrum and its 1st and 2nd derivatives of
|
xue@1
|
183 a complex sinusoid on [0:N-1] at frequency f bins with zero central phase.
|
xue@1
|
184
|
xue@1
|
185 In: f: frequency, in bins
|
xue@1
|
186 N: window size
|
xue@1
|
187 M, c[]: cosine-family window decomposition coefficients
|
xue@1
|
188 Out: x[0...K2-K1] containing the spectrum at bins K1, ..., K2,
|
xue@1
|
189 dx[0...K2-K1] containing the derivative spectrum at bins K1, ..., K2
|
xue@1
|
190 ddx[0...K2-K1] containing the 2nd-order derivative spectrum at bins K1, ..., K2
|
xue@1
|
191
|
xue@1
|
192 No return value.
|
xue@1
|
193 */
|
xue@1
|
194 void ddWindow(cdouble* ddx, cdouble* dx, cdouble* x, double f, int N, int M, double* c, int K1, int K2)
|
xue@1
|
195 {
|
xue@1
|
196 if (K1<0) K1=0;
|
xue@1
|
197 if (K2>N/2-1) K2=N/2-1;
|
xue@1
|
198 memset(x, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
199 memset(dx, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
200 memset(ddx, 0, sizeof(cdouble)*(K2-K1+1));
|
xue@1
|
201
|
xue@1
|
202 for (int l=K1-M; l<=K2+M; l++)
|
xue@1
|
203 {
|
xue@1
|
204 double ang=(f-l), Omg=ang*M_PI, omg=Omg/N;
|
xue@1
|
205 long double si, co, sinn=sin(Omg), cosn=cos(Omg);
|
xue@1
|
206 si=sin(omg), co=cos(omg);
|
xue@1
|
207 double sa=(ang==0)?N:(sinn/si), dsa=dsincd_unn(ang, N), ddsa=ddsincd_unn(ang, N);
|
xue@1
|
208 double saco=sa*co, dsaco=dsa*co, sinnpi_n=sinn*M_PI/N, sinnpipi=sinn*M_PI*M_PI, cosnpi=cosn*M_PI,
|
xue@1
|
209 cosnpipi_n=cosnpi*M_PI/N, sipi_n=si*M_PI/N;
|
xue@1
|
210
|
xue@1
|
211 int k1=l-M, k2=l+M;
|
xue@1
|
212 if (k1<K1) k1=K1;
|
xue@1
|
213 if (k2>K2) k2=K2;
|
xue@1
|
214
|
xue@1
|
215 for (int k=k1; k<=k2; k++)
|
xue@1
|
216 {
|
xue@1
|
217 int m=k-l, kt=k-K1;
|
xue@1
|
218 if (m<0) m=-m;
|
xue@1
|
219 if (k%2)
|
xue@1
|
220 {
|
xue@1
|
221 x[kt].x-=c[m]*saco;
|
xue@1
|
222 x[kt].y+=c[m]*sinn;
|
xue@1
|
223 dx[kt].x-=c[m]*(-sinnpi_n+dsaco);
|
xue@1
|
224 dx[kt].y+=c[m]*cosnpi;
|
xue@1
|
225 ddx[kt].x-=c[m]*(-cosnpipi_n+ddsa*co-dsa*sipi_n);
|
xue@1
|
226 ddx[kt].y-=c[m]*sinnpipi;
|
xue@1
|
227 }
|
xue@1
|
228 else
|
xue@1
|
229 {
|
xue@1
|
230 x[kt].x+=c[m]*saco;
|
xue@1
|
231 x[kt].y-=c[m]*sinn;
|
xue@1
|
232 dx[kt].x+=c[m]*(-sinnpi_n+dsaco);
|
xue@1
|
233 dx[kt].y-=c[m]*cosnpi;
|
xue@1
|
234 ddx[kt].x+=c[m]*(-cosnpipi_n+ddsa*co-dsa*sipi_n);
|
xue@1
|
235 ddx[kt].y+=c[m]*sinnpipi;
|
xue@1
|
236 }
|
xue@1
|
237 }
|
xue@1
|
238 }
|
xue@1
|
239 }//ddWindow
|
xue@1
|
240
|
xue@1
|
241 //---------------------------------------------------------------------------
|
xue@1
|
242 /*
|
xue@1
|
243 function IPWindow: computes the truncated inner product of a windowed spectrum with that of a sinusoid
|
xue@1
|
244 at reference frequency f.
|
xue@1
|
245
|
xue@1
|
246 In: x[0:N-1]: input spectrum
|
xue@1
|
247 f: reference frequency, in bins
|
xue@1
|
248 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
249 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
250 returnamplitude: specifies return value, true for amplitude, false for angle
|
xue@1
|
251
|
xue@1
|
252 Returns the amplitude or phase of the inner product, as specified by $returnamplitude. The return
|
xue@1
|
253 value is interpreted as the actual amplitude/phase of a sinusoid being estimated at f.
|
xue@1
|
254 */
|
xue@1
|
255 double IPWindow(double f, cdouble* x, int N, int M, double* c, double iH2, int K1, int K2, bool returnamplitude)
|
xue@1
|
256 {
|
xue@1
|
257 cdouble r=IPWindowC(f, x, N, M, c, iH2, K1, K2);
|
xue@1
|
258 double result;
|
xue@1
|
259 if (returnamplitude) result=sqrt(r.x*r.x+r.y*r.y);
|
xue@1
|
260 else result=arg(r);
|
xue@1
|
261 return result;
|
xue@1
|
262 }//IPWindow
|
xue@1
|
263 //wrapper function
|
xue@1
|
264 double IPWindow(double f, void* params)
|
xue@1
|
265 {
|
xue@1
|
266 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
267 return IPWindow(f, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, true);
|
xue@1
|
268 }//IPWindow
|
xue@1
|
269
|
xue@1
|
270 /*
|
xue@1
|
271 function ddIPWindow: computes the norm of the truncated inner product of a windowed spectrum with
|
xue@1
|
272 that of a sinusoid at reference frequency f, as well as its 1st and 2nd derivatives.
|
xue@1
|
273
|
xue@1
|
274 In: x[0:N-1]: input spectrum
|
xue@1
|
275 f: reference frequency, in bins
|
xue@1
|
276 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
277 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
278 Out: ipwindow and dipwindow: the truncated inner product norm and its derivative
|
xue@1
|
279
|
xue@1
|
280 Returns the 2nd derivative of the norm of the truncated inner product.
|
xue@1
|
281 */
|
xue@1
|
282 double ddIPWindow(double f, cdouble* x, int N, int M, double* c, double iH2, int K1, int K2, double& dipwindow, double& ipwindow)
|
xue@1
|
283 {
|
xue@1
|
284 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
285 int K=K2-K1+1;
|
xue@1
|
286 cdouble *w=new cdouble[K*3], *dw=&w[K], *ddw=&w[K*2], *lx=&x[K1];
|
xue@1
|
287 ddWindow(ddw, dw, w, f, N, M, c, K1, K2);
|
xue@1
|
288 cdouble r=Inner(K, lx, w), dr=Inner(K, lx, dw), ddr=Inner(K, lx, ddw);
|
xue@1
|
289 delete[] w;
|
xue@1
|
290
|
xue@1
|
291 double R2=~r,
|
xue@1
|
292 R=sqrt(R2),
|
xue@1
|
293 dR2=2*(r.x*dr.x+r.y*dr.y),
|
xue@1
|
294 dR=dR2/(2*R),
|
xue@1
|
295 ddR2=2*(r.x*ddr.x+r.y*ddr.y+~dr),
|
xue@1
|
296 ddR=(R*ddR2-dR2*dR)/(2*R2);
|
xue@1
|
297 ipwindow=R*iH2;
|
xue@1
|
298 dipwindow=dR*iH2;
|
xue@1
|
299 return ddR*iH2;
|
xue@1
|
300 }//ddIPWindow
|
xue@1
|
301 //wrapper function
|
xue@1
|
302 double ddIPWindow(double f, void* params)
|
xue@1
|
303 {
|
xue@1
|
304 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
305 return ddIPWindow(f, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->dipwindow, p->ipwindow);
|
xue@1
|
306 }//ddIPWindow
|
xue@1
|
307
|
xue@1
|
308 //---------------------------------------------------------------------------
|
xue@1
|
309 /*
|
xue@1
|
310 function IPWindowC: computes the truncated inner product of a windowed spectrum with that of a
|
xue@1
|
311 sinusoid at reference frequency f.
|
xue@1
|
312
|
xue@1
|
313 In: x[0:N-1]: input spectrum
|
xue@1
|
314 f: reference frequency, in bins
|
xue@1
|
315 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
316 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
317
|
xue@1
|
318 Returns the inner product. The return value is interpreted as the actual amplitude-phase factor of a
|
xue@1
|
319 sinusoid being estimated at f.
|
xue@1
|
320 */
|
xue@1
|
321 cdouble IPWindowC(double f, cdouble* x, int N, int M, double* c, double iH2, int K1, int K2)
|
xue@1
|
322 {
|
xue@1
|
323 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
324 int K=K2-K1+1;
|
xue@1
|
325 cdouble *w=new cdouble[K];
|
xue@1
|
326 cdouble *lx=&x[K1], result=0;
|
xue@1
|
327 Window(w, f, N, M, c, K1, K2);
|
xue@1
|
328 for (int k=0; k<K; k++) result+=lx[k]^w[k];
|
xue@1
|
329 delete[] w;
|
xue@1
|
330 result*=iH2;
|
xue@1
|
331 return result;
|
xue@1
|
332 }//IPWindowC
|
xue@1
|
333
|
xue@1
|
334 //---------------------------------------------------------------------------
|
xue@1
|
335 /*
|
xue@1
|
336 function sIPWindow: computes the total energy of truncated inner products between multiple windowed
|
xue@1
|
337 spectra and that of a sinusoid at a reference frequency f. This does not consider phase alignment
|
xue@1
|
338 between the spectra, supposedly measured at a sequence of known instants.
|
xue@1
|
339
|
xue@1
|
340 In: x[L][N]: input spectra
|
xue@1
|
341 f: reference frequency, in bins
|
xue@1
|
342 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
343 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
344 Out: lmd[L]: the actual individual inner products representing actual ampltiude-phase factors (optional)
|
xue@1
|
345
|
xue@1
|
346 Returns the energy of the vector of inner products.
|
xue@1
|
347 */
|
xue@1
|
348 double sIPWindow(double f, int L, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, cdouble* lmd)
|
xue@1
|
349 {
|
xue@1
|
350 double sip=0;
|
xue@1
|
351 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
352 int K=K2-K1+1;
|
xue@1
|
353 cdouble *w=new cdouble[K];
|
xue@1
|
354 Window(w, f, N, M, c, K1, K2);
|
xue@1
|
355 for (int l=0; l<L; l++)
|
xue@1
|
356 {
|
xue@1
|
357 cdouble *lx=&x[l][K1];
|
xue@1
|
358 cdouble r=Inner(K, lx, w);
|
xue@1
|
359 if (lmd) lmd[l]=r*iH2;
|
xue@1
|
360 sip+=~r;
|
xue@1
|
361 }
|
xue@1
|
362 sip*=iH2;
|
xue@1
|
363 delete[] w;
|
xue@1
|
364 return sip;
|
xue@1
|
365 }//sIPWindow
|
xue@1
|
366 //wrapper function
|
xue@1
|
367 double sIPWindow(double f, void* params)
|
xue@1
|
368 {
|
xue@1
|
369 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; int Fr; cdouble** x; double dipwindow; double ipwindow; cdouble* lmd;} *p=(l_ip *)params;
|
xue@1
|
370 return sIPWindow(f, p->Fr, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->lmd);
|
xue@1
|
371 }//sIPWindow
|
xue@1
|
372
|
xue@1
|
373 /*
|
xue@1
|
374 function dsIPWindow: computes the total energy of truncated inner products between multiple windowed
|
xue@1
|
375 spectra and that of a sinusoid at a reference frequency f, as well as its derivative. This does not
|
xue@1
|
376 consider phase synchronization between the spectra, supposedly measured at a sequence of known
|
xue@1
|
377 instants.
|
xue@1
|
378
|
xue@1
|
379 In: x[L][N]: input spectra
|
xue@1
|
380 f: reference frequency, in bins
|
xue@1
|
381 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
382 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
383 Out: sip, the energy of the vector of inner products.
|
xue@1
|
384
|
xue@1
|
385 Returns the derivative of the energy of the vector of inner products.
|
xue@1
|
386 */
|
xue@1
|
387 double dsIPWindow(double f, int L, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, double& sip)
|
xue@1
|
388 {
|
xue@1
|
389 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
390 int K=K2-K1+1;
|
xue@1
|
391 cdouble *w=new cdouble[K*2], *dw=&w[K];
|
xue@1
|
392 dWindow(dw, w, f, N, M, c, K1, K2);
|
xue@1
|
393 double dsip; sip=0;
|
xue@1
|
394 for (int l=0; l<L; l++)
|
xue@1
|
395 {
|
xue@1
|
396 cdouble* lx=&x[l][K1];
|
xue@1
|
397 cdouble r=Inner(K, lx, w), dr=Inner(K, lx, dw);
|
xue@1
|
398 double R2=~r, dR2=2*(r.x*dr.x+r.y*dr.y);
|
xue@1
|
399 sip+=R2, dsip+=dR2;
|
xue@1
|
400 }
|
xue@1
|
401 sip*=iH2, dsip*=iH2;
|
xue@1
|
402 delete[] w;
|
xue@1
|
403 return dsip;
|
xue@1
|
404 }//dsIPWindow
|
xue@1
|
405 //wrapper function
|
xue@1
|
406 double dsIPWindow(double f, void* params)
|
xue@1
|
407 {
|
xue@1
|
408 struct l_ip1 {int N; int k1; int k2; int M; double* c; double iH2; int Fr; cdouble** x; double sip;} *p=(l_ip1 *)params;
|
xue@1
|
409 return dsIPWindow(f, p->Fr, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->sip);
|
xue@1
|
410 }//dsIPWindow
|
xue@1
|
411
|
xue@1
|
412 /*
|
xue@1
|
413 function dsdIPWindow_unn: computes the energy of unnormalized truncated inner products between a given
|
xue@1
|
414 windowed spectrum and that of a sinusoid at a reference frequency f, as well as its 1st and 2nd
|
xue@1
|
415 derivatives. "Unnormalized" indicates that the inner product cannot be taken as the actual amplitude-
|
xue@1
|
416 phase factor of a sinusoid, but deviate from that by an unspecified factor.
|
xue@1
|
417
|
xue@1
|
418 In: x[N]: input spectrum
|
xue@1
|
419 f: reference frequency, in bins
|
xue@1
|
420 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
421 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
422 Out: sipwindow and dsipwindow, the energy and its derivative of the unnormalized inner product.
|
xue@1
|
423
|
xue@1
|
424 Returns the 2nd derivative of the inner product.
|
xue@1
|
425 */
|
xue@1
|
426 double ddsIPWindow_unn(double f, cdouble* x, int N, int M, double* c, int K1, int K2, double& dsipwindow, double& sipwindow, cdouble* w_unn)
|
xue@1
|
427 {
|
xue@1
|
428 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
429 int K=K2-K1+1;
|
xue@1
|
430
|
xue@1
|
431 cdouble *w=new cdouble[K*3], *dw=&w[K], *ddw=&w[K*2];
|
xue@1
|
432
|
xue@1
|
433 ddWindow(ddw, dw, w, f, N, M, c, K1, K2);
|
xue@1
|
434
|
xue@1
|
435 double rr=0, ri=0, drr=0, dri=0, ddrr=0, ddri=0;
|
xue@1
|
436 cdouble *lx=&x[K1];
|
xue@1
|
437 for (int k=0; k<K; k++)
|
xue@1
|
438 {
|
xue@1
|
439 rr+=lx[k].x*w[k].x+lx[k].y*w[k].y;
|
xue@1
|
440 ri+=lx[k].y*w[k].x-lx[k].x*w[k].y;
|
xue@1
|
441 drr+=lx[k].x*dw[k].x+lx[k].y*dw[k].y;
|
xue@1
|
442 dri+=lx[k].y*dw[k].x-lx[k].x*dw[k].y;
|
xue@1
|
443 ddrr+=lx[k].x*ddw[k].x+lx[k].y*ddw[k].y;
|
xue@1
|
444 ddri+=lx[k].y*ddw[k].x-lx[k].x*ddw[k].y;
|
xue@1
|
445 }
|
xue@1
|
446 delete[] w;
|
xue@1
|
447
|
xue@1
|
448 double R2=rr*rr+ri*ri,
|
xue@1
|
449 dR2=2*(rr*drr+ri*dri),
|
xue@1
|
450 ddR2=2*(rr*ddrr+ri*ddri+drr*drr+dri*dri);
|
xue@1
|
451 sipwindow=R2;
|
xue@1
|
452 dsipwindow=dR2;
|
xue@1
|
453 if (w_unn) w_unn->x=rr, w_unn->y=ri;
|
xue@1
|
454 return ddR2;
|
xue@1
|
455 }//ddsIPWindow_unn
|
xue@1
|
456
|
xue@1
|
457 /*
|
xue@1
|
458 function ddsIPWindow: computes the total energy of truncated inner products between multiple windowed
|
xue@1
|
459 spectra and that of a sinusoid at a reference frequency f, as well as its 1st and 2nd derivatives.
|
xue@1
|
460 This does not consider phase synchronization between the spectra, supposedly measured at a sequence
|
xue@1
|
461 of known instants.
|
xue@1
|
462
|
xue@1
|
463 In: x[L][N]: input spectra
|
xue@1
|
464 f: reference frequency, in bins
|
xue@1
|
465 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
466 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
467 Out: sip and dsip, the energy of the vector of inner products and its derivative.
|
xue@1
|
468
|
xue@1
|
469 Returns the 2nd derivative of the energy of the vector of inner products.
|
xue@1
|
470 */
|
xue@1
|
471 double ddsIPWindow(double f, int L, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, double& dsip, double& sip)
|
xue@1
|
472 {
|
xue@1
|
473 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
474 int K=K2-K1+1;
|
xue@1
|
475 cdouble *w=new cdouble[K*3], *dw=&w[K], *ddw=&w[K*2];
|
xue@1
|
476 ddWindow(ddw, dw, w, f, N, M, c, K1, K2);
|
xue@1
|
477 double ddsip=0; dsip=sip=0;
|
xue@1
|
478 for (int l=0; l<L; l++)
|
xue@1
|
479 {
|
xue@1
|
480 cdouble* lx=&x[l][K1];
|
xue@1
|
481 cdouble r=Inner(K, lx, w), dr=Inner(K, lx, dw), ddr=Inner(K, lx, ddw);
|
xue@1
|
482 double R2=~r, dR2=2*(r.x*dr.x+r.y*dr.y), ddR2=2*(r.x*ddr.x+r.y*ddr.y+~dr);
|
xue@1
|
483 sip+=R2, dsip+=dR2, ddsip+=ddR2;
|
xue@1
|
484 }
|
xue@1
|
485 sip*=iH2, dsip*=iH2, ddsip*=iH2;
|
xue@1
|
486 delete[] w;
|
xue@1
|
487 return ddsip;
|
xue@1
|
488 }//ddsIPWindow
|
xue@1
|
489 //wrapper function
|
xue@1
|
490 double ddsIPWindow(double f, void* params)
|
xue@1
|
491 {
|
xue@1
|
492 struct l_ip1 {int N; int k1; int k2; int M; double* c; double iH2; int Fr; cdouble** x; double dsip; double sip;} *p=(l_ip1 *)params;
|
xue@1
|
493 return ddsIPWindow(f, p->Fr, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->dsip, p->sip);
|
xue@1
|
494 }//ddsIPWindow
|
xue@1
|
495
|
xue@1
|
496 //---------------------------------------------------------------------------
|
xue@1
|
497 /*
|
xue@1
|
498 function sIPWindowC: computes the total energy of truncated inner products between multiple frames of
|
xue@1
|
499 a spectrogram and multiple frames of a spectrogram of a sinusoid at a reference frequency f.
|
xue@1
|
500
|
xue@1
|
501 In: x[L][N]: the spectrogram
|
xue@1
|
502 offst_rel: frame offset, relative to frame size
|
xue@1
|
503 f: reference frequency, in bins
|
xue@1
|
504 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
505 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
506 Out: lmd[L]: the actual individual inner products representing actual ampltiude-phase factors (optional)
|
xue@1
|
507
|
xue@1
|
508 Returns the energy of the vector of inner products.
|
xue@1
|
509 */
|
xue@1
|
510 double sIPWindowC(double f, int L, double offst_rel, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, cdouble* lmd)
|
xue@1
|
511 {
|
xue@1
|
512 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
513 int K=K2-K1+1;
|
xue@1
|
514 cdouble *w=new cdouble[K];
|
xue@1
|
515 double Cr=0;
|
xue@1
|
516 cdouble Cc=0;
|
xue@1
|
517 Window(w, f, N, M, c, K1, K2);
|
xue@1
|
518 for (int l=0; l<L; l++)
|
xue@1
|
519 {
|
xue@1
|
520 cdouble *lx=&x[l][K1];
|
xue@1
|
521 cdouble r=Inner(K, lx, w);
|
xue@1
|
522 Cr+=~r;
|
xue@1
|
523 double ph=-4*M_PI*f*offst_rel*l;
|
xue@1
|
524 cdouble r2=r*r;
|
xue@1
|
525 Cc+=r2.rotate(ph);
|
xue@1
|
526 if (lmd) lmd[l]=r;
|
xue@1
|
527 }
|
xue@1
|
528 delete[] w;
|
xue@1
|
529 double result=0.5*iH2*(Cr+abs(Cc));
|
xue@1
|
530 if (lmd)
|
xue@1
|
531 {
|
xue@1
|
532 double absCc=abs(Cc), hiH2=0.5*iH2;
|
xue@1
|
533 cdouble ej2ph=Cc/absCc;
|
xue@1
|
534 for (int l=0; l<L; l++)
|
xue@1
|
535 {
|
xue@1
|
536 double ph=4*M_PI*f*offst_rel*l;
|
xue@1
|
537 lmd[l]=hiH2*(lmd[l]+(ej2ph**lmd[l]).rotate(ph));
|
xue@1
|
538 }
|
xue@1
|
539 }
|
xue@1
|
540 return result;
|
xue@1
|
541 }//sIPWindowC
|
xue@1
|
542 //wrapper function
|
xue@1
|
543 double sIPWindowC(double f, void* params)
|
xue@1
|
544 {
|
xue@1
|
545 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; int L; double offst_rel; cdouble** x; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
546 return sIPWindowC(f, p->L, p->offst_rel, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2);
|
xue@1
|
547 }//sIPWindowC
|
xue@1
|
548
|
xue@1
|
549 /*
|
xue@1
|
550 function dsIPWindowC: computes the total energy of truncated inner products between multiple frames of
|
xue@1
|
551 a spectrogram and multiple frames of a spectrogram of a sinusoid at a reference frequency f, together
|
xue@1
|
552 with its derivative.
|
xue@1
|
553
|
xue@1
|
554 In: x[L][N]: the spectrogram
|
xue@1
|
555 offst_rel: frame offset, relative to frame size
|
xue@1
|
556 f: reference frequency, in bins
|
xue@1
|
557 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
558 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
559 Out: sip: energy of the vector of the inner products
|
xue@1
|
560
|
xue@1
|
561 Returns the 1st derivative of the energy of the vector of inner products.
|
xue@1
|
562 */
|
xue@1
|
563 double dsIPWindowC(double f, int L, double offst_rel, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, double& sip)
|
xue@1
|
564 {
|
xue@1
|
565 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
566 int K=K2-K1+1;
|
xue@1
|
567
|
xue@1
|
568 cdouble *w=new cdouble[K*2], *dw=&w[K];
|
xue@1
|
569 dWindow(dw, w, f, N, M, c, K1, K2);
|
xue@1
|
570 double Cr=0, dCr=0;
|
xue@1
|
571 cdouble Cc=0, dCc=0;
|
xue@1
|
572 for (int l=0; l<L; l++)
|
xue@1
|
573 {
|
xue@1
|
574 cdouble *lx=&x[l][K1];
|
xue@1
|
575 cdouble r=Inner(K, lx, w), dr=Inner(K, lx, dw);
|
xue@1
|
576 Cr+=~r; dCr+=2*(r.x*dr.x+r.y*dr.y);
|
xue@1
|
577 int two=2;
|
xue@1
|
578 cdouble r2=r*r, dr2=r*dr*two;
|
xue@1
|
579 double lag=-4*M_PI*offst_rel*l, ph=lag*f;
|
xue@1
|
580 Cc=Cc+cdouble(r2).rotate(ph), dCc=dCc+(dr2+cdouble(0,lag)*r2).rotate(ph);
|
xue@1
|
581 }
|
xue@1
|
582 double Cc2=~Cc, dCc2=2*(Cc.x*dCc.x+Cc.y*dCc.y);
|
xue@1
|
583 double Cc1=sqrt(Cc2), dCc1=dCc2/(2*Cc1);
|
xue@1
|
584 sip=0.5*iH2*(Cr+Cc1);
|
xue@1
|
585 double dsip=0.5*iH2*(dCr+dCc1);
|
xue@1
|
586 delete[] w;
|
xue@1
|
587 return dsip;
|
xue@1
|
588 }//dsIPWindowC
|
xue@1
|
589 //wrapper function
|
xue@1
|
590 double dsIPWindowC(double f, void* params)
|
xue@1
|
591 {
|
xue@1
|
592 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; int L; double offst_rel; cdouble** x; double sip;} *p=(l_ip *)params;
|
xue@1
|
593 return dsIPWindowC(f, p->L, p->offst_rel, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->sip);
|
xue@1
|
594 }//dsIPWindowC
|
xue@1
|
595
|
xue@1
|
596 /*
|
xue@1
|
597 function ddsIPWindowC: computes the total energy of truncated inner products between multiple frames
|
xue@1
|
598 of a spectrogram and multiple frames of a spectrogram of a sinusoid at a reference frequency f,
|
xue@1
|
599 together with its 1st and 2nd derivatives.
|
xue@1
|
600
|
xue@1
|
601 In: x[L][N]: the spectrogram
|
xue@1
|
602 offst_rel: frame offset, relative to frame size
|
xue@1
|
603 f: reference frequency, in bins
|
xue@1
|
604 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
605 K1, K2: spectrum truncation bounds, in bins, inclusive
|
xue@1
|
606 Out: sipwindow, dsipwindow: energy of the vector of the inner products and its derivative
|
xue@1
|
607
|
xue@1
|
608 Returns the 2nd derivative of the energy of the vector of inner products.
|
xue@1
|
609 */
|
xue@1
|
610 double ddsIPWindowC(double f, int L, double offst_rel, cdouble** x, int N, int M, double* c, double iH2, int K1, int K2, double& dsipwindow, double& sipwindow)
|
xue@1
|
611 {
|
xue@1
|
612 if (K1<0) K1=0; if (K2>=N/2) K2=N/2-1;
|
xue@1
|
613 int K=K2-K1+1;
|
xue@1
|
614
|
xue@1
|
615 cdouble *w=new cdouble[K*3], *dw=&w[K], *ddw=&w[K*2];
|
xue@1
|
616 ddWindow(ddw, dw, w, f, N, M, c, K1, K2);
|
xue@1
|
617 double Cr=0, dCr=0, ddCr=0;
|
xue@1
|
618 cdouble Cc=0, dCc=0, ddCc=0;
|
xue@1
|
619 for (int l=0; l<L; l++)
|
xue@1
|
620 {
|
xue@1
|
621 cdouble *lx=&x[l][K1];
|
xue@1
|
622 cdouble r=Inner(K, lx, w), dr=Inner(K, lx, dw), ddr=Inner(K, lx, ddw);
|
xue@1
|
623 Cr+=~r; dCr+=2*(r.x*dr.x+r.y*dr.y); ddCr+=2*(r.x*ddr.x+r.y*ddr.y+~dr);
|
xue@1
|
624 int two=2;
|
xue@1
|
625 cdouble r2=r*r, dr2=r*dr*two, ddr2=(dr*dr+r*ddr)*two;
|
xue@1
|
626 double lag=-4*M_PI*offst_rel*l, ph=lag*f;
|
xue@1
|
627 Cc=Cc+cdouble(r2).rotate(ph), dCc=dCc+(dr2+cdouble(0,lag)*r2).rotate(ph), ddCc=ddCc+(ddr2+cdouble(0,2*lag)*dr2-r2*lag*lag).rotate(ph);
|
xue@1
|
628 }
|
xue@1
|
629 double Cc2=~Cc, dCc2=2*(Cc.x*dCc.x+Cc.y*dCc.y), ddCc2=2*(Cc.x*ddCc.x+Cc.y*ddCc.y+~dCc);
|
xue@1
|
630 double Cc1=sqrt(Cc2), dCc1=dCc2/(2*Cc1), ddCc1=(Cc1*ddCc2-dCc2*dCc1)/(2*Cc2);
|
xue@1
|
631 sipwindow=0.5*iH2*(Cr+Cc1);
|
xue@1
|
632 dsipwindow=0.5*iH2*(dCr+dCc1);
|
xue@1
|
633 double ddsipwindow=0.5*iH2*(ddCr+ddCc1);
|
xue@1
|
634 delete[] w;
|
xue@1
|
635 return ddsipwindow;
|
xue@1
|
636 }//ddsIPWindowC
|
xue@1
|
637 //wrapper function
|
xue@1
|
638 double ddsIPWindowC(double f, void* params)
|
xue@1
|
639 {
|
xue@1
|
640 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; int L; double offst_rel; cdouble** x; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
641 return ddsIPWindowC(f, p->L, p->offst_rel, p->x, p->N, p->M, p->c, p->iH2, p->k1, p->k2, p->dipwindow, p->ipwindow);
|
xue@1
|
642 }//ddsIPWindowC
|
xue@1
|
643
|
xue@1
|
644 //--------------------------------------------------------------------------
|
xue@1
|
645 /*
|
xue@1
|
646 Least-square-error sinusoid detection function
|
xue@1
|
647
|
xue@1
|
648 version1: picking the highest peak and take measurement of a single sinusoid
|
xue@1
|
649 version2: given a rough peak location and take measurement of a single sinusoid
|
xue@1
|
650
|
xue@1
|
651 Complex spectrum x is calculated using N data points windowed by a window function that is specified
|
xue@1
|
652 by the parameter set (M, c, iH2). c[0:M] is provided according to Table 3 in the transfer report, on
|
xue@1
|
653 pp.11. iH2 is simply 1/H2, where H2 can be calculated using formula (2.17) on pp.12.
|
xue@1
|
654
|
xue@1
|
655 f & epf are given/returned in bins.
|
xue@1
|
656
|
xue@1
|
657 Further reading: "Least-square-error estimation of sinusoids.pdf"
|
xue@1
|
658 */
|
xue@1
|
659
|
xue@1
|
660 /*
|
xue@1
|
661 function LSESinusoid: LSE estimation of the predominant stationary sinusoid.
|
xue@1
|
662
|
xue@1
|
663 In: x[N]: windowed spectrum
|
xue@1
|
664 B: spectral truncation half width, in bins.
|
xue@1
|
665 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
666 epf: frequency error tolerance, in bins
|
xue@1
|
667 Out: a and pp: amplitude and phase estimates
|
xue@1
|
668
|
xue@1
|
669 Returns the frequency estimate, in bins.
|
xue@1
|
670 */
|
xue@1
|
671 double LSESinusoid(cdouble* x, int N, double B, int M, double* c, double iH2, double& a, double& pp, double epf)
|
xue@1
|
672 {
|
xue@1
|
673 struct l_hx {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dhxpeak; double hxpeak;} p={N, 0, 0, M, c, iH2, x, 0, 0}; //(l_hx *)¶ms;
|
xue@1
|
674 int dfshift=int(&((l_hx*)0)->dhxpeak);
|
xue@1
|
675
|
xue@1
|
676 int inp;
|
xue@1
|
677 double minp=0;
|
xue@1
|
678 for (int i=0; i<N; i++)
|
xue@1
|
679 {
|
xue@1
|
680 double lf=i, tmp;
|
xue@1
|
681 p.k1=ceil(lf-B); if (p.k1<0) p.k1=0;
|
xue@1
|
682 p.k2=floor(lf+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
683 tmp=IPWindow(lf, &p);
|
xue@1
|
684 if (minp<tmp) inp=i, minp=tmp;
|
xue@1
|
685 }
|
xue@1
|
686
|
xue@1
|
687 double f=inp;
|
xue@1
|
688 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
689 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
690 double tmp=Newton(f, ddIPWindow, &p, dfshift, epf);
|
xue@1
|
691 if (tmp==-1)
|
xue@1
|
692 {
|
xue@1
|
693 Search1Dmax(f, &p, IPWindow, inp-1, inp+1, &a, epf);
|
xue@1
|
694 }
|
xue@1
|
695 else
|
xue@1
|
696 a=p.hxpeak;
|
xue@1
|
697 pp=IPWindow(f, x, N, M, c, iH2, p.k1, p.k2, false);
|
xue@1
|
698 return f;
|
xue@1
|
699 }//LSESinusoid
|
xue@1
|
700
|
xue@1
|
701 /*function LSESinusoid: LSE estimation of stationary sinusoid near a given initial frequency.
|
xue@1
|
702
|
xue@1
|
703 In: x[N]: windowed spectrum
|
xue@1
|
704 f: initial frequency, in bins
|
xue@1
|
705 B: spectral truncation half width, in bins.
|
xue@1
|
706 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
707 epf: frequency error tolerance, in bins
|
xue@1
|
708 Out: f, a and pp: frequency, amplitude and phase estimates
|
xue@1
|
709
|
xue@1
|
710 No return value.
|
xue@1
|
711 */
|
xue@1
|
712 void LSESinusoid(double& f, cdouble* x, int N, double B, int M, double* c, double iH2, double& a, double& pp, double epf)
|
xue@1
|
713 {
|
xue@1
|
714 struct l_hx {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dhxpeak; double hxpeak;} p={N, 0, 0, M, c, iH2, x, 0, 0};
|
xue@1
|
715 int dfshift=int(&((l_hx*)0)->dhxpeak);
|
xue@1
|
716
|
xue@1
|
717 double inp=f;
|
xue@1
|
718 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
719 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
720 double tmp=Newton(f, ddIPWindow, &p, dfshift, epf);
|
xue@1
|
721 if (tmp==-1)
|
xue@1
|
722 {
|
xue@1
|
723 Search1Dmax(f, &p, IPWindow, inp-1, inp+1, &a, epf);
|
xue@1
|
724 }
|
xue@1
|
725 else
|
xue@1
|
726 a=p.hxpeak;
|
xue@1
|
727 pp=IPWindow(f, x, N, M, c, iH2, p.k1, p.k2, false);
|
xue@1
|
728 }//LSESinusoid
|
xue@1
|
729
|
xue@1
|
730 /*
|
xue@1
|
731 function LSESinusoid: LSE estimation of stationary sinusoid predominant within [f1, f2].
|
xue@1
|
732
|
xue@1
|
733 In: x[N]: windowed spectrum
|
xue@1
|
734 [f1, f2]: frequency range
|
xue@1
|
735 B: spectral truncation half width, in bins.
|
xue@1
|
736 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
737 epf: frequency error tolerance, in bins
|
xue@1
|
738 Out: a and pp: amplitude and phase estimates
|
xue@1
|
739
|
xue@1
|
740 Returns the frequency estimate, in bins.
|
xue@1
|
741 */
|
xue@1
|
742 double LSESinusoid(int f1, int f2, cdouble* x, int N, double B, int M, double* c, double iH2, double& a, double& pp, double epf)
|
xue@1
|
743 {
|
xue@1
|
744 struct l_hx {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dhxpeak; double hxpeak;} p={N, 0, 0, M, c, iH2, x, 0, 0};
|
xue@1
|
745 int dfshift=int(&((l_hx*)0)->dhxpeak);
|
xue@1
|
746
|
xue@1
|
747 int inp;
|
xue@1
|
748 double minp=0;
|
xue@1
|
749 for (int i=f1; i<f2; i++)
|
xue@1
|
750 {
|
xue@1
|
751 double lf=i, tmp;
|
xue@1
|
752 p.k1=ceil(lf-B); if (p.k1<0) p.k1=0;
|
xue@1
|
753 p.k2=floor(lf+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
754 tmp=IPWindow(lf, &p);
|
xue@1
|
755 if (minp<tmp) inp=i, minp=tmp;
|
xue@1
|
756 }
|
xue@1
|
757
|
xue@1
|
758 double f=inp;
|
xue@1
|
759 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
760 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
761 double tmp=Newton(f, ddIPWindow, &p, dfshift, epf);
|
xue@1
|
762 if (tmp==-1)
|
xue@1
|
763 {
|
xue@1
|
764 Search1Dmax(f, &p, IPWindow, inp-1, inp+1, &a, epf);
|
xue@1
|
765 }
|
xue@1
|
766 else
|
xue@1
|
767 a=p.hxpeak;
|
xue@1
|
768 pp=IPWindow(f, x, N, M, c, iH2, p.k1, p.k2, false);
|
xue@1
|
769 return f;
|
xue@1
|
770 }//LSESinusoid
|
xue@1
|
771
|
xue@1
|
772 /*
|
xue@1
|
773 function LSESinusoid: LSE estimation of stationary sinusoid near a given initial frequency within [f1,
|
xue@1
|
774 f2].
|
xue@1
|
775
|
xue@1
|
776 In: x[N]: windowed spectrum
|
xue@1
|
777 f: initial frequency, in bins
|
xue@1
|
778 [f1, f2]: frequency range
|
xue@1
|
779 B: spectral truncation half width, in bins.
|
xue@1
|
780 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
781 epf: frequency error tolerance, in bins
|
xue@1
|
782 Out: f, a and pp: frequency, amplitude and phase estimates
|
xue@1
|
783
|
xue@1
|
784 Returns 1 if managed to find a sinusoid, 0 if not, upon which $a and $pp are estimated at the initial
|
xue@1
|
785 f.
|
xue@1
|
786 */
|
xue@1
|
787 int LSESinusoid(double& f, double f1, double f2, cdouble* x, int N, double B, int M, double* c, double iH2, double& a, double& pp, double epf)
|
xue@1
|
788 {
|
xue@1
|
789 struct l_hx {int N; int k1; int k2; int M; double* c; double iH2; cdouble* x; double dhxpeak; double hxpeak;} p={N, 0, 0, M, c, iH2, x, 0, 0};//(l_hx *)¶ms;
|
xue@1
|
790 int dfshift=int(&((l_hx*)0)->dhxpeak);
|
xue@1
|
791
|
xue@1
|
792 int result=0;
|
xue@1
|
793 double inp=f;
|
xue@1
|
794 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
795 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
796 double tmp=Newton(f, ddIPWindow, &p, dfshift, epf, 100, 1e-256, f1, f2);
|
xue@1
|
797 if (tmp!=-1 && f>f1 && f<f2)
|
xue@1
|
798 {
|
xue@1
|
799 result=1;
|
xue@1
|
800 a=p.hxpeak;
|
xue@1
|
801 pp=IPWindow(f, x, N, M, c, iH2, p.k1, p.k2, false);
|
xue@1
|
802 }
|
xue@1
|
803 else
|
xue@1
|
804 {
|
xue@1
|
805 Search1DmaxEx(f, &p, IPWindow, f1, f2, &a, epf);
|
xue@1
|
806 if (f<=f1 || f>=f2)
|
xue@1
|
807 {
|
xue@1
|
808 f=inp;
|
xue@1
|
809 cdouble r=IPWindowC(f, x, N, M, c, iH2, p.k1, p.k2);
|
xue@1
|
810 a=abs(r);
|
xue@1
|
811 pp=arg(r);
|
xue@1
|
812 }
|
xue@1
|
813 else
|
xue@1
|
814 {
|
xue@1
|
815 result=1;
|
xue@1
|
816 pp=IPWindow(f, x, N, M, c, iH2, p.k1, p.k2, false);
|
xue@1
|
817 }
|
xue@1
|
818 }
|
xue@1
|
819 return result;
|
xue@1
|
820 }//LSESinusoid
|
xue@1
|
821
|
xue@1
|
822 /*
|
xue@1
|
823 function LSESinusoidMP: LSE estimation of a stationary sinusoid from multi-frames spectrogram without
|
xue@1
|
824 considering phase-frequency consistency across frames.
|
xue@1
|
825
|
xue@1
|
826 In: x[Fr][N]: spectrogram
|
xue@1
|
827 f: initial frequency, in bins
|
xue@1
|
828 [f1, f2]: frequency range
|
xue@1
|
829 B: spectral truncation half width, in bins.
|
xue@1
|
830 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
831 epf: frequency error tolerance, in bins
|
xue@1
|
832 Out: f, a[Fr] and ph[Fr]: frequency, amplitudes and phase angles estimates
|
xue@1
|
833
|
xue@1
|
834 Returns an error bound of the frequency estimate.
|
xue@1
|
835 */
|
xue@1
|
836 double LSESinusoidMP(double& f, double f1, double f2, cdouble** x, int Fr, int N, double B, int M, double* c, double iH2, double* a, double* ph, double epf)
|
xue@1
|
837 {
|
xue@1
|
838 struct l_ip1 {int N; int k1; int k2; int M; double* c; double iH2; int L; cdouble** x; double dsip; double sip; cdouble* lmd;} p={N, 0, 0, M, c,iH2, Fr, x, 0, 0, 0};
|
xue@1
|
839 int dfshift=int(&((l_ip1*)0)->dsip), fshift=int(&((l_ip1*)0)->sip);
|
xue@1
|
840
|
xue@1
|
841 double inp=f;
|
xue@1
|
842 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
843 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
844 double errf=Newton1dmax(f, f1, f2, ddsIPWindow, &p, dfshift, fshift, dsIPWindow, dfshift, epf);
|
xue@1
|
845 if (errf<0) errf=Search1Dmax(f, &p, sIPWindow, f1, f2, a, epf);
|
xue@1
|
846 if (a || ph)
|
xue@1
|
847 {
|
xue@1
|
848 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
849 {
|
xue@1
|
850 cdouble r=IPWindowC(f, x[fr], N, M, c, iH2, p.k1, p.k2);
|
xue@1
|
851 if (a) a[fr]=abs(r);
|
xue@1
|
852 if (ph) ph[fr]=arg(r);
|
xue@1
|
853 }
|
xue@1
|
854 }
|
xue@1
|
855 return errf;
|
xue@1
|
856 }//LSESinusoidMP
|
xue@1
|
857
|
xue@1
|
858 /*
|
xue@1
|
859 function LSESinusoidMP: LSE estimation of a stationary sinusoid from multi-frames spectrogram without
|
xue@1
|
860 considering phase-frequency consistency across frames.
|
xue@1
|
861
|
xue@1
|
862 In: x[Fr][N]: spectrogram
|
xue@1
|
863 f: initial frequency, in bins
|
xue@1
|
864 [f1, f2]: frequency range
|
xue@1
|
865 B: spectral truncation half width, in bins.
|
xue@1
|
866 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
867 epf: frequency error tolerance, in bins
|
xue@1
|
868 Out: f, a[Fr] and ph[Fr]: frequency, amplitudes and phase angles estimates
|
xue@1
|
869
|
xue@1
|
870 Returns an error bound of the frequency estimate. Although the frequencies are estimated assuming
|
xue@1
|
871 cross-frame frequency-phase consistency, the final output phase angles are reestimated independently
|
xue@1
|
872 for each frame using the frequency estimate.
|
xue@1
|
873 */
|
xue@1
|
874 double LSESinusoidMPC(double& f, double f1, double f2, cdouble** x, int Fr, int N, int Offst, double B, int M, double* c, double iH2, double* a, double* ph, double epf)
|
xue@1
|
875 {
|
xue@1
|
876 struct l_ip {int N; int k1; int k2; int M; double* c; double iH2; int L; double offst_rel; cdouble** x; double sdip; double sip;}
|
xue@1
|
877 p={N, 0, 0, M, c,iH2, Fr, Offst*1.0/N, x, 0, 0};
|
xue@1
|
878 int dfshift=int(&((l_ip*)0)->sdip), fshift=int(&((l_ip*)0)->sip);
|
xue@1
|
879
|
xue@1
|
880 double inp=f;
|
xue@1
|
881 p.k1=ceil(inp-B); if (p.k1<0) p.k1=0;
|
xue@1
|
882 p.k2=floor(inp+B); if (p.k2>=p.N/2) p.k2=p.N/2-1;
|
xue@1
|
883 double errf=Newton1dmax(f, f1, f2, ddsIPWindowC, &p, dfshift, fshift, dsIPWindowC, dfshift, epf);
|
xue@1
|
884 if (errf<0) errf=Search1Dmax(f, &p, sIPWindowC, f1, f2, a, epf);
|
xue@1
|
885 if (a || ph)
|
xue@1
|
886 {
|
xue@1
|
887 cdouble* lmd=new cdouble[Fr];
|
xue@1
|
888 sIPWindowC(f, Fr, Offst*1.0/N, x, N, M, c, iH2, p.k1, p.k2, lmd);
|
xue@1
|
889 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
890 {
|
xue@1
|
891 lmd[fr]=IPWindowC(f, x[fr], N, M, c, iH2, p.k1, p.k2);
|
xue@1
|
892
|
xue@1
|
893 if (a) a[fr]=abs(lmd[fr]);
|
xue@1
|
894 if (ph) ph[fr]=arg(lmd[fr]);
|
xue@1
|
895 }
|
xue@1
|
896 delete[] lmd;
|
xue@1
|
897 }
|
xue@1
|
898 return errf;
|
xue@1
|
899 }//LSESinusoidMPC
|
xue@1
|
900
|
xue@1
|
901 //---------------------------------------------------------------------------
|
xue@1
|
902 /*
|
xue@1
|
903 function IPMulti: least square estimation of multiple sinusoids, given their frequencies and an energy
|
xue@1
|
904 suppression index of eps, i.e. the least square error is minimized with an additional eps*||lmd||^2
|
xue@1
|
905 term.
|
xue@1
|
906
|
xue@1
|
907 In: x[Wid]: spectrum
|
xue@1
|
908 f[I]: frequencies
|
xue@1
|
909 M, c[]: cosine-family window specification parameters
|
xue@1
|
910 K1, K2: spectral truncation range, i.e. bins outside [K1, K2] are ignored
|
xue@1
|
911 eps: energy suppression factor
|
xue@1
|
912 Out: lmd[I]: amplitude-phase factors
|
xue@1
|
913
|
xue@1
|
914 No return value.
|
xue@1
|
915 */
|
xue@1
|
916 void IPMulti(int I, double* f, cdouble* lmd, cdouble* x, int Wid, int K1, int K2, int M, double* c, double eps)
|
xue@1
|
917 {
|
xue@1
|
918 if (K1<0) K1=0; if (K2>=Wid/2) K2=Wid/2-1; int K=K2-K1+1;
|
xue@1
|
919 MList* List=new MList;
|
xue@1
|
920 cdouble** Allocate2L(cdouble, I, K, wt, List);
|
xue@1
|
921 for (int i=0; i<I; i++) Window(wt[i], f[i], Wid, M, c, K1, K2);
|
xue@1
|
922 cdouble** whw=MultiplyXcXt(I, K, wt, List);
|
xue@1
|
923 cdouble* whx=MultiplyXcy(I, K, wt, &x[K1], List);
|
xue@1
|
924 for (int i=0; i<I; i++) whw[i][i]+=eps;
|
xue@1
|
925 GECP(I, lmd, whw, whx);
|
xue@1
|
926 delete List;
|
xue@1
|
927 }//IPMulti
|
xue@1
|
928
|
xue@1
|
929 /*
|
xue@1
|
930 function IPMulti: least square estimation of multiple sinusoids, given their frequencies and an energy
|
xue@1
|
931 suppression index of eps, and optionally returns residue and sensitivity indicators for each sinusoid.
|
xue@1
|
932
|
xue@1
|
933 In: x[Wid]: spectrum
|
xue@1
|
934 f[I]: frequencies
|
xue@1
|
935 M, c[]: cosine-family window specification parameters
|
xue@1
|
936 K1, K2: spectral truncation range, i.e. bins outside [K1, K2] are ignored
|
xue@1
|
937 eps: energy suppression factor
|
xue@1
|
938 Out: lmd[I]: amplitude-phase factors
|
xue@1
|
939 sens[I]: sensitivity indicators
|
xue@1
|
940 r1[I]: residue indicators, measured by correlating residue with sinusoid spectra, optional
|
xue@1
|
941
|
xue@1
|
942 No return value. Sensibitily is computed BEFORE applying eps.
|
xue@1
|
943 */
|
xue@1
|
944 void IPMulti(int I, double* f, cdouble* lmd, cfloat* x, int Wid, int K1, int K2, int M, double* c, double eps, double* sens, double* r1)
|
xue@1
|
945 {
|
xue@1
|
946 if (K1<0) K1=0; if (K2>=Wid/2) K2=Wid/2-1; int K=K2-K1+1;
|
xue@1
|
947 MList* List=new MList;
|
xue@1
|
948 cdouble** Allocate2L(cdouble, I, K, wt, List);
|
xue@1
|
949 for (int i=0; i<I; i++) Window(wt[i], f[i], Wid, M, c, K1, K2);
|
xue@1
|
950 cdouble** whw=MultiplyXcXt(I, K, wt, List);
|
xue@1
|
951
|
xue@1
|
952 //*computes sensitivity if required
|
xue@1
|
953 if (sens)
|
xue@1
|
954 {
|
xue@1
|
955 cdouble** iwhw=Copy(I, whw, List);
|
xue@1
|
956 GICP(I, iwhw);
|
xue@1
|
957 cdouble** u=MultiplyXYc(I, I, K, iwhw, wt, List);
|
xue@1
|
958 for (int i=0; i<I; i++)
|
xue@1
|
959 {
|
xue@1
|
960 sens[i]=0; for (int k=0; k<K; k++) sens[i]+=~u[i][k]; sens[i]=sqrt(sens[i]);
|
xue@1
|
961 }
|
xue@1
|
962 } //*/
|
xue@1
|
963 cdouble* whx=MultiplyXcy(I, K, wt, &x[K1], List);
|
xue@1
|
964 for (int i=0; i<I; i++) whw[i][i]+=eps;
|
xue@1
|
965 GECP(I, lmd, whw, whx);
|
xue@1
|
966 //compute residue if required
|
xue@1
|
967 if (r1)
|
xue@1
|
968 {
|
xue@1
|
969 cdouble* wlmd=MultiplyXty(K, I, wt, lmd, List); //reconstruct
|
xue@1
|
970 for (int k=0; k<K; k++) wlmd[k]=wlmd[k]-x[K1+k]; //-residue
|
xue@1
|
971 for (int i=0; i<I; i++) //r1[i]=Inner(K, wlmd, wt[i]).abs(); //-residue weighted by window
|
xue@1
|
972 {
|
xue@1
|
973 r1[i]=0;
|
xue@1
|
974 for (int k=0; k<K; k++) r1[i]+=abs(wlmd[k])*abs(wt[i][k]);
|
xue@1
|
975 }
|
xue@1
|
976 }
|
xue@1
|
977 delete List;
|
xue@1
|
978 }//IPMulti
|
xue@1
|
979
|
xue@1
|
980 /*
|
xue@1
|
981 function IPMultiSens: computes the sensitivity of the least square estimation of multiple sinusoids given
|
xue@1
|
982 their frequencies .
|
xue@1
|
983
|
xue@1
|
984 In: f[I]: frequencies
|
xue@1
|
985 M, c[]: cosine-family window specification parameters
|
xue@1
|
986 K1, K2: spectral truncation range, i.e. bins outside [K1, K2] are ignored
|
xue@1
|
987 eps: energy suppression factor
|
xue@1
|
988 Out: sens[I]: sensitivity indicators
|
xue@1
|
989
|
xue@1
|
990 No return value. Sensibility is computed AFTER applying eps
|
xue@1
|
991 */
|
xue@1
|
992 void IPMultiSens(int I, double* f, int Wid, int K1, int K2, int M, double* c, double* sens, double eps)
|
xue@1
|
993 {
|
xue@1
|
994 if (K1<0) K1=0; if (K2>=Wid/2) K2=Wid/2-1; int K=K2-K1+1;
|
xue@1
|
995 MList* List=new MList;
|
xue@1
|
996 cdouble** Allocate2L(cdouble, I, K, wt, List);
|
xue@1
|
997 for (int i=0; i<I; i++) Window(wt[i], f[i], Wid, M, c, K1, K2);
|
xue@1
|
998
|
xue@1
|
999 cdouble** whw=MultiplyXcXt(I, K, wt, List);
|
xue@1
|
1000 for (int i=0; i<I; i++) whw[i][i]+=eps;
|
xue@1
|
1001
|
xue@1
|
1002 cdouble** iwhw=Copy(I, whw, List);
|
xue@1
|
1003 GICP(I, iwhw);
|
xue@1
|
1004 cdouble** u=MultiplyXYc(I, I, K, iwhw, wt, List);
|
xue@1
|
1005 for (int i=0; i<I; i++)
|
xue@1
|
1006 {
|
xue@1
|
1007 sens[i]=0; for (int k=0; k<K; k++) sens[i]+=~u[i][k]; sens[i]=sqrt(sens[i]);
|
xue@1
|
1008 }
|
xue@1
|
1009 delete List;
|
xue@1
|
1010 }//IPMultiSens
|
xue@1
|
1011
|
xue@1
|
1012 /*
|
xue@1
|
1013 function IPMulti: least square estimation of multi-sinusoids with GIVEN frequencies. This version
|
xue@1
|
1014 operates in groups at least B bins from each other, rather than LSE all frequencies together.
|
xue@1
|
1015
|
xue@1
|
1016 In: x[Wid]: spectrum
|
xue@1
|
1017 f[I]: frequencies, must be ordered low to high.
|
xue@1
|
1018 B: number of bins beyond which sinusoids are treated as non-interfering
|
xue@1
|
1019 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
1020 Out: lmd[I]: amplitude-phase factors
|
xue@1
|
1021
|
xue@1
|
1022 Returns 0.
|
xue@1
|
1023 */
|
xue@1
|
1024 double IPMulti(int I, double* f, cdouble* lmd, cdouble* x, int Wid, int M, double* c, double iH2, int B)
|
xue@1
|
1025 {
|
xue@1
|
1026 int i=0, ist=0;
|
xue@1
|
1027 double Bw=B;
|
xue@1
|
1028 while (i<I)
|
xue@1
|
1029 {
|
xue@1
|
1030 if ((i>0 && f[i]-f[i-1]>Bw) || i==I-1)
|
xue@1
|
1031 {
|
xue@1
|
1032 if (i==I-1) i++;
|
xue@1
|
1033 //process frequencies from ist to i-1
|
xue@1
|
1034 if (i-1==ist) //one sinusoid
|
xue@1
|
1035 {
|
xue@1
|
1036 double fb=f[ist]; int K1=floor(fb-B+0.5), K2=floor(fb+B+0.5);
|
xue@1
|
1037 lmd[ist]=IPWindowC(fb, x, Wid, M, c, iH2, K1, K2);
|
xue@1
|
1038 }
|
xue@1
|
1039 else
|
xue@1
|
1040 {
|
xue@1
|
1041 MList* List=new MList;
|
xue@1
|
1042 int N=i-ist, K1=floor(f[ist]-B+0.5), K2=floor(f[i-1]+B+0.5), K=K2-K1+1;
|
xue@1
|
1043 cdouble** Allocate2L(cdouble, N, K, wt, List);
|
xue@1
|
1044 for (int n=0; n<N; n++) Window(wt[n], f[ist+n], Wid, M, c, K1, K2);
|
xue@1
|
1045 cdouble* whx=MultiplyXcy(N, K, wt, &x[K1], List); //w*'x=(wt*)x
|
xue@1
|
1046 cdouble** whw=MultiplyXcXt(N, K, wt, List);
|
xue@1
|
1047 /*debug cdouble** C=SubMatrix(0, whw, 1, 4, 1, 4, List); cdouble** C2=SubMatrix(0, whw, 1, 4, 1, 4, List); cdouble** Bh=SubMatrix(0, whw, 1, 4, 0, 1, List); cdouble* Y2=SubVector(0, whx, 1, 4);
|
xue@1
|
1048 cdouble x2[4]; cdouble x1=lmd[ist], Bhx1[4], dx2[4]; for (int j=0; j<4; j++) Bhx1[j]=x1^Bh[j][0]; GECP(4, x2, C, Y2); GECP(4, dx2, C2, Bhx1);*/
|
xue@1
|
1049 GECP(N, &lmd[ist], whw, whx); //solving complex linear system (w*'w)a=w*'x
|
xue@1
|
1050 delete List;
|
xue@1
|
1051 }
|
xue@1
|
1052 ist=i;
|
xue@1
|
1053 }
|
xue@1
|
1054 i++;
|
xue@1
|
1055 }
|
xue@1
|
1056 return 0;
|
xue@1
|
1057 }//IPMulti
|
xue@1
|
1058
|
xue@1
|
1059 /*
|
xue@1
|
1060 function IPMulti_Direct: LSE estimation of multiple sinusoids given frequencies AND PHASES (direct
|
xue@1
|
1061 method)
|
xue@1
|
1062
|
xue@1
|
1063 In: x[Wid]: spectrum
|
xue@1
|
1064 f[I], ph[I]: frequencies and phase angles.
|
xue@1
|
1065 B: spectral truncation half width, in bins; sinusoids over 3B bins apart are regarded non-interfering
|
xue@1
|
1066 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
1067 Out: a[I]: amplitudes
|
xue@1
|
1068
|
xue@1
|
1069 Returns square norm of the residue.
|
xue@1
|
1070 */
|
xue@1
|
1071 double IPMulti_Direct(int I, double* f, double* ph, double* a, cdouble* x, int Wid, int M, double* c, double iH2, int B)
|
xue@1
|
1072 {
|
xue@1
|
1073 MList* List=new MList;
|
xue@1
|
1074 int i=0, ist=0, hWid=Wid/2;
|
xue@1
|
1075 cdouble* r=Copy(hWid, x, List); //to store the residue
|
xue@1
|
1076
|
xue@1
|
1077 double Bw=3.0*B;
|
xue@1
|
1078 while (i<I)
|
xue@1
|
1079 {
|
xue@1
|
1080 if ((i>0 && f[i]-f[i-1]>Bw) || i==I-1)
|
xue@1
|
1081 {
|
xue@1
|
1082 if (i==I-1) i++;
|
xue@1
|
1083
|
xue@1
|
1084 //process frequencies from ist to i-1
|
xue@1
|
1085 if (i-1==ist) //one sinusoid
|
xue@1
|
1086 {
|
xue@1
|
1087 double fb=f[ist];
|
xue@1
|
1088 cdouble* w=Window(0, fb, Wid, M, c, 0, hWid-1);
|
xue@1
|
1089 for (int k=0; k<hWid; k++) w[k].rotate(ph[ist]);
|
xue@1
|
1090 double ip=Inner(2*hWid, (double*)x, (double*)w);
|
xue@1
|
1091 a[ist]=ip*iH2;
|
xue@1
|
1092 MultiAdd(hWid, r, r, w, -a[ist]);
|
xue@1
|
1093 delete[] w;
|
xue@1
|
1094 }
|
xue@1
|
1095 else
|
xue@1
|
1096 {
|
xue@1
|
1097 int N=i-ist;
|
xue@1
|
1098 cdouble** Allocate2L(cdouble, N, hWid, wt, List);
|
xue@1
|
1099 for (int n=0; n<N; n++)
|
xue@1
|
1100 {
|
xue@1
|
1101 Window(wt[n], f[ist+n], Wid, M, c, 0, hWid-1);
|
xue@1
|
1102 for (int k=0; k<hWid; k++) wt[n][k].rotate(ph[ist+n]);
|
xue@1
|
1103 }
|
xue@1
|
1104 double* whxr=MultiplyXy(N, hWid*2, (double**)wt, (double*)x, List); //w*'x=(wt*)x
|
xue@1
|
1105 double** whwr=MultiplyXXt(N, hWid*2, (double**)wt, List);
|
xue@1
|
1106 GECP(N, &a[ist], whwr, whxr); //solving complex linear system (w*'w)a=w*'x
|
xue@1
|
1107 for (int n=0; n<N; n++) MultiAdd(hWid, r, r, wt[n], -a[ist+n]);
|
xue@1
|
1108 }
|
xue@1
|
1109 ist=i;
|
xue@1
|
1110 }
|
xue@1
|
1111 i++;
|
xue@1
|
1112 }
|
xue@1
|
1113 double result=Inner(hWid, r, r).x;
|
xue@1
|
1114 delete List;
|
xue@1
|
1115 return result;
|
xue@1
|
1116 }//IPMulti_Direct
|
xue@1
|
1117
|
xue@1
|
1118 /*
|
xue@1
|
1119 function IPMulti_GS: LSE estimation of multiple sinusoids given frequencies AND PHASES (Gram-Schmidt method)
|
xue@1
|
1120
|
xue@1
|
1121 In: x[Wid]: spectrum
|
xue@1
|
1122 f[I], ph[I]: frequencies and phase angles.
|
xue@1
|
1123 B: spectral truncation, in bins; sinusoids over 3B bins apart are regarded non-interfering
|
xue@1
|
1124 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
1125 Out: a[I]: amplitudes
|
xue@1
|
1126
|
xue@1
|
1127 Returns square norm of the residue.
|
xue@1
|
1128 */
|
xue@1
|
1129 double IPMulti_GS(int I, double* f, double* ph, double* a, cdouble* x, int Wid, int M, double* c, double iH2, int B, double** L, double** Q)
|
xue@1
|
1130 {
|
xue@1
|
1131 MList* List=new MList;
|
xue@1
|
1132 int i=0, ist=0, hWid=Wid/2;
|
xue@1
|
1133 cdouble* r=Copy(hWid, x, List); //to store the residue
|
xue@1
|
1134 double Bw=3.0*B;
|
xue@1
|
1135 while (i<I)
|
xue@1
|
1136 {
|
xue@1
|
1137 if ((i>0 && f[i]-f[i-1]>Bw) || i==I-1)
|
xue@1
|
1138 {
|
xue@1
|
1139 if (i==I-1) i++;
|
xue@1
|
1140
|
xue@1
|
1141 //process frequencies from ist to i-1
|
xue@1
|
1142 if (i-1==ist) //one sinusoid
|
xue@1
|
1143 {
|
xue@1
|
1144 double fb=f[ist];
|
xue@1
|
1145 cdouble* w=Window(0, fb, Wid, M, c, 0, hWid-1);
|
xue@1
|
1146 for (int k=0; k<hWid; k++) w[k].rotate(ph[ist]);
|
xue@1
|
1147 double ip=Inner(2*hWid, (double*)x, (double*)w);
|
xue@1
|
1148 a[ist]=ip*iH2;
|
xue@1
|
1149 MultiAdd(hWid, r, r, w, -a[ist]);
|
xue@1
|
1150 delete[] w;
|
xue@1
|
1151 }
|
xue@1
|
1152 else
|
xue@1
|
1153 {
|
xue@1
|
1154 int N=i-ist;
|
xue@1
|
1155 cdouble** Allocate2L(cdouble, N, hWid, wt, List);
|
xue@1
|
1156 Alloc2L(N, N, L, List); Alloc2L(N, hWid*2, Q, List);
|
xue@1
|
1157 for (int n=0; n<N; n++)
|
xue@1
|
1158 {
|
xue@1
|
1159 Window(wt[n], f[ist+n], Wid, M, c, 0, hWid-1);
|
xue@1
|
1160 for (int k=0; k<hWid; k++) wt[n][k].rotate(ph[ist+n]);
|
xue@1
|
1161 }
|
xue@1
|
1162 LQ_GS(N, hWid*2, (double**)wt, L, Q);
|
xue@1
|
1163 double* atl=MultiplyxYt(N, hWid*2, (double*)x, Q, List);
|
xue@1
|
1164 GExL(N, &a[ist], L, atl);
|
xue@1
|
1165 for (int n=0; n<N; n++) MultiAdd(hWid, r, r, wt[n], -a[ist+n]);
|
xue@1
|
1166 }
|
xue@1
|
1167 ist=i;
|
xue@1
|
1168 }
|
xue@1
|
1169 i++;
|
xue@1
|
1170 }
|
xue@1
|
1171 double result=Inner(hWid, r, r).x;
|
xue@1
|
1172 delete List;
|
xue@1
|
1173 return result;
|
xue@1
|
1174 }//IPMulti_GS
|
xue@1
|
1175
|
xue@1
|
1176 /*
|
xue@1
|
1177 function IPMulti: LSE estimation of I sinusoids given frequency and phase and J sinusoids given
|
xue@1
|
1178 frequency only
|
xue@1
|
1179
|
xue@1
|
1180 In: x[Wid]: spectrum
|
xue@1
|
1181 f[I+J], ph[I]: frequencies and phase angles
|
xue@1
|
1182 M, c[], iH2: cosine-family window specification parameters
|
xue@1
|
1183 Out: a[I+J]: amplitudes
|
xue@1
|
1184 ph[I:I+J-1]: phase angles not given on start
|
xue@1
|
1185 wt[I+2J][hWid], Q[I+2J][hWid], L[I+2J][I+2J]: internal w matrix and its LQ factorization, optional
|
xue@1
|
1186
|
xue@1
|
1187 Returns the residue vector, newly created and registered to RetList, if specified. On start a[] should
|
xue@1
|
1188 have valid storage no less than I+2J.
|
xue@1
|
1189 */
|
xue@1
|
1190 cdouble* IPMulti(int I, int J, double* f, double* ph, double* a, cdouble* x, int Wid, int M, double* c, cdouble** wt, cdouble** Q, double** L, MList* RetList)
|
xue@1
|
1191 {
|
xue@1
|
1192 MList* List=new MList;
|
xue@1
|
1193 int hWid=Wid/2;
|
xue@1
|
1194 cdouble* r=Copy(hWid, x, RetList); //to store the residue
|
xue@1
|
1195 if (!wt){Allocate2L(cdouble, I+J*2, hWid, wt, List);}
|
xue@1
|
1196 if (!Q){Allocate2L(cdouble, I+J*2, hWid, Q, List);}
|
xue@1
|
1197 if (!L){Allocate2L(double, I+J*2, I+J*2, L, List);}
|
xue@1
|
1198 memset(wt[0], 0, sizeof(cdouble)*(I+J*2)*hWid);
|
xue@1
|
1199 memset(Q[0], 0, sizeof(cdouble)*(I+J*2)*hWid);
|
xue@1
|
1200 memset(L[0], 0, sizeof(double)*(I+J*2)*(I+J*2));
|
xue@1
|
1201
|
xue@1
|
1202 //*The direct form
|
xue@1
|
1203 for (int i=0; i<I; i++)
|
xue@1
|
1204 {
|
xue@1
|
1205 Window(wt[i], f[i], Wid, M, c, 0, hWid-1);
|
xue@1
|
1206 for (int k=0; k<hWid; k++) wt[i][k].rotate(ph[i]);
|
xue@1
|
1207 }
|
xue@1
|
1208 for (int j=0; j<J; j++)
|
xue@1
|
1209 {
|
xue@1
|
1210 cdouble *w1=wt[I+j*2], *w2=wt[I+j*2+1];
|
xue@1
|
1211 Window(w1, f[I+j], Wid, M, c, 0, hWid-1);
|
xue@1
|
1212 for (int k=0; k<hWid; k++) w2[k].y=w1[k].x, w2[k].x=-w1[k].y;
|
xue@1
|
1213 }
|
xue@1
|
1214
|
xue@1
|
1215 LQ_GS(I+J*2, hWid*2, (double**)wt, L, (double**)Q);
|
xue@1
|
1216 double *atl=MultiplyxYt(I+J*2, hWid*2, (double*)x, (double**)Q, List);
|
xue@1
|
1217 GExL(I+J*2, a, L, atl);
|
xue@1
|
1218
|
xue@1
|
1219 for (int i=0; i<I+J*2; i++) MultiAdd(hWid, r, r, wt[i], -a[i]);
|
xue@1
|
1220 for (int j=0; j<J; j++)
|
xue@1
|
1221 {
|
xue@1
|
1222 double xx=a[I+j*2], yy=a[I+j*2+1];
|
xue@1
|
1223 a[I+j]=sqrt(xx*xx+yy*yy);
|
xue@1
|
1224 ph[I+j]=atan2(yy, xx);
|
xue@1
|
1225 }
|
xue@1
|
1226 delete List;
|
xue@1
|
1227 return r;
|
xue@1
|
1228 }//IPMulti
|
xue@1
|
1229
|
xue@1
|
1230 //---------------------------------------------------------------------------
|
xue@1
|
1231 /*
|
xue@1
|
1232 Routines for estimation two sinusoids with 1 fixed and 1 flexible frequency
|
xue@1
|
1233
|
xue@1
|
1234 Further reading: "LSE estimation for 2 sinusoids with 1 at a fixed frequency.pdf"
|
xue@1
|
1235 */
|
xue@1
|
1236
|
xue@1
|
1237 /*
|
xue@1
|
1238 function WindowDuo: calcualtes the square norm of the inner product between windowed spectra of two
|
xue@1
|
1239 sinusoids at frequencies f1 and f2, df=f1-f2.
|
xue@1
|
1240
|
xue@1
|
1241 In: df: frequency difference, in bins
|
xue@1
|
1242 N: DFT size
|
xue@1
|
1243 M, d[]: cosine-family window specification parameters (see "further reading").
|
xue@1
|
1244 Out: w[0], the inner product, optional
|
xue@1
|
1245
|
xue@1
|
1246 Returns square norm of the inner product.
|
xue@1
|
1247 */
|
xue@1
|
1248 double WindowDuo(double df, int N, double* d, int M, cdouble* w)
|
xue@1
|
1249 {
|
xue@1
|
1250 double wr=0, wi=0;
|
xue@1
|
1251 for (int m=-2*M; m<=2*M; m++)
|
xue@1
|
1252 {
|
xue@1
|
1253 double ang=df+m, Omg=ang*M_PI, omg=Omg/N;
|
xue@1
|
1254 double si=sin(omg), co=cos(omg), sinn=sin(Omg);
|
xue@1
|
1255 double sa=(ang==0)?N:(sinn/si);
|
xue@1
|
1256 double dm; if (m<0) dm=d[-m]; else dm=d[m];
|
xue@1
|
1257 wr+=dm*sa*co, wi+=-dm*sinn;
|
xue@1
|
1258 }
|
xue@1
|
1259 wr*=N, wi*=N;
|
xue@1
|
1260 if (w) w->x=wr, w->y=wi;
|
xue@1
|
1261 double result=wr*wr+wi*wi;
|
xue@1
|
1262 return result;
|
xue@1
|
1263 }//WindowDuo
|
xue@1
|
1264
|
xue@1
|
1265 /*
|
xue@1
|
1266 function ddWindowDuo: calcualtes the square norm of the inner product between windowed spectra of two
|
xue@1
|
1267 sinusoids at frequencies f1 and f2, df=f1-f2, with its 1st and 2nd derivatives
|
xue@1
|
1268
|
xue@1
|
1269 In: df: frequency difference, in bins
|
xue@1
|
1270 N: DFT size
|
xue@1
|
1271 M, d[]: cosine-family window specification parameters (see "further reading" for d[]).
|
xue@1
|
1272 Out: w[0], the inner product, optional
|
xue@1
|
1273 window, dwindow: square norm and its derivative, of the inner product
|
xue@1
|
1274
|
xue@1
|
1275 Returns 2nd derivative of the square norm of the inner product.
|
xue@1
|
1276 */
|
xue@1
|
1277 double ddWindowDuo(double df, int N, double* d, int M, double& dwindow, double& window, cdouble* w)
|
xue@1
|
1278 {
|
xue@1
|
1279 double wr=0, wi=0, dwr=0, dwi=0, ddwr=0, ddwi=0, PI_N=M_PI/N, PIPI_N=PI_N*M_PI, PIPI=M_PI*M_PI;
|
xue@1
|
1280 for (int m=-2*M; m<=2*M; m++)
|
xue@1
|
1281 {
|
xue@1
|
1282 double ang=df+m, Omg=ang*M_PI, omg=Omg/N;
|
xue@1
|
1283 double si=sin(omg), co=cos(omg), sinn=sin(Omg), cosn=cos(Omg);
|
xue@1
|
1284 double sa=(ang==0)?N:(sinn/si), dsa=dsincd_unn(ang, N), ddsa=ddsincd_unn(ang, N);
|
xue@1
|
1285 double dm; if (m<0) dm=d[-m]; else dm=d[m];
|
xue@1
|
1286 wr+=dm*sa*co, wi+=-dm*sinn;
|
xue@1
|
1287 dwr+=dm*(dsa*co-PI_N*sinn), dwi+=-dm*M_PI*cosn;
|
xue@1
|
1288 ddwr+=dm*(ddsa*co-PI_N*dsa*si-PIPI_N*cosn), ddwi+=dm*PIPI*sinn;
|
xue@1
|
1289 }
|
xue@1
|
1290 wr*=N, wi*=N, dwr*=N, dwi*=N, ddwr*=N, ddwi*=N;
|
xue@1
|
1291 window=wr*wr+wi*wi;
|
xue@1
|
1292 dwindow=2*(wr*dwr+wi*dwi);
|
xue@1
|
1293 if (w) w->x=wr, w->y=wi;
|
xue@1
|
1294 double ddwindow=2*(wr*ddwr+dwr*dwr+wi*ddwi+dwi*dwi);
|
xue@1
|
1295 return ddwindow;
|
xue@1
|
1296 }//ddWindowDuo
|
xue@1
|
1297
|
xue@1
|
1298 /*
|
xue@1
|
1299 function sIPWindowDuo: calculates the square norm of the orthogonal projection of a windowed spectrum
|
xue@1
|
1300 onto the linear span of the windowed spectra of two sinusoids at reference frequencies f1 and f2.
|
xue@1
|
1301
|
xue@1
|
1302 In: x[N]: spectrum
|
xue@1
|
1303 f1, f2: reference frequencies.
|
xue@1
|
1304 M, c[], d[], iH2: cosine-family window specification parameters.
|
xue@1
|
1305 K1, K2: spectrum truncation range, i.e. bins outside [K1, K2] are ignored.
|
xue@1
|
1306 Out: lmd1, lmd2: projection coefficients, interpreted as actual amplitude-phase factors
|
xue@1
|
1307
|
xue@1
|
1308 Returns the square norm of the orthogonal projection.
|
xue@1
|
1309 */
|
xue@1
|
1310 double sIPWindowDuo(double f1, double f2, cdouble* x, int N, double* c, double* d, int M, double iH2, int K1, int K2, cdouble& lmd1, cdouble& lmd2)
|
xue@1
|
1311 {
|
xue@1
|
1312 int K=K2-K1+1;
|
xue@1
|
1313 cdouble xw1=0, *lx=&x[K1], *w1=new cdouble[K*2], *r1=&w1[K];
|
xue@1
|
1314 Window(w1, f1, N, M, c, K1, K2);
|
xue@1
|
1315 double w1w1=0;
|
xue@1
|
1316 for (int k=0; k<K; k++) xw1+=(lx[k]^w1[k]), w1w1+=~w1[k]; cdouble mu1=xw1/w1w1;
|
xue@1
|
1317 for (int k=0; k<K; k++) r1[k]=lx[k]-mu1*w1[k];
|
xue@1
|
1318 Window(w1, f2, N, M, c, K1, K2);
|
xue@1
|
1319 cdouble r1w2=0, w12; for (int k=0; k<K; k++) r1w2+=(r1[k]^w1[k]);
|
xue@1
|
1320 double w=WindowDuo(f1-f2, N, d, M, &w12);
|
xue@1
|
1321 double v=1.0/iH2-w*iH2;
|
xue@1
|
1322 double result=~xw1/w1w1+~r1w2/v;
|
xue@1
|
1323 cdouble mu2=r1w2/v;
|
xue@1
|
1324 lmd2=mu2; lmd1=mu1-(mu2^w12)*iH2;
|
xue@1
|
1325 delete[] w1;
|
xue@1
|
1326 return result;
|
xue@1
|
1327 }//sIPWindowDuo
|
xue@1
|
1328 //wrapper function
|
xue@1
|
1329 double sIPWindowDuo(double f2, void* params)
|
xue@1
|
1330 {
|
xue@1
|
1331 struct l_ip {int N; int k1; int k2; double* c; double* d; int M; double iH2; cdouble* x; double f1; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
1332 cdouble r1, r2;
|
xue@1
|
1333 return sIPWindowDuo(p->f1, f2, p->x, p->N, p->c, p->d, p->M, p->iH2, p->k1, p->k2, r1, r2);
|
xue@1
|
1334 }//sIPWindowDuo
|
xue@1
|
1335
|
xue@1
|
1336 /*
|
xue@1
|
1337 function ddsIPWindowDuo: calculates the square norm, and its 1st and 2nd derivatives against f2,, of
|
xue@1
|
1338 the orthogonal projection of a windowed spectrum onto the linear span of the windowed spectra of two
|
xue@1
|
1339 sinusoids at reference frequencies f1 and f2.
|
xue@1
|
1340
|
xue@1
|
1341 In: x[N]: spectrum
|
xue@1
|
1342 f1, f2: reference frequencies.
|
xue@1
|
1343 M, c[], d[], iH2: cosine-family window specification parameters.
|
xue@1
|
1344 K1, K2: spectrum truncation range, i.e. bins outside [K1, K2] are ignored.
|
xue@1
|
1345
|
xue@1
|
1346 Out: lmd1, lmd2: projection coefficients, interpreted as actual amplitude-phase factors
|
xue@1
|
1347 ddsip[3]: the 2nd, 1st and 0th derivatives (against f2) of the square norm.
|
xue@1
|
1348
|
xue@1
|
1349 No return value.
|
xue@1
|
1350 */
|
xue@1
|
1351 void ddsIPWindowDuo(double* ddsip2, double f1, double f2, cdouble* x, int N, double* c, double* d, int M, double iH2, int K1, int K2, cdouble& lmd1, cdouble& lmd2)
|
xue@1
|
1352 {
|
xue@1
|
1353 int K=K2-K1+1;
|
xue@1
|
1354 cdouble xw1=0, *lx=&x[K1], *w1=new cdouble[K*2], *r1=&w1[K];
|
xue@1
|
1355 Window(w1, f1, N, M, c, K1, K2);
|
xue@1
|
1356 double w1w1=0;
|
xue@1
|
1357 for (int k=0; k<K; k++) xw1+=(lx[k]^w1[k]), w1w1+=~w1[k]; cdouble mu1=xw1/w1w1;
|
xue@1
|
1358 for (int k=0; k<K; k++) r1[k]=lx[k]-mu1*w1[k];
|
xue@1
|
1359
|
xue@1
|
1360 cdouble r1w2, w12;
|
xue@1
|
1361 double u, du, ddu=ddsIPWindow_unn(f2, &r1[-K1], N, M, c, K1, K2, du, u, &r1w2);
|
xue@1
|
1362 double w, dw, ddw=ddWindowDuo(f1-f2, N, d, M, dw, w, &w12); dw=-dw;
|
xue@1
|
1363 double v=1.0/iH2-w*iH2, dv=-iH2*dw, ddv=-iH2*ddw;
|
xue@1
|
1364 double iv=1.0/v;//, div=-dv*iv*iv, ddiv=(2*dv*dv-v*ddv)*iv*iv*iv;
|
xue@1
|
1365
|
xue@1
|
1366 ddsip2[2]=~xw1/w1w1+u*iv;
|
xue@1
|
1367 ddsip2[1]=iv*(du-iv*u*dv);
|
xue@1
|
1368 ddsip2[0]=iv*(ddu-iv*(u*ddv+2*du*dv-2*iv*u*dv*dv));
|
xue@1
|
1369
|
xue@1
|
1370 cdouble mu2=r1w2*iv;
|
xue@1
|
1371 lmd2=mu2; lmd1=mu1-(mu2^w12)*iH2;
|
xue@1
|
1372
|
xue@1
|
1373 delete[] w1;
|
xue@1
|
1374 }//ddsIPWindowDuo
|
xue@1
|
1375 //wrapper function
|
xue@1
|
1376 double ddsIPWindowDuo(double f2, void* params)
|
xue@1
|
1377 {
|
xue@1
|
1378 struct l_ip {int N; int k1; int k2; double* c; double* d; int M; double iH2; cdouble* x; double f1; double dipwindow; double ipwindow;} *p=(l_ip *)params;
|
xue@1
|
1379 double ddsip2[3]; cdouble r1, r2;
|
xue@1
|
1380 ddsIPWindowDuo(ddsip2, p->f1, f2, p->x, p->N, p->c, p->d, p->M, p->iH2, p->k1, p->k2, r1, r2);
|
xue@1
|
1381 p->dipwindow=ddsip2[1], p->ipwindow=ddsip2[2];
|
xue@1
|
1382 return ddsip2[0];
|
xue@1
|
1383 }//ddsIPWindowDuo
|
xue@1
|
1384
|
xue@1
|
1385 /*
|
xue@1
|
1386 function LSEDuo: least-square estimation of two sinusoids of which one has a fixed frequency
|
xue@1
|
1387
|
xue@1
|
1388 In: x[N]: the windowed spectrum
|
xue@1
|
1389 f1: the fixed frequency
|
xue@1
|
1390 f2: initial value of the flexible frequency
|
xue@1
|
1391 fmin, fmax: search range for f2, the flexible frequency
|
xue@1
|
1392 B: spectral truncation half width
|
xue@1
|
1393 M, c[], d[], iH2:
|
xue@1
|
1394 epf: frequency error tolerance
|
xue@1
|
1395 Out: f2: frequency estimate
|
xue@1
|
1396 lmd1, lmd2: amplitude-phase factor estimates
|
xue@1
|
1397 Returns 1 if managed to find a good f2, 0 if not, upon which the initial f2 is used for estimating
|
xue@1
|
1398
|
xue@1
|
1399 amplitudes and phase angles.
|
xue@1
|
1400 */
|
xue@1
|
1401 int LSEDuo(double& f2, double fmin, double fmax, double f1, cdouble* x, int N, double B, double* c, double* d, int M, double iH2, cdouble& r1, cdouble &r2, double epf)
|
xue@1
|
1402 {
|
xue@1
|
1403 int result=0;
|
xue@1
|
1404 double inp=f2;
|
xue@1
|
1405 int k1=ceil(inp-B); if (k1<0) k1=0;
|
xue@1
|
1406 int k2=floor(inp+B); if (k2>=N/2) k2=N/2-1;
|
xue@1
|
1407 struct l_hx {int N; int k1; int k2; double* c; double* d; int M; double iH2; cdouble* x; double f1; double dipwindow; double ipwindow;} p={N, k1, k2, c, d, M, iH2, x, f1, 0, 0};
|
xue@1
|
1408 int dfshift=int(&((l_hx*)0)->dipwindow);// fshift=int(&((l_hx*)0)->ipwindow);
|
xue@1
|
1409
|
xue@1
|
1410 double tmp=Newton(f2, ddsIPWindowDuo, &p, dfshift, epf, 100, 1e-256, fmin, fmax);
|
xue@1
|
1411 if (tmp!=-1 && f2>fmin && f2<fmax) result=1;
|
xue@1
|
1412 else
|
xue@1
|
1413 {
|
xue@1
|
1414 Search1DmaxEx(f2, &p, sIPWindowDuo, fmin, fmax, NULL, epf);
|
xue@1
|
1415 if (f2<=fmin || f2>=fmax) f2=inp;
|
xue@1
|
1416 else result=1;
|
xue@1
|
1417 }
|
xue@1
|
1418 sIPWindowDuo(f1, f2, x, N, c, d, M, iH2, k1, k2, r1, r2);
|
xue@1
|
1419 return result;
|
xue@1
|
1420 }//LSEDuo
|
xue@1
|
1421
|
xue@1
|
1422 //---------------------------------------------------------------------------
|
xue@1
|
1423 /*
|
xue@1
|
1424 Time-frequency reassignment sinusoid estimation routines.
|
xue@1
|
1425
|
xue@1
|
1426 Further reading: A. R?bel, ¡°Estimating partial frequency and frequency slope using reassignment
|
xue@1
|
1427 operators,¡± in Proc. ICMC¡¯02. G?teborg. 2002.
|
xue@1
|
1428 */
|
xue@1
|
1429
|
xue@1
|
1430 /*
|
xue@1
|
1431 function CDFTW: single-frequency windowed DTFT, centre-aligned
|
xue@1
|
1432
|
xue@1
|
1433 In: data[Wid]: waveform data x
|
xue@1
|
1434 win[Wid+1]: window function
|
xue@1
|
1435 k: frequency, in bins, where bin=1/Wid
|
xue@1
|
1436 Out: X: DTFT of xw at frequency k bins
|
xue@1
|
1437
|
xue@1
|
1438 No return value.
|
xue@1
|
1439 */
|
xue@1
|
1440 void CDFTW(cdouble& X, double k, int Wid, cdouble* data, double* win)
|
xue@1
|
1441 {
|
xue@1
|
1442 X=0;
|
xue@1
|
1443 int hWid=Wid/2;
|
xue@1
|
1444 for (int i=0; i<Wid; i++)
|
xue@1
|
1445 {
|
xue@1
|
1446 cdouble tmp=data[i]*win[Wid-i];
|
xue@1
|
1447 double ph=-2*M_PI*(i-hWid)*k/Wid;
|
xue@1
|
1448 tmp.rotate(ph);
|
xue@1
|
1449 X+=tmp;
|
xue@1
|
1450 }
|
xue@1
|
1451 }//CDFTW
|
xue@1
|
1452
|
xue@1
|
1453 /*
|
xue@1
|
1454 function CuDFTW: single-frequency windowed DTFT of t*data[t], centre-aligned
|
xue@1
|
1455
|
xue@1
|
1456 In: data[Wid]: waveform data x
|
xue@1
|
1457 wid[Wid+1]: window function
|
xue@1
|
1458 k: frequency, in bins
|
xue@1
|
1459 Out: X: DTFT of txw at frequency k bins
|
xue@1
|
1460
|
xue@1
|
1461 No return value.
|
xue@1
|
1462 */
|
xue@1
|
1463 void CuDFTW(cdouble& X, int k, int Wid, cdouble* data, double* win)
|
xue@1
|
1464 {
|
xue@1
|
1465 X=0;
|
xue@1
|
1466 int hWid=Wid/2;
|
xue@1
|
1467 for (int i=0; i<Wid; i++)
|
xue@1
|
1468 {
|
xue@1
|
1469 double tw=((i-hWid)*win[Wid-i]);
|
xue@1
|
1470 cdouble tmp=data[i]*tw;
|
xue@1
|
1471 double ph=-2*M_PI*(i-hWid)*k/Wid;
|
xue@1
|
1472 tmp.rotate(ph);
|
xue@1
|
1473 X+=tmp;
|
xue@1
|
1474 }
|
xue@1
|
1475 }//CuDFTW
|
xue@1
|
1476
|
xue@1
|
1477 /*
|
xue@1
|
1478 function TFReas: time-frequency reassignment
|
xue@1
|
1479
|
xue@1
|
1480 In: data[Wid]: waveform data
|
xue@1
|
1481 win[Wid+1], dwin[Wid+1], ddwin[Wid+1]: window function and its derivatives
|
xue@1
|
1482 f, t: initial digital frequency and time
|
xue@1
|
1483 Out: f, t: reassigned digital frequency and time
|
xue@1
|
1484 fslope: estimate of frequency derivative
|
xue@1
|
1485 plogaslope[0]: estimate of the derivative of logarithmic amplitude, optional
|
xue@1
|
1486
|
xue@1
|
1487 No return value.
|
xue@1
|
1488 */
|
xue@1
|
1489 void TFReas(double& f, double& t, double& fslope, int Wid, cdouble* data, double* win, double* dwin, double* ddwin, double* plogaslope)
|
xue@1
|
1490 {
|
xue@1
|
1491 int fi=floor(f*Wid+0.5);
|
xue@1
|
1492
|
xue@1
|
1493 cdouble x, xt, xw;
|
xue@1
|
1494 CDFTW(x, fi, Wid, data, win);
|
xue@1
|
1495 CuDFTW(xw, fi, Wid, data, win); xt.x=xw.y; xw.y=-xw.x; xw.x=xt.x;
|
xue@1
|
1496 CDFTW(xt, fi, Wid, data, dwin);
|
xue@1
|
1497 double px=~x;
|
xue@1
|
1498 t=t-(xw.y*x.x-xw.x*x.y)/px;
|
xue@1
|
1499 f=1.0*fi/Wid+(xt.y*x.x-xt.x*x.y)/px/(2*M_PI);
|
xue@1
|
1500 if (plogaslope) plogaslope[0]=-(xt.x*x.x+xt.y*x.y)/px;
|
xue@1
|
1501 cdouble xtt, xtw;
|
xue@1
|
1502 CuDFTW(xtw, fi, Wid, data, dwin); xtt.x=xtw.y; xtw.y=-xtw.x; xtw.x=xtt.x;
|
xue@1
|
1503 CDFTW(xtt, fi, Wid, data, ddwin);
|
xue@1
|
1504 double dtdt=-(xtw.y*x.x-xtw.x*x.y)/px+((xt.y*x.x-xt.x*x.y)*(xw.x*x.x+xw.y*x.y)+(xt.x*x.x+xt.y*x.y)*(xw.y*x.x-xw.x*x.y))/px/px,
|
xue@1
|
1505 dwdt=(xtt.y*x.x-xtt.x*x.y)/px-2*(xt.x*x.x+xt.y*x.y)*(xt.y*x.x-xt.x*x.y)/px/px;
|
xue@1
|
1506 if (dtdt!=0) fslope=dwdt/dtdt/(2*M_PI);
|
xue@1
|
1507 else fslope=0;
|
xue@1
|
1508 } //TFReas*/
|
xue@1
|
1509
|
xue@1
|
1510 /*
|
xue@1
|
1511 function TFReas: sinusoid estimation using reassignment method
|
xue@1
|
1512
|
xue@1
|
1513 In: data[Wid]: waveform data
|
xue@1
|
1514 w[Wid+1], dw[Wid+1], ddw[Wid+1]: window function and its derivatives
|
xue@1
|
1515 win[Wid]: window function used for estimating amplitude and phase by projection onto a chirp
|
xue@1
|
1516 t: time for which the parameters are estimated
|
xue@1
|
1517 f: initial frequency at t
|
xue@1
|
1518 Out: f, a, ph: digital frequency, amplitude and phase angle estimated at t
|
xue@1
|
1519 fslope: frequency derivative estimate
|
xue@1
|
1520
|
xue@1
|
1521 No return value.
|
xue@1
|
1522 */
|
xue@1
|
1523 void TFReas(double& f, double t, double& a, double& ph, double& fslope, int Wid, cdouble* data, double* w, double* dw, double* ddw, double* win)
|
xue@1
|
1524 {
|
xue@1
|
1525 double localt=t, logaslope;
|
xue@1
|
1526 TFReas(f, localt, fslope, Wid, data, w, dw, ddw, &logaslope);
|
xue@1
|
1527
|
xue@1
|
1528 if (logaslope*Wid>6) logaslope=6.0/Wid;
|
xue@1
|
1529 else if (logaslope*Wid<-6) logaslope=-6.0/Wid;
|
xue@1
|
1530
|
xue@1
|
1531 f=f+fslope*(t-localt); //obtain frequency estimate at t
|
xue@1
|
1532
|
xue@1
|
1533 cdouble x=0;
|
xue@1
|
1534 if (win==0)
|
xue@1
|
1535 {
|
xue@1
|
1536 for (int n=0; n<Wid; n++)
|
xue@1
|
1537 {
|
xue@1
|
1538 double ni=n-t;
|
xue@1
|
1539 cdouble tmp=data[n];
|
xue@1
|
1540 double p=-2*M_PI*(f+0.5*fslope*ni)*ni;
|
xue@1
|
1541 tmp.rotate(p);
|
xue@1
|
1542 x+=tmp;
|
xue@1
|
1543 }
|
xue@1
|
1544 a=abs(x)/Wid;
|
xue@1
|
1545 }
|
xue@1
|
1546 else
|
xue@1
|
1547 {
|
xue@1
|
1548 double sumwin=0;
|
xue@1
|
1549 for (int n=0; n<Wid; n++)
|
xue@1
|
1550 {
|
xue@1
|
1551 double ni=n-t;
|
xue@1
|
1552 cdouble tmp=data[n]*win[n];
|
xue@1
|
1553 double p=-2*M_PI*(f+0.5*fslope*ni)*ni;
|
xue@1
|
1554 tmp.rotate(p);
|
xue@1
|
1555 x+=tmp; sumwin+=win[n];
|
xue@1
|
1556 }
|
xue@1
|
1557 a=abs(x)/sumwin;
|
xue@1
|
1558 }
|
xue@1
|
1559 ph=arg(x);
|
xue@1
|
1560 }//TFReas
|
xue@1
|
1561
|
xue@1
|
1562 //---------------------------------------------------------------------------
|
xue@1
|
1563 /*
|
xue@1
|
1564 Routines for additive and multiplicative reestimation of sinusoids.
|
xue@1
|
1565
|
xue@1
|
1566 Further reading: Wen X. and M. Sandler, "Additive and multiplicative reestimation schemes
|
xue@1
|
1567 for the sinusoid modeling of audio," in Proc. EUSIPCO'09, Glasgow, 2009.
|
xue@1
|
1568 */
|
xue@1
|
1569
|
xue@1
|
1570 /*
|
xue@1
|
1571 function AdditiveUpdate: additive reestimation of time-varying sinusoid
|
xue@1
|
1572
|
xue@1
|
1573 In: x[Count]: waveform data
|
xue@1
|
1574 Wid, Offst: frame size and hop
|
xue@1
|
1575 fs[Count], as[Count], phs[Count]: initial estimate of sinusoid parameters
|
xue@1
|
1576 das[Count]: initial estimate of amplitude derivative
|
xue@1
|
1577 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1578 LogA: indicates if amplitudes are interpolated at cubic spline or exponential cubic spline
|
xue@1
|
1579 Out: fs[Count], as[Count], phs[Count], das[Count]: estimates after additive update
|
xue@1
|
1580
|
xue@1
|
1581 No return value.
|
xue@1
|
1582 */
|
xue@1
|
1583 void AdditiveUpdate(double* fs, double* as, double* phs, double* das, cdouble* x, int Count, int Wid, int Offst, TBasicAnalyzer BasicAnalyzer, int reserved, bool LogA)
|
xue@1
|
1584 {
|
xue@1
|
1585 int HWid=Wid/2, Fr=(Count-Wid)/Offst+1;
|
xue@1
|
1586
|
xue@1
|
1587 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
1588 {
|
xue@1
|
1589 int i=HWid+Offst*fr;
|
xue@1
|
1590 if (fs[i]<0 || fs[i]>0.5){}
|
xue@1
|
1591 }
|
xue@1
|
1592
|
xue@1
|
1593 cdouble *y=new cdouble[Count];
|
xue@1
|
1594 double *lf=new double[Count*4], *la=&lf[Count], *lp=&lf[Count*2], *lda=&lf[Count*3];
|
xue@1
|
1595
|
xue@1
|
1596 __int16* ref=new __int16[Count];
|
xue@1
|
1597 for (int i=0; i<Count; i++) y[i]=x[i].x-as[i]*cos(phs[i]), ref[i]=floor(fs[i]*Wid+0.5);
|
xue@1
|
1598 memcpy(lf, fs, sizeof(double)*Count);
|
xue@1
|
1599 BasicAnalyzer(lf, la, lp, lda, y, Count, Wid, Offst, ref, reserved, LogA);
|
xue@1
|
1600
|
xue@1
|
1601 //merge and interpolate
|
xue@1
|
1602 double *fa=new double[Fr*12], *fb=&fa[Fr], *fc=&fa[Fr*2], *fd=&fa[Fr*3],
|
xue@1
|
1603 *aa=&fa[Fr*4], *ab=&aa[Fr], *ac=&aa[Fr*2], *ad=&aa[Fr*3],
|
xue@1
|
1604 *xs=&fa[Fr*8], *ffr=&xs[Fr], *afr=&xs[Fr*2], *pfr=&xs[Fr*3];
|
xue@1
|
1605 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
1606 {
|
xue@1
|
1607 int i=HWid+Offst*fr;
|
xue@1
|
1608 double a=as[i], b=la[i], fai=phs[i], thet=lp[i], f=fs[i], g=lf[i], delt=fai-thet, da=das[i], db=lda[i];
|
xue@1
|
1609 xs[fr]=i;
|
xue@1
|
1610 if (fabs(f-g)*Wid>1)
|
xue@1
|
1611 {
|
xue@1
|
1612 afr[fr]=a, pfr[fr]=fai, ffr[fr]=f;
|
xue@1
|
1613 }
|
xue@1
|
1614 else
|
xue@1
|
1615 {
|
xue@1
|
1616 double rr=a*cos(fai)+b*cos(thet);
|
xue@1
|
1617 double ii=a*sin(fai)+b*sin(thet);
|
xue@1
|
1618 ffr[fr]=(a*f*(a+b*cos(delt))+b*g*(b+a*cos(delt))+(da*b-a*db)*sin(delt)/(2*M_PI))/(a*a+b*b+2*a*b*cos(delt));
|
xue@1
|
1619 afr[fr]=sqrt(rr*rr+ii*ii);
|
xue@1
|
1620 pfr[fr]=atan2(ii, rr);
|
xue@1
|
1621 }
|
xue@1
|
1622 if (LogA) afr[fr]=log(afr[fr]);
|
xue@1
|
1623 }
|
xue@1
|
1624 CubicSpline(Fr-1, fa, fb, fc, fd, xs, ffr, 1, 1);
|
xue@1
|
1625 CubicSpline(Fr-1, aa, ab, ac, ad, xs, afr, 1, 1);
|
xue@1
|
1626 for (int fr=0; fr<Fr-1; fr++) Sinusoid(&fs[int(xs[fr])], &as[int(xs[fr])], &phs[int(xs[fr])], &das[int(xs[fr])], 0, Offst, aa[fr], ab[fr], ac[fr], ad[fr], fa[fr], fb[fr], fc[fr], fd[fr], pfr[fr], pfr[fr+1], LogA);
|
xue@1
|
1627 Sinusoid(&fs[int(xs[0])], &as[int(xs[0])], &phs[int(xs[0])], &das[int(xs[0])], -HWid, 0, aa[0], ab[0], ac[0], ad[0], fa[0], fb[0], fc[0], fd[0], pfr[0], pfr[1], LogA);
|
xue@1
|
1628 Sinusoid(&fs[int(xs[Fr-2])], &as[int(xs[Fr-2])], &phs[int(xs[Fr-2])], &das[int(xs[Fr-2])], Offst, Offst+HWid, aa[Fr-2], ab[Fr-2], ac[Fr-2], ad[Fr-2], fa[Fr-2], fb[Fr-2], fc[Fr-2], fd[Fr-2], pfr[Fr-2], pfr[Fr-1], LogA);
|
xue@1
|
1629 delete[] fa; //*/
|
xue@1
|
1630 /*
|
xue@1
|
1631 for (int i=0; i<Count; i++)
|
xue@1
|
1632 {
|
xue@1
|
1633 double rr=as[i]*cos(phs[i])+la[i]*cos(lp[i]);
|
xue@1
|
1634 double ii=as[i]*sin(phs[i])+la[i]*sin(lp[i]);
|
xue@1
|
1635 as[i]=sqrt(rr*rr+ii*ii);
|
xue@1
|
1636 phs[i]=atan2(ii, rr);
|
xue@1
|
1637 } //*/
|
xue@1
|
1638 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
1639 {
|
xue@1
|
1640 int i=HWid+Offst*fr;
|
xue@1
|
1641 if (fs[i]<0 || fs[i]>0.5){}
|
xue@1
|
1642 }
|
xue@1
|
1643 delete[] y; delete[] lf; delete[] ref;
|
xue@1
|
1644 }//AdditiveUpdate
|
xue@1
|
1645
|
xue@1
|
1646 /*
|
xue@1
|
1647 function AdditiveAnalyzer: sinusoid analyzer with one additive update
|
xue@1
|
1648
|
xue@1
|
1649 In: x[Count]: waveform data
|
xue@1
|
1650 Wid, Offst: frame size and hop size
|
xue@1
|
1651 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1652 ref[Count]: reference frequencies, in bins, used by BasicAnalyzer
|
xue@1
|
1653 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1654 LogA: indicates if amplitudes are interpolated at cubic spline or exponential cubic spline
|
xue@1
|
1655 Out: fs[Count], as[Count], phs[Count]: sinusoid parameter estimates
|
xue@1
|
1656 das[Count]: estimate of amplitude derivative
|
xue@1
|
1657
|
xue@1
|
1658 No return value.
|
xue@1
|
1659 */
|
xue@1
|
1660 void AdditiveAnalyzer(double* fs, double* as, double* phs, double* das, cdouble* x, int Count, int Wid, int Offst, __int16* ref, TBasicAnalyzer BasicAnalyzer, int reserved, bool LogA)
|
xue@1
|
1661 {
|
xue@1
|
1662 BasicAnalyzer(fs, as, phs, das, x, Count, Wid, Offst, ref, reserved, LogA);
|
xue@1
|
1663 AdditiveUpdate(fs, as, phs, das, x, Count, Wid, Offst, BasicAnalyzer, reserved, LogA);
|
xue@1
|
1664 }//AdditiveAnalyzer
|
xue@1
|
1665
|
xue@1
|
1666 /*
|
xue@1
|
1667 function MultiplicativeUpdate: multiplicative reestimation of time-varying sinusoid
|
xue@1
|
1668
|
xue@1
|
1669 In: x[Count]: waveform data
|
xue@1
|
1670 Wid, Offst: frame size and hop
|
xue@1
|
1671 fs[Count], as[Count], phs[Count]: initial estimate of sinusoid parameters
|
xue@1
|
1672 das[Count]: initial estimate of amplitude derivative
|
xue@1
|
1673 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1674 LogA: indicates if amplitudes are interpolated at cubic spline or exponential cubic spline
|
xue@1
|
1675 Out: fs[Count], as[Count], phs[Count], das[Count]: estimates after additive update
|
xue@1
|
1676
|
xue@1
|
1677 No return value.
|
xue@1
|
1678 */
|
xue@1
|
1679 void MultiplicativeUpdate(double* fs, double* as, double* phs, double* das, cdouble* x, int Count, int Wid, int Offst, TBasicAnalyzer BasicAnalyzer, int reserved, bool LogA)
|
xue@1
|
1680 {
|
xue@1
|
1681 int HWid=Wid/2;
|
xue@1
|
1682 cdouble *y=new cdouble[Count];
|
xue@1
|
1683 double *lf=new double[Count*8], *la=&lf[Count], *lp=&lf[Count*2], *lda=&lf[Count*3],
|
xue@1
|
1684 *lf2=&lf[Count*4], *la2=&lf2[Count], *lp2=&lf2[Count*2], *lda2=&lf2[Count*3];
|
xue@1
|
1685 __int16 *lref=new __int16[Count];
|
xue@1
|
1686
|
xue@1
|
1687 for (int i=0; i<Count; i++) y[i]=x[i]*(cdouble(1.0).rotate(-phs[i]+i*0.15*2*M_PI)),
|
xue@1
|
1688 lref[i]=0.15*Wid;
|
xue@1
|
1689 BasicAnalyzer(lf, la, lp, lda, y, Count, Wid, Offst, lref, reserved, LogA);
|
xue@1
|
1690 for (int i=0; i<Count; i++) y[i]=y[i]*(cdouble(1.0/la[i]).rotate(-lp[i]+i*0.15*2*M_PI)), lref[i]=0.15*Wid;
|
xue@1
|
1691 BasicAnalyzer(lf2, la2, lp2, lda2, y, Count, Wid, Offst, lref, reserved, LogA);
|
xue@1
|
1692
|
xue@1
|
1693 /*
|
xue@1
|
1694 for (int i=0; i<Count; i++)
|
xue@1
|
1695 {
|
xue@1
|
1696 as[i]=la[i]*la2[i];
|
xue@1
|
1697 phs[i]=phs[i]+lp[i]+lp2[i]-0.3*2*M_PI*i;
|
xue@1
|
1698 fs[i]=fs[i]+lf[i]+lf2[i]-0.3;
|
xue@1
|
1699 } //*/
|
xue@1
|
1700
|
xue@1
|
1701 //merge
|
xue@1
|
1702 int Fr=(Count-Wid)/Offst+1;
|
xue@1
|
1703 double *fa=new double[Fr*12], *fb=&fa[Fr], *fc=&fa[Fr*2], *fd=&fa[Fr*3],
|
xue@1
|
1704 *aa=&fa[Fr*4], *ab=&aa[Fr], *ac=&aa[Fr*2], *ad=&aa[Fr*3],
|
xue@1
|
1705 *xs=&fa[Fr*8], *ffr=&xs[Fr], *afr=&xs[Fr*2], *pfr=&xs[Fr*3];
|
xue@1
|
1706 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
1707 {
|
xue@1
|
1708 int i=HWid+Offst*fr;
|
xue@1
|
1709 xs[fr]=i;
|
xue@1
|
1710 afr[fr]=la[i]*la2[i];
|
xue@1
|
1711 if (LogA) afr[fr]=log(afr[fr]);
|
xue@1
|
1712 ffr[fr]=fs[i]+lf[i]-0.15+lf2[i]-0.15;
|
xue@1
|
1713 pfr[fr]=phs[i]+lp[i]+lp2[i]-0.3*i*2*M_PI;
|
xue@1
|
1714 }
|
xue@1
|
1715 CubicSpline(Fr-1, fa, fb, fc, fd, xs, ffr, 1, 1);
|
xue@1
|
1716 CubicSpline(Fr-1, aa, ab, ac, ad, xs, afr, 1, 1);
|
xue@1
|
1717 for (int fr=0; fr<Fr-1; fr++) Sinusoid(&fs[int(xs[fr])], &as[int(xs[fr])], &phs[int(xs[fr])], &das[int(xs[fr])], 0, Offst, aa[fr], ab[fr], ac[fr], ad[fr], fa[fr], fb[fr], fc[fr], fd[fr], pfr[fr], pfr[fr+1], LogA);
|
xue@1
|
1718 Sinusoid(&fs[int(xs[0])], &as[int(xs[0])], &phs[int(xs[0])], &das[int(xs[0])], -HWid, 0, aa[0], ab[0], ac[0], ad[0], fa[0], fb[0], fc[0], fd[0], pfr[0], pfr[1], LogA);
|
xue@1
|
1719 Sinusoid(&fs[int(xs[Fr-2])], &as[int(xs[Fr-2])], &phs[int(xs[Fr-2])], &das[int(xs[Fr-2])], Offst, Offst+HWid, aa[Fr-2], ab[Fr-2], ac[Fr-2], ad[Fr-2], fa[Fr-2], fb[Fr-2], fc[Fr-2], fd[Fr-2], pfr[Fr-2], pfr[Fr-1], LogA);
|
xue@1
|
1720 delete[] fa; //*/
|
xue@1
|
1721
|
xue@1
|
1722 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
1723 {
|
xue@1
|
1724 int i=HWid+Offst*fr;
|
xue@1
|
1725 if (fs[i]<0 || fs[i]>0.5){}
|
xue@1
|
1726 }
|
xue@1
|
1727
|
xue@1
|
1728 delete[] y; delete[] lf; delete[] lref;
|
xue@1
|
1729 }//MultiplicativeUpdate
|
xue@1
|
1730
|
xue@1
|
1731 /*
|
xue@1
|
1732 function MultiplicativeAnalyzer: sinusoid analyzer with one multiplicative update
|
xue@1
|
1733
|
xue@1
|
1734 In: x[Count]: waveform data
|
xue@1
|
1735 Wid, Offst: frame size and hop size
|
xue@1
|
1736 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1737 ref[Count]: reference frequencies, in bins, used by BasicAnalyzer
|
xue@1
|
1738 BasicAnalyzer: pointer to a sinusoid analyzer
|
xue@1
|
1739 LogA: indicates if amplitudes are interpolated at cubic spline or exponential cubic spline
|
xue@1
|
1740 Out: fs[Count], as[Count], phs[Count]: sinusoid parameter estimates
|
xue@1
|
1741 das[Count]: estimate of amplitude derivative
|
xue@1
|
1742
|
xue@1
|
1743 No return value.
|
xue@1
|
1744 */
|
xue@1
|
1745 void MultiplicativeAnalyzer(double* fs, double* as, double* phs, double* das, cdouble* x, int Count, int Wid, int Offst, __int16* ref, TBasicAnalyzer BasicAnalyzer, int reserved, bool LogA)
|
xue@1
|
1746 {
|
xue@1
|
1747 BasicAnalyzer(fs, as, phs, das, x, Count, Wid, Offst, ref, reserved, LogA);
|
xue@1
|
1748 MultiplicativeUpdate(fs, as, phs, das, x, Count, Wid, Offst, BasicAnalyzer, reserved);
|
xue@1
|
1749 }//MultiplicativeAnalyzer
|
xue@1
|
1750
|
xue@1
|
1751 /*
|
xue@1
|
1752 This is an earlier version of the multiplicative method without using a user-provided BasicAnalyzer.
|
xue@1
|
1753 This updates the sinusoid estimates at the selected consecutive FRAMES of x. Only frequency modulation
|
xue@1
|
1754 is included in the multiplier. The first frame (0) is centred at x[Wid/2]. fs, as, and phs are based
|
xue@1
|
1755 on frames rather than samples. Updates include frame frst, but not frame fren.
|
xue@1
|
1756 */
|
xue@1
|
1757 void MultiplicativeUpdateF(double* fs, double* as, double* phs, __int16* x, int Fr, int frst, int fren, int Wid, int Offst)
|
xue@1
|
1758 {
|
xue@1
|
1759 int HWid=Wid/2;
|
xue@1
|
1760
|
xue@1
|
1761 double *fa=new double[Fr*12], *fb=&fa[Fr], *fc=&fa[Fr*2], *fd=&fa[Fr*3],
|
xue@1
|
1762 *xs=&fa[Fr*8];
|
xue@1
|
1763 for (int fr=0; fr<Fr; fr++) xs[fr]=HWid+Offst*fr;
|
xue@1
|
1764 CubicSpline(Fr-1, fa, fb, fc, fd, xs, fs, 1, 1);
|
xue@1
|
1765
|
xue@1
|
1766 int dst=Offst*frst, den=Offst*(fren-1)+Wid, dcount=den-dst;
|
xue@1
|
1767 double *f=new double[dcount*2], *ph=&f[dcount];
|
xue@1
|
1768 for (int fr=frst; fr<fren-1; fr++) Sinusoid(&f[int(xs[fr])-dst], &ph[int(xs[fr])-dst], 0, Offst, fa[fr], fb[fr], fc[fr], fd[fr], phs[fr], phs[fr+1]);
|
xue@1
|
1769 if (frst==0) Sinusoid(&f[int(xs[0])-dst], &ph[int(xs[0])-dst], -HWid, 0, fa[0], fb[0], fc[0], fd[0], phs[0], phs[1]);
|
xue@1
|
1770 else Sinusoid(&f[int(xs[frst-1])-dst], &ph[int(xs[frst-1])-dst], 0, Offst, fa[frst-1], fb[frst-1], fc[frst-1], fd[frst-1], phs[frst-1], phs[frst]);
|
xue@1
|
1771 if (fren==Fr) Sinusoid(&f[int(xs[fren-2])-dst], &ph[int(xs[fren-2])-dst], Offst, Offst+HWid, fa[fren-2], fb[fren-2], fc[fren-2], fd[fren-2], phs[fren-2], phs[fren-1]);
|
xue@1
|
1772 else Sinusoid(&f[int(xs[fren-1])-dst], &ph[int(xs[fren-1])-dst], 0, Offst, fa[fren-1], fb[fren-1], fc[fren-1], fd[fren-1], phs[fren-1], phs[fren]);
|
xue@1
|
1773
|
xue@1
|
1774 cdouble* y=new cdouble[Wid];
|
xue@1
|
1775 AllocateFFTBuffer(Wid, Amp, W, X);
|
xue@1
|
1776 double* win=NewWindow(wtHann, Wid);
|
xue@1
|
1777 int M; double c[10], iH2; windowspec(wtHann, Wid, &M, c, &iH2);
|
xue@1
|
1778 for (int fr=frst; fr<fren; fr++)
|
xue@1
|
1779 {
|
xue@1
|
1780 __int16* lx=&x[Offst*fr];
|
xue@1
|
1781 double* lph=&ph[Offst*(fr-frst)];
|
xue@1
|
1782 for (int i=0; i<Wid; i++) y[i]=cdouble(lx[i]).rotate(-lph[i]+i*0.15*2*M_PI);
|
xue@1
|
1783 CFFTCW(y, win, Amp, 0, log2(Wid), W, X);
|
xue@1
|
1784 int pf=0.15*Wid, mpf=pf;
|
xue@1
|
1785 for (int k=pf-4; k<=pf+4; k++) if (Amp[k]>Amp[mpf]) mpf=k;
|
xue@1
|
1786 if (mpf>pf-4 && mpf<pf+4) pf=mpf;
|
xue@1
|
1787 double lfs=pf, lphs;
|
xue@1
|
1788 LSESinusoid(lfs, pf-3, pf+3, X, Wid, 3, M, c, iH2, as[fr], lphs, 1e-3);
|
xue@1
|
1789 fs[fr]=fs[fr]+lfs/Wid-0.15;
|
xue@1
|
1790 phs[fr]+=lphs-0.15*Wid*M_PI;
|
xue@1
|
1791 as[fr]*=2;
|
xue@1
|
1792 }
|
xue@1
|
1793
|
xue@1
|
1794 delete[] y;
|
xue@1
|
1795 delete[] f;
|
xue@1
|
1796 delete[] win;
|
xue@1
|
1797 delete[] fa;
|
xue@1
|
1798 FreeFFTBuffer(Amp);
|
xue@1
|
1799 }//MultiplicativeUpdateF
|
xue@1
|
1800
|
xue@1
|
1801 //---------------------------------------------------------------------------
|
xue@1
|
1802 /*
|
xue@1
|
1803 Earlier reestimation method routines.
|
xue@1
|
1804
|
xue@1
|
1805 Further reading: Wen X. and M. Sandler, "Evaluating parameters of time-varying
|
xue@1
|
1806 sinusoids by demodulation," in Proc. DAFx'08, Espoo, 2008.
|
xue@1
|
1807 */
|
xue@1
|
1808
|
xue@1
|
1809 /*
|
xue@1
|
1810 function ReEstFreq: sinusoid reestimation by demodulating frequency.
|
xue@1
|
1811
|
xue@1
|
1812 In: x[Wid+Offst*(FrCount-1)]: waveform data
|
xue@1
|
1813 FrCount, Wid, Offst: frame count, frame size and hop size
|
xue@1
|
1814 fbuf[FrCount], ns[FrCount]: initial frequency estiamtes and their timing
|
xue@1
|
1815 win[Wid]: window function for estimating demodulated sinusoid
|
xue@1
|
1816 M, c[], iH2: cosine-family window specification parameters, must be consistent with win[]
|
xue@1
|
1817 Wids[FrCount]: specifies frame sizes for estimating individual frames of demodulated sinusoid, optional
|
xue@1
|
1818 w[Wid/2], ps[Wid], xs[Wid], xc[Wid], fa[FrCount-1], fb[FrCount-1], fc[FrCount-1], fd[FrCount-1]: buffers
|
xue@1
|
1819 Out: fbuf[FrCount], abuf[FrCount], pbuf[FrCount]: reestimated frequencies, amplitudes and phase angles
|
xue@1
|
1820
|
xue@1
|
1821 No return value.
|
xue@1
|
1822 */
|
xue@1
|
1823 void ReEstFreq(int FrCount, int Wid, int Offst, double* x, double* fbuf, double* abuf, double* pbuf, double* win, int M, double* c, double iH2, cdouble* w, cdouble* xc, cdouble* xs, double* ps, double* fa, double* fb, double* fc, double* fd, double* ns, int* Wids)
|
xue@1
|
1824 {
|
xue@1
|
1825 int hWid=Wid/2;
|
xue@1
|
1826 //reestimate using frequency track
|
xue@1
|
1827 CubicSpline(FrCount-1, fa, fb, fc, fd, ns, fbuf, 0, 1);
|
xue@1
|
1828 for (int fr=0; fr<FrCount; fr++)
|
xue@1
|
1829 {
|
xue@1
|
1830 //find ps
|
xue@1
|
1831 if (fr==0)
|
xue@1
|
1832 {
|
xue@1
|
1833 double lfd=0, lfc=fc[0], lfb=fb[0], lfa=fa[0];
|
xue@1
|
1834 for (int j=0; j<Wid; j++)
|
xue@1
|
1835 {
|
xue@1
|
1836 double lx=j-hWid;
|
xue@1
|
1837 ps[j]=2*M_PI*lx*(lfd+lx*(lfc/2+lx*(lfb/3+lx*lfa/4)));
|
xue@1
|
1838 }
|
xue@1
|
1839 // memset(ps, 0, sizeof(double)*hWid);
|
xue@1
|
1840 }
|
xue@1
|
1841 else if (fr==FrCount-1)
|
xue@1
|
1842 {
|
xue@1
|
1843 int lfr=FrCount-2;
|
xue@1
|
1844 double lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
1845 double lfd=-(hWid*(lfc+hWid*(lfb+hWid*lfa)));
|
xue@1
|
1846 ps[0]=-2*M_PI*hWid*(lfd+hWid*(lfc/2+hWid*(lfb/3+hWid*lfa/4)));
|
xue@1
|
1847 for (int j=1; j<Wid; j++)
|
xue@1
|
1848 {
|
xue@1
|
1849 ps[j]=ps[0]+2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
1850 }
|
xue@1
|
1851 // memset(&ps[hWid], 0, sizeof(double)*hWid);
|
xue@1
|
1852 }
|
xue@1
|
1853 else
|
xue@1
|
1854 {
|
xue@1
|
1855 int lfr=fr-1;
|
xue@1
|
1856 double lfd=fd[lfr]-fd[fr], lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
1857 ps[0]=-2*M_PI*hWid*(lfd+hWid*(lfc/2+hWid*(lfb/3+hWid*lfa/4)));
|
xue@1
|
1858 for (int j=1; j<hWid+1; j++)
|
xue@1
|
1859 {
|
xue@1
|
1860 ps[j]=ps[0]+2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
1861 }
|
xue@1
|
1862 lfr=fr;
|
xue@1
|
1863 lfd=0, lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
1864 for (int j=1; j<hWid; j++)
|
xue@1
|
1865 {
|
xue@1
|
1866 ps[j+hWid]=2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
1867 }
|
xue@1
|
1868 }
|
xue@1
|
1869 double* ldata=&x[fr*Offst];
|
xue@1
|
1870 for (int j=0; j<Wid; j++)
|
xue@1
|
1871 {
|
xue@1
|
1872 xs[j].x=ldata[j]*cos(-ps[j]);
|
xue@1
|
1873 xs[j].y=ldata[j]*sin(-ps[j]);
|
xue@1
|
1874 }
|
xue@1
|
1875
|
xue@1
|
1876 if (Wids)
|
xue@1
|
1877 {
|
xue@1
|
1878 int lWid=Wids[fr], lhWid=Wids[fr]/2, lM;
|
xue@1
|
1879 SetTwiddleFactors(lWid, w);
|
xue@1
|
1880 double *lwin=NewWindow(wtHann, lWid), lc[4], liH2;
|
xue@1
|
1881 windowspec(wtHann, lWid, &lM, lc, &liH2);
|
xue@1
|
1882 CFFTCW(&xs[hWid-lhWid], lwin, NULL, NULL, log2(lWid), w, xc);
|
xue@1
|
1883 delete[] lwin;
|
xue@1
|
1884 double lf=fbuf[fr]*lWid, la, lp;
|
xue@1
|
1885 LSESinusoid(lf, lf-3, lf+3, xc, lWid, 3, lM, lc, liH2, la, lp, 1e-3);
|
xue@1
|
1886 if (la*2>abuf[fr]) fbuf[fr]=lf/lWid, abuf[fr]=la*2, pbuf[fr]=lp;
|
xue@1
|
1887 }
|
xue@1
|
1888 else
|
xue@1
|
1889 {
|
xue@1
|
1890 CFFTCW(xs, win, NULL, NULL, log2(Wid), w, xc);
|
xue@1
|
1891 double lf=fbuf[fr]*Wid, la, lp;
|
xue@1
|
1892 LSESinusoid(lf, lf-3, lf+3, xc, Wid, 3, M, c, iH2, la, lp, 1e-3);
|
xue@1
|
1893 if (la*2>abuf[fr])
|
xue@1
|
1894 fbuf[fr]=lf/Wid, abuf[fr]=la*2, pbuf[fr]=lp;
|
xue@1
|
1895 }
|
xue@1
|
1896 }
|
xue@1
|
1897 }//ReEstFreq
|
xue@1
|
1898
|
xue@1
|
1899 /*
|
xue@1
|
1900 function ReEstFreq_2: sinusoid reestimation by demodulating frequency. This is that same as ReEstFreq(...)
|
xue@1
|
1901 except that it calls Sinusoid(...) to synthesize the phase track used for demodulation and that it
|
xue@1
|
1902 does not allow variable window sizes for estimating demodulated sinusoid.
|
xue@1
|
1903
|
xue@1
|
1904 In: x[Wid+Offst*(FrCount-1)]: waveform data
|
xue@1
|
1905 FrCount, Wid, Offst: frame count, frame size and hop size
|
xue@1
|
1906 fbuf[FrCount], ns[FrCount]: initial frequency estiamtes and their timing
|
xue@1
|
1907 win[Wid]: window function for LSE sinusoid estimation
|
xue@1
|
1908 M, c[], iH2: cosine-family window specification parameters, must be consistent with M, c, iH2
|
xue@1
|
1909 w[Wid/2], xs[Wid], xc[Wid], f3[FrCount-1], f2[FrCount-1], f1[FrCount-1], f0[FrCount-1]: buffers
|
xue@1
|
1910 Out: fbuf[FrCount], abuf[FrCount], pbuf[FrCount]: reestimated frequencies, amplitudes and phase angles
|
xue@1
|
1911
|
xue@1
|
1912 No return value.
|
xue@1
|
1913 */
|
xue@1
|
1914 void ReEstFreq_2(int FrCount, int Wid, int Offst, double* x, double* fbuf, double* abuf, double* pbuf, double* win, int M, double* c, double iH2, cdouble* w, cdouble* xc, cdouble* xs, double* f3, double* f2, double* f1, double* f0, double* ns)
|
xue@1
|
1915 {
|
xue@1
|
1916 int hWid=Wid/2;
|
xue@1
|
1917 //reestimate using frequency track
|
xue@1
|
1918 CubicSpline(FrCount-1, f3, f2, f1, f0, ns, fbuf, 1, 1);
|
xue@1
|
1919 double *refcos=(double*)malloc8(sizeof(double)*Wid), *refsin=&refcos[hWid], ph=0, centralph;
|
xue@1
|
1920
|
xue@1
|
1921 memset(f0, 0, sizeof(double)*FrCount);
|
xue@1
|
1922
|
xue@1
|
1923 int N=Wid+Offst*(FrCount-1);
|
xue@1
|
1924 double* cosine=new double[N], *sine=new double[N];
|
xue@1
|
1925 Sinusoid(&cosine[hWid], &sine[hWid], -hWid, 0, f3[0], f2[0], f1[0], f0[0], ph);
|
xue@1
|
1926 for (int fr=0; fr<FrCount-1; fr++)
|
xue@1
|
1927 {
|
xue@1
|
1928 int ncentre=hWid+Offst*fr;
|
xue@1
|
1929 if (fr==FrCount-2) Sinusoid(&cosine[ncentre], &sine[ncentre], 0, Wid, f3[fr], f2[fr], f1[fr], f0[fr], ph);
|
xue@1
|
1930 else Sinusoid(&cosine[ncentre], &sine[ncentre], 0, hWid, f3[fr], f2[fr], f1[fr], f0[fr], ph);
|
xue@1
|
1931 }
|
xue@1
|
1932 double err=0;
|
xue@1
|
1933 for (int n=0; n<N; n++) {double tmp=cosine[n]-x[n-hWid]; err+=tmp*tmp; tmp=cosine[n]*cosine[n]+sine[n]*sine[n]-1; err+=tmp*tmp;}
|
xue@1
|
1934
|
xue@1
|
1935 ph=0;
|
xue@1
|
1936 for (int fr=0; fr<FrCount; fr++)
|
xue@1
|
1937 {
|
xue@1
|
1938 double* ldata=&x[fr*Offst-hWid];
|
xue@1
|
1939
|
xue@1
|
1940 //store first half of demodulated frame to xs[0:hWid-1]
|
xue@1
|
1941 if (fr==0)
|
xue@1
|
1942 {
|
xue@1
|
1943 Sinusoid(&refcos[hWid], &refsin[hWid], -hWid, 0, f3[0], f2[0], f1[0], f0[0], ph);
|
xue@1
|
1944 for (int i=0; i<hWid; i++) xs[i].x=ldata[i]*refcos[i], xs[i].y=-ldata[i]*refsin[i];
|
xue@1
|
1945 }
|
xue@1
|
1946 else
|
xue@1
|
1947 {
|
xue@1
|
1948 ph=0;
|
xue@1
|
1949 Sinusoid(refcos, refsin, 0, hWid, f3[fr-1], f2[fr-1], f1[fr-1], f0[fr-1], ph);
|
xue@1
|
1950 for (int i=0; i<hWid; i++) xs[i].x=ldata[i]*refcos[i], xs[i].y=-ldata[i]*refsin[i];
|
xue@1
|
1951 }
|
xue@1
|
1952
|
xue@1
|
1953 //taking care of phase angles
|
xue@1
|
1954 if (fr==FrCount-1) {double tmp=ph; ph=centralph; centralph=tmp;}
|
xue@1
|
1955 else centralph=ph;
|
xue@1
|
1956
|
xue@1
|
1957 double *lrefcos=&refcos[-hWid], *lrefsin=&refsin[-hWid];
|
xue@1
|
1958 //store second half of demodulated frame to xs[hWid:Wid-1]
|
xue@1
|
1959 if (fr==FrCount-1)
|
xue@1
|
1960 {
|
xue@1
|
1961 Sinusoid(lrefcos, lrefsin, hWid, Wid, f3[FrCount-2], f2[FrCount-2], f1[FrCount-2], f0[FrCount-2], ph);
|
xue@1
|
1962 for (int i=hWid; i<Wid; i++) xs[i].x=ldata[i]*lrefcos[i], xs[i].y=-ldata[i]*lrefsin[i];
|
xue@1
|
1963 }
|
xue@1
|
1964 else
|
xue@1
|
1965 {
|
xue@1
|
1966 Sinusoid(refcos, refsin, 0, hWid, f3[fr], f2[fr], f1[fr], f0[fr], ph);
|
xue@1
|
1967 for (int i=hWid; i<Wid; i++) xs[i].x=ldata[i]*lrefcos[i], xs[i].y=-ldata[i]*lrefsin[i];
|
xue@1
|
1968 }
|
xue@1
|
1969
|
xue@1
|
1970 CFFTCW(xs, win, NULL, NULL, log2(Wid), w, xc);
|
xue@1
|
1971 double lf=fbuf[fr]*Wid, la, lp;
|
xue@1
|
1972 LSESinusoid(lf, lf-3, lf+3, xc, Wid, 3, M, c, iH2, la, lp, 1e-3);
|
xue@1
|
1973 if (la*2>abuf[fr])
|
xue@1
|
1974 fbuf[fr]=lf/Wid, abuf[fr]=la*2, pbuf[fr]=lp+centralph;
|
xue@1
|
1975 }
|
xue@1
|
1976 }//ReEstFreq_2
|
xue@1
|
1977
|
xue@1
|
1978 /*
|
xue@1
|
1979 function ReEstFreqAmp: sinusoid reestimation by demodulating frequency and amplitude.
|
xue@1
|
1980
|
xue@1
|
1981 In: x[Wid+Offst*(FrCount-1)]: waveform data
|
xue@1
|
1982 FrCount, Wid, Offst: frame count, frame size and hop size
|
xue@1
|
1983 fbuf[FrCount], abuf[FrCount], ns[FrCount]: initial frequency and amplitude estiamtes and their
|
xue@1
|
1984 timing
|
xue@1
|
1985 win[Wid]: window function for estimating demodulated sinusoid
|
xue@1
|
1986 M, c[], iH2: cosine-family window specification parameters, must be consistent with win[]
|
xue@1
|
1987 Wids[FrCount]: specifies frame sizes for estimating individual frames of demodulated sinusoid,
|
xue@1
|
1988 optional
|
xue@1
|
1989 w[Wid/2], ps[Wid], xs[Wid], xc[Wid]: buffers
|
xue@1
|
1990 fa[FrCount-1], fb[FrCount-1], fc[FrCount-1], fd[FrCount-1]: buffers
|
xue@1
|
1991 aa[FrCount-1], ab[FrCount-1], ac[FrCount-1], ad[FrCount-1]: buffers
|
xue@1
|
1992 Out: fbuf[FrCount], abuf[FrCount], pbuf[FrCount]: reestimated frequencies, amplitudes and phase angles
|
xue@1
|
1993
|
xue@1
|
1994 No return value.
|
xue@1
|
1995 */
|
xue@1
|
1996 void ReEstFreqAmp(int FrCount, int Wid, int Offst, double* x, double* fbuf, double* abuf, double* pbuf, double* win, int M, double* c, double iH2, cdouble* w, cdouble* xc, cdouble* xs, double* ps, double* as, double* fa, double* fb, double* fc, double* fd, double* aa, double* ab, double* ac, double* ad, double* ns, int* Wids)
|
xue@1
|
1997 {
|
xue@1
|
1998 int hWid=Wid/2;
|
xue@1
|
1999 //reestimate using amplitude and frequency track
|
xue@1
|
2000 CubicSpline(FrCount-1, fa, fb, fc, fd, ns, fbuf, 0, 1);
|
xue@1
|
2001 CubicSpline(FrCount-1, aa, ab, ac, ad, ns, abuf, 0, 1);
|
xue@1
|
2002 for (int fr=0; fr<FrCount; fr++)
|
xue@1
|
2003 {
|
xue@1
|
2004 if (fr==0)
|
xue@1
|
2005 {
|
xue@1
|
2006 double lfd=0, lfc=fc[0], lfb=fb[0], lfa=fa[0],
|
xue@1
|
2007 lad=ad[0], lac=ac[0], lab=ab[0], laa=aa[0];
|
xue@1
|
2008 for (int j=0; j<Wid; j++)
|
xue@1
|
2009 {
|
xue@1
|
2010 double lx=j-hWid;
|
xue@1
|
2011 ps[j]=2*M_PI*lx*(lfd+lx*(lfc/2+lx*(lfb/3+lx*lfa/4)));
|
xue@1
|
2012 }
|
xue@1
|
2013 for (int j=0; j<Wid; j++)
|
xue@1
|
2014 {
|
xue@1
|
2015 double lx=j-hWid;
|
xue@1
|
2016 as[j]=lad+lx*(lac+lx*(lab+lx*laa));
|
xue@1
|
2017 }
|
xue@1
|
2018 }
|
xue@1
|
2019 else if (fr==FrCount-1)
|
xue@1
|
2020 {
|
xue@1
|
2021 int lfr=FrCount-2;
|
xue@1
|
2022 double lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
2023 double lfd=-(hWid*(lfc+hWid*(lfb+hWid*lfa)));
|
xue@1
|
2024 double lad=ad[lfr], lac=ac[lfr], lab=ab[lfr], laa=aa[lfr];
|
xue@1
|
2025 ps[0]=-2*M_PI*hWid*(lfd+hWid*(lfc/2+hWid*(lfb/3+hWid*lfa/4)));
|
xue@1
|
2026 for (int j=1; j<Wid; j++)
|
xue@1
|
2027 {
|
xue@1
|
2028 ps[j]=ps[0]+2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
2029 }
|
xue@1
|
2030 as[0]=ad[lfr];
|
xue@1
|
2031 for (int j=0; j<Wid; j++)
|
xue@1
|
2032 {
|
xue@1
|
2033 as[j]=lad+j*(lac+j*(lab+j*laa));
|
xue@1
|
2034 }
|
xue@1
|
2035 }
|
xue@1
|
2036 else
|
xue@1
|
2037 {
|
xue@1
|
2038 int lfr=fr-1;
|
xue@1
|
2039 double lfd=fd[lfr]-fd[fr], lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
2040 double lad=ad[lfr], lac=ac[lfr], lab=ab[lfr], laa=aa[lfr];
|
xue@1
|
2041 ps[0]=-2*M_PI*hWid*(lfd+hWid*(lfc/2+hWid*(lfb/3+hWid*lfa/4)));
|
xue@1
|
2042 for (int j=0; j<hWid+1; j++)
|
xue@1
|
2043 {
|
xue@1
|
2044 ps[j]=ps[0]+2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
2045 as[j]=lad+j*(lac+j*(lab+j*laa));
|
xue@1
|
2046 }
|
xue@1
|
2047 lfr=fr;
|
xue@1
|
2048 lfd=0, lfc=fc[lfr], lfb=fb[lfr], lfa=fa[lfr];
|
xue@1
|
2049 lad=ad[lfr], lac=ac[lfr], lab=ab[lfr], laa=aa[lfr];
|
xue@1
|
2050 for (int j=1; j<hWid; j++)
|
xue@1
|
2051 {
|
xue@1
|
2052 ps[j+hWid]=2*M_PI*j*(lfd+j*(lfc/2+j*(lfb/3+j*lfa/4)));
|
xue@1
|
2053 as[j+hWid]=lad+j*(lac+j*(lab+j*laa));
|
xue@1
|
2054 }
|
xue@1
|
2055 }
|
xue@1
|
2056 double *ldata=&x[fr*Offst];
|
xue@1
|
2057 for (int j=0; j<Wid; j++)
|
xue@1
|
2058 {
|
xue@1
|
2059 double tmp;
|
xue@1
|
2060 if ((fr==0 && j<hWid) || (fr==FrCount-1 && j>=hWid)) tmp=1;
|
xue@1
|
2061 else if (as[hWid]>100*as[j]) tmp=100;
|
xue@1
|
2062 else tmp=as[hWid]/as[j];
|
xue@1
|
2063 tmp=tmp*ldata[j];
|
xue@1
|
2064 xs[j].x=tmp*cos(-ps[j]);
|
xue@1
|
2065 xs[j].y=tmp*sin(-ps[j]);
|
xue@1
|
2066 }
|
xue@1
|
2067
|
xue@1
|
2068 if (Wids)
|
xue@1
|
2069 {
|
xue@1
|
2070 int lWid=Wids[fr], lhWid=Wids[fr]/2, lM;
|
xue@1
|
2071 SetTwiddleFactors(lWid, w);
|
xue@1
|
2072 double *lwin=NewWindow(wtHann, lWid), lc[4], liH2;
|
xue@1
|
2073 windowspec(wtHann, lWid, &lM, lc, &liH2);
|
xue@1
|
2074 CFFTCW(&xs[hWid-lhWid], lwin, NULL, NULL, log2(lWid), w, xc);
|
xue@1
|
2075 delete[] lwin;
|
xue@1
|
2076 double lf=fbuf[fr]*lWid, la, lp;
|
xue@1
|
2077 LSESinusoid(lf, lf-3, lf+3, xc, lWid, 3, lM, lc, liH2, la, lp, 1e-3);
|
xue@1
|
2078 if (la*2>abuf[fr]) fbuf[fr]=lf/lWid, abuf[fr]=la*2, pbuf[fr]=lp;
|
xue@1
|
2079 }
|
xue@1
|
2080 else
|
xue@1
|
2081 {
|
xue@1
|
2082 CFFTCW(xs, win, NULL, NULL, log2(Wid), w, xc);
|
xue@1
|
2083 double lf=fbuf[fr]*Wid, la, lp;
|
xue@1
|
2084 LSESinusoid(lf, lf-3, lf+3, xc, Wid, 3, M, c, iH2, la, lp, 1e-3);
|
xue@1
|
2085 if (la*2>abuf[fr]) fbuf[fr]=lf/Wid, abuf[fr]=la*2, pbuf[fr]=lp;
|
xue@1
|
2086 }
|
xue@1
|
2087 }
|
xue@1
|
2088 }//ReEstFreqAmp
|
xue@1
|
2089
|
xue@1
|
2090 /*
|
xue@1
|
2091 function Reestimate2: iterative demodulation method for sinusoid parameter reestimation.
|
xue@1
|
2092
|
xue@1
|
2093 In: x[(FrCount-1)*Offst+Wid]: waveform data
|
xue@1
|
2094 FrCount, Wid, Offst: frame count, frame size and hop size
|
xue@1
|
2095 win[Wid]: window function
|
xue@1
|
2096 M, c[], iH2: cosine-family window specification parameters, must be consistent with win[]
|
xue@1
|
2097 Wids[FrCount]: specifies frame sizes for estimating individual frames of demodulated sinusoid,
|
xue@1
|
2098 optional
|
xue@1
|
2099 maxiter: maximal number of iterates
|
xue@1
|
2100 ae[FrCount], fe[FrCount], pe[FrCount]: initial amplitude, frequency and phase estimates
|
xue@1
|
2101 Out: aret[FrCount], fret[FrCount], pret[FrCount]: reestimated amplitudes, frequencies and phase angles
|
xue@1
|
2102
|
xue@1
|
2103 Returns the number of unused iterates left of the total of maxiter.
|
xue@1
|
2104 */
|
xue@1
|
2105 int Reestimate2(int FrCount, int Wid, int Offst, double* win, int M, double* c, double iH2, double* x, double* ae, double* fe, double* pe, double* aret, double* fret, double *pret, int maxiter, int* Wids)
|
xue@1
|
2106 {
|
xue@1
|
2107 AllocateFFTBuffer(Wid, fft, w, xc);
|
xue@1
|
2108 double convep=1e-4, dif=0, lastdif=0; //convep is the hard-coded threshold that stops the iteration
|
xue@1
|
2109 int iter=1, hWid=Wid/2;
|
xue@1
|
2110
|
xue@1
|
2111 double *ns=new double[FrCount*12], *as=new double[Wid*5];
|
xue@1
|
2112 double *fbuf=&ns[FrCount], *abuf=&ns[FrCount*2],
|
xue@1
|
2113 *aa=&ns[FrCount*3], *ab=&ns[FrCount*4], *ac=&ns[FrCount*5], *ad=&ns[FrCount*6],
|
xue@1
|
2114 *fa=&ns[FrCount*7], *fb=&ns[FrCount*8], *fc=&ns[FrCount*9], *fd=&ns[FrCount*10],
|
xue@1
|
2115 *pbuf=&ns[FrCount*11];
|
xue@1
|
2116 double *ps=&as[Wid];
|
xue@1
|
2117 cdouble *xs=(cdouble*)&as[Wid*3];
|
xue@1
|
2118
|
xue@1
|
2119 memcpy(fbuf, fe, sizeof(double)*FrCount);
|
xue@1
|
2120 memcpy(abuf, ae, sizeof(double)*FrCount);
|
xue@1
|
2121 memcpy(pbuf, pe, sizeof(double)*FrCount);
|
xue@1
|
2122 for (int i=0; i<FrCount; i++)
|
xue@1
|
2123 {
|
xue@1
|
2124 ns[i]=hWid+i*Offst;
|
xue@1
|
2125 }
|
xue@1
|
2126
|
xue@1
|
2127 while (iter<=maxiter)
|
xue@1
|
2128 {
|
xue@1
|
2129 ReEstFreq(FrCount, Wid, Offst, x, fbuf, abuf, pbuf, win, M, c, iH2, w, xc, xs, ps, fa, fb, fc, fd, ns, Wids);
|
xue@1
|
2130 ReEstFreq(FrCount, Wid, Offst, x, fbuf, abuf, pbuf, win, M, c, iH2, w, xc, xs, ps, fa, fb, fc, fd, ns, Wids);
|
xue@1
|
2131 ReEstFreqAmp(FrCount, Wid, Offst, x, fbuf, abuf, pbuf, win, M, c, iH2, w, xc, xs, ps, as, fa, fb, fc, fd, aa, ab, ac, ad, ns, Wids);
|
xue@1
|
2132
|
xue@1
|
2133 if (iter>1) lastdif=dif;
|
xue@1
|
2134 dif=0;
|
xue@1
|
2135 if (iter==1)
|
xue@1
|
2136 {
|
xue@1
|
2137 for (int fr=0; fr<FrCount; fr++)
|
xue@1
|
2138 {
|
xue@1
|
2139 if (fabs(abuf[fr])>fabs(ae[fr]))
|
xue@1
|
2140 dif+=fabs(fe[fr]-fbuf[fr])*Wid+fabs((ae[fr]-abuf[fr])/abuf[fr]);
|
xue@1
|
2141 else
|
xue@1
|
2142 dif+=fabs(fe[fr]-fbuf[fr])*Wid+fabs((ae[fr]-abuf[fr])/ae[fr]);
|
xue@1
|
2143 }
|
xue@1
|
2144 }
|
xue@1
|
2145 else
|
xue@1
|
2146 {
|
xue@1
|
2147 for (int fr=0; fr<FrCount; fr++)
|
xue@1
|
2148 {
|
xue@1
|
2149 if (fabs(abuf[fr])>fabs(aret[fr]))
|
xue@1
|
2150 dif+=fabs(fret[fr]-fbuf[fr])*Wid+fabs((aret[fr]-abuf[fr])/abuf[fr]);
|
xue@1
|
2151 else
|
xue@1
|
2152 dif+=fabs(fret[fr]-fbuf[fr])*Wid+fabs((aret[fr]-abuf[fr])/aret[fr]);
|
xue@1
|
2153 }
|
xue@1
|
2154 }
|
xue@1
|
2155 memcpy(fret, fbuf, sizeof(double)*FrCount);
|
xue@1
|
2156 memcpy(aret, abuf, sizeof(double)*FrCount);
|
xue@1
|
2157 dif/=FrCount;
|
xue@1
|
2158 if (fabs(dif)<convep || (iter>1 && fabs(dif-lastdif)<convep*lastdif)) break;
|
xue@1
|
2159 iter++;
|
xue@1
|
2160 }
|
xue@1
|
2161
|
xue@1
|
2162 memcpy(pret, pbuf, sizeof(double)*FrCount);
|
xue@1
|
2163
|
xue@1
|
2164 delete[] ns;
|
xue@1
|
2165 delete[] as;
|
xue@1
|
2166 delete[] fft;
|
xue@1
|
2167
|
xue@1
|
2168 return maxiter-iter;
|
xue@1
|
2169 }//Reestimate2
|
xue@1
|
2170
|
xue@1
|
2171 //---------------------------------------------------------------------------
|
xue@1
|
2172 /*
|
xue@1
|
2173 Derivative method as proposed in DAFx09
|
xue@1
|
2174
|
xue@1
|
2175 Further reading: Wen X. and M. Sandler, "Notes on model-based non-stationary sinusoid estimation methods
|
xue@1
|
2176 using derivatives," in Proc. DAFx'09, Como, 2009.
|
xue@1
|
2177 */
|
xue@1
|
2178
|
xue@1
|
2179 /*
|
xue@1
|
2180 function Derivative: derivative method for estimating amplitude derivative, frequency, and frequency derivative given
|
xue@1
|
2181 signal and its derivatives.
|
xue@1
|
2182
|
xue@1
|
2183 In: x[Wid], dx[Wid], ddx[Wid]: waveform and its derivatives
|
xue@1
|
2184 win[Wid]: window function
|
xue@1
|
2185 f0: initial digital frequency estimate
|
xue@1
|
2186 Out: f0: new estimate of digital frequency
|
xue@1
|
2187 f1, a1: estimates of frequency and amplitude derivatives
|
xue@1
|
2188
|
xue@1
|
2189 No return value.
|
xue@1
|
2190 */
|
xue@1
|
2191 void Derivative(int Wid, double* win, cdouble* x, cdouble* dx, cdouble* ddx, double& f0, double* f1, double* a0, double* a1, double* ph)
|
xue@1
|
2192 {
|
xue@1
|
2193 AllocateFFTBuffer(Wid, fft, W, X);
|
xue@1
|
2194 CFFTCW(x, win, fft, NULL, log2(Wid), W, X);
|
xue@1
|
2195 int m=f0*Wid, m0=m-10, m1=m+10, hWid=Wid/2;
|
xue@1
|
2196 if (m0<0) m0=0; if (m1>hWid) m1=hWid;
|
xue@1
|
2197 for (int n=m0; n<=m1; n++) if (fft[n]>fft[m]) m=n;
|
xue@1
|
2198 cdouble Sw=0, S1w=0, S2w=0;
|
xue@1
|
2199 for (int n=0; n<Wid; n++)
|
xue@1
|
2200 {
|
xue@1
|
2201 cdouble tmp=x[n]*win[n];
|
xue@1
|
2202 Sw+=tmp.rotate(-2*M_PI*m*(n-hWid)/Wid);
|
xue@1
|
2203 tmp=dx[n]*win[n];
|
xue@1
|
2204 S1w+=tmp.rotate(-2*M_PI*m*(n-hWid)/Wid);
|
xue@1
|
2205 }
|
xue@1
|
2206 double omg0=(S1w/Sw).y;
|
xue@1
|
2207 Sw=0, S1w=0;
|
xue@1
|
2208 for (int n=0; n<Wid; n++)
|
xue@1
|
2209 {
|
xue@1
|
2210 cdouble tmp=x[n]*win[n];
|
xue@1
|
2211 Sw+=tmp.rotate(-omg0*(n-hWid)/Wid);
|
xue@1
|
2212 tmp=dx[n]*win[n];
|
xue@1
|
2213 S1w+=tmp.rotate(-omg0*(n-hWid)/Wid);
|
xue@1
|
2214 tmp=ddx[n]*win[n];
|
xue@1
|
2215 S2w+=tmp.rotate(-omg0*(n-hWid)/Wid);
|
xue@1
|
2216 }
|
xue@1
|
2217 omg0=(S1w/Sw).y;
|
xue@1
|
2218 double miu0=(S1w/Sw).x;
|
xue@1
|
2219 double psi0=(S2w/Sw).y-2*miu0*omg0;
|
xue@1
|
2220
|
xue@1
|
2221 f0=omg0/(2*M_PI);
|
xue@1
|
2222 *f1=psi0/(2*M_PI);
|
xue@1
|
2223 *a1=miu0;
|
xue@1
|
2224
|
xue@1
|
2225 FreeFFTBuffer(fft);
|
xue@1
|
2226 }//Derivative
|
xue@1
|
2227
|
xue@1
|
2228 /*
|
xue@1
|
2229 function Xkw: computes windowed spectrum of x and its derivatives up to order K at angular frequency omg,
|
xue@1
|
2230 from x using window w and its derivatives.
|
xue@1
|
2231
|
xue@1
|
2232 In: x[Wid]: waveform data
|
xue@1
|
2233 w[K+1][Wid]: window functions and its derivatives up to order K
|
xue@1
|
2234 omg: angular frequency
|
xue@1
|
2235 Out: X[K+1]: windowed spectrum and its derivatives up to order K
|
xue@1
|
2236
|
xue@1
|
2237 No return value. This function is for internal use.
|
xue@1
|
2238 */
|
xue@1
|
2239 void Xkw(cdouble* X, int K, int Wid, double* x, double** w, double omg)
|
xue@1
|
2240 {
|
xue@1
|
2241 int hWid=Wid/2;
|
xue@1
|
2242 //calculate the first row
|
xue@1
|
2243 memset(X, 0, sizeof(cdouble)*(K+1));
|
xue@1
|
2244 for (int i=0; i<Wid; i++)
|
xue@1
|
2245 {
|
xue@1
|
2246 double n=i-hWid;
|
xue@1
|
2247 double ph=omg*n;
|
xue@1
|
2248 for (int k=0; k<=K; k++)
|
xue@1
|
2249 {
|
xue@1
|
2250 cdouble tmp=x[i]*w[k][i];
|
xue@1
|
2251 X[k]+=tmp.rotate(-ph);
|
xue@1
|
2252 }
|
xue@1
|
2253 }
|
xue@1
|
2254 //calculate the rest rows
|
xue@1
|
2255 for (int k=1; k<=K; k++)
|
xue@1
|
2256 {
|
xue@1
|
2257 cdouble *thisX=&X[k], *lastX=&X[k-1];
|
xue@1
|
2258 for (int kk=K-k; kk>=0; kk--) thisX[kk]=-lastX[kk+1]+cdouble(0, omg)*lastX[kk];
|
xue@1
|
2259 }
|
xue@1
|
2260 }//Xkw
|
xue@1
|
2261
|
xue@1
|
2262 /*
|
xue@1
|
2263 function Xkw: computes windowed spectrum of x and its derivatives up to order K at angular frequency
|
xue@1
|
2264 omg, from x and its derivatives using window w.
|
xue@1
|
2265
|
xue@1
|
2266 In: x[K+1][Wid]: waveform data and its derivatives up to order K.
|
xue@1
|
2267 w[Wid]: window function
|
xue@1
|
2268 omg: angular frequency
|
xue@1
|
2269 Out: X[K+1]: windowed spectrum and its derivatives up to order K
|
xue@1
|
2270
|
xue@1
|
2271 No return value. This function is for testing only.
|
xue@1
|
2272 */
|
xue@1
|
2273 void Xkw(cdouble* X, int K, int Wid, double** x, double* w, double omg)
|
xue@1
|
2274 {
|
xue@1
|
2275 int hWid=Wid/2;
|
xue@1
|
2276 memset(X, 0, sizeof(cdouble)*(K+1));
|
xue@1
|
2277 for (int i=0; i<Wid; i++)
|
xue@1
|
2278 {
|
xue@1
|
2279 double n=i-hWid;
|
xue@1
|
2280 double ph=omg*n;
|
xue@1
|
2281 for (int k=0; k<=K; k++)
|
xue@1
|
2282 {
|
xue@1
|
2283 cdouble tmp=x[k][i]*w[i];
|
xue@1
|
2284 X[k]+=tmp.rotate(-ph);
|
xue@1
|
2285 }
|
xue@1
|
2286 }
|
xue@1
|
2287 }//Xkw
|
xue@1
|
2288
|
xue@1
|
2289 /*
|
xue@1
|
2290 function Derivative: derivative method for estimating the model log(s)=h[M]'r[M], by discarding extra
|
xue@1
|
2291 equations
|
xue@1
|
2292
|
xue@1
|
2293 In: s[Wid]: waveform data
|
xue@1
|
2294 win[][Wid]: window function and its derivatives
|
xue@1
|
2295 h[M], dh[M]: pointers to basis functions and their derivatives
|
xue@1
|
2296 harg: pointer argument to be used by calls to functions in h[] amd dh[].
|
xue@1
|
2297 p0[p0s]: zero-constraints on real parts of r, i.e. Re(r[p0[*]]) are constrained to 0.
|
xue@1
|
2298 q0[q0s]: zero-constraints on imaginary parts of r, i.e. Im(r[q0[*]]) are constrained to 0.
|
xue@1
|
2299 omg: initial angular frequency
|
xue@1
|
2300 Out: r[M]: estimated coefficients to h[M].
|
xue@1
|
2301
|
xue@1
|
2302 No return value.
|
xue@1
|
2303 */
|
xue@1
|
2304 void Derivative(int M, double (**h)(double t, void*), double (**dh)(double t, void*), cdouble* r, int p0s, int* p0, int q0s, int* q0, int Wid, double* s, double** win, double omg, void* harg)
|
xue@1
|
2305 {
|
xue@1
|
2306 int hWid=Wid/2, M1=M-1;
|
xue@1
|
2307 int Kr=(M1)*2-p0s-q0s; //number of real unknowns apart from p0 and q0
|
xue@1
|
2308 int Kc=ceil(Kr/2.0); //number of derivatives required
|
xue@1
|
2309
|
xue@1
|
2310 //ind marks the 2*M1 real elements of an M1-array of complex unknowns with
|
xue@1
|
2311 // numerical indices (0-based) or -1 if it is not a real unknown variable
|
xue@1
|
2312 //uind marks the Kr real unknowns with their positions in ind
|
xue@1
|
2313 int *uind=new int[Kr], *ind=new int[2*M1];
|
xue@1
|
2314 memset(ind, 0, sizeof(int)*2*M1);
|
xue@1
|
2315 for (int p=0; p<p0s; p++) ind[2*(p0[p]-1)]=-1;
|
xue@1
|
2316 for (int q=0; q<q0s; q++) ind[2*(q0[q]-1)+1]=-1;
|
xue@1
|
2317 {
|
xue@1
|
2318 int p=0, up=0;
|
xue@1
|
2319 while (p<2*M1)
|
xue@1
|
2320 {
|
xue@1
|
2321 if (ind[p]>=0)
|
xue@1
|
2322 {
|
xue@1
|
2323 uind[up]=p;
|
xue@1
|
2324 ind[p]=up;
|
xue@1
|
2325 up++;
|
xue@1
|
2326 }
|
xue@1
|
2327 p++;
|
xue@1
|
2328 }
|
xue@1
|
2329 if (up!=Kr) throw("");
|
xue@1
|
2330 }
|
xue@1
|
2331
|
xue@1
|
2332 cdouble* Skw=new cdouble[M];
|
xue@1
|
2333 Xkw(Skw, Kc, Wid, s, win, omg);
|
xue@1
|
2334
|
xue@1
|
2335 double* x=new double[Wid];
|
xue@1
|
2336 cdouble** Allocate2(cdouble, M, Kc, Smkw);
|
xue@1
|
2337 for (int m=1; m<M; m++)
|
xue@1
|
2338 {
|
xue@1
|
2339 for (int i=0; i<Wid; i++) x[i]=dh[m](i-hWid, harg)*s[i];
|
xue@1
|
2340 Xkw(Smkw[m], Kc-1, Wid, x, win, omg);
|
xue@1
|
2341 }
|
xue@1
|
2342
|
xue@1
|
2343 //allocate buffer for linear system A(pq)=b
|
xue@1
|
2344 Alloc2(2*Kc+2, Kr, A); double** AA; double *bb, *pqpq;
|
xue@1
|
2345 double *b=A[2*Kc], *pq=A[2*Kc+1];
|
xue@1
|
2346 for (int k=0; k<Kr; k++) b[k]=((double*)(&Skw[1]))[k];
|
xue@1
|
2347 // *pq=(double*)(&r[1]);
|
xue@1
|
2348 for (int k=0; k<Kc; k++) //looping through rows of A
|
xue@1
|
2349 {
|
xue@1
|
2350 //columns of A includes rows of Smkw corresponding to real unknowns
|
xue@1
|
2351 for (int m=0; m<M1; m++)
|
xue@1
|
2352 {
|
xue@1
|
2353 int lind;
|
xue@1
|
2354 if ((lind=ind[2*m])>=0) //the real part being unknown
|
xue@1
|
2355 {
|
xue@1
|
2356 A[2*k][lind]=Smkw[m+1][k].x;
|
xue@1
|
2357 A[2*k+1][lind]=Smkw[m+1][k].y;
|
xue@1
|
2358 }
|
xue@1
|
2359 if ((lind=ind[2*m+1])>=0) //the imag part being unknown
|
xue@1
|
2360 {
|
xue@1
|
2361 A[2*k+1][lind]=Smkw[m+1][k].x;
|
xue@1
|
2362 A[2*k][lind]=-Smkw[m+1][k].y;
|
xue@1
|
2363 }
|
xue@1
|
2364 }
|
xue@1
|
2365 }
|
xue@1
|
2366
|
xue@1
|
2367 bool dropeq=(2*Kc-1==Kr);
|
xue@1
|
2368 if (dropeq)
|
xue@1
|
2369 {
|
xue@1
|
2370 Allocate2(double, Kr+2, Kr, AA);
|
xue@1
|
2371 bb=AA[Kr], pqpq=AA[Kr+1];
|
xue@1
|
2372 memcpy(AA[0], A[0], sizeof(double)*Kr*(Kr-1));
|
xue@1
|
2373 memcpy(AA[Kr-1], A[Kr], sizeof(double)*Kr);
|
xue@1
|
2374 memcpy(bb, b, sizeof(double)*(Kr-1));
|
xue@1
|
2375 bb[Kr-1]=((double*)(&Skw[1]))[Kr];
|
xue@1
|
2376 }
|
xue@1
|
2377
|
xue@1
|
2378 double det;
|
xue@1
|
2379 GECP(Kr, pq, A, b, &det);
|
xue@1
|
2380 if (dropeq)
|
xue@1
|
2381 {
|
xue@1
|
2382 double det2;
|
xue@1
|
2383 GECP(Kr, pqpq, AA, bb, &det2);
|
xue@1
|
2384 if (fabs(det2)>fabs(det)) memcpy(pq, pqpq, sizeof(double)*Kr);
|
xue@1
|
2385 DeAlloc2(AA);
|
xue@1
|
2386 }
|
xue@1
|
2387 memset(&r[1], 0, sizeof(double)*M1*2);
|
xue@1
|
2388 for (int k=0; k<Kr; k++) ((double*)(&r[1]))[uind[k]]=pq[k];
|
xue@1
|
2389
|
xue@1
|
2390 //estiamte r0
|
xue@1
|
2391 cdouble e0=0;
|
xue@1
|
2392 for (int i=0; i<Wid; i++)
|
xue@1
|
2393 {
|
xue@1
|
2394 cdouble expo=0;
|
xue@1
|
2395 double n=i-hWid;
|
xue@1
|
2396 for (int m=1; m<M; m++){double lhm=h[m](n, harg); expo+=r[m]*lhm;}
|
xue@1
|
2397 cdouble tmp=exp(expo)*win[0][i];
|
xue@1
|
2398 e0+=tmp.rotate(-omg*n);
|
xue@1
|
2399 }
|
xue@1
|
2400 r[0]=log(Skw[0]/e0);
|
xue@1
|
2401
|
xue@1
|
2402 delete[] x;
|
xue@1
|
2403 delete[] Skw;
|
xue@1
|
2404 delete[] uind;
|
xue@1
|
2405 delete[] ind;
|
xue@1
|
2406 DeAlloc2(Smkw);
|
xue@1
|
2407 DeAlloc2(A);
|
xue@1
|
2408 }//Derivative*/
|
xue@1
|
2409
|
xue@1
|
2410 /*
|
xue@1
|
2411 function DerivativeLS: derivative method for estimating the model log(s)=h[M]'r[M], least-square
|
xue@1
|
2412 implementation
|
xue@1
|
2413
|
xue@1
|
2414 In: s[Wid]: waveform data
|
xue@1
|
2415 win[][Wid]: window function and its derivatives
|
xue@1
|
2416 h[M], dh[M]: pointers to basis functions and their derivatives
|
xue@1
|
2417 harg: pointer argument to be used by calls to functions in h[] amd dh[].
|
xue@1
|
2418 K: number of derivatives to take
|
xue@1
|
2419 p0[p0s]: zero-constraints on real parts of r, i.e. Re(r[p0[*]]) are constrained to 0.
|
xue@1
|
2420 q0[q0s]: zero-constraints on imaginary parts of r, i.e. Im(r[q0[*]]) are constrained to 0.
|
xue@1
|
2421 omg: initial angular frequency
|
xue@1
|
2422 Out: r[M]: estimated coefficients to h[M].
|
xue@1
|
2423
|
xue@1
|
2424 No return value.
|
xue@1
|
2425 */
|
xue@1
|
2426 void DerivativeLS(int K, int M, double (**h)(double t, void* harg), double (**dh)(double t, void* harg), cdouble* r, int p0s, int* p0, int q0s, int* q0, int Wid, double* s, double** win, double omg, void* harg, bool r0)
|
xue@1
|
2427 {
|
xue@1
|
2428 int hWid=Wid/2, M1=M-1;
|
xue@1
|
2429 int Kr=(M1)*2-p0s-q0s; //number of real unknowns apart from p0 and q0
|
xue@1
|
2430 int Kc=ceil(Kr/2.0); //number of derivatives required
|
xue@1
|
2431 if (Kc<K) Kc=K;
|
xue@1
|
2432
|
xue@1
|
2433 int *uind=new int[Kr], *ind=new int[2*M1];
|
xue@1
|
2434 memset(ind, 0, sizeof(int)*2*M1);
|
xue@1
|
2435 for (int p=0; p<p0s; p++) ind[2*(p0[p]-1)]=-1;
|
xue@1
|
2436 for (int q=0; q<q0s; q++) ind[2*(q0[q]-1)+1]=-1;
|
xue@1
|
2437 {int p=0, up=0; while (p<2*M1){if (ind[p]>=0){uind[up]=p; ind[p]=up; up++;} p++;} if (up!=Kr) throw("");}
|
xue@1
|
2438
|
xue@1
|
2439 //allocate buffer for linear system A(pq)=b
|
xue@1
|
2440 cdouble* Skw=new cdouble[Kc+1];
|
xue@1
|
2441 double* x=new double[Wid];
|
xue@1
|
2442 cdouble** Allocate2(cdouble, M, Kc, Smkw);
|
xue@1
|
2443
|
xue@1
|
2444 Alloc2(2*Kc+2, 2*Kc, A);
|
xue@1
|
2445 double *b=A[2*Kc], *pq=A[2*Kc+1];
|
xue@1
|
2446
|
xue@1
|
2447 Xkw(Skw, Kc, Wid, s, win, omg);
|
xue@1
|
2448 for (int m=1; m<M; m++)
|
xue@1
|
2449 {
|
xue@1
|
2450 for (int i=0; i<Wid; i++) x[i]=dh[m](i-hWid, harg)*s[i];
|
xue@1
|
2451 Xkw(Smkw[m], Kc-1, Wid, x, win, omg);
|
xue@1
|
2452 }
|
xue@1
|
2453
|
xue@1
|
2454 for (int k=0; k<2*Kc; k++) b[k]=((double*)(&Skw[1]))[k];
|
xue@1
|
2455 for (int k=0; k<Kc; k++)
|
xue@1
|
2456 {
|
xue@1
|
2457 for (int m=0; m<M1; m++)
|
xue@1
|
2458 {
|
xue@1
|
2459 int lind;
|
xue@1
|
2460 if ((lind=ind[2*m])>=0)
|
xue@1
|
2461 {
|
xue@1
|
2462 A[2*k][lind]=Smkw[m+1][k].x;
|
xue@1
|
2463 A[2*k+1][lind]=Smkw[m+1][k].y;
|
xue@1
|
2464 }
|
xue@1
|
2465 if ((lind=ind[2*m+1])>=0)
|
xue@1
|
2466 {
|
xue@1
|
2467 A[2*k+1][lind]=Smkw[m+1][k].x;
|
xue@1
|
2468 A[2*k][lind]=-Smkw[m+1][k].y;
|
xue@1
|
2469 }
|
xue@1
|
2470 }
|
xue@1
|
2471 }
|
xue@1
|
2472
|
xue@1
|
2473 if (2*Kc==Kr) GECP(Kr, pq, A, b);
|
xue@1
|
2474 else LSLinear2(2*Kc, Kr, pq, A, b);
|
xue@1
|
2475
|
xue@1
|
2476 memset(&r[1], 0, sizeof(double)*M1*2);
|
xue@1
|
2477 for (int k=0; k<Kr; k++) ((double*)(&r[1]))[uind[k]]=pq[k];
|
xue@1
|
2478 //estiamte r0
|
xue@1
|
2479 if (r0)
|
xue@1
|
2480 {
|
xue@1
|
2481 cdouble e0=0;
|
xue@1
|
2482 for (int i=0; i<Wid; i++)
|
xue@1
|
2483 {
|
xue@1
|
2484 cdouble expo=0;
|
xue@1
|
2485 double n=i-hWid;
|
xue@1
|
2486 for (int m=1; m<M; m++){double lhm=h[m](n, harg); expo+=r[m]*lhm;}
|
xue@1
|
2487 cdouble tmp=exp(expo)*win[0][i];
|
xue@1
|
2488 e0+=tmp.rotate(-omg*n);
|
xue@1
|
2489 }
|
xue@1
|
2490 r[0]=log(Skw[0]/e0);
|
xue@1
|
2491 }
|
xue@1
|
2492 delete[] x;
|
xue@1
|
2493 delete[] Skw;
|
xue@1
|
2494 delete[] uind;
|
xue@1
|
2495 delete[] ind;
|
xue@1
|
2496 DeAlloc2(Smkw);
|
xue@1
|
2497 DeAlloc2(A);
|
xue@1
|
2498 }//DerivativeLS
|
xue@1
|
2499
|
xue@1
|
2500 /*
|
xue@1
|
2501 function DerivativeLS: derivative method for estimating the model log(s)=h[M]'r[M] using Fr
|
xue@1
|
2502 measurement points a quarter of Wid apart from each other, implemented by least-square.
|
xue@1
|
2503
|
xue@1
|
2504 In: s[Wid+(Fr-1)*Wid/4]: waveform data
|
xue@1
|
2505 win[][Wid]: window function and its derivatives
|
xue@1
|
2506 h[M], dh[M]: pointers to basis functions and their derivatives
|
xue@1
|
2507 harg: pointer argument to be used by calls to functions in h[] amd dh[].
|
xue@1
|
2508 Fr: number of measurement points
|
xue@1
|
2509 K: number of derivatives to take at each measurement point
|
xue@1
|
2510 p0[p0s]: zero-constraints on real parts of r, i.e. Re(r[p0[*]]) are constrained to 0.
|
xue@1
|
2511 q0[q0s]: zero-constraints on imaginary parts of r, i.e. Im(r[q0[*]]) are constrained to 0.
|
xue@1
|
2512 omg: initial angular frequency
|
xue@1
|
2513 r0: specifies if r[0] is to be computed.
|
xue@1
|
2514 Out: r[M]: estimated coefficients to h[M].
|
xue@1
|
2515
|
xue@1
|
2516 No return value.
|
xue@1
|
2517 */
|
xue@1
|
2518 void DerivativeLS(int Fr, int K, int M, double (**h)(double t, void* harg), double (**dh)(double t, void* harg), cdouble* r, int p0s, int* p0, int q0s, int* q0, int Wid, double* s, double** win, double omg, void* harg, bool r0)
|
xue@1
|
2519 {
|
xue@1
|
2520 int hWid=Wid/2, qWid=Wid/4, M1=M-1;
|
xue@1
|
2521 int Kr=(M1)*2-p0s-q0s; //number of real unknowns apart from p0 and q0
|
xue@1
|
2522 int Kc=ceil(Kr/2.0/Fr); //number of derivatives required
|
xue@1
|
2523 if (Kc<K) Kc=K;
|
xue@1
|
2524
|
xue@1
|
2525 int *uind=new int[Kr], *ind=new int[2*M1];
|
xue@1
|
2526 memset(ind, 0, sizeof(int)*2*M1);
|
xue@1
|
2527 for (int p=0; p<p0s; p++) ind[2*(p0[p]-1)]=-1;
|
xue@1
|
2528 for (int q=0; q<q0s; q++) ind[2*(q0[q]-1)+1]=-1;
|
xue@1
|
2529 {int p=0, up=0; while (p<2*M1){if (ind[p]>=0){uind[up]=p; ind[p]=up; up++;} p++;}}
|
xue@1
|
2530
|
xue@1
|
2531 //allocate buffer for linear system A(pq)=b
|
xue@1
|
2532 cdouble* Skw=new cdouble[Kc+1], Skw00;
|
xue@1
|
2533 double* x=new double[Wid];
|
xue@1
|
2534 cdouble** Allocate2(cdouble, M, Kc, Smkw);
|
xue@1
|
2535
|
xue@1
|
2536 Alloc2(2*Fr*Kc, 2*Fr*Kc, A);
|
xue@1
|
2537 double *pq=new double[2*Fr*Kc], *b=new double[2*Fr*Kc];
|
xue@1
|
2538
|
xue@1
|
2539 for (int fr=0; fr<Fr; fr++)
|
xue@1
|
2540 {
|
xue@1
|
2541 int Offst=qWid*fr; double* ss=&s[Offst];
|
xue@1
|
2542
|
xue@1
|
2543 Xkw(Skw, Kc, Wid, ss, win, omg); if (fr==0) Skw00=Skw[0];
|
xue@1
|
2544 for (int m=1; m<M; m++)
|
xue@1
|
2545 {
|
xue@1
|
2546 for (int i=0; i<Wid; i++) x[i]=dh[m](i+Offst-hWid, harg)*ss[i];
|
xue@1
|
2547 Xkw(Smkw[m], Kc-1, Wid, x, win, omg);
|
xue@1
|
2548 }
|
xue@1
|
2549
|
xue@1
|
2550 for (int k=0; k<2*Kc; k++) b[2*fr*Kc+k]=((double*)(&Skw[1]))[k];
|
xue@1
|
2551 for (int k=0; k<Kc; k++)
|
xue@1
|
2552 {
|
xue@1
|
2553 for (int m=0; m<M1; m++)
|
xue@1
|
2554 {
|
xue@1
|
2555 int lind;
|
xue@1
|
2556 if ((lind=ind[2*m])>=0)
|
xue@1
|
2557 {
|
xue@1
|
2558 A[2*fr*Kc+2*k][lind]=Smkw[m+1][k].x;
|
xue@1
|
2559 A[2*fr*Kc+2*k+1][lind]=Smkw[m+1][k].y;
|
xue@1
|
2560 }
|
xue@1
|
2561 if ((lind=ind[2*m+1])>=0)
|
xue@1
|
2562 {
|
xue@1
|
2563 A[2*fr*Kc+2*k+1][lind]=Smkw[m+1][k].x;
|
xue@1
|
2564 A[2*fr*Kc+2*k][lind]=-Smkw[m+1][k].y;
|
xue@1
|
2565 }
|
xue@1
|
2566 }
|
xue@1
|
2567 }
|
xue@1
|
2568 }
|
xue@1
|
2569 if (2*Fr*Kc==Kr) GECP(Kr, pq, A, b);
|
xue@1
|
2570 else LSLinear2(2*Fr*Kc, Kr, pq, A, b);
|
xue@1
|
2571
|
xue@1
|
2572 memset(&r[1], 0, sizeof(double)*M1*2);
|
xue@1
|
2573 for (int k=0; k<Kr; k++) ((double*)(&r[1]))[uind[k]]=pq[k];
|
xue@1
|
2574 //estiamte r0
|
xue@1
|
2575 if (r0)
|
xue@1
|
2576 {
|
xue@1
|
2577 cdouble e0=0;
|
xue@1
|
2578 for (int i=0; i<Wid; i++)
|
xue@1
|
2579 {
|
xue@1
|
2580 cdouble expo=0;
|
xue@1
|
2581 double n=i-hWid;
|
xue@1
|
2582 for (int m=1; m<M; m++){double lhm=h[m](n, harg); expo+=r[m]*lhm;}
|
xue@1
|
2583 cdouble tmp=exp(expo)*win[0][i];
|
xue@1
|
2584 e0+=tmp.rotate(-omg*n);
|
xue@1
|
2585 }
|
xue@1
|
2586 r[0]=log(Skw00/e0);
|
xue@1
|
2587 }
|
xue@1
|
2588 delete[] x;
|
xue@1
|
2589 delete[] Skw;
|
xue@1
|
2590 delete[] uind;
|
xue@1
|
2591 delete[] ind;
|
xue@1
|
2592 DeAlloc2(Smkw);
|
xue@1
|
2593 DeAlloc2(A);
|
xue@1
|
2594 delete[] pq; delete[] b;
|
xue@1
|
2595 }//DerivativeLS
|
xue@1
|
2596
|
xue@1
|
2597 //---------------------------------------------------------------------------
|
xue@1
|
2598 /*
|
xue@1
|
2599 Abe-Smith sinusoid estimator 2005
|
xue@1
|
2600
|
xue@1
|
2601 Further reading: M. Abe and J. O. Smith III, ¡°AM/FM rate estimation for time-varying sinusoidal
|
xue@1
|
2602 modeling,¡± in Proc. ICASSP'05, Philadelphia, 2005.
|
xue@1
|
2603 */
|
xue@1
|
2604
|
xue@1
|
2605 /*
|
xue@1
|
2606 function RDFTW: windowed DTFT at frequency k bins
|
xue@1
|
2607
|
xue@1
|
2608 In: data[Wid]: waveform data
|
xue@1
|
2609 w[Wid]: window function
|
xue@1
|
2610 k: frequency, in bins
|
xue@1
|
2611 Out: Xr, Xi: real and imaginary parts of the DTFT of xw at frequency k bins
|
xue@1
|
2612
|
xue@1
|
2613 No return value.
|
xue@1
|
2614 */
|
xue@1
|
2615 void RDFTW(double& Xr, double& Xi, double k, int Wid, double* data, double* w)
|
xue@1
|
2616 {
|
xue@1
|
2617 Xr=Xi=0;
|
xue@1
|
2618 int hWid=Wid/2;
|
xue@1
|
2619 double* lw=&w[Wid];
|
xue@1
|
2620 for (int i=0; i<=Wid; i++)
|
xue@1
|
2621 {
|
xue@1
|
2622 double tmp;
|
xue@1
|
2623 tmp=*data**lw;
|
xue@1
|
2624 data++, lw--;
|
xue@1
|
2625 //*
|
xue@1
|
2626 double ph=-2*M_PI*(i-hWid)*k/Wid;
|
xue@1
|
2627 Xr+=tmp*cos(ph);
|
xue@1
|
2628 Xi+=tmp*sin(ph); //*/
|
xue@1
|
2629 }
|
xue@1
|
2630 }//RDFTW
|
xue@1
|
2631
|
xue@1
|
2632 /*
|
xue@1
|
2633 function TFAS05: the Abe-Smith method 2005
|
xue@1
|
2634
|
xue@1
|
2635 In: data[Wid]: waveform data
|
xue@1
|
2636 w[Wid]: window function
|
xue@1
|
2637 res: resolution of frequency for QIFFT
|
xue@1
|
2638 Out: f, a, ph: frequency, amplitude and phase angle estimates
|
xue@1
|
2639 aesp, fslope: estimates of log amplitude and frequency derivatives
|
xue@1
|
2640
|
xue@1
|
2641 No return value.
|
xue@1
|
2642 */
|
xue@1
|
2643 void TFAS05(double& f, double& t, double& a, double& ph, double& aesp, double& fslope, int Wid, double* data, double* w, double res)
|
xue@1
|
2644 {
|
xue@1
|
2645 double fi=floor(f*Wid+0.5); //frequency (int) in bins
|
xue@1
|
2646 double xr0, xi0, xr_1, xi_1, xr1, xi1;
|
xue@1
|
2647 RDFTW(xr0, xi0, fi, Wid, data, w);
|
xue@1
|
2648 RDFTW(xr_1, xi_1, fi-res, Wid, data, w);
|
xue@1
|
2649 RDFTW(xr1, xi1, fi+res, Wid, data, w);
|
xue@1
|
2650 double winnorm=0; for (int i=0; i<=Wid; i++) winnorm+=w[i];
|
xue@1
|
2651 double y0=log(sqrt(xr0*xr0+xi0*xi0)/winnorm),
|
xue@1
|
2652 y_1=log(sqrt(xr_1*xr_1+xi_1*xi_1)/winnorm),
|
xue@1
|
2653 y1=log(sqrt(xr1*xr1+xi1*xi1)/winnorm);
|
xue@1
|
2654 double df=0;
|
xue@1
|
2655 //*
|
xue@1
|
2656 if (y0<y1)
|
xue@1
|
2657 {
|
xue@1
|
2658 double newfi=fi+res;
|
xue@1
|
2659 while (y0<y1)
|
xue@1
|
2660 {
|
xue@1
|
2661 y_1=y0, xr_1=xr0, xi_1=xi0;
|
xue@1
|
2662 y0=y1, xr0=xr1, xi0=xi1;
|
xue@1
|
2663 newfi+=res;
|
xue@1
|
2664 RDFTW(xr1, xi1, newfi, Wid, data, w);
|
xue@1
|
2665 y1=log(sqrt(xr1*xr1+xi1*xi1)/winnorm);
|
xue@1
|
2666 fi+=res;
|
xue@1
|
2667 }
|
xue@1
|
2668 }
|
xue@1
|
2669 else if(y0<y_1)
|
xue@1
|
2670 {
|
xue@1
|
2671 double newfi=fi-res;
|
xue@1
|
2672 while (y0<y_1)
|
xue@1
|
2673 {
|
xue@1
|
2674 y1=y0, xr1=xr0, xi1=xi0;
|
xue@1
|
2675 y0=y_1, xr0=xr_1, xi0=xi_1;
|
xue@1
|
2676 newfi-=res;
|
xue@1
|
2677 RDFTW(xr_1, xi_1, newfi, Wid, data, w);
|
xue@1
|
2678 y_1=log(sqrt(xr_1*xr_1+xi_1*xi_1)/winnorm);
|
xue@1
|
2679 fi-=res;
|
xue@1
|
2680 }
|
xue@1
|
2681 } //*/
|
xue@1
|
2682
|
xue@1
|
2683 double a2=(y1+y_1)*0.5-y0, a1=(y1-y_1)*0.5, a0=y0;
|
xue@1
|
2684 df=-a1*0.5/a2;
|
xue@1
|
2685 f=fi+df*res; //in bins
|
xue@1
|
2686 double y=a0-0.25*a1*a1/a2;
|
xue@1
|
2687 a=exp(y);
|
xue@1
|
2688 double ph0=(xi0==0 && xr0==0)?0:atan2(xi0, xr0),
|
xue@1
|
2689 ph_1=(xi_1==0 && xr_1==0)?0:atan2(xi_1, xr_1),
|
xue@1
|
2690 ph1=(xi1==0 && xr1==0)?0:atan2(xi1, xr1);
|
xue@1
|
2691 if (fabs(ph_1-ph0)>M_PI)
|
xue@1
|
2692 {
|
xue@1
|
2693 if (ph_1-ph0>0) ph_1-=M_PI*2;
|
xue@1
|
2694 else ph_1+=M_PI*2;
|
xue@1
|
2695 }
|
xue@1
|
2696 if (fabs(ph1-ph0)>M_PI)
|
xue@1
|
2697 {
|
xue@1
|
2698 if (ph1-ph0>0) ph1-=M_PI*2;
|
xue@1
|
2699 else ph1+=M_PI*2;
|
xue@1
|
2700 }
|
xue@1
|
2701 double b2=(ph1+ph_1)*0.5-ph0, b1=(ph1-ph_1)*0.5, b0=ph0;
|
xue@1
|
2702 ph=b0+b1*(df+b2*df);
|
xue@1
|
2703 //now we have the QI estimates
|
xue@1
|
2704 double uff=2*a2, vf=b1+2*b2*df, vff=2*b2;
|
xue@1
|
2705 double dfdp=Wid/(2*M_PI*res);
|
xue@1
|
2706 double upp=uff*dfdp*dfdp, vp=vf*dfdp, vpp=vff*dfdp*dfdp;
|
xue@1
|
2707 double p=-upp*0.5/(upp*upp+vpp*vpp);
|
xue@1
|
2708 double alf=-2*p*vp, beta=p*vpp/upp;
|
xue@1
|
2709 //*direct method
|
xue@1
|
2710 double beta_p=beta/p;
|
xue@1
|
2711 double feses=f-alf*beta/p /(2*M_PI)*Wid,
|
xue@1
|
2712 yeses=y-alf*alf*0.25/p+0.25*log(1+beta_p*beta_p),
|
xue@1
|
2713 pheses=ph+alf*alf*beta*0.25/p-0.5*atan(beta_p); //*/
|
xue@1
|
2714 /*adapted method
|
xue@1
|
2715 double zt[]={0, 0.995354, 0.169257, 1.393056, 0.442406, -0.717980, -0.251620, 0.177511, 0.158120, -0.503299};
|
xue@1
|
2716 double delt=res/Wid; double delt0=df*delt;
|
xue@1
|
2717 beta=zt[3]*beta+zt[4]*delt0*alf;
|
xue@1
|
2718 alf=(zt[1]+zt[2]*delt*delt)*alf;
|
xue@1
|
2719 double beta_p=beta/p;
|
xue@1
|
2720 double feses=f+zt[5]*alf*beta/p /(2*M_PI)*Wid,
|
xue@1
|
2721 yeses=y+zt[6]*alf*alf/p+zt[7]*log(1+beta_p*beta_p),
|
xue@1
|
2722 pheses=ph+zt[8]*alf*alf*beta/p+zt[9]*atan(beta_p); //*/
|
xue@1
|
2723 f=feses/Wid, a=exp(yeses), ph=pheses, fslope=2*beta/2/M_PI, aesp=alf;
|
xue@1
|
2724 }//TFAS05
|
xue@1
|
2725
|
xue@1
|
2726 /*
|
xue@1
|
2727 function TFAS05_enh: the Abe-Smith method 2005 enhanced by LSE amplitude and phase estimation
|
xue@1
|
2728
|
xue@1
|
2729 In: data[Wid]: waveform data
|
xue@1
|
2730 w[Wid]: window function
|
xue@1
|
2731 res: resolution of frequency for QIFFT
|
xue@1
|
2732 Out: f, a, ph: frequency, amplitude and phase angle estimates
|
xue@1
|
2733 aesp, fslope: estimates of log amplitude and frequency derivatives
|
xue@1
|
2734
|
xue@1
|
2735 No return value.
|
xue@1
|
2736 */
|
xue@1
|
2737 void TFAS05_enh(double& f, double& t, double& a, double& ph, double& aesp, double& fslope, int Wid, double* data, double* w, double res)
|
xue@1
|
2738 {
|
xue@1
|
2739 TFAS05(f, t, a, ph, aesp, fslope, Wid, data, w, res);
|
xue@1
|
2740 double xr=0, xi=0, p, win2=0;
|
xue@1
|
2741 for (int n=0; n<=Wid; n++)
|
xue@1
|
2742 {
|
xue@1
|
2743 double ni=n-Wid/2, tmp=data[n]*w[n]*w[n];//*exp(-aesp*(n-Wid/2)); if (IsInfinite(tmp)) continue;
|
xue@1
|
2744 p=-2*M_PI*(f+0.5*fslope*ni)*ni;
|
xue@1
|
2745 xr+=tmp*cos(p);
|
xue@1
|
2746 xi+=tmp*sin(p);
|
xue@1
|
2747 win2+=w[n]*w[n];
|
xue@1
|
2748 }
|
xue@1
|
2749 a=sqrt(xr*xr+xi*xi)/win2;
|
xue@1
|
2750 ph=(xr==0 && xi==0)?0:atan2(xi, xr);
|
xue@1
|
2751 }//TFAS05_enh
|
xue@1
|
2752 //version without returning aesp and fslope
|
xue@1
|
2753 void TFAS05_enh(double& f, double& t, double& a, double& ph, int Wid, double* data, double* w, double res)
|
xue@1
|
2754 {
|
xue@1
|
2755 double aesp, fslope;
|
xue@1
|
2756 TFAS05_enh(f, t, a, ph, aesp, fslope, Wid, data, w, res);
|
xue@1
|
2757 }//TFAS05_enh
|
xue@1
|
2758
|
xue@1
|
2759 //---------------------------------------------------------------------------
|
xue@1
|
2760 /*
|
xue@1
|
2761 function DerivativeLSv_AmpPh: estimate the constant-term in the local derivative method. This is used
|
xue@1
|
2762 by the local derivative algorithm, whose implementation is found in the header file as templates.
|
xue@1
|
2763
|
xue@1
|
2764 In: sv0: inner product <s, v0>, where s is the sinusoid being estimated.
|
xue@1
|
2765 integr_h[M][Wid]: M vectors containing samples of the integral of basis functions h[M].
|
xue@1
|
2766 v0[M]: a test function
|
xue@1
|
2767 lmd[M]: coefficients to h[M]
|
xue@1
|
2768
|
xue@1
|
2769 Returns coefficient of integr_h[0]=1.
|
xue@1
|
2770 */
|
xue@1
|
2771 cdouble DerivativeLSv_AmpPh(int Wid, int M, double** integr_h, cdouble* lmd, cdouble* v0, cdouble sv0)
|
xue@1
|
2772 {
|
xue@1
|
2773 cdouble e0=0;
|
xue@1
|
2774 for (int n=0; n<Wid; n++)
|
xue@1
|
2775 {
|
xue@1
|
2776 cdouble expo=0;
|
xue@1
|
2777 for (int m=1; m<=M; m++) expo+=lmd[m]*integr_h[m][n];
|
xue@1
|
2778 e0+=exp(expo)**v0[n];
|
xue@1
|
2779 }
|
xue@1
|
2780 return log(sv0/e0);
|
xue@1
|
2781 }//DerivativeLSv_AmpPh
|
xue@1
|
2782
|
xue@1
|
2783 //---------------------------------------------------------------------------
|
xue@1
|
2784 /*
|
xue@1
|
2785 Piecewise derivative algorithm
|
xue@1
|
2786
|
xue@1
|
2787 Further reading: Wen X. and M. Sandler, "Spline exponential approximation of time-varying
|
xue@1
|
2788 sinusoids," under review.
|
xue@1
|
2789 */
|
xue@1
|
2790
|
xue@1
|
2791 /*
|
xue@1
|
2792 function setv: computes I test functions v[I] by modulation u[I] to frequency f
|
xue@1
|
2793
|
xue@1
|
2794 In: u[I+1][Wid], du[I+1][Wid]: base-band test functions and their derivatives
|
xue@1
|
2795 f: carrier frequency
|
xue@1
|
2796 Out: v[I][Wid], dv[I][Wid]: test functions and their derivatives
|
xue@1
|
2797
|
xue@1
|
2798 No return value.
|
xue@1
|
2799 */
|
xue@1
|
2800 void setv(int I, int Wid, cdouble** v, cdouble** dv, double f, cdouble** u, cdouble** du)
|
xue@1
|
2801 {
|
xue@1
|
2802 double fbin=floor(f*Wid+0.5)/Wid;
|
xue@1
|
2803 double omg=fbin*2*M_PI;
|
xue@1
|
2804 cdouble jomg=cdouble(0, omg);
|
xue@1
|
2805 for (int c=0; c<Wid; c++)
|
xue@1
|
2806 {
|
xue@1
|
2807 double t=c;
|
xue@1
|
2808 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
2809 for (int i=0; i<I-1; i++) v[i][c]=u[i][c]*rot;
|
xue@1
|
2810 for (int i=0; i<I-1; i++) dv[i][c]=du[i][c]*rot+jomg*v[i][c];
|
xue@1
|
2811 //Here it is assumed that elements of u[] are modulated at 0, 1, -1, 2, -2, 3, -3, 4, ...;
|
xue@1
|
2812 //if f is under fbin then the closest ones are in order 0, -1, 1, -2, 3, -3, 3, .... This
|
xue@1
|
2813 //makes a difference to the whole of v[] only if I is even.
|
xue@1
|
2814 if (f>=fbin || I%2==1){v[I-1][c]=u[I-1][c]*rot; dv[I-1][c]=du[I-1][c]*rot+jomg*v[I-1][c];}
|
xue@1
|
2815 else{v[I-1][c]=u[I][c]*rot; dv[I-1][c]=du[I][c]*rot+jomg*v[I-1][c];}
|
xue@1
|
2816 }
|
xue@1
|
2817 }//setv
|
xue@1
|
2818
|
xue@1
|
2819 /*
|
xue@1
|
2820 function setvhalf: computes I half-size test functions v[I] by modulation u[I] to frequency f.
|
xue@1
|
2821
|
xue@1
|
2822 In: u[I][hWid*2], du[I][Wid*2]: base-band test functions and their derivatives
|
xue@1
|
2823 f: carrier frequency
|
xue@1
|
2824 Out: v[I][hWid], dv[hWid]: half-size test functions and their derivatives
|
xue@1
|
2825
|
xue@1
|
2826 No return value.
|
xue@1
|
2827 */void setvhalf(int I, int hWid, cdouble** v, cdouble** dv, double f, cdouble** u, cdouble** du)
|
xue@1
|
2828 {
|
xue@1
|
2829 double fbin=floor(f*hWid)/hWid;
|
xue@1
|
2830 double omg=fbin*2*M_PI;
|
xue@1
|
2831 cdouble jomg=cdouble(0, omg);
|
xue@1
|
2832 for (int c=0; c<hWid; c++)
|
xue@1
|
2833 {
|
xue@1
|
2834 double t=c;
|
xue@1
|
2835 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
2836 for (int i=0; i<I; i++) v[i][c]=u[i][c*2]*rot;
|
xue@1
|
2837 for (int i=0; i<I; i++) dv[i][c]=rot*du[i][c*2]*cdouble(2.0)+jomg*v[i][c];
|
xue@1
|
2838 }
|
xue@1
|
2839 }//setvhalf
|
xue@1
|
2840
|
xue@1
|
2841 //#define ERROR_CHECK
|
xue@1
|
2842
|
xue@1
|
2843 /*
|
xue@1
|
2844 function DerivativePiecewise: Piecewise derivative algorithm. In this implementation of the piecewise
|
xue@1
|
2845 method the test functions v are constructed from I "basic" (single-frame) test functions, each
|
xue@1
|
2846 covering the same period of 2T, by shifting these I functions by steps of T. A total number of (L-1)I
|
xue@1
|
2847 test functions are used.
|
xue@1
|
2848
|
xue@1
|
2849 In: s[LT+1]: waveform data
|
xue@1
|
2850 ds[LT+1]: derivative of s[LT], used only if ERROR_CHECK is defined.
|
xue@1
|
2851 L, T: number and length of pieces.
|
xue@1
|
2852 N: number of independent coefficients
|
xue@1
|
2853 h[M][T]: piecewise basis functions
|
xue@1
|
2854 A[L][M][N]: L matrices that map independent coefficients onto component coefficients over the L pieces
|
xue@1
|
2855 u[I][2T}, du[I][2T]: base-band test functions
|
xue@1
|
2856 f[L+1]: reference frequencies at 0, T, ..., LT, only f[1]...f[L-1] are used
|
xue@1
|
2857 endmode: set to 1 or 3 to apply half-size testing over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
2858 Out: aita[N]: independent coefficients
|
xue@1
|
2859
|
xue@1
|
2860 No return value.
|
xue@1
|
2861 */
|
xue@1
|
2862 void DerivativePiecewise(int N, cdouble* aita, int L, double* f, int T, cdouble* s, double*** A, int M, double** h, int I, cdouble** u, cdouble** du, int endmode, cdouble* ds)
|
xue@1
|
2863 {
|
xue@1
|
2864 MList* mlist=new MList;
|
xue@1
|
2865 int L_1=(endmode==0)?(L-1):((endmode==3)?(L+1):L);
|
xue@1
|
2866 cdouble** Allocate2L(cdouble, L_1, I, sv, mlist);
|
xue@1
|
2867 cdouble** Allocate2(cdouble, I, T*2, v);
|
xue@1
|
2868 cdouble** Allocate2(cdouble, I, T*2, dv);
|
xue@1
|
2869 //compute <sr, v>
|
xue@1
|
2870 cdouble*** Allocate3L(cdouble, L_1, I, N, srv, mlist);
|
xue@1
|
2871 cdouble** Allocate2L(cdouble, I, M, shv1, mlist);
|
xue@1
|
2872 cdouble** Allocate2L(cdouble, I, M, shv2, mlist);
|
xue@1
|
2873
|
xue@1
|
2874 #ifdef ERROR_CHECK
|
xue@1
|
2875 cdouble dsv1[128], dsv2[128];
|
xue@1
|
2876 #endif
|
xue@1
|
2877 for (int l=0; l<L-1; l++)
|
xue@1
|
2878 {
|
xue@1
|
2879 //v from u given f[l]
|
xue@1
|
2880 double fbin=floor(f[l+1]*T*2)/(T*2.0);
|
xue@1
|
2881 double omg=fbin*2*M_PI;
|
xue@1
|
2882 cdouble jomg=cdouble(0, omg);
|
xue@1
|
2883 for (int c=0; c<T*2; c++)
|
xue@1
|
2884 {
|
xue@1
|
2885 double t=c-T;
|
xue@1
|
2886 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
2887 for (int i=0; i<I; i++) v[i][c]=u[i][c]*rot;
|
xue@1
|
2888 for (int i=0; i<I; i++) dv[i][c]=du[i][c]*rot+jomg*v[i][c];
|
xue@1
|
2889 }
|
xue@1
|
2890
|
xue@1
|
2891 //compute -<s, v'> over the lth frame
|
xue@1
|
2892 cdouble* ls=&s[l*T]; for (int i=0; i<I; i++) sv[l][i]=-Inner(2*T, ls, dv[i]);
|
xue@1
|
2893
|
xue@1
|
2894 //compute <sr, v> over the lth frame
|
xue@1
|
2895 cdouble *ls1=&s[l*T], *ls2=&s[l*T+T];
|
xue@1
|
2896 for (int i=0; i<I; i++)
|
xue@1
|
2897 for (int m=0; m<M; m++)
|
xue@1
|
2898 shv1[i][m]=Inner(T, ls1, h[m], v[i]), shv2[i][m]=Inner(T, ls2, h[m], &v[i][T]);
|
xue@1
|
2899 //memset(srv[l][0], 0, sizeof(cdouble)*I*N);
|
xue@1
|
2900 MultiplyXY(I, M, N, srv[l], shv1, A[l]);
|
xue@1
|
2901 MultiAddXY(I, M, N, srv[l], shv2, A[l+1]);
|
xue@1
|
2902
|
xue@1
|
2903 #ifdef ERROR_CHECK
|
xue@1
|
2904 //error check: <s', v>=-<s, v'>
|
xue@1
|
2905 if (ds)
|
xue@1
|
2906 {
|
xue@1
|
2907 cdouble* lds=&ds[l*T];
|
xue@1
|
2908 for (int i=0; i<I && l*I+1<36; i++)
|
xue@1
|
2909 {
|
xue@1
|
2910 cdouble lsv=Inner(2*T, lds, v[i]); //compute <s', v[i]>
|
xue@1
|
2911 //cdouble* ls=&s[l*T];
|
xue@1
|
2912 //cdouble lsv2=Inner(2*T, ls, dv[i]);
|
xue@1
|
2913 dsv1[l*I+i]=lsv-sv[l][i]; //i.e. <s', v[i]>=-<s, v[i]'>+dsv1[lI+i]
|
xue@1
|
2914 }
|
xue@1
|
2915
|
xue@1
|
2916 //error check: srv[l]*pq=<s',v>
|
xue@1
|
2917 for (int i=0; i<I && l*I+i<36; i++)
|
xue@1
|
2918 {
|
xue@1
|
2919 cdouble lsv=0;
|
xue@1
|
2920 for (int n=0; n<N; n++) lsv+=srv[l][i][n]*aita[n];
|
xue@1
|
2921 dsv2[l*I+i]=lsv-sv[l][i]-dsv1[l*I+i];
|
xue@1
|
2922 }
|
xue@1
|
2923 }
|
xue@1
|
2924 #endif
|
xue@1
|
2925 }
|
xue@1
|
2926 L_1=L-1;
|
xue@1
|
2927 if (endmode==1 || endmode==3)
|
xue@1
|
2928 {
|
xue@1
|
2929 //v from u given f[l]
|
xue@1
|
2930 int hT=T/2;
|
xue@1
|
2931 double fbin=floor((f[0]+f[1])*hT)/T;
|
xue@1
|
2932 double omg=fbin*2*M_PI;
|
xue@1
|
2933 cdouble jomg=cdouble(0, omg);
|
xue@1
|
2934 for (int c=0; c<T; c++)
|
xue@1
|
2935 {
|
xue@1
|
2936 double t=c-hT;
|
xue@1
|
2937 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
2938 for (int i=0; i<I; i++) v[i][c]=u[i][c*2]*rot;
|
xue@1
|
2939 for (int i=0; i<I; i++) dv[i][c]=rot*du[i][c*2]*cdouble(2.0)+jomg*v[i][c];
|
xue@1
|
2940 }
|
xue@1
|
2941
|
xue@1
|
2942 //compute -<s, v'> over the lth frame
|
xue@1
|
2943 cdouble* ls=&s[0]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
2944
|
xue@1
|
2945 //compute <sr, v> over the lth frame
|
xue@1
|
2946 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
2947 //memset(srv[L_1][0], 0, sizeof(cdouble)*I*N);
|
xue@1
|
2948 MultiplyXY(I, M, N, srv[L_1], shv1, A[0]);
|
xue@1
|
2949 #ifdef ERROR_CHECK
|
xue@1
|
2950 //error check: <s', v>=-<s, v'>
|
xue@1
|
2951 if (ds)
|
xue@1
|
2952 {
|
xue@1
|
2953 cdouble* lds=&ds[0];
|
xue@1
|
2954 for (int i=0; i<I && L_1*I+1<36; i++)
|
xue@1
|
2955 {
|
xue@1
|
2956 cdouble lsv=Inner(T, lds, v[i]); //compute <s', v[i]>
|
xue@1
|
2957 //cdouble* ls=&s[l*T];
|
xue@1
|
2958 //cdouble lsv2=Inner(2*T, ls, dv[i]);
|
xue@1
|
2959 dsv1[L_1*I+i]=lsv-sv[L_1][i]; //i.e. <s', v[i]>=-<s, v[i]'>+dsv1[lI+i]
|
xue@1
|
2960 }
|
xue@1
|
2961
|
xue@1
|
2962 //error check: srv[l]*pq=<s',v>
|
xue@1
|
2963 for (int i=0; i<I && L_1*I+i<36; i++)
|
xue@1
|
2964 {
|
xue@1
|
2965 cdouble lsv=0;
|
xue@1
|
2966 for (int n=0; n<N; n++) lsv+=srv[L_1][i][n]*aita[n];
|
xue@1
|
2967 dsv2[L_1*I+i]=lsv-sv[L_1][i]-dsv1[L_1*I+i];
|
xue@1
|
2968 }
|
xue@1
|
2969 }
|
xue@1
|
2970 #endif
|
xue@1
|
2971 L_1++;
|
xue@1
|
2972 }
|
xue@1
|
2973 if (endmode==2 || endmode==3)
|
xue@1
|
2974 {
|
xue@1
|
2975 //v from u given f[l]
|
xue@1
|
2976 int hT=T/2;
|
xue@1
|
2977 double fbin=floor((f[L-1]+f[L])*hT)/T;
|
xue@1
|
2978 double omg=fbin*2*M_PI;
|
xue@1
|
2979 cdouble jomg=cdouble(0, omg);
|
xue@1
|
2980 for (int c=0; c<T; c++)
|
xue@1
|
2981 {
|
xue@1
|
2982 double t=c-hT;
|
xue@1
|
2983 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
2984 for (int i=0; i<I; i++) v[i][c]=u[i][c*2]*rot;
|
xue@1
|
2985 for (int i=0; i<I; i++) dv[i][c]=cdouble(2.0)*du[i][c*2]*rot+jomg*v[i][c];
|
xue@1
|
2986 }
|
xue@1
|
2987
|
xue@1
|
2988 //compute -<s, v'> over the lth frame
|
xue@1
|
2989 cdouble* ls=&s[(L-1)*T]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
2990
|
xue@1
|
2991 //compute <sr, v> over the lth frame
|
xue@1
|
2992 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
2993 //memset(srv[L_1][0], 0, sizeof(cdouble)*I*N);
|
xue@1
|
2994 MultiplyXY(I, M, N, srv[L_1], shv1, A[L-1]);
|
xue@1
|
2995 #ifdef ERROR_CHECK
|
xue@1
|
2996 //error check: <s', v>=-<s, v'>
|
xue@1
|
2997 if (ds)
|
xue@1
|
2998 {
|
xue@1
|
2999 cdouble* lds=&ds[(L-1)*T];
|
xue@1
|
3000 for (int i=0; i<I && L_1*I+1<36; i++)
|
xue@1
|
3001 {
|
xue@1
|
3002 cdouble lsv=Inner(T, lds, v[i]); //compute <s', v[i]>
|
xue@1
|
3003 //cdouble* ls=&s[l*T];
|
xue@1
|
3004 //cdouble lsv2=Inner(2*T, ls, dv[i]);
|
xue@1
|
3005 dsv1[L_1*I+i]=lsv-sv[L_1][i]; //i.e. <s', v[i]>=-<s, v[i]'>+dsv1[lI+i]
|
xue@1
|
3006 }
|
xue@1
|
3007
|
xue@1
|
3008 //error check: srv[l]*pq=<s',v>
|
xue@1
|
3009 for (int i=0; i<I && L_1*I+i<36; i++)
|
xue@1
|
3010 {
|
xue@1
|
3011 cdouble lsv=0;
|
xue@1
|
3012 for (int n=0; n<N; n++) lsv+=srv[L_1][i][n]*aita[n];
|
xue@1
|
3013 dsv2[L_1*I+i]=lsv-sv[L_1][i]-dsv1[L_1*I+i];
|
xue@1
|
3014 }
|
xue@1
|
3015 }
|
xue@1
|
3016 #endif
|
xue@1
|
3017 L_1++;
|
xue@1
|
3018 }
|
xue@1
|
3019
|
xue@1
|
3020 if (L_1*2*I==2*N) GECP(N, aita, srv[0], sv[0]);
|
xue@1
|
3021 else LSLinear(L_1*I, N, aita, srv[0], sv[0]);
|
xue@1
|
3022
|
xue@1
|
3023 delete mlist;
|
xue@1
|
3024 }//DerivativePiecewise
|
xue@1
|
3025
|
xue@1
|
3026 /*
|
xue@1
|
3027 function DerivativePiecewise2: Piecewise derivative algorithm in which the real and imaginary parts of
|
xue@1
|
3028 the exponent are modelled separately. In this implementation of the piecewise method the test
|
xue@1
|
3029 functions v are constructed from I "basic" (single-frame) test functions, each covering the same
|
xue@1
|
3030 period of 2T, by shifting these I functions by steps of T. A total number of (L-1)I test functions are
|
xue@1
|
3031 used.
|
xue@1
|
3032
|
xue@1
|
3033 In: s[LT+1]: waveform data
|
xue@1
|
3034 ds[LT+1]: derivative of s[LT], used only if ERROR_CHECK is defined.
|
xue@1
|
3035 L, T: number and length of pieces.
|
xue@1
|
3036 N: number of independent coefficients
|
xue@1
|
3037 h[M][T]: piecewise basis functions
|
xue@1
|
3038 A[L][M][Np]: L matrices that do coefficient mapping (real part) over the L pieces
|
xue@1
|
3039 B[L][M][Nq]: L matrices that do coefficient mapping (imaginary part) over the L pieces
|
xue@1
|
3040 u[I][2T}, du[I][2T]: base-band test functions
|
xue@1
|
3041 f[L+1]: reference frequencies at 0, T, ..., LT, only f[1]...f[L-1] are used
|
xue@1
|
3042 endmode: set to 1 or 3 to apply half-size testing over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
3043 Out: p[Np], q[Nq]: independent coefficients
|
xue@1
|
3044
|
xue@1
|
3045 No return value.
|
xue@1
|
3046 */
|
xue@1
|
3047 void DerivativePiecewise2(int Np, double* p, int Nq, double* q, int L, double* f, int T, cdouble* s, double*** A, double*** B,
|
xue@1
|
3048 int M, double** h, int I, cdouble** u, cdouble** du, int endmode, cdouble* ds)
|
xue@1
|
3049 {
|
xue@1
|
3050 MList* mlist=new MList;
|
xue@1
|
3051 int L_1=(endmode==0)?(L-1):((endmode==3)?(L+1):L);
|
xue@1
|
3052 cdouble** Allocate2L(cdouble, L_1, I, sv, mlist);
|
xue@1
|
3053 cdouble** Allocate2(cdouble, I, T*2, v);
|
xue@1
|
3054 cdouble** Allocate2(cdouble, I, T*2, dv);
|
xue@1
|
3055 //compute <sr, v>
|
xue@1
|
3056 cdouble*** Allocate3L(cdouble, L_1, I, Np, srav, mlist);
|
xue@1
|
3057 cdouble*** srbv;
|
xue@1
|
3058 if (Np==Nq && B==A) srbv=srav; else {Allocate3L(cdouble, L_1, I, Nq, srbv, mlist);} //same model for amplitude and phase
|
xue@1
|
3059 cdouble** Allocate2L(cdouble, I, M, shv1, mlist);
|
xue@1
|
3060 cdouble** Allocate2L(cdouble, I, M, shv2, mlist);
|
xue@1
|
3061
|
xue@1
|
3062 for (int l=0; l<L-1; l++)
|
xue@1
|
3063 {
|
xue@1
|
3064 //v from u given f[l]
|
xue@1
|
3065 double fbin=floor(f[l+1]*T*2)/(T*2.0);
|
xue@1
|
3066 double omg=fbin*2*M_PI;
|
xue@1
|
3067 cdouble jomg=cdouble(0, omg);
|
xue@1
|
3068 for (int c=0; c<T*2; c++)
|
xue@1
|
3069 {
|
xue@1
|
3070 double t=c-T;
|
xue@1
|
3071 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
3072 for (int i=0; i<I; i++) v[i][c]=u[i][c]*rot;
|
xue@1
|
3073 for (int i=0; i<I; i++) dv[i][c]=du[i][c]*rot+jomg*v[i][c];
|
xue@1
|
3074 }
|
xue@1
|
3075
|
xue@1
|
3076 //compute -<s, v'> over the lth frame
|
xue@1
|
3077 cdouble* ls=&s[l*T]; for (int i=0; i<I; i++) sv[l][i]=-Inner(2*T, ls, dv[i]);
|
xue@1
|
3078
|
xue@1
|
3079 //compute <sr, v> over the lth frame
|
xue@1
|
3080 cdouble *ls1=&s[l*T], *ls2=&s[l*T+T];
|
xue@1
|
3081 for (int i=0; i<I; i++)
|
xue@1
|
3082 for (int m=0; m<M; m++)
|
xue@1
|
3083 shv1[i][m]=Inner(T, ls1, h[m], v[i]), shv2[i][m]=Inner(T, ls2, h[m], &v[i][T]);
|
xue@1
|
3084 memset(srav[l][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3085 MultiplyXY(I, M, Np, srav[l], shv1, A[l]);
|
xue@1
|
3086 MultiAddXY(I, M, Np, srav[l], shv2, A[l+1]);
|
xue@1
|
3087 if (srbv!=srav) //so that either B!=A or Np!=Nq
|
xue@1
|
3088 {
|
xue@1
|
3089 //memset(srbv[l][0], 0, sizeof(cdouble)*I*Nq);
|
xue@1
|
3090 MultiplyXY(I, M, Nq, srbv[l], shv1, B[l]);
|
xue@1
|
3091 MultiAddXY(I, M, Nq, srbv[l], shv2, B[l+1]);
|
xue@1
|
3092 }
|
xue@1
|
3093 }
|
xue@1
|
3094 L_1=L-1;
|
xue@1
|
3095 if (endmode==1 || endmode==3)
|
xue@1
|
3096 {
|
xue@1
|
3097 //v from u given f[l]
|
xue@1
|
3098 int hT=T/2;
|
xue@1
|
3099 double fbin=floor((f[0]+f[1])*hT)/T;
|
xue@1
|
3100 double omg=fbin*2*M_PI;
|
xue@1
|
3101 cdouble jomg=cdouble(0, omg);
|
xue@1
|
3102 for (int c=0; c<T; c++)
|
xue@1
|
3103 {
|
xue@1
|
3104 double t=c-hT;
|
xue@1
|
3105 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
3106 for (int i=0; i<I; i++) v[i][c]=u[i][c*2]*rot;
|
xue@1
|
3107 for (int i=0; i<I; i++) dv[i][c]=rot*du[i][c*2]*cdouble(2.0)+jomg*v[i][c];
|
xue@1
|
3108 }
|
xue@1
|
3109
|
xue@1
|
3110 //compute -<s, v'> over the lth frame
|
xue@1
|
3111 cdouble* ls=&s[0]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
3112
|
xue@1
|
3113 //compute <sr, v> over the lth frame
|
xue@1
|
3114 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
3115 //memset(srav[L_1][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3116 MultiplyXY(I, M, Np, srav[L_1], shv1, A[0]);
|
xue@1
|
3117 if (srbv!=srav) {memset(srbv[L_1][0], 0, sizeof(cdouble)*I*Nq); MultiplyXY(I, M, Nq, srbv[L_1], shv1, B[0]);}
|
xue@1
|
3118 L_1++;
|
xue@1
|
3119 }
|
xue@1
|
3120 if (endmode==2 || endmode==3)
|
xue@1
|
3121 {
|
xue@1
|
3122 //v from u given f[l]
|
xue@1
|
3123 int hT=T/2;
|
xue@1
|
3124 double fbin=floor((f[L-1]+f[L])*hT)/T;
|
xue@1
|
3125 double omg=fbin*2*M_PI;
|
xue@1
|
3126 cdouble jomg=cdouble(0, omg);
|
xue@1
|
3127 for (int c=0; c<T; c++)
|
xue@1
|
3128 {
|
xue@1
|
3129 double t=c-hT;
|
xue@1
|
3130 cdouble rot=polar(1.0, omg*t);
|
xue@1
|
3131 for (int i=0; i<I; i++) v[i][c]=u[i][c*2]*rot;
|
xue@1
|
3132 for (int i=0; i<I; i++) dv[i][c]=cdouble(2.0)*du[i][c*2]*rot+jomg*v[i][c];
|
xue@1
|
3133 }
|
xue@1
|
3134
|
xue@1
|
3135 //compute -<s, v'> over the lth frame
|
xue@1
|
3136 cdouble* ls=&s[(L-1)*T]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
3137
|
xue@1
|
3138 //compute <sr, v> over the lth frame
|
xue@1
|
3139 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
3140 memset(srav[L_1][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3141 MultiplyXY(I, M, Np, srav[L_1], shv1, A[L-1]);
|
xue@1
|
3142 if (srbv!=srav)
|
xue@1
|
3143 {
|
xue@1
|
3144 //memset(srbv[L_1][0], 0, sizeof(cdouble)*I*Nq);
|
xue@1
|
3145 MultiplyXY(I, M, Nq, srbv[L_1], shv1, B[L-1]);
|
xue@1
|
3146 }
|
xue@1
|
3147 L_1++;
|
xue@1
|
3148 }
|
xue@1
|
3149
|
xue@1
|
3150 //real implementation of <sr,v>aita=<s',v>
|
xue@1
|
3151 double** Allocate2L(double, L_1*I*2, Np+Nq, AM, mlist);
|
xue@1
|
3152 for (int l=0; l<L_1; l++) for (int i=0; i<I; i++)
|
xue@1
|
3153 {
|
xue@1
|
3154 int li=l*I+i, li_H=li+L_1*I;
|
xue@1
|
3155 for (int n=0; n<Np; n++)
|
xue@1
|
3156 {
|
xue@1
|
3157 AM[li][n]=srav[l][i][n].x;
|
xue@1
|
3158 AM[li_H][n]=srav[l][i][n].y;
|
xue@1
|
3159 }
|
xue@1
|
3160 for (int n=0; n<Nq; n++)
|
xue@1
|
3161 {
|
xue@1
|
3162 AM[li][Np+n]=-srbv[l][i][n].y;
|
xue@1
|
3163 AM[li_H][Np+n]=srbv[l][i][n].x;
|
xue@1
|
3164 }
|
xue@1
|
3165 }
|
xue@1
|
3166 //least-square solution of (srv)(aita)=(sv)
|
xue@1
|
3167 double* pq=new double[Np+Nq]; mlist->Add(pq, 1);
|
xue@1
|
3168 double* b=new double[2*L_1*I]; for (int i=0; i<L_1*I; i++) b[i]=sv[0][i].x, b[i+L_1*I]=sv[0][i].y;
|
xue@1
|
3169
|
xue@1
|
3170 if (L_1*2*I==Np+Nq) GECP(Np+Nq, pq, AM, b);
|
xue@1
|
3171 else LSLinear(2*L_1*I, Np+Nq, pq, AM, b);
|
xue@1
|
3172
|
xue@1
|
3173 memcpy(p, pq, sizeof(double)*Np); memcpy(q, &pq[Np], sizeof(double)*Nq);
|
xue@1
|
3174
|
xue@1
|
3175 delete mlist;
|
xue@1
|
3176 }//DerivativePiecewise2
|
xue@1
|
3177
|
xue@1
|
3178 /*
|
xue@1
|
3179 Error check: test that ds[LT] equals s[LT] times reconstructed R'. Notice that DA is D time A where D
|
xue@1
|
3180 is a pre-emphasis because p[Np] applies to log amplitude rather than its derivative.
|
xue@1
|
3181 */
|
xue@1
|
3182 double testds_pqA(int Np, double* p, int Nq, double* q, int L, int T, cdouble* s, cdouble* ds, int M, double** h, double** dh, double*** DA, double*** B, cdouble* errds=0)
|
xue@1
|
3183 {
|
xue@1
|
3184 double err=0, ene=0, *lamdax=new double[M*2], *lamday=&lamdax[M];
|
xue@1
|
3185 for (int l=0; l<L; l++)
|
xue@1
|
3186 {
|
xue@1
|
3187 MultiplyXy(M, Np, lamdax, DA[l], p);
|
xue@1
|
3188 MultiplyXy(M, Nq, lamday, B[l], q);
|
xue@1
|
3189 for (int t=0; t<T; t++)
|
xue@1
|
3190 {
|
xue@1
|
3191 double drtx=0; for (int m=0; m<M; m++) drtx+=lamdax[m]*h[m][t];
|
xue@1
|
3192 double drty=0; for (int m=0; m<M; m++) drty+=lamday[m]*h[m][t];
|
xue@1
|
3193 cdouble drt=cdouble(drtx, drty);
|
xue@1
|
3194 cdouble eds=ds[l*T+t]-s[l*T+t]*drt;
|
xue@1
|
3195 err+=~eds; ene+=~ds[l*T+t];
|
xue@1
|
3196 if (errds) errds[l*T+t]=eds;
|
xue@1
|
3197 }
|
xue@1
|
3198 }
|
xue@1
|
3199 delete[] lamdax;
|
xue@1
|
3200 return err/ene;
|
xue@1
|
3201 }//testds_pqA
|
xue@1
|
3202
|
xue@1
|
3203 /*
|
xue@1
|
3204 Error check: dsv1[I] tests that <s', v[I]> equals -<s, v[I]'>, dsv2[I] tests that <sr, v[I]>*pq=
|
xue@1
|
3205 <s',v[I]>
|
xue@1
|
3206 */
|
xue@1
|
3207 void testdsv(cdouble* dsv1, cdouble* dsv2, int Np, double* p, int Nq, double* q, int TT, cdouble* dsl, int I, cdouble** vl, cdouble* svl, cdouble** sravl, cdouble** srbvl)
|
xue@1
|
3208 {
|
xue@1
|
3209 for (int i=0; i<I; i++)
|
xue@1
|
3210 {
|
xue@1
|
3211 cdouble lsv=Inner(TT, dsl, vl[i]); //compute <s', v[i]>
|
xue@1
|
3212 //cdouble* ls=&s[l*T];
|
xue@1
|
3213 dsv1[i]=lsv-svl[i]; //i.e. <s', v[i]>=-<s, v[i]'>+dsv1[lI+i]
|
xue@1
|
3214 //sv[l][i]=lsv;
|
xue@1
|
3215 }
|
xue@1
|
3216 //error check: srv[l]*pq=<s',v>
|
xue@1
|
3217 for (int i=0; i<I; i++)
|
xue@1
|
3218 {
|
xue@1
|
3219 cdouble lsv=0;
|
xue@1
|
3220 for (int n=0; n<Np; n++) lsv+=sravl[i][n]*p[n];
|
xue@1
|
3221 for (int n=0; n<Nq; n++) lsv+=srbvl[i][n]*cdouble(0, q[n]);
|
xue@1
|
3222 dsv2[i]=lsv-svl[i]-dsv1[i];
|
xue@1
|
3223 }
|
xue@1
|
3224 }//testdsv
|
xue@1
|
3225
|
xue@1
|
3226 /*
|
xue@1
|
3227 Error check: tests A[MN]x[N1]=b[N1], returns square error
|
xue@1
|
3228 */
|
xue@1
|
3229 double testlinearsystem(int M, int N, double** A, double* x, double* b)
|
xue@1
|
3230 {
|
xue@1
|
3231 double err=0;
|
xue@1
|
3232 for (int m=0; m<M; m++)
|
xue@1
|
3233 {
|
xue@1
|
3234 double errli=Inner(N, A[m], x)-b[m];
|
xue@1
|
3235 err+=errli*errli;
|
xue@1
|
3236 }
|
xue@1
|
3237 return err;
|
xue@1
|
3238 }//testlinearsystem
|
xue@1
|
3239
|
xue@1
|
3240 /*
|
xue@1
|
3241 Error check: test the total square norm of <s, v>
|
xue@1
|
3242 */
|
xue@1
|
3243 double testsv(int L, double* f, int T, cdouble* s, int I, cdouble** u, cdouble** du, int endmode)
|
xue@1
|
3244 {
|
xue@1
|
3245 cdouble** Allocate2(cdouble, I, T*2, v);
|
xue@1
|
3246 cdouble** Allocate2(cdouble, I, T*2, dv);
|
xue@1
|
3247 double ene=0;
|
xue@1
|
3248 for (int l=0; l<L-1; l++)
|
xue@1
|
3249 {
|
xue@1
|
3250 //v from u given f[l]
|
xue@1
|
3251 setv(I, T*2, v, dv, f[l+1], u, du);
|
xue@1
|
3252 //compute -<s, v'> over the lth frame
|
xue@1
|
3253 cdouble* ls=&s[l*T];
|
xue@1
|
3254 for (int i=0; i<I; i++)
|
xue@1
|
3255 {
|
xue@1
|
3256 cdouble d=Inner(2*T, ls, v[i]);
|
xue@1
|
3257 ene+=~d;
|
xue@1
|
3258 }
|
xue@1
|
3259 }
|
xue@1
|
3260 if (endmode==1 || endmode==3)
|
xue@1
|
3261 {
|
xue@1
|
3262 //v from u given f[l]
|
xue@1
|
3263 setvhalf(I, T, v, dv, (f[0]+f[1])/2, u, du);
|
xue@1
|
3264 cdouble* ls=&s[0];
|
xue@1
|
3265 for (int i=0; i<I; i++)
|
xue@1
|
3266
|
xue@1
|
3267 ene+=~Inner(T, ls, v[i]);
|
xue@1
|
3268 }
|
xue@1
|
3269 if (endmode==2 || endmode==3)
|
xue@1
|
3270 {
|
xue@1
|
3271 //v from u given f[l]
|
xue@1
|
3272 setvhalf(I, T, v, dv, (f[L-1]+f[L])/2, u, du);
|
xue@1
|
3273 cdouble* ls=&s[(L-1)*T];
|
xue@1
|
3274 for (int i=0; i<I; i++)
|
xue@1
|
3275 ene+=~Inner(T, ls, v[i]);
|
xue@1
|
3276 }
|
xue@1
|
3277 DeAlloc2(v); DeAlloc2(dv);
|
xue@1
|
3278 return ene;
|
xue@1
|
3279 }//testsv
|
xue@1
|
3280
|
xue@1
|
3281 /*
|
xue@1
|
3282 function DerivativePiecewise3: Piecewise derivative algorithm in which the log amplitude and frequeny
|
xue@1
|
3283 are modeled separately as piecewise functions. In this implementation of the piecewise method the test
|
xue@1
|
3284 functions v are constructed from I "basic" (single-frame) test functions, each covering the same
|
xue@1
|
3285 period of 2T, by shifting these I functions by steps of T. A total number of (L-1)I test functions are
|
xue@1
|
3286 used.
|
xue@1
|
3287
|
xue@1
|
3288 In: s[LT+1]: waveform data
|
xue@1
|
3289 ds[LT+1]: derivative of s[LT], used only if ERROR_CHECK is defined.
|
xue@1
|
3290 L, T: number and length of pieces.
|
xue@1
|
3291 N: number of independent coefficients
|
xue@1
|
3292 h[M][T]: piecewise basis functions
|
xue@1
|
3293 dh[M][T]: derivative of h[M][T], used only if ERROR_CHECK is defined.
|
xue@1
|
3294 DA[L][M][Np]: L matrices that do coefficient mapping (real part) over the L pieces
|
xue@1
|
3295 B[L][M][Nq]: L matrices that do coefficient mapping (imaginary part) over the L pieces
|
xue@1
|
3296 u[I][2T}, du[I][2T]: base-band test functions
|
xue@1
|
3297 f[L+1]: reference frequencies at 0, T, ..., LT, only f[1]...f[L-1] are used
|
xue@1
|
3298 endmode: set to 1 or 3 to apply half-size testing over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
3299 Out: p[Np], q[Nq]: independent coefficients
|
xue@1
|
3300
|
xue@1
|
3301 No return value.
|
xue@1
|
3302 */
|
xue@1
|
3303 void DerivativePiecewise3(int Np, double* p, int Nq, double* q, int L, double* f, int T, cdouble* s, double*** DA, double*** B,
|
xue@1
|
3304 int M, double** h, int I, cdouble** u, cdouble** du, int endmode, cdouble* ds, double** dh)
|
xue@1
|
3305 {
|
xue@1
|
3306 MList* mlist=new MList;
|
xue@1
|
3307 int L_1=(endmode==0)?(L-1):((endmode==3)?(L+1):L);
|
xue@1
|
3308 cdouble** Allocate2L(cdouble, L_1, I, sv, mlist);
|
xue@1
|
3309 cdouble** Allocate2L(cdouble, I, T*2, v, mlist);
|
xue@1
|
3310 cdouble** Allocate2L(cdouble, I, T*2, dv, mlist);
|
xue@1
|
3311 //compute <sr, v>
|
xue@1
|
3312 cdouble*** Allocate3L(cdouble, L_1, I, Np, srav, mlist);
|
xue@1
|
3313 cdouble*** srbv;
|
xue@1
|
3314 if (Np==Nq && B==DA) srbv=srav; else {Allocate3L(cdouble, L_1, I, Nq, srbv, mlist);} //same model for amplitude and phase
|
xue@1
|
3315 cdouble** Allocate2L(cdouble, I, M, shv1, mlist);
|
xue@1
|
3316 cdouble** Allocate2L(cdouble, I, M, shv2, mlist);
|
xue@1
|
3317
|
xue@1
|
3318 #ifdef ERROR_CHECK
|
xue@1
|
3319 cdouble dsv1in[128], dsv2in[128];
|
xue@1
|
3320 #endif
|
xue@1
|
3321
|
xue@1
|
3322 for (int l=0; l<L-1; l++)
|
xue@1
|
3323 {
|
xue@1
|
3324 //v from u given f[l]
|
xue@1
|
3325 setv(I, T*2, v, dv, f[l+1], u, du);
|
xue@1
|
3326 //compute -<s, v'> over the lth frame
|
xue@1
|
3327 cdouble* ls=&s[l*T]; for (int i=0; i<I; i++) sv[l][i]=-Inner(2*T, ls, dv[i]);
|
xue@1
|
3328
|
xue@1
|
3329 //compute <sr, v> over the lth frame
|
xue@1
|
3330 cdouble *ls1=&s[l*T], *ls2=&s[l*T+T];
|
xue@1
|
3331 for (int i=0; i<I; i++)
|
xue@1
|
3332 for (int m=0; m<M; m++)
|
xue@1
|
3333 shv1[i][m]=Inner(T, ls1, h[m], v[i]), shv2[i][m]=Inner(T, ls2, h[m], &v[i][T]);
|
xue@1
|
3334 memset(srav[l][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3335 MultiplyXY(I, M, Np, srav[l], shv1, DA[l]);
|
xue@1
|
3336 MultiAddXY(I, M, Np, srav[l], shv2, DA[l+1]);
|
xue@1
|
3337 if (srbv!=srav) //so that either B!=A or Np!=Nq
|
xue@1
|
3338 {
|
xue@1
|
3339 MultiplyXY(I, M, Nq, srbv[l], shv1, B[l]);
|
xue@1
|
3340 MultiAddXY(I, M, Nq, srbv[l], shv2, B[l+1]);
|
xue@1
|
3341 }
|
xue@1
|
3342 #ifdef ERROR_CHECK
|
xue@1
|
3343 //error check: <s', v>=-<s, v'> and srv[l]*pq=<s',v>
|
xue@1
|
3344 if (ds) testdsv(&dsv1in[l*I], &dsv2in[l*I], Np, p, Nq, q, T*2, &ds[l*T], I, v, sv[l], srav[l], srbv[l]);
|
xue@1
|
3345 #endif
|
xue@1
|
3346 }
|
xue@1
|
3347 L_1=L-1;
|
xue@1
|
3348 if (endmode==1 || endmode==3)
|
xue@1
|
3349 {
|
xue@1
|
3350 //v from u given f[l]
|
xue@1
|
3351 setvhalf(I, T, v, dv, (f[0]+f[1])/2, u, du);
|
xue@1
|
3352 //compute -<s, v'> over the lth frame
|
xue@1
|
3353 cdouble* ls=&s[0]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
3354 //compute <sr, v> over the lth frame
|
xue@1
|
3355 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
3356 //memset(srav[L_1][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3357 MultiplyXY(I, M, Np, srav[L_1], shv1, DA[0]);
|
xue@1
|
3358 if (srbv!=srav) {memset(srbv[L_1][0], 0, sizeof(cdouble)*I*Nq); MultiplyXY(I, M, Nq, srbv[L_1], shv1, B[0]);}
|
xue@1
|
3359 #ifdef ERROR_CHECK
|
xue@1
|
3360 //error check: <s', v>=-<s, v'> and srv[l]*pq=<s',v>
|
xue@1
|
3361 if (ds) testdsv(&dsv1in[L_1*I], &dsv2in[L_1*I], Np, p, Nq, q, T, &ds[0], I, v, sv[L_1], srav[L_1], srbv[L_1]);
|
xue@1
|
3362 #endif
|
xue@1
|
3363 L_1++;
|
xue@1
|
3364 }
|
xue@1
|
3365 if (endmode==2 || endmode==3)
|
xue@1
|
3366 {
|
xue@1
|
3367 //v from u given f[l]
|
xue@1
|
3368 setvhalf(I, T, v, dv, (f[L-1]+f[L])/2, u, du);
|
xue@1
|
3369 //compute -<s, v'> over the lth frame
|
xue@1
|
3370 cdouble* ls=&s[(L-1)*T]; for (int i=0; i<I; i++) sv[L_1][i]=-Inner(T, ls, dv[i]);
|
xue@1
|
3371 //compute <sr, v> over the lth frame
|
xue@1
|
3372 for (int i=0; i<I; i++) for (int m=0; m<M; m++) shv1[i][m]=Inner(T, ls, h[m], v[i]);
|
xue@1
|
3373 memset(srav[L_1][0], 0, sizeof(cdouble)*I*Np);
|
xue@1
|
3374 MultiplyXY(I, M, Np, srav[L_1], shv1, DA[L-1]);
|
xue@1
|
3375 if (srbv!=srav) MultiplyXY(I, M, Nq, srbv[L_1], shv1, B[L-1]);
|
xue@1
|
3376 #ifdef ERROR_CHECK
|
xue@1
|
3377 //error check: <s', v>=-<s, v'> and srv[l]*pq=<s',v>
|
xue@1
|
3378 if (ds) testdsv(&dsv1in[L_1*I], &dsv2in[L_1*I], Np, p, Nq, q, T, &ds[(L-1)*T], I, v, sv[L_1], srav[L_1], srbv[L_1]);
|
xue@1
|
3379 #endif
|
xue@1
|
3380 L_1++;
|
xue@1
|
3381 }
|
xue@1
|
3382
|
xue@1
|
3383 //real implementation of <sr,v>aita=<s',v>
|
xue@1
|
3384 double** Allocate2L(double, L_1*I*2, Np+Nq, AM, mlist);
|
xue@1
|
3385 for (int l=0; l<L_1; l++) for (int i=0; i<I; i++)
|
xue@1
|
3386 {
|
xue@1
|
3387 int li=l*I+i, li_H=li+L_1*I;
|
xue@1
|
3388 for (int n=0; n<Np; n++)
|
xue@1
|
3389 {
|
xue@1
|
3390 AM[li][n]=srav[l][i][n].x;
|
xue@1
|
3391 AM[li_H][n]=srav[l][i][n].y;
|
xue@1
|
3392 }
|
xue@1
|
3393 for (int n=0; n<Nq; n++)
|
xue@1
|
3394 {
|
xue@1
|
3395 AM[li][Np+n]=-srbv[l][i][n].y;
|
xue@1
|
3396 AM[li_H][Np+n]=srbv[l][i][n].x;
|
xue@1
|
3397 }
|
xue@1
|
3398 }
|
xue@1
|
3399 //least-square solution of (srv)(aita)=(sv)
|
xue@1
|
3400 double* pq=new double[Np+Nq]; mlist->Add(pq, 1);
|
xue@1
|
3401 double* b=new double[2*L_1*I]; for (int i=0; i<L_1*I; i++) b[i]=sv[0][i].x, b[i+L_1*I]=sv[0][i].y; mlist->Add(b, 1);
|
xue@1
|
3402 #ifdef ERROR_CHECK
|
xue@1
|
3403 //tests that AM is invariant to a constant shift of p
|
xue@1
|
3404 double errAM=0, errAM2=0, err1, err2;
|
xue@1
|
3405 for (int l=0; l<L_1*I; l++){double errli=0; for (int n=0; n<Np; n++) errli+=AM[l][n]; errAM+=errli*errli; errli=0; for (int n=0; n<Np; n+=2) errli+=AM[l][n]; errAM2+=errli*errli;}
|
xue@1
|
3406 //test square error of the input pq
|
xue@1
|
3407 if (ds)
|
xue@1
|
3408 {
|
xue@1
|
3409 memcpy(pq, p, sizeof(double)*Np); memcpy(&pq[Np], q, sizeof(double)*Nq);
|
xue@1
|
3410 err1=testlinearsystem(L_1*I*2, Np+Nq, AM, pq, b);
|
xue@1
|
3411 }
|
xue@1
|
3412 //test error of s'-sR where R is synthesized from the input pq
|
xue@1
|
3413 double errdsin, errdsvin; cdouble* edsin;
|
xue@1
|
3414 if (ds && dh)
|
xue@1
|
3415 {
|
xue@1
|
3416 edsin=new cdouble[L*T]; mlist->Add(edsin, 1);
|
xue@1
|
3417 errdsin=testds_pqA(Np, p, Nq, q, L, T, s, ds, M, h, dh, DA, B, edsin);
|
xue@1
|
3418 errdsvin=testsv(L, f, T, edsin, I, u, du, endmode);
|
xue@1
|
3419 }
|
xue@1
|
3420 #endif
|
xue@1
|
3421 Alloc2L(L_1*I*2, Np+Nq-1, Am, mlist);
|
xue@1
|
3422 for (int l=0; l<L_1*I*2; l++) memcpy(Am[l], &AM[l][1], sizeof(double)*(Np+Nq-1));
|
xue@1
|
3423 pq[0]=0;
|
xue@1
|
3424 //if (L_1*2*I==Np+Nq) GECP(Np+Nq, pq, AM, b);
|
xue@1
|
3425 //else LSLinear(2*L_1*I, Np+Nq, pq, AM, b);
|
xue@1
|
3426 if (L_1*2*I==Np+Nq-1) GECP(Np+Nq-1, &pq[1], Am, b);
|
xue@1
|
3427 else LSLinear(2*L_1*I, Np+Nq-1, &pq[1], Am, b);
|
xue@1
|
3428 #ifdef ERROR_CHECK
|
xue@1
|
3429 //test square error of output pq
|
xue@1
|
3430 if (ds) err2=testlinearsystem(L_1*I*2, Np+Nq, AM, pq, b);
|
xue@1
|
3431 //test error of s'-sR of the output pq
|
xue@1
|
3432 double errdsout, errdsvout; cdouble* edsout;
|
xue@1
|
3433 if (ds && dh)
|
xue@1
|
3434 {
|
xue@1
|
3435 edsout=new cdouble[L*T]; mlist->Add(edsout, 1);
|
xue@1
|
3436 errdsout=testds_pqA(Np, pq, Nq, &pq[Np], L, T, s, ds, M, h, dh, DA, B, edsout);
|
xue@1
|
3437 errdsvout=testsv(L, f, T, edsout, I, u, du, endmode);
|
xue@1
|
3438 }
|
xue@1
|
3439 #endif
|
xue@1
|
3440 memcpy(p, pq, sizeof(double)*Np); memcpy(q, &pq[Np], sizeof(double)*Nq);
|
xue@1
|
3441
|
xue@1
|
3442 delete mlist;
|
xue@1
|
3443 }//DerivativePiecewise3
|
xue@1
|
3444
|
xue@1
|
3445 //initialization routines for the piecewise derivative method
|
xue@1
|
3446
|
xue@1
|
3447 /*
|
xue@1
|
3448 function seth: set h[M] to a series of power functions.
|
xue@1
|
3449
|
xue@1
|
3450 In: M, T.
|
xue@1
|
3451 Out: h[M][T], where h[m] is power function of order m.
|
xue@1
|
3452
|
xue@1
|
3453 No return value. h is allocated anew and must be freed by caller.
|
xue@1
|
3454 */
|
xue@1
|
3455 void seth(int M, int T, double**& h, MList* mlist)
|
xue@1
|
3456 {
|
xue@1
|
3457 if (M<=0){h=0; return;}
|
xue@1
|
3458 Allocate2L(double, M, T, h, mlist);
|
xue@1
|
3459 double* hm=h[0]; for (int t=0; t<T; t++) hm[t]=1;
|
xue@1
|
3460 for (int m=1; m<M; m++)
|
xue@1
|
3461 {
|
xue@1
|
3462 hm=h[m]; for (int t=0; t<T; t++) hm[t]=pow(t*1.0, m);
|
xue@1
|
3463 }
|
xue@1
|
3464 }//seth
|
xue@1
|
3465
|
xue@1
|
3466 /*
|
xue@1
|
3467 function setdh: set dh[M] to the derivative of a series of power functions.
|
xue@1
|
3468
|
xue@1
|
3469 In: M, T.
|
xue@1
|
3470 Out: dh[M][T], where dh[m] is derivative of the power function of order m.
|
xue@1
|
3471
|
xue@1
|
3472 No return value. dh is allocated anew and must be freed by caller.
|
xue@1
|
3473 */
|
xue@1
|
3474 void setdh(int M, int T, double**& dh, MList* mlist)
|
xue@1
|
3475 {
|
xue@1
|
3476 if (M<=0){dh=0; return;}
|
xue@1
|
3477 Allocate2L(double, M, T, dh, mlist);
|
xue@1
|
3478 double* dhm=dh[0]; memset(dhm, 0, sizeof(double)*T);
|
xue@1
|
3479 if (M>1){dhm=dh[1]; for (int t=0; t<T; t++) dhm[t]=1;}
|
xue@1
|
3480 for (int m=2; m<M; m++)
|
xue@1
|
3481 {
|
xue@1
|
3482 dhm=dh[m]; for (int t=0; t<T; t++) dhm[t]=m*pow(t*1.0, m-1);
|
xue@1
|
3483 }
|
xue@1
|
3484 }//setdh
|
xue@1
|
3485
|
xue@1
|
3486 /*
|
xue@1
|
3487 function setdih: set dih[M] to the difference of the integral of a series of power functions.
|
xue@1
|
3488
|
xue@1
|
3489 In: M, I
|
xue@1
|
3490 Out: dih[M][I], where the accumulation of dih[m] is the integral of the power function of order m.
|
xue@1
|
3491
|
xue@1
|
3492 No return value. dih is allocated anew and must be freed by caller.
|
xue@1
|
3493 */
|
xue@1
|
3494 void setdih(int M, int T, double**& dih, MList* mlist)
|
xue@1
|
3495 {
|
xue@1
|
3496 if (M<=0){dih=0; return;}
|
xue@1
|
3497 Allocate2L(double, M, T, dih, mlist);
|
xue@1
|
3498 double* dihm=dih[0]; for (int t=0; t<T; t++) dihm[t]=1;
|
xue@1
|
3499 for (int m=1; m<M; m++)
|
xue@1
|
3500 {
|
xue@1
|
3501 dihm=dih[m]; for (int t=0; t<T; t++) dihm[t]=(pow(t+1.0, m+1)-pow(t*1.0, m+1))/(m+1);
|
xue@1
|
3502 }
|
xue@1
|
3503 }//setdih
|
xue@1
|
3504
|
xue@1
|
3505 /*
|
xue@1
|
3506 function sshLinear: sets M and h[M] for the linear spline model
|
xue@1
|
3507
|
xue@1
|
3508 In: T
|
xue@1
|
3509 Out: M=2, h[2][T] filled out for linear spline model.
|
xue@1
|
3510
|
xue@1
|
3511 No return value. h is allocated anew and must be freed by caller.
|
xue@1
|
3512 */
|
xue@1
|
3513 void sshLinear(int T, int& M, double** &h, MList* mlist)
|
xue@1
|
3514 {
|
xue@1
|
3515 M=2; Allocate2L(double, M, T, h, mlist);
|
xue@1
|
3516 for (int t=0; t<T; t++) h[0][t]=1, h[1][t]=t;
|
xue@1
|
3517 }//sshLinear
|
xue@1
|
3518
|
xue@1
|
3519 /*
|
xue@1
|
3520 function sdihLinear: sets dih[M] for the linear spline model. For testing only.
|
xue@1
|
3521
|
xue@1
|
3522 In: T
|
xue@1
|
3523 Out: dih[2][T] filled out for linear spline model.
|
xue@1
|
3524
|
xue@1
|
3525 No return value. dih is allocated anew and must be freed by caller.
|
xue@1
|
3526 */
|
xue@1
|
3527 void sdihLinear(int T, double**& dih, MList* mlist)
|
xue@1
|
3528 {
|
xue@1
|
3529 Allocate2L(double, 2, T, dih, mlist);
|
xue@1
|
3530 for (int t=0; t<T; t++) dih[0][t]=1, dih[1][t]=t+0.5;
|
xue@1
|
3531 }//sdihLinear
|
xue@1
|
3532
|
xue@1
|
3533 /*
|
xue@1
|
3534 function sshCubic: sets M and h[M] for cubic spline models.
|
xue@1
|
3535
|
xue@1
|
3536 In: T
|
xue@1
|
3537 Out: M=4 and h[M] filled out for cubic spline models, including cubic and cubic-Hermite.
|
xue@1
|
3538
|
xue@1
|
3539 No return value. h is allocated anew and must be freed by caller.
|
xue@1
|
3540 */
|
xue@1
|
3541 void sshCubic(int T, int& M, double** &h, MList* mlist)
|
xue@1
|
3542 {
|
xue@1
|
3543 M=4; Allocate2L(double, M, T, h, mlist);
|
xue@1
|
3544 for (int t=0; t<T; t++) h[3][t]=t*t*t, h[2][t]=t*t, h[1][t]=t, h[0][t]=1;
|
xue@1
|
3545 }//sshCubic
|
xue@1
|
3546
|
xue@1
|
3547 /*
|
xue@1
|
3548 function sdihCubic: sets dih[M] for cubic spline models.
|
xue@1
|
3549
|
xue@1
|
3550 In: T
|
xue@1
|
3551 Out: dih[4] filled out for cubic spline models.
|
xue@1
|
3552
|
xue@1
|
3553 No return value. dih is allocated anew and must be freed by caller.
|
xue@1
|
3554 */
|
xue@1
|
3555 void sdihCubic(int T, double** &dih, MList* mlist)
|
xue@1
|
3556 {
|
xue@1
|
3557 Allocate2L(double, 4, T, dih, mlist);
|
xue@1
|
3558 for (int t=0; t<T; t++)
|
xue@1
|
3559 {
|
xue@1
|
3560 dih[3][t]=t*(t*(t+1.5)+1)+0.25, dih[2][t]=t*(t+1)+1.0/3, dih[1][t]=t+0.5, dih[0][t]=1;
|
xue@1
|
3561 }
|
xue@1
|
3562 }//sdihCubic*/
|
xue@1
|
3563
|
xue@1
|
3564 /*
|
xue@1
|
3565 function ssALinearSpline: sets N and A[L] for the linear spline model
|
xue@1
|
3566
|
xue@1
|
3567 In: L, M, T
|
xue@1
|
3568 Out: N=L+1, A[L][M][N] filled out for the linear spline model
|
xue@1
|
3569
|
xue@1
|
3570 No return value. A is created anew and bust be freed by caller.
|
xue@1
|
3571 */
|
xue@1
|
3572 void ssALinearSpline(int L, int T, int M, int& N, double*** &A, MList* mlist, int mode)
|
xue@1
|
3573 {
|
xue@1
|
3574 N=L+1;
|
xue@1
|
3575 Allocate3L(double, L, M, N, A, mlist);
|
xue@1
|
3576 memset(A[0][0], 0, sizeof(double)*L*M*N);
|
xue@1
|
3577 double iT=1.0/T; for (int l=0; l<L; l++) A[l][0][l]=1, A[l][1][l]=-iT, A[l][1][l+1]=iT;
|
xue@1
|
3578 }//ssALinearSpline
|
xue@1
|
3579
|
xue@1
|
3580 /*
|
xue@1
|
3581 function ssLinearSpline: sets M, N, h and A for the linear spline model
|
xue@1
|
3582
|
xue@1
|
3583 In: L, M, T
|
xue@1
|
3584 Out: N and h[][] and A[][][] filled out for the linear spline model
|
xue@1
|
3585
|
xue@1
|
3586 No reutrn value. A and h are created anew and bust be freed by caller.
|
xue@1
|
3587 */
|
xue@1
|
3588 void ssLinearSpline(int L, int T, int M, int &N, double** &h, double*** &A, MList* mlist, int mode)
|
xue@1
|
3589 {
|
xue@1
|
3590 seth(M, T, h, mlist);
|
xue@1
|
3591 ssALinearSpline(L, T, M, N, A, mlist);
|
xue@1
|
3592 }//ssLinearSpline
|
xue@1
|
3593
|
xue@1
|
3594 /*
|
xue@1
|
3595 function ssACubicHermite: sets N and A[L] for cubic Hermite spline model
|
xue@1
|
3596
|
xue@1
|
3597 In: L, M, T
|
xue@1
|
3598 Out: N=2(L+1), A[L][M][N] filled out for the cubic Hermite spline
|
xue@1
|
3599
|
xue@1
|
3600 No return value. A is created anew and must be freed by caller.
|
xue@1
|
3601 */
|
xue@1
|
3602 void ssACubicHermite(int L, int T, int M, int& N, double*** &A, MList* mlist, int mode)
|
xue@1
|
3603 {
|
xue@1
|
3604 N=2*(L+1);
|
xue@1
|
3605 Allocate3L(double, L, M, N, A, mlist); memset(A[0][0], 0, sizeof(double)*L*M*N);
|
xue@1
|
3606 double iT=1.0/T, iT2=iT*iT, iT3=iT2*iT;
|
xue@1
|
3607 for (int l=0; l<L; l++)
|
xue@1
|
3608 {
|
xue@1
|
3609 A[l][3][2*l]=2*iT3; A[l][3][2*l+2]=-2*iT3; A[l][3][2*l+1]=A[l][3][2*l+3]=iT2;
|
xue@1
|
3610 A[l][2][2*l]=-3*iT2; A[l][2][2*l+1]=-2*iT; A[l][2][2*l+2]=3*iT2; A[l][2][2*l+3]=-iT;
|
xue@1
|
3611 A[l][1][2*l+1]=1;
|
xue@1
|
3612 A[l][0][2*l]=1;
|
xue@1
|
3613 }
|
xue@1
|
3614 }//ssACubicHermite
|
xue@1
|
3615
|
xue@1
|
3616 /*
|
xue@1
|
3617 function ssLinearSpline: sets M, N, h and A for the cubic Hermite spline model
|
xue@1
|
3618
|
xue@1
|
3619 In: L, M, T
|
xue@1
|
3620 Out: N and h[][] and A[][][] filled out for the cubic Hermite spline model
|
xue@1
|
3621
|
xue@1
|
3622 No reutrn value. A and h are created anew and bust be freed by caller.
|
xue@1
|
3623 */
|
xue@1
|
3624 void ssCubicHermite(int L, int T, int M, int& N, double** &h, double*** &A, MList* mlist, int mode)
|
xue@1
|
3625 {
|
xue@1
|
3626 seth(M, T, h, mlist);
|
xue@1
|
3627 ssACubicHermite(L, T, M, N, A, mlist);
|
xue@1
|
3628 }//ssCubicHermite
|
xue@1
|
3629
|
xue@1
|
3630 /*
|
xue@1
|
3631 function ssACubicSpline: sets N and A[L] for cubic spline model
|
xue@1
|
3632
|
xue@1
|
3633 In: L, M, T
|
xue@1
|
3634 mode: boundary mode of cubic spline, 0=natural, 1=quadratic run-out, 2=cubic run-out
|
xue@1
|
3635 Out: N=2(L+1), A[L][M][N] filled out for the cubic spline
|
xue@1
|
3636
|
xue@1
|
3637 No return value. A is created anew and must be freed by caller.
|
xue@1
|
3638 */
|
xue@1
|
3639 void ssACubicSpline(int L, int T, int M, int& N, double*** &A, MList* mlist, int mode)
|
xue@1
|
3640 {
|
xue@1
|
3641 N=L+1;
|
xue@1
|
3642 Allocate3L(double, L, M, N, A, mlist); memset(A[0][0], 0, sizeof(double)*L*M*N);
|
xue@1
|
3643 Alloc2(L+1, L+1, ML); memset(ML[0], 0, sizeof(double)*(L+1)*(L+1));
|
xue@1
|
3644 Alloc2(L+1, L+1, MR); memset(MR[0], 0, sizeof(double)*(L+1)*(L+1));
|
xue@1
|
3645 //fill in ML and MR. The only difference between various cubic splines are ML.
|
xue@1
|
3646 double _6iT2=6.0/(T*T);
|
xue@1
|
3647 ML[0][0]=ML[L][L]=1;
|
xue@1
|
3648 for (int l=1; l<L; l++) ML[l][l-1]=ML[l][l+1]=1, ML[l][l]=4,
|
xue@1
|
3649 MR[l][l-1]=MR[l][l+1]=_6iT2, MR[l][l]=-2*_6iT2;
|
xue@1
|
3650 if (mode==0){} //no more coefficients are needed for natural cubic spline
|
xue@1
|
3651 else if (mode==1) ML[0][1]=ML[L][L-1]=-1; //setting for quadratic run-out
|
xue@1
|
3652 else if (mode==2) ML[0][1]=ML[L][L-1]=-2, ML[0][2]=ML[L][L-2]=1; //setting for cubic run-out
|
xue@1
|
3653 GICP(L+1, ML);
|
xue@1
|
3654 double** MM=MultiplyXY(L+1, ML, ML, MR);
|
xue@1
|
3655 double iT=1.0/T;
|
xue@1
|
3656 Alloc2(4, 2, M42); M42[3][0]=-1.0/6/T, M42[3][1]=1.0/6/T, M42[2][0]=0.5, M42[2][1]=M42[0][0]=M42[0][1]=0, M42[1][0]=-T/3.0, M42[1][1]=-T/6.0;
|
xue@1
|
3657 for (int l=0; l<L; l++)
|
xue@1
|
3658 {
|
xue@1
|
3659 MultiplyXY(4, 2, N, A[l], M42, &MM[l]);
|
xue@1
|
3660 A[l][1][l]-=iT; A[l][1][l+1]+=iT; A[l][0][l]+=1;
|
xue@1
|
3661 }
|
xue@1
|
3662 DeAlloc2(ML); DeAlloc2(MR); DeAlloc2(M42);
|
xue@1
|
3663 }//ssACubicSpline
|
xue@1
|
3664
|
xue@1
|
3665 /*
|
xue@1
|
3666 function ssLinearSpline: sets M, N, h and A for the cubic spline model
|
xue@1
|
3667
|
xue@1
|
3668 In: L, M, T
|
xue@1
|
3669 Out: N and h[][] and A[][][] filled out for the cubic spline model
|
xue@1
|
3670
|
xue@1
|
3671 No reutrn value. A and h are created anew and bust be freed by caller.
|
xue@1
|
3672 */
|
xue@1
|
3673 void ssCubicSpline(int L, int T, int M, int& N, double** &h, double*** &A, MList* mlist, int mode)
|
xue@1
|
3674 {
|
xue@1
|
3675 seth(M, T, h, mlist);
|
xue@1
|
3676 ssACubicSpline(L, T, M, N, A, mlist, mode);
|
xue@1
|
3677 }//ssCubicSpline
|
xue@1
|
3678
|
xue@1
|
3679 /*
|
xue@1
|
3680 function setu: sets u[I+1] as base-band windowed Fourier atoms, whose frequencies come in the order of
|
xue@1
|
3681 0, 1, -1, 2, -2, 3, -3, 4, etc, in bins.
|
xue@1
|
3682
|
xue@1
|
3683 In: I, Wid: number and size of atoms to generate.
|
xue@1
|
3684 WinOrder: order (=vanishing moment) of window function to use (2=Hann, 4=Hann^2, etc.)
|
xue@1
|
3685 Out: u[I+1][Wid], du[I+1]{Wid]: the I+1 atoms and their derivatives.
|
xue@1
|
3686
|
xue@1
|
3687 No return value. u and du are created anew and must be freed by caller.
|
xue@1
|
3688 */
|
xue@1
|
3689 void setu(int I, int Wid, cdouble**& u, cdouble**& du, int WinOrder, MList* mlist)
|
xue@1
|
3690 {
|
xue@1
|
3691 Allocate2L(cdouble, I+1, Wid, u, mlist);
|
xue@1
|
3692 Allocate2L(cdouble, I+1, Wid, du, mlist);
|
xue@1
|
3693
|
xue@1
|
3694 double** wins=CosineWindows(WinOrder, Wid, (double**)0, 2);
|
xue@1
|
3695 double omg=2*M_PI/Wid; cdouble jomg=cdouble(0, omg);
|
xue@1
|
3696 for (int t=0; t<Wid; t++)
|
xue@1
|
3697 {
|
xue@1
|
3698 u[0][t]=wins[0][t], du[0][t]=wins[1][t];
|
xue@1
|
3699 int li=1;
|
xue@1
|
3700 for (int i=1; i<=I; i++)
|
xue@1
|
3701 {
|
xue@1
|
3702 cdouble rot=polar(1.0, li*omg*t);
|
xue@1
|
3703 u[i][t]=u[0][t]*rot; du[i][t]=du[0][t]*rot+jomg*li*u[i][t];
|
xue@1
|
3704 li=-li; if (li>0) li++;
|
xue@1
|
3705 }
|
xue@1
|
3706 }
|
xue@1
|
3707 DeAlloc2(wins);
|
xue@1
|
3708 }//setu
|
xue@1
|
3709
|
xue@1
|
3710 /*
|
xue@1
|
3711 function DerivativePiecewiseI: wrapper for DerivativePiecewise(), doing the initialization ,etc.
|
xue@1
|
3712
|
xue@1
|
3713 In: L, T: number and length of pieces
|
xue@1
|
3714 s[LT]: waveform signal
|
xue@1
|
3715 ds[LT]: derivative of s[LT], used only when ERROR_CHECK is defined.
|
xue@1
|
3716 f[L+1]: reference frequencies at knots
|
xue@1
|
3717 M: polynomial degree of piecewise approximation
|
xue@1
|
3718 SpecifyA, ssmode: pointer to a function that fills A[L], and mode argument to call it
|
xue@1
|
3719 WinOrder: order(=vanishing moment) of window used for constructing test functions
|
xue@1
|
3720 I: number of test functions per frame.
|
xue@1
|
3721 endmode: set to 1 or 3 to apply half-size frame over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
3722 Out: aita[N]: independent coefficients, where N is specified by SpecifyA.
|
xue@1
|
3723
|
xue@1
|
3724 No return vlue.
|
xue@1
|
3725 */
|
xue@1
|
3726 void DerivativePiecewiseI(cdouble* aita, int L, double* f, int T, cdouble* s, int M,
|
xue@1
|
3727 void (*SpecifyA)(int L, int T, int M, int &N, double*** &A, MList* mlist, int mode), int ssmode,
|
xue@1
|
3728 int WinOrder, int I, int endmode, cdouble* ds)
|
xue@1
|
3729 {
|
xue@1
|
3730 MList* mlist=new MList;
|
xue@1
|
3731 cdouble **u, **du;
|
xue@1
|
3732 setu(I, 2*T, u, du, WinOrder, mlist);
|
xue@1
|
3733
|
xue@1
|
3734 int N; double **h, ***A;
|
xue@1
|
3735 seth(M, T, h, mlist);
|
xue@1
|
3736 SpecifyA(L, T, M, N, A, mlist, ssmode);
|
xue@1
|
3737
|
xue@1
|
3738 DerivativePiecewise(N, aita, L, f, T, s, A, M, h, I, u, du, endmode, ds);
|
xue@1
|
3739 delete mlist;
|
xue@1
|
3740 }//DerivativePiecewiseI
|
xue@1
|
3741
|
xue@1
|
3742 /*
|
xue@1
|
3743 function DerivativePiecewiseII: wrapper for DerivativePiecewise2(), doing the initialization ,etc.
|
xue@1
|
3744 This models the derivative of log ampltiude and frequency as separate piecewise polynomials, the first
|
xue@1
|
3745 specified by SpecifyA, the second by SpecifyB.
|
xue@1
|
3746
|
xue@1
|
3747 In: L, T: number and length of pieces
|
xue@1
|
3748 s[LT]: waveform signal
|
xue@1
|
3749 ds[LT]: derivative of s[LT], used only when ERROR_CHECK is defined.
|
xue@1
|
3750 f[L+1]: reference frequencies at knots
|
xue@1
|
3751 M: polynomial degree of piecewise approximation
|
xue@1
|
3752 SpecifyA, ssAmode: pointer to a function that fills A[L], and mode argument to call it
|
xue@1
|
3753 SpecifyB, ssBmode: pointer to a function that fills B[L], and mode argument to call it
|
xue@1
|
3754 WinOrder: order(=vanishing moment) of window used for constructing test functions
|
xue@1
|
3755 I: number of test functions per frame.
|
xue@1
|
3756 endmode: set to 1 or 3 to apply half-size frame over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
3757 Out: p[Np], q[Nq]: independent coefficients, where Np and Nq are specified by SpecifyA and SpecifyB.
|
xue@1
|
3758
|
xue@1
|
3759 No reutrn value.
|
xue@1
|
3760 */
|
xue@1
|
3761 void DerivativePiecewiseII(double* p, double* q, int L, double* f, int T, cdouble* s, int M,
|
xue@1
|
3762 void (*SpecifyA)(int L, int T, int M, int &N, double*** &A, MList* mlist, int mode), int ssAmode,
|
xue@1
|
3763 void (*SpecifyB)(int L, int T, int M, int &N, double*** &B, MList* mlist, int mode), int ssBmode,
|
xue@1
|
3764 int WinOrder, int I, int endmode, cdouble* ds)
|
xue@1
|
3765 {
|
xue@1
|
3766 MList* mlist=new MList;
|
xue@1
|
3767 cdouble **u, **du;
|
xue@1
|
3768 setu(I, 2*T, u, du, WinOrder, mlist);
|
xue@1
|
3769
|
xue@1
|
3770 int Np, Nq;
|
xue@1
|
3771 double **h, ***A, ***B;
|
xue@1
|
3772 seth(M, T, h, mlist);
|
xue@1
|
3773 SpecifyA(L, T, M, Np, A, mlist, ssAmode);
|
xue@1
|
3774 SpecifyB(L, T, M, Nq, B, mlist, ssBmode);
|
xue@1
|
3775
|
xue@1
|
3776 DerivativePiecewise2(Np, p, Nq, q, L, f, T, s, A, B, M, h, I, u, du, endmode, ds);
|
xue@1
|
3777
|
xue@1
|
3778 delete mlist;
|
xue@1
|
3779 }//DerivativePiecewiseII
|
xue@1
|
3780
|
xue@1
|
3781 /*
|
xue@1
|
3782 function DerivativePiecewiseIII: wrapper for DerivativePiecewise3(), doing the initialization ,etc.
|
xue@1
|
3783 Notice that this time the log amplitude, rather than its derivative, is modeled as a piecewise
|
xue@1
|
3784 polynomial specified by SpecifyA.
|
xue@1
|
3785
|
xue@1
|
3786 In: L, T: number and length of pieces
|
xue@1
|
3787 s[LT]: waveform signal
|
xue@1
|
3788 ds[LT]: derivative of s[LT], used only when ERROR_CHECK is defined.
|
xue@1
|
3789 f[L+1]: reference frequencies at knots
|
xue@1
|
3790 M: polynomial degree of piecewise approximation
|
xue@1
|
3791 SpecifyA, ssAmode: pointer to a function that fills A[L], and mode argument to call it
|
xue@1
|
3792 SpecifyB, ssBmode: pointer to a function that fills B[L], and mode argument to call it
|
xue@1
|
3793 WinOrder: order(=vanishing moment) of window used for constructing test functions
|
xue@1
|
3794 I: number of test functions per frame.
|
xue@1
|
3795 endmode: set to 1 or 3 to apply half-size frame over [0, T], to 2 or 3 to apply over [LT-T, LT]
|
xue@1
|
3796 Out: p[Np], q[Nq]: independent coefficients, where Np and Nq are specified by SpecifyA and SpecifyB.
|
xue@1
|
3797
|
xue@1
|
3798 No reutrn value.
|
xue@1
|
3799 */
|
xue@1
|
3800 void DerivativePiecewiseIII(double* p, double* q, int L, double* f, int T, cdouble* s, int M,
|
xue@1
|
3801 void (*SpecifyA)(int L, int T, int M, int &N, double*** &A, MList* mlist, int mode), int ssAmode,
|
xue@1
|
3802 void (*SpecifyB)(int L, int T, int M, int &N, double*** &B, MList* mlist, int mode), int ssBmode,
|
xue@1
|
3803 int WinOrder, int I, int endmode, cdouble* ds)
|
xue@1
|
3804 {
|
xue@1
|
3805 MList* mlist=new MList;
|
xue@1
|
3806 int Np, Nq;
|
xue@1
|
3807 double **h, ***A, ***B, **dh=0;
|
xue@1
|
3808 cdouble **u, **du;
|
xue@1
|
3809 setu(I, T*2, u, du, WinOrder, mlist);
|
xue@1
|
3810 seth(M, T, h, mlist);
|
xue@1
|
3811 if (ds) setdh(M, T, dh, mlist);
|
xue@1
|
3812 SpecifyA(L, T, M, Np, A, mlist, ssAmode);
|
xue@1
|
3813 SpecifyB(L, T, M, Nq, B, mlist, ssBmode);
|
xue@1
|
3814 Alloc2L(M, M, DM, mlist);
|
xue@1
|
3815 memset(DM[0], 0, sizeof(double)*M*M); for (int m=0; m<M-1; m++) DM[m][m+1]=m+1;
|
xue@1
|
3816 double** DA=0;
|
xue@1
|
3817
|
xue@1
|
3818 for (int l=0; l<L; l++)
|
xue@1
|
3819 {
|
xue@1
|
3820 DA=MultiplyXY(M, M, Np, DA, DM, A[l], mlist);
|
xue@1
|
3821 Copy(M, Np, A[l], DA);
|
xue@1
|
3822 }
|
xue@1
|
3823
|
xue@1
|
3824 DerivativePiecewise3(Np, p, Nq, q, L, f, T, s, A, B, M, h, I, u, du, endmode, ds, dh);
|
xue@1
|
3825
|
xue@1
|
3826 delete mlist;
|
xue@1
|
3827 }//DerivativePiecewiseIII
|
xue@1
|
3828
|
xue@1
|
3829 /*
|
xue@1
|
3830 function AmpPhCorrectionExpA: model-preserving amplitude and phase correction in piecewise derivative
|
xue@1
|
3831 method.
|
xue@1
|
3832
|
xue@1
|
3833 In: aita[N]: inital independent coefficients
|
xue@1
|
3834 L, T: number and size of pieces
|
xue@1
|
3835 sre[LT]: waveform data
|
xue@1
|
3836 h[M][T], dih[M][T]: piecewise basis functions and their difference-integrals
|
xue@1
|
3837 A[L][M][N]: L coefficient mapping matrices
|
xue@1
|
3838 SpecifyA: pointer to the function used for constructing A
|
xue@1
|
3839 WinOrder: order(=vanishing moment) of window used for constructing test functions
|
xue@1
|
3840 Out: aita[N]: corrected independent coefficients
|
xue@1
|
3841 s2[LT]: reconstruct sinusoid BEFORE correction
|
xue@1
|
3842
|
xue@1
|
3843 Returns the estimate of phase angle at 0.
|
xue@1
|
3844 */
|
xue@1
|
3845 double AmpPhCorrectionExpA(cdouble* s2, int N, cdouble* aita, int L, int T, cdouble* sre, int M, double** h, double** dih, double*** A,
|
xue@1
|
3846 void (*SpecifyA)(int L, int T, int M, int &N, double*** &A, MList* mlist, int mode), int WinOrder)
|
xue@1
|
3847 {
|
xue@1
|
3848 MList* mlist=new MList;
|
xue@1
|
3849 //*amplitude and phase correction
|
xue@1
|
3850 //amplitude is done by updating p, i.e. Re(aita)
|
xue@1
|
3851 double *s2ph=new double[L+1]; mlist->Add(s2ph, 1);
|
xue@1
|
3852 double *phcor=new double[L+1]; mlist->Add(phcor, 1);
|
xue@1
|
3853 cdouble* lamda=new cdouble[M]; mlist->Add(lamda, 1);
|
xue@1
|
3854 double* lamdax=new double[M]; mlist->Add(lamdax, 1);
|
xue@1
|
3855 double* lamday=new double[M]; mlist->Add(lamday, 1);
|
xue@1
|
3856 {
|
xue@1
|
3857 double tmpph=0;
|
xue@1
|
3858 memset(s2ph, 0, sizeof(double)*(L+1));
|
xue@1
|
3859 s2ph[0]=tmpph;
|
xue@1
|
3860 for (int l=0; l<L; l++)
|
xue@1
|
3861 {
|
xue@1
|
3862 MultiplyXy(M, N, lamda, A[l], aita); for (int m=0; m<M; m++) lamdax[m]=lamda[m].x, lamday[m]=lamda[m].y;
|
xue@1
|
3863 SinusoidExpA(T, &s2[l*T], M, lamdax, lamday, h, dih, tmpph); s2ph[l+1]=tmpph;
|
xue@1
|
3864 }
|
xue@1
|
3865 double* win=new double[2*T+1]; CosineWindows(WinOrder, 2*T, &win, 1); mlist->Add(win, 1);
|
xue@1
|
3866 for (int l=1; l<L; l++)
|
xue@1
|
3867 {
|
xue@1
|
3868 cdouble inn=Inner(2*T, &sre[l*T-T], win, &s2[l*T-T])/Inner(2*T, &s2[l*T-T], win, &s2[l*T-T]);
|
xue@1
|
3869 cdouble loginn=log(inn);
|
xue@1
|
3870 if (SpecifyA==ssACubicHermite)
|
xue@1
|
3871 {
|
xue@1
|
3872 aita[l*2]+=loginn.x;
|
xue@1
|
3873 s2ph[l]+=loginn.y;
|
xue@1
|
3874 phcor[l]=loginn.y;
|
xue@1
|
3875 if (l==1) aita[0]+=loginn.x, phcor[0]=loginn.y, s2ph[0]+=loginn.y;
|
xue@1
|
3876 if (l==L-1) aita[L*2]+=loginn.x, phcor[L]=loginn.y, s2ph[L]+=loginn.y;
|
xue@1
|
3877 }
|
xue@1
|
3878 else
|
xue@1
|
3879 {
|
xue@1
|
3880 aita[l]+=loginn.x;
|
xue@1
|
3881 s2ph[l]+=loginn.y;
|
xue@1
|
3882 phcor[l]=loginn.y;
|
xue@1
|
3883 if (l==1)
|
xue@1
|
3884 {
|
xue@1
|
3885 inn=Inner(T, sre, &win[T], s2)/Inner(T, s2, &win[T], s2);
|
xue@1
|
3886 loginn=log(inn);
|
xue@1
|
3887 aita[0]+=loginn.x;
|
xue@1
|
3888 s2ph[0]+=loginn.y;
|
xue@1
|
3889 phcor[0]=loginn.y;
|
xue@1
|
3890 }
|
xue@1
|
3891 if (l==L-1)
|
xue@1
|
3892 {
|
xue@1
|
3893 inn=Inner(T, &sre[L*T-T], win, &s2[L*T-T])/Inner(T, &s2[L*T-T], win, &s2[L*T-T]);
|
xue@1
|
3894 loginn=log(inn);
|
xue@1
|
3895 aita[L]+=loginn.x;
|
xue@1
|
3896 s2ph[L]+=loginn.y;
|
xue@1
|
3897 phcor[L]=loginn.y;
|
xue@1
|
3898 }
|
xue@1
|
3899 }
|
xue@1
|
3900 }
|
xue@1
|
3901
|
xue@1
|
3902 for (int l=1; l<=L; l++)
|
xue@1
|
3903 {
|
xue@1
|
3904 int k=floor((phcor[l]-phcor[l-1])/(2*M_PI)+0.5);
|
xue@1
|
3905 if (k!=0)
|
xue@1
|
3906 phcor[l]+=2*M_PI*k;
|
xue@1
|
3907 }
|
xue@1
|
3908 //*
|
xue@1
|
3909 //now phcor[] contains phase corrector to be interpolated
|
xue@1
|
3910 double *b=new double[L], *zet=new double[L+1], *dzet=new double[L+1]; memset(zet, 0, sizeof(double)*(L+1)); memset(dzet, 0, sizeof(double)*(L+1));
|
xue@1
|
3911 mlist->Add(b, 1); mlist->Add(zet, 1); mlist->Add(dzet, 1);
|
xue@1
|
3912 double ihT[]={T, T/2.0*T, T/3.0*T*T, T/4.0*T*T*T};
|
xue@1
|
3913
|
xue@1
|
3914 Alloc2L(L, N, BB, mlist);
|
xue@1
|
3915 //prepare linear system (BB)(zet)=(b)
|
xue@1
|
3916 for (int l=0; l<L; l++)
|
xue@1
|
3917 {
|
xue@1
|
3918 MultiplyxY(N, 4, BB[l], ihT, A[l]);
|
xue@1
|
3919 b[l]=phcor[l+1]-phcor[l];
|
xue@1
|
3920 }
|
xue@1
|
3921 Alloc2L(L, L, copyA, mlist);
|
xue@1
|
3922 if (L+1==N) for (int l=0; l<L; l++) memcpy(copyA[l], &BB[l][1], sizeof(double)*L);
|
xue@1
|
3923 else if (L+1==N/2) for (int l=0; l<L; l++) for (int k=0; k<L; k++) copyA[l][k]=BB[l][2*k+2];
|
xue@1
|
3924 double* copyb=Copy(L, b, mlist);
|
xue@1
|
3925 zet[0]=0; GECP(L, &zet[1], copyA, copyb);
|
xue@1
|
3926 if (L+1==N) for (int l=0; l<L; l++) memcpy(copyA[l], &BB[l][1], sizeof(double)*L);
|
xue@1
|
3927 else if (L+1==N/2) for (int l=0; l<L; l++) for (int k=0; k<L; k++) copyA[l][k]=BB[l][2*k+2];
|
xue@1
|
3928 Copy(L, copyb, b); for (int l=0; l<L; l++) copyb[l]-=BB[l][0];
|
xue@1
|
3929 dzet[0]=1; GECP(L, &dzet[1], copyA, copyb);
|
xue@1
|
3930
|
xue@1
|
3931 #ifdef ERROR_CHECK
|
xue@1
|
3932 //Test that (BB)(zet)=b and (BB)(dzet)=b
|
xue@1
|
3933 double* bbzet=MultiplyXy(L, L+1, BB, zet, mlist);
|
xue@1
|
3934 MultiAdd(L, bbzet, bbzet, b, -1);
|
xue@1
|
3935 double err1=Inner(L, bbzet, bbzet);
|
xue@1
|
3936 double* bbdzet=MultiplyXy(L, L+1, BB, dzet, mlist);
|
xue@1
|
3937 MultiAdd(L, bbdzet, bbdzet, b, -1);
|
xue@1
|
3938 double err2=Inner(L, bbdzet, bbdzet);
|
xue@1
|
3939 MultiAdd(L+1, dzet, dzet, zet, -1);
|
xue@1
|
3940 //Test that (BB)dzet=0
|
xue@1
|
3941 MultiplyXy(L, L+1, bbdzet, BB, dzet);
|
xue@1
|
3942 double err3=Inner(L, bbzet, bbzet);
|
xue@1
|
3943 #endif
|
xue@1
|
3944 //now that (zet)+(miu)(dzet) is the general solution to (BB)(zet)=b,
|
xue@1
|
3945 // we look for (miu) that maximizes smoothness
|
xue@1
|
3946
|
xue@1
|
3947 double innuv=0, innvv=0, lmd0[4], lmdd[4], clmdd[4],
|
xue@1
|
3948 T2=T*T, T3=T2*T, T4=T3*T, T5=T4*T;
|
xue@1
|
3949 for (int l=0; l<L; l++)
|
xue@1
|
3950 {
|
xue@1
|
3951 MultiplyXy(4, L+1, lmd0, A[l], zet);
|
xue@1
|
3952 MultiplyXy(4, L+1, lmdd, A[l], dzet);
|
xue@1
|
3953 clmdd[1]=T*lmdd[1]+T2*lmdd[2]+T3*lmdd[3];
|
xue@1
|
3954 clmdd[2]=T2*lmdd[1]+(4.0/3)*T3*lmdd[2]+1.5*T4*lmdd[3];
|
xue@1
|
3955 clmdd[3]=T3*lmdd[1]+1.5*T4*lmdd[2]+1.8*T5*lmdd[3];
|
xue@1
|
3956 innuv+=Inner(3, &lmd0[1], &clmdd[1]);
|
xue@1
|
3957 innvv+=Inner(3, &lmdd[1], &clmdd[1]);
|
xue@1
|
3958 }
|
xue@1
|
3959 MultiAdd(L+1, zet, zet, dzet, -innuv/innvv);
|
xue@1
|
3960
|
xue@1
|
3961 if (SpecifyA==ssACubicHermite)
|
xue@1
|
3962 for (int l=0; l<=L; l++) aita[2*l].y+=zet[l];
|
xue@1
|
3963 else
|
xue@1
|
3964 for (int l=0; l<=L; l++) aita[l].y+=zet[l];
|
xue@1
|
3965 //*/
|
xue@1
|
3966 }
|
xue@1
|
3967 double result=s2ph[0];
|
xue@1
|
3968 delete mlist;
|
xue@1
|
3969 return result;
|
xue@1
|
3970 }//AmpPhCorrectionExpA
|