xue@1
|
1 //---------------------------------------------------------------------------
|
xue@1
|
2
|
xue@1
|
3
|
xue@1
|
4 #include <math.h>
|
xue@1
|
5 #include "multires.h"
|
xue@1
|
6 #include "arrayalloc.h"
|
xue@1
|
7 #include "procedures.h"
|
xue@1
|
8
|
xue@1
|
9 //---------------------------------------------------------------------------
|
xue@1
|
10
|
xue@1
|
11 //function xlogx(x): returns x*log(x)
|
xue@1
|
12 inline double xlogx(double x)
|
xue@1
|
13 {
|
xue@1
|
14 if (x==0) return 0;
|
xue@1
|
15 else return x*log(x);
|
xue@1
|
16 }//xlogx
|
xue@1
|
17
|
xue@1
|
18 //macro NORMAL_: a normalization step used for tiling
|
xue@1
|
19 #define NORMAL_(A, a) A=a*(A+log(a));
|
xue@1
|
20 // #define NORMAL_(A, a) A=a*a*A;
|
xue@1
|
21 // #define NORMAL_(A, a) A=sqrt(a)*A;
|
xue@1
|
22
|
xue@1
|
23 /*
|
xue@1
|
24 function DoCutSpectrogramSquare: find optimal tiling of a square. This is a recursive procedure.
|
xue@1
|
25
|
xue@1
|
26 In: Specs[0][1][N], Specs[1][2][N/2], ..., Specs[log2(N)][N][1], multiresolution power spectrogram
|
xue@1
|
27 e[Res]: total power of each level, e[i] equals the sum of Specs[i][][]
|
xue@1
|
28 NN: maximal tile height
|
xue@1
|
29 Out: cuts[N-1]: the tiling result
|
xue@1
|
30 ents[Res]
|
xue@1
|
31
|
xue@1
|
32 Returns the entropy of the output tiling.
|
xue@1
|
33 */
|
xue@1
|
34 double DoCutSpectrogramSquare(int* cuts, double*** Specs, double* e, int N, int NN, bool Norm, double* ents)
|
xue@1
|
35 {
|
xue@1
|
36 double result;
|
xue@1
|
37 int Res=log2(N)+1;
|
xue@1
|
38
|
xue@1
|
39 if (N==1) // 1*1 only(no cuts), returns the sample function.
|
xue@1
|
40 {
|
xue@1
|
41 double sp00;
|
xue@1
|
42 if (e[0]!=0) sp00=Specs[0][0][0]/e[0]; else sp00=0;
|
xue@1
|
43 ents[0]=xlogx(sp00);
|
xue@1
|
44 return ents[0];
|
xue@1
|
45 }
|
xue@1
|
46 else if (N==2)
|
xue@1
|
47 {
|
xue@1
|
48 double sp00, sp01, sp10, sp11;
|
xue@1
|
49 if (e[0]!=0) sp00=Specs[0][0][0]/e[0], sp01=Specs[0][0][1]/e[0]; else sp00=sp01=0;
|
xue@1
|
50 if (e[1]!=0) sp10=Specs[1][0][0]/e[1], sp11=Specs[1][1][0]/e[1]; else sp10=sp11=0;
|
xue@1
|
51 double ent0=xlogx(sp00)+xlogx(sp01);
|
xue@1
|
52 double ent1=xlogx(sp10)+xlogx(sp11);
|
xue@1
|
53 if (ent0<ent1)
|
xue@1
|
54 {
|
xue@1
|
55 cuts[0]=1;
|
xue@1
|
56 ents[0]=0, ents[1]=ent1;
|
xue@1
|
57 }
|
xue@1
|
58 else
|
xue@1
|
59 {
|
xue@1
|
60 cuts[0]=0;
|
xue@1
|
61 ents[0]=ent0, ents[1]=0;
|
xue@1
|
62 }
|
xue@1
|
63 }
|
xue@1
|
64 else
|
xue@1
|
65 {
|
xue@1
|
66 int* tmpcuts=new int[N-2];
|
xue@1
|
67 int *lcuts, *rcuts;
|
xue@1
|
68 double ***lSpecs, ***rSpecs, *el, *er, ent0, ent1, a;
|
xue@1
|
69 double *entl0=new double[Res-1], *entr0=new double[Res-1],
|
xue@1
|
70 *entl1=new double[Res-1], *entr1=new double[Res-1];
|
xue@1
|
71 //vertical cuts: l->left half, r->right half
|
xue@1
|
72 if (N<=NN)
|
xue@1
|
73 {
|
xue@1
|
74 lcuts=&cuts[1], rcuts=&cuts[N/2];
|
xue@1
|
75 VSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
76 el=new double[Res-1], er=new double[Res-1];
|
xue@1
|
77 memset(el, 0, sizeof(double)*(Res-1)); memset(er, 0, sizeof(double)*(Res-1));
|
xue@1
|
78 if (Norm)
|
xue@1
|
79 {
|
xue@1
|
80 //normalization
|
xue@1
|
81 for (int i=0, Fr=1, n=N/2; i<Res-1; i++, Fr*=2, n/=2)
|
xue@1
|
82 for (int j=0; j<Fr; j++) for (int k=0; k<n; k++)
|
xue@1
|
83 el[i]+=lSpecs[i][j][k], er[i]+=rSpecs[i][j][k];
|
xue@1
|
84 }
|
xue@1
|
85 else
|
xue@1
|
86 for (int i=0; i<Res-1; i++) el[i]=er[i]=1;
|
xue@1
|
87
|
xue@1
|
88 DoCutSpectrogramSquare(lcuts, lSpecs, el, N/2, NN, Norm, entl1);
|
xue@1
|
89 DoCutSpectrogramSquare(rcuts, rSpecs, er, N/2, NN, Norm, entr1);
|
xue@1
|
90
|
xue@1
|
91 ent1=0;
|
xue@1
|
92
|
xue@1
|
93 for (int i=0; i<Res-1; i++)
|
xue@1
|
94 {
|
xue@1
|
95 if (e[i]!=0)
|
xue@1
|
96 {
|
xue@1
|
97 a=el[i]/e[i]; if (a>0) {NORMAL_(entl1[i], a);} else entl1[i]=0; ent1=ent1+entl1[i];
|
xue@1
|
98 a=er[i]/e[i]; if (a>0) {NORMAL_(entr1[i], a);} else entr1[i]=0; ent1=ent1+entr1[i];
|
xue@1
|
99 }
|
xue@1
|
100 else
|
xue@1
|
101 entl1[i]=entr1[i]=0;
|
xue@1
|
102 }
|
xue@1
|
103
|
xue@1
|
104 DeAlloc2(lSpecs); DeAlloc2(rSpecs);
|
xue@1
|
105 delete[] el; delete[] er;
|
xue@1
|
106
|
xue@1
|
107 }
|
xue@1
|
108 //horizontal cuts: l->lower half, r->upper half
|
xue@1
|
109 lcuts=tmpcuts, rcuts=&tmpcuts[N/2-1];
|
xue@1
|
110 HSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
111 el=new double[Res-1], er=new double[Res-1];
|
xue@1
|
112 memset(el, 0, sizeof(double)*(Res-1)); memset(er, 0, sizeof(double)*(Res-1));
|
xue@1
|
113 if (Norm)
|
xue@1
|
114 {
|
xue@1
|
115 //normalization
|
xue@1
|
116 for (int i=0, Fr=1, n=N/2; i<Res-1; i++, Fr*=2, n/=2)
|
xue@1
|
117 for (int j=0; j<Fr; j++) for (int k=0; k<n; k++)
|
xue@1
|
118 el[i]+=lSpecs[i][j][k], er[i]+=rSpecs[i][j][k];
|
xue@1
|
119 }
|
xue@1
|
120 else
|
xue@1
|
121 for (int i=0; i<Res-1; i++) el[i]=er[i]=1;
|
xue@1
|
122
|
xue@1
|
123 DoCutSpectrogramSquare(lcuts, lSpecs, el, N/2, NN, Norm, entl0);
|
xue@1
|
124 DoCutSpectrogramSquare(rcuts, rSpecs, er, N/2, NN, Norm, entr0);
|
xue@1
|
125
|
xue@1
|
126 ent0=0;
|
xue@1
|
127
|
xue@1
|
128 if (Norm)
|
xue@1
|
129 for (int i=0; i<Res-1; i++)
|
xue@1
|
130 {
|
xue@1
|
131 if (e[i]!=0)
|
xue@1
|
132 {
|
xue@1
|
133 a=el[i]/e[i]; if (a>0) {NORMAL_(entl0[i], a);} else entl0[i]=0; ent0=ent0+entl0[i];
|
xue@1
|
134 a=er[i]/e[i]; if (a>0) {NORMAL_(entr0[i], a);} else entr0[i]=0; ent0=ent0+entr0[i];
|
xue@1
|
135 }
|
xue@1
|
136 else
|
xue@1
|
137 entl0[i]=entr0[i]=0;
|
xue@1
|
138 }
|
xue@1
|
139
|
xue@1
|
140 DeAlloc2(lSpecs); DeAlloc2(rSpecs);
|
xue@1
|
141 delete[] el; delete[] er;
|
xue@1
|
142
|
xue@1
|
143 if (N<=NN && ent0<ent1)
|
xue@1
|
144 {
|
xue@1
|
145 cuts[0]=1;
|
xue@1
|
146 result=ent1;
|
xue@1
|
147 for (int i=0; i<Res-1; i++)
|
xue@1
|
148 {
|
xue@1
|
149 ents[i+1]=entl1[i]+entr1[i];
|
xue@1
|
150 }
|
xue@1
|
151 ents[0]=0;
|
xue@1
|
152 }
|
xue@1
|
153 else
|
xue@1
|
154 {
|
xue@1
|
155 memcpy(&cuts[1], tmpcuts, sizeof(int)*(N-2));
|
xue@1
|
156 cuts[0]=0;
|
xue@1
|
157 result=ent0;
|
xue@1
|
158 for (int i=0; i<Res-1; i++)
|
xue@1
|
159 {
|
xue@1
|
160 ents[i]=entl0[i]+entr0[i];
|
xue@1
|
161 }
|
xue@1
|
162 ents[Res-1]=0;
|
xue@1
|
163 }
|
xue@1
|
164
|
xue@1
|
165 delete[] tmpcuts;
|
xue@1
|
166 delete[] entl0; delete[] entl1; delete[] entr0; delete[] entr1;
|
xue@1
|
167 }
|
xue@1
|
168
|
xue@1
|
169 return result;
|
xue@1
|
170 }//DoCutSpectrogramSquare
|
xue@1
|
171
|
xue@1
|
172 /*
|
xue@1
|
173 function DoMixSpectrogramSquare: renders a composite spectrogram on a pixel grid. This is a recursive
|
xue@1
|
174 procedure.
|
xue@1
|
175
|
xue@1
|
176 In: Specs[0][1][N], Specs[1][2][N/2], Specs[2][4][N/4], ..., Specs[][N][1]: multiresolution power
|
xue@1
|
177 spectrogram
|
xue@1
|
178 cuts[N-1]: tiling
|
xue@1
|
179 X, Y: dimensions of pixel grid to render
|
xue@1
|
180 Out: Spec[X][Y]: pixel grid rendered to represent the given spectrograms and tiling
|
xue@1
|
181
|
xue@1
|
182 No return value;
|
xue@1
|
183 */
|
xue@1
|
184 void DoMixSpectrogramSquare(double** Spec, int* cuts, double*** Specs, int N, bool Norm, int X=0, int Y=0)
|
xue@1
|
185 {
|
xue@1
|
186 if (X==0 && Y==0) X=Y=N;
|
xue@1
|
187
|
xue@1
|
188 if (N==1)
|
xue@1
|
189 {
|
xue@1
|
190 double value=Specs[0][0][0];//sqrt(Specs[0][0][0]);
|
xue@1
|
191 value=value;
|
xue@1
|
192 for (int x=0; x<X; x++) for (int y=0; y<Y; y++) Spec[x][y]=value;
|
xue@1
|
193 }
|
xue@1
|
194 else
|
xue@1
|
195 {
|
xue@1
|
196 double* e;
|
xue@1
|
197 int Res;
|
xue@1
|
198
|
xue@1
|
199 if (Norm)
|
xue@1
|
200 {
|
xue@1
|
201 //normalization
|
xue@1
|
202 Res=log2(N)+1;
|
xue@1
|
203 e=new double[Res];
|
xue@1
|
204 memset(e, 0, sizeof(double)*Res);
|
xue@1
|
205 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
206 for (int j=0; j<Fr; j++)
|
xue@1
|
207 for (int k=0; k<n; k++)
|
xue@1
|
208 e[i]+=Specs[i][j][k];
|
xue@1
|
209 double em=e[0];
|
xue@1
|
210 for (int i=1; i<Res; i++)
|
xue@1
|
211 {
|
xue@1
|
212 if (e[i]>em) e[i]=em/e[i];
|
xue@1
|
213 else e[i]=1;
|
xue@1
|
214 if (e[i]>em) em=e[i];
|
xue@1
|
215 } e[0]=1;
|
xue@1
|
216 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
217 {
|
xue@1
|
218 if (e[i]!=0 && e[1]!=1)
|
xue@1
|
219 for (int j=0; j<Fr; j++)
|
xue@1
|
220 for (int k=0; k<n; k++)
|
xue@1
|
221 Specs[i][j][k]*=e[i];
|
xue@1
|
222 }
|
xue@1
|
223 }
|
xue@1
|
224
|
xue@1
|
225 double **lSpec, **rSpec, ***lSpecs, ***rSpecs;
|
xue@1
|
226 if (cuts[0]) //1: vertical split
|
xue@1
|
227 {
|
xue@1
|
228 VSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
229 VSplitSpec(X, Y, Spec, lSpec, rSpec);
|
xue@1
|
230 DoMixSpectrogramSquare(lSpec, &cuts[1], lSpecs, N/2, Norm, X/2, Y);
|
xue@1
|
231 DoMixSpectrogramSquare(rSpec, &cuts[N/2], rSpecs, N/2, Norm, X/2, Y);
|
xue@1
|
232 }
|
xue@1
|
233 else //0: horizontal split
|
xue@1
|
234 {
|
xue@1
|
235 HSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
236 HSplitSpec(X, Y, Spec, lSpec, rSpec);
|
xue@1
|
237 DoMixSpectrogramSquare(lSpec, &cuts[1], lSpecs, N/2, Norm, X, Y/2);
|
xue@1
|
238 DoMixSpectrogramSquare(rSpec, &cuts[N/2], rSpecs, N/2, Norm, X, Y/2);
|
xue@1
|
239 }
|
xue@1
|
240
|
xue@1
|
241 if (Norm)
|
xue@1
|
242 {
|
xue@1
|
243 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
244 {
|
xue@1
|
245 if (e[i]!=0 && e[1]!=1)
|
xue@1
|
246 for (int j=0; j<Fr; j++)
|
xue@1
|
247 for (int k=0; k<n; k++)
|
xue@1
|
248 Specs[i][j][k]/=e[i];
|
xue@1
|
249 }
|
xue@1
|
250 delete[] e;
|
xue@1
|
251 }
|
xue@1
|
252
|
xue@1
|
253 delete[] lSpec; delete[] rSpec; DeAlloc2(lSpecs); DeAlloc2(rSpecs);
|
xue@1
|
254 }
|
xue@1
|
255 }//DoMixSpectrogramSquare
|
xue@1
|
256
|
xue@1
|
257 /*
|
xue@1
|
258 function DoMixSpectrogramSquare: retrieves a composite spectrogram as a vector. This is a recursive
|
xue@1
|
259 procedure.
|
xue@1
|
260
|
xue@1
|
261 In: Specs[0][1][N], Specs[1][2][N/2], Specs[2][4][N/4], ..., Specs[][N][1]: multiresolution power
|
xue@1
|
262 spectrogram
|
xue@1
|
263 cuts[N-1]: tiling
|
xue@1
|
264 Out: Spec[N]: composite spectrogram sampled fron Specs according to tiling cut[]
|
xue@1
|
265
|
xue@1
|
266 No return value;
|
xue@1
|
267 */
|
xue@1
|
268 void DoMixSpectrogramSquare(double* Spec, int* cuts, double*** Specs, int N, bool Norm)
|
xue@1
|
269 {
|
xue@1
|
270 // if (X==0 && Y==0) X=Y=N;
|
xue@1
|
271
|
xue@1
|
272 if (N==1)
|
xue@1
|
273 Spec[0]=Specs[0][0][0];//sqrt(Specs[0][0][0]);
|
xue@1
|
274 else
|
xue@1
|
275 {
|
xue@1
|
276 double* e;
|
xue@1
|
277 int Res;
|
xue@1
|
278
|
xue@1
|
279 //Norm=false;
|
xue@1
|
280 if (Norm)
|
xue@1
|
281 {
|
xue@1
|
282 //normalization
|
xue@1
|
283 Res=log2(N)+1;
|
xue@1
|
284 e=new double[Res];
|
xue@1
|
285 memset(e, 0, sizeof(double)*Res);
|
xue@1
|
286 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
287 for (int j=0; j<Fr; j++)
|
xue@1
|
288 for (int k=0; k<n; k++)
|
xue@1
|
289 e[i]+=Specs[i][j][k];
|
xue@1
|
290 double em=e[0];
|
xue@1
|
291 for (int i=1; i<Res; i++)
|
xue@1
|
292 {
|
xue@1
|
293 if (e[i]>em) e[i]=em/e[i];
|
xue@1
|
294 else e[i]=1;
|
xue@1
|
295 if (e[i]>em) em=e[i];
|
xue@1
|
296 } e[0]=1;
|
xue@1
|
297 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
298 {
|
xue@1
|
299 if (e[i]!=0 && e[i]!=1)
|
xue@1
|
300 for (int j=0; j<Fr; j++)
|
xue@1
|
301 for (int k=0; k<n; k++)
|
xue@1
|
302 Specs[i][j][k]*=e[i];
|
xue@1
|
303 }
|
xue@1
|
304 }
|
xue@1
|
305
|
xue@1
|
306 double ***lSpecs, ***rSpecs;
|
xue@1
|
307 if (cuts[0]) //1: vertical split
|
xue@1
|
308 {
|
xue@1
|
309 VSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
310 DoMixSpectrogramSquare(Spec, &cuts[1], lSpecs, N/2, Norm);
|
xue@1
|
311 DoMixSpectrogramSquare(&Spec[N/2], &cuts[N/2], rSpecs, N/2, Norm);
|
xue@1
|
312 }
|
xue@1
|
313 else //0: horizontal split
|
xue@1
|
314 {
|
xue@1
|
315 HSplitSpecs(N, Specs, lSpecs, rSpecs);
|
xue@1
|
316 DoMixSpectrogramSquare(Spec, &cuts[1], lSpecs, N/2, Norm);
|
xue@1
|
317 DoMixSpectrogramSquare(&Spec[N/2], &cuts[N/2], rSpecs, N/2, Norm);
|
xue@1
|
318 }
|
xue@1
|
319
|
xue@1
|
320 if (Norm)
|
xue@1
|
321 {
|
xue@1
|
322 for (int i=0, Fr=1, n=N; i<Res; i++, Fr*=2, n/=2)
|
xue@1
|
323 {
|
xue@1
|
324 if (e[i]!=0 && e[1]!=1)
|
xue@1
|
325 for (int j=0; j<Fr; j++)
|
xue@1
|
326 for (int k=0; k<n; k++)
|
xue@1
|
327 Specs[i][j][k]/=e[i];
|
xue@1
|
328 }
|
xue@1
|
329 delete[] e;
|
xue@1
|
330 }
|
xue@1
|
331
|
xue@1
|
332 DeAlloc2(lSpecs); DeAlloc2(rSpecs);
|
xue@1
|
333 }
|
xue@1
|
334 }//DoMixSpectrogramSquare
|
xue@1
|
335
|
xue@1
|
336 //---------------------------------------------------------------------------
|
xue@1
|
337 /*
|
xue@1
|
338 function HSplitSpec: split a spectrogram horizontally into lower and upper halves.
|
xue@1
|
339
|
xue@1
|
340 In: Spec[X][Y]: spectrogram to split
|
xue@1
|
341 Out: lSpec[X][Y/2], uSpec[X][Y/2]: the two half spectrograms
|
xue@1
|
342
|
xue@1
|
343 No return value. Both lSpec and uSpec are allocated anew. The caller is responsible to free these
|
xue@1
|
344 buffers.
|
xue@1
|
345 */
|
xue@1
|
346 void HSplitSpec(int X, int Y, double** Spec, double**& lSpec, double**& uSpec)
|
xue@1
|
347 {
|
xue@1
|
348 lSpec=new double*[X], uSpec=new double*[X];
|
xue@1
|
349 for (int i=0; i<X; i++)
|
xue@1
|
350 lSpec[i]=Spec[i], uSpec[i]=&Spec[i][Y/2];
|
xue@1
|
351 }//HSplitSpec
|
xue@1
|
352
|
xue@1
|
353 /*
|
xue@1
|
354 function HSplitSpecs: split a multiresolution spectrogram horizontally into lower and upper halves
|
xue@1
|
355
|
xue@1
|
356 A full spectrogram array is given in log2(N)+1 spectrograms, with the base spec of 1*N, 1st octave of
|
xue@1
|
357 2*(N/2), ..., last octave of N*1. When this array is split into two spectrogram arrays horizontally,
|
xue@1
|
358 the last spec (with the highest time resolution). Each of the two new arrays is given in log2(N)
|
xue@1
|
359 spectrograms.
|
xue@1
|
360
|
xue@1
|
361 In: Specs[nRes+1][][]: multiresolution spectrogram
|
xue@1
|
362 Out: lSpecs[nRes][][], uSpecs[nRes][][], the two half multiresolution spectrograms
|
xue@1
|
363
|
xue@1
|
364 This function allocates two 2nd order arrays of double*, which the caller is responsible to free.
|
xue@1
|
365 */
|
xue@1
|
366 void HSplitSpecs(int N, double*** Specs, double***& lSpecs, double***& uSpecs)
|
xue@1
|
367 {
|
xue@1
|
368 int nRes=log2(N); // new number of resolutions
|
xue@1
|
369 lSpecs=new double**[nRes], uSpecs=new double**[nRes];
|
xue@1
|
370 lSpecs[0]=new double*[nRes*N/2], uSpecs[0]=new double*[nRes*N/2]; for (int i=1; i<nRes; i++) lSpecs[i]=&lSpecs[0][i*N/2], uSpecs[i]=&uSpecs[0][i*N/2];
|
xue@1
|
371 for (int i=0, Fr=1, n=N; i<nRes; i++, Fr*=2, n/=2)
|
xue@1
|
372 for (int j=0; j<Fr; j++) lSpecs[i][j]=Specs[i][j], uSpecs[i][j]=&Specs[i][j][n/2];
|
xue@1
|
373 }//HSplitSpecs
|
xue@1
|
374
|
xue@1
|
375 //---------------------------------------------------------------------------
|
xue@1
|
376 /*
|
xue@1
|
377 function MixSpectrogramSquare: find composite spectrogram from multiresolution spectrogram,output as
|
xue@1
|
378 pixel grid
|
xue@1
|
379
|
xue@1
|
380 In: Specs[0][1][N], Specs[1][2][N/2], ..., Specs[Res-1][N][1], multiresolution spectrogram
|
xue@1
|
381 Out: Spec[N][N]: composite spectrogram as pixel grid (N time redundant)
|
xue@1
|
382 cuts[N-1] (optional): tiling
|
xue@1
|
383
|
xue@1
|
384 Returns entropy.
|
xue@1
|
385 */
|
xue@1
|
386 double MixSpectrogramSquare(double** Spec, double*** Specs, int N, int Res, bool Norm, bool NormMix, int* cuts=0)
|
xue@1
|
387 {
|
xue@1
|
388 int tRes=log2(N)+1;
|
xue@1
|
389 if (Res==0) Res=tRes;
|
xue@1
|
390 int NN=1<<(Res-1);
|
xue@1
|
391 bool createcuts=(cuts==0);
|
xue@1
|
392 if (createcuts) cuts=new int[N];
|
xue@1
|
393 double* e=new double[tRes], *ents=new double[tRes];
|
xue@1
|
394 //normalization
|
xue@1
|
395 memset(e, 0, sizeof(double)*Res);
|
xue@1
|
396 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
397 for (int j=0; j<Fr; j++)
|
xue@1
|
398 for (int k=0; k<n; k++)
|
xue@1
|
399 e[i]+=Specs[i][j][k];
|
xue@1
|
400
|
xue@1
|
401 if (!Norm)
|
xue@1
|
402 {
|
xue@1
|
403 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
404 {
|
xue@1
|
405 if (e[i]!=0 && e[i]!=1)
|
xue@1
|
406 for (int j=0; j<Fr; j++)
|
xue@1
|
407 for (int k=0; k<n; k++)
|
xue@1
|
408 Specs[i][j][k]/=e[i];
|
xue@1
|
409 }
|
xue@1
|
410 // for (int i=0; i<Res; i++) e[i]=1;
|
xue@1
|
411 }
|
xue@1
|
412
|
xue@1
|
413 double result=DoCutSpectrogramSquare(cuts, Specs, e, N, NN, Norm, ents);
|
xue@1
|
414 delete[] ents;
|
xue@1
|
415
|
xue@1
|
416 if (!Norm)
|
xue@1
|
417 {
|
xue@1
|
418 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
419 {
|
xue@1
|
420 if (e[i]!=0 && e[i]!=1)
|
xue@1
|
421 for (int j=0; j<Fr; j++)
|
xue@1
|
422 for (int k=0; k<n; k++)
|
xue@1
|
423 Specs[i][j][k]*=e[i];
|
xue@1
|
424 }
|
xue@1
|
425 }
|
xue@1
|
426 DoMixSpectrogramSquare(Spec, cuts, Specs, N, NormMix, N, N);
|
xue@1
|
427
|
xue@1
|
428 delete[] e;
|
xue@1
|
429 if (createcuts) delete[] cuts;
|
xue@1
|
430 return result;
|
xue@1
|
431 }//MixSpectrogramSquare
|
xue@1
|
432
|
xue@1
|
433 //---------------------------------------------------------------------------
|
xue@1
|
434 /*
|
xue@1
|
435 function MixSpectrogramSquare: find composite spectrogram from multiresolution spectrogram,output as
|
xue@1
|
436 vectors
|
xue@1
|
437
|
xue@1
|
438 In: Specs[0][1][N], Specs[1][2][N/2], ..., Specs[Res-1][N][1], multiresolution spectrogram
|
xue@1
|
439 Out: spl[N-1], Spec[N]: composite spectrogram as tiling and value vectors
|
xue@1
|
440
|
xue@1
|
441 Returns entropy.
|
xue@1
|
442 */
|
xue@1
|
443 double MixSpectrogramSquare(int* spl, double* Spec, double*** Specs, int N, int Res, bool Norm, bool NormMix)
|
xue@1
|
444 {
|
xue@1
|
445 int tRes=log2(N)+1;
|
xue@1
|
446 if (Res==0) Res=tRes;
|
xue@1
|
447 int NN=1<<(Res-1);
|
xue@1
|
448
|
xue@1
|
449 double* e=new double[tRes], *ents=new double[tRes];
|
xue@1
|
450
|
xue@1
|
451 //normalization
|
xue@1
|
452 memset(e, 0, sizeof(double)*Res);
|
xue@1
|
453 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
454 for (int j=0; j<Fr; j++)
|
xue@1
|
455 for (int k=0; k<n; k++)
|
xue@1
|
456 e[i]+=Specs[i][j][k];
|
xue@1
|
457
|
xue@1
|
458 if (!Norm)
|
xue@1
|
459 {
|
xue@1
|
460 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
461 {
|
xue@1
|
462 if (e[i]!=0 && e[i]!=1)
|
xue@1
|
463 for (int j=0; j<Fr; j++)
|
xue@1
|
464 for (int k=0; k<n; k++)
|
xue@1
|
465 Specs[i][j][k]/=e[i];
|
xue@1
|
466 }
|
xue@1
|
467 // for (int i=0; i<Res; i++) e[i]=1;
|
xue@1
|
468 }
|
xue@1
|
469
|
xue@1
|
470 double result=DoCutSpectrogramSquare(spl, Specs, e, N, NN, Norm, ents);
|
xue@1
|
471 delete[] ents;
|
xue@1
|
472 if (!Norm)
|
xue@1
|
473 {
|
xue@1
|
474 for (int i=0, Fr=1, n=N; i<tRes; i++, Fr*=2, n/=2)
|
xue@1
|
475 {
|
xue@1
|
476 if (e[i]!=0 && e[i]!=1)
|
xue@1
|
477 for (int j=0; j<Fr; j++)
|
xue@1
|
478 for (int k=0; k<n; k++)
|
xue@1
|
479 Specs[i][j][k]*=e[i];
|
xue@1
|
480 }
|
xue@1
|
481 }
|
xue@1
|
482 DoMixSpectrogramSquare(Spec, spl, Specs, N, NormMix);
|
xue@1
|
483 return result;
|
xue@1
|
484 }//MixSpectrogramSquare
|
xue@1
|
485
|
xue@1
|
486 //---------------------------------------------------------------------------
|
xue@1
|
487 /*
|
xue@1
|
488 function MixSpectrogramBlock: obtain the composite spectrogram of a waveform block as pixel grid.
|
xue@1
|
489
|
xue@1
|
490 This method deals with the effective duration of WID/2 samples of a frame of WID samples. The maximal
|
xue@1
|
491 frame width is WID, minimal frame width is wid. The spectrum with frame width WID (base) is given in
|
xue@1
|
492 lSpecs[0][0], the spectra with frame width WID/2 (1st octave) is given in lSpecs[1][0] & lSpecs[1][1],
|
xue@1
|
493 etc.
|
xue@1
|
494
|
xue@1
|
495 The output Spec[WID/wid][WID] is a spectrogram sampled from lSpecs, with a redundancy factor WID/wid.
|
xue@1
|
496 The sampling is optimized so that a maximal entropy is achieved globally. This maximal entropy is
|
xue@1
|
497 returned.
|
xue@1
|
498
|
xue@1
|
499 In: Specs[0][1][WID], Specs[1][2][WID/2], ..., Specs[Res-1][WID/wid][wid], multiresolution spectrogram
|
xue@1
|
500 Out: Spec[WID/wid][WID], composite spectrogram as pixel grid
|
xue@1
|
501 cuts[wid][WID/wid-1] (optional), tilings of the wid squares the block is divided into.
|
xue@1
|
502
|
xue@1
|
503 Returns entropy.
|
xue@1
|
504 */
|
xue@1
|
505 double MixSpectrogramBlock(double** Spec, double*** Specs, int WID, int wid, bool norm, bool normmix, int** cuts=0)
|
xue@1
|
506 {
|
xue@1
|
507 int N=WID/wid, Res=log2(WID/wid)+1;
|
xue@1
|
508 double result=0, **lSpec=new double*[N], ***lSpecs=new double**[Res];
|
xue@1
|
509 lSpecs[0]=new double*[Res*N]; for (int i=1; i<Res; i++) lSpecs[i]=&lSpecs[0][i*N];
|
xue@1
|
510
|
xue@1
|
511 bool createcuts=(cuts==0);
|
xue@1
|
512 if (createcuts)
|
xue@1
|
513 {
|
xue@1
|
514 cuts=new int*[wid];
|
xue@1
|
515 memset(cuts, 0, sizeof(int*)*wid);
|
xue@1
|
516 }
|
xue@1
|
517
|
xue@1
|
518 for (int i=0; i<wid; i++)
|
xue@1
|
519 {
|
xue@1
|
520 for (int j=0; j<N; j++)
|
xue@1
|
521 lSpec[j]=&Spec[j][i*N];
|
xue@1
|
522 for (int j=0, n=N, Fr=1; j<Res; j++, n/=2, Fr*=2)
|
xue@1
|
523 {
|
xue@1
|
524 for (int k=0; k<Fr; k++)
|
xue@1
|
525 lSpecs[j][k]=&Specs[j][k][i*n];
|
xue@1
|
526 }
|
xue@1
|
527
|
xue@1
|
528 int Log2N=log2(N);
|
xue@1
|
529 if (i==0)
|
xue@1
|
530 result+=MixSpectrogramSquare(lSpec, lSpecs, N, Log2N>2?(log2(N)-1):2, norm, normmix, cuts[i]);
|
xue@1
|
531 else if (i==1)
|
xue@1
|
532 result+=MixSpectrogramSquare(lSpec, lSpecs, N, Log2N>1?Log2N:2, norm, normmix, cuts[i]);
|
xue@1
|
533 else
|
xue@1
|
534 result+=MixSpectrogramSquare(lSpec, lSpecs, N, Log2N+1, norm, normmix, cuts[i]);
|
xue@1
|
535 }
|
xue@1
|
536 delete[] lSpec;
|
xue@1
|
537 DeAlloc2(lSpecs);
|
xue@1
|
538 if (createcuts) delete[] cuts;
|
xue@1
|
539 return result;
|
xue@1
|
540 }//MixSpectrogramBlock
|
xue@1
|
541
|
xue@1
|
542 /*
|
xue@1
|
543 function MixSpectrogramBlock: obtain the composite spectrogram of a waveform block as vectors.
|
xue@1
|
544
|
xue@1
|
545 In: Specs[0][1][WID], Specs[1][2][WID/2], ..., Specs[Res-1][WID/wid][wid], multiresolution spectrogram
|
xue@1
|
546 Out: spl[WID], Spec[WID], composite spectrogram as tiling and value vectors. Each of the vectors is
|
xue@1
|
547 made up of $wid subvectors, each subvector pair describing a N*N block of the composite
|
xue@1
|
548 spectrogram.
|
xue@1
|
549
|
xue@1
|
550 Returns entropy.
|
xue@1
|
551 */
|
xue@1
|
552 double MixSpectrogramBlock(int* spl, double* Spec, double*** Specs, int WID, int wid, bool norm, bool normmix)
|
xue@1
|
553 {
|
xue@1
|
554 int N=WID/wid, Res=log2(WID/wid)+1, *lspl;
|
xue@1
|
555 double result=0, *lSpec, ***lSpecs=new double**[Res];
|
xue@1
|
556 lSpecs[0]=new double*[Res*N]; for (int i=1; i<Res; i++) lSpecs[i]=&lSpecs[0][i*N];
|
xue@1
|
557
|
xue@1
|
558 for (int i=0; i<wid; i++)
|
xue@1
|
559 {
|
xue@1
|
560 lspl=&spl[i*N];
|
xue@1
|
561 lSpec=&Spec[i*N];
|
xue@1
|
562 for (int j=0, n=N, Fr=1; j<Res; j++, n/=2, Fr*=2)
|
xue@1
|
563 {
|
xue@1
|
564 for (int k=0; k<Fr; k++)
|
xue@1
|
565 lSpecs[j][k]=&Specs[j][k][i*n];
|
xue@1
|
566 }
|
xue@1
|
567 int Log2N=log2(N);
|
xue@1
|
568 /*
|
xue@1
|
569 if (i==0)
|
xue@1
|
570 result+=MixSpectrogramSquare(lspl, lSpec, lSpecs, N, Log2N>2?(log2(N)-1):2, norm, normmix);
|
xue@1
|
571 else if (i==1)
|
xue@1
|
572 result+=MixSpectrogramSquare(lspl, lSpec, lSpecs, N, Log2N>1?Log2N:2, norm, normmix);
|
xue@1
|
573 else */
|
xue@1
|
574 result+=MixSpectrogramSquare(lspl, lSpec, lSpecs, N, Log2N+1, norm, normmix);
|
xue@1
|
575
|
xue@1
|
576 }
|
xue@1
|
577 DeAlloc2(lSpecs);
|
xue@1
|
578
|
xue@1
|
579 return result;
|
xue@1
|
580 }//MixSpectrogramBlock
|
xue@1
|
581
|
xue@1
|
582
|
xue@1
|
583 //---------------------------------------------------------------------------
|
xue@1
|
584 /*
|
xue@1
|
585 Functions names as ...Block2(...) implement the same functions as the above directly without
|
xue@1
|
586 explicitly dividing the multiresolution spectrogram into square blocks.
|
xue@1
|
587 */
|
xue@1
|
588
|
xue@1
|
589 /*
|
xue@1
|
590 function DoCutSpectrogramBlock2: find optimal tiling for a block
|
xue@1
|
591
|
xue@1
|
592 In: Specs[R0][x0:x0+x-1][Y0:Y0+Y-1], [R0+1][2x0:2x0+2x-1][Y0/2:Y0/2+Y/2-1],...,
|
xue@1
|
593 Specs[R0+?][Nx0:Nx0+Nx-1][Y0/N:Y0/N+Y/N-1], multiresolution spectrogram
|
xue@1
|
594 Out: spl[Y-1], tiling of this block
|
xue@1
|
595
|
xue@1
|
596 Returns entropy.
|
xue@1
|
597 */
|
xue@1
|
598 double DoCutSpectrogramBlock2(int* spl, double*** Specs, int Y, int R0, int x0, int Y0, int N, double& ene)
|
xue@1
|
599 {
|
xue@1
|
600 double ent=0;
|
xue@1
|
601 if (Y>N) //N=WID/wid, the actual square size
|
xue@1
|
602 {
|
xue@1
|
603 spl[0]=0;
|
xue@1
|
604 double ene1, ene2;
|
xue@1
|
605 ent+=DoCutSpectrogramBlock2(&spl[1], Specs, Y/2, R0, x0, Y0, N, ene1);
|
xue@1
|
606 ent+=DoCutSpectrogramBlock2(&spl[Y/2], Specs, Y/2, R0, x0, Y0+Y/2, N, ene2);
|
xue@1
|
607 ene=ene1+ene2;
|
xue@1
|
608 }
|
xue@1
|
609 else if (N==1)
|
xue@1
|
610 {
|
xue@1
|
611 double tmp=Specs[R0][x0][Y0];
|
xue@1
|
612 ene=tmp;
|
xue@1
|
613 ent=xlogx(tmp);
|
xue@1
|
614 }
|
xue@1
|
615 else //Y==N, the square case
|
xue@1
|
616 {
|
xue@1
|
617 double enel, ener, enet, eneb, entl, entr, entt, entb;
|
xue@1
|
618 int* tmpspl=new int[Y];
|
xue@1
|
619 entl=DoCutSpectrogramBlock2(&spl[1], Specs, Y/2, R0+1, 2*x0, Y0/2, N/2, enel);
|
xue@1
|
620 entr=DoCutSpectrogramBlock2(&spl[Y/2], Specs, Y/2, R0+1, 2*x0+1, Y0/2, N/2, ener);
|
xue@1
|
621 entb=DoCutSpectrogramBlock2(&tmpspl[1], Specs, Y/2, R0, x0, Y0, N/2, eneb);
|
xue@1
|
622 entt=DoCutSpectrogramBlock2(&tmpspl[Y/2], Specs, Y/2, R0, x0, Y0+Y/2, N/2, enet);
|
xue@1
|
623 double ene0=enet+eneb, ene1=enel+ener, ent0=entt+entb, ent1=entl+entr;
|
xue@1
|
624 //normalize
|
xue@1
|
625 double eneres=1-(ene0+ene1)/2, norment0, norment1;
|
xue@1
|
626 //double a0=1/(ene0+eneres), a1=1/(ene1+eneres);
|
xue@1
|
627 //quasi-global normalization
|
xue@1
|
628 norment0=(ent0-ene0*log(ene0+eneres))/(ene0+eneres), norment1=(ent1-ene1*log(ene1+eneres))/(ene1+eneres);
|
xue@1
|
629 //local normalization
|
xue@1
|
630 //if (ene0>0) norment0=ent0/ene0-log(ene0); else norment0=0; if (ene1>0) norment1=ent1/ene1-log(ene1); else norment1=0;
|
xue@1
|
631 if (norment1<norment0)
|
xue@1
|
632 {
|
xue@1
|
633 spl[0]=0;
|
xue@1
|
634 ent=ent0, ene=ene0;
|
xue@1
|
635 memcpy(&spl[1], &tmpspl[1], sizeof(int)*(Y-2));
|
xue@1
|
636 }
|
xue@1
|
637 else
|
xue@1
|
638 {
|
xue@1
|
639 spl[0]=1;
|
xue@1
|
640 ent=ent1, ene=ene1;
|
xue@1
|
641 }
|
xue@1
|
642 }
|
xue@1
|
643 return ent;
|
xue@1
|
644 }//DoCutSpectrogramBlock2
|
xue@1
|
645
|
xue@1
|
646 /*
|
xue@1
|
647 function DoMixSpectrogramBlock2: sampling multiresolution spectrogram according to given tiling
|
xue@1
|
648
|
xue@1
|
649 In: Specs[R0][x0:x0+x-1][Y0:Y0+Y-1], [R0+1][2x0:2x0+2x-1][Y0/2:Y0/2+Y/2-1],...,
|
xue@1
|
650 Specs[R0+?][Nx0:Nx0+Nx-1][Y0/N:Y0/N+Y/N-1], multiresolution spectrogram
|
xue@1
|
651 spl[Y-1]; tiling of this block
|
xue@1
|
652 Out: Spec[Y], composite spectrogram as value vector
|
xue@1
|
653
|
xue@1
|
654 Returns 0.
|
xue@1
|
655 */
|
xue@1
|
656 double DoMixSpectrogramBlock2(int* spl, double* Spec, double*** Specs, int Y, int R0, int x0, int Y0, bool normmix, int res, double* e)
|
xue@1
|
657 {
|
xue@1
|
658 if (Y==1)
|
xue@1
|
659 {
|
xue@1
|
660 Spec[0]=Specs[R0][x0][Y0]*e[0];
|
xue@1
|
661 }
|
xue@1
|
662 else
|
xue@1
|
663 {
|
xue@1
|
664 double le[32];
|
xue@1
|
665 if (normmix && Y<(1<<res))
|
xue@1
|
666 {
|
xue@1
|
667 for (int i=0, j=1, k=Y; i<res; i++, j*=2, k/=2)
|
xue@1
|
668 {
|
xue@1
|
669 double lle=0;
|
xue@1
|
670 for (int fr=0; fr<j; fr++) for (int n=0; n<k; n++) lle+=Specs[i+R0][x0+fr][Y0+n]*Specs[i+R0][x0+fr][Y0+n];
|
xue@1
|
671 lle=sqrt(lle)*e[i];
|
xue@1
|
672 if (i==0) le[0]=lle;
|
xue@1
|
673 else if (lle>le[0]*2) le[i]=e[i]*le[0]*2/lle;
|
xue@1
|
674 else le[i]=e[i];
|
xue@1
|
675 }
|
xue@1
|
676 le[0]=e[0];
|
xue@1
|
677 }
|
xue@1
|
678 else
|
xue@1
|
679 {
|
xue@1
|
680 memcpy(le, e, sizeof(double)*res);
|
xue@1
|
681 }
|
xue@1
|
682
|
xue@1
|
683 if (spl[0]==0)
|
xue@1
|
684 {
|
xue@1
|
685 int newres;
|
xue@1
|
686 if (Y>=(1<<res)) newres=res;
|
xue@1
|
687 else newres=res-1;
|
xue@1
|
688 DoMixSpectrogramBlock2(&spl[1], Spec, Specs, Y/2, R0, x0, Y0, normmix, newres, le);
|
xue@1
|
689 DoMixSpectrogramBlock2(&spl[Y/2], &Spec[Y/2], Specs, Y/2, R0, x0, Y0+Y/2, normmix, newres, le);
|
xue@1
|
690 }
|
xue@1
|
691 else
|
xue@1
|
692 {
|
xue@1
|
693 DoMixSpectrogramBlock2(&spl[1], Spec, Specs, Y/2, R0+1, x0*2, Y0/2, normmix, res-1, &le[1]);
|
xue@1
|
694 DoMixSpectrogramBlock2(&spl[Y/2], &Spec[Y/2], Specs, Y/2, R0+1, x0*2+1, Y0/2, normmix, res-1, &le[1]);
|
xue@1
|
695 }
|
xue@1
|
696 }
|
xue@1
|
697 return 0;
|
xue@1
|
698 }//DoMixSpectrogramBlock2
|
xue@1
|
699
|
xue@1
|
700 /*
|
xue@1
|
701 function MixSpectrogramBlock2: obtain the composite spectrogram of a waveform block as vectors.
|
xue@1
|
702
|
xue@1
|
703 In: Specs[0][1][WID], Specs[1][2][WID/2], ..., Specs[Res-1][WID/wid][wid], multiresolution spectrogram
|
xue@1
|
704 Out: spl[WID], Spec[WID], composite spectrogram as tiling and value vectors. Each of the
|
xue@1
|
705 vectors is made up of $wid subvectors, each subvector pair describing a N*N block of the
|
xue@1
|
706 composite spectrogram.
|
xue@1
|
707
|
xue@1
|
708 Returns entropy.
|
xue@1
|
709 */
|
xue@1
|
710 double MixSpectrogramBlock2(int* spl, double* Spec, double*** Specs, int WID, int wid, bool normmix)
|
xue@1
|
711 {
|
xue@1
|
712 double ene[32];
|
xue@1
|
713 //find the total energy and normalize
|
xue@1
|
714 for (int i=0, Fr=1, Wid=WID; Wid>=wid; i++, Fr*=2, Wid/=2)
|
xue@1
|
715 {
|
xue@1
|
716 double lene=0;
|
xue@1
|
717 for (int fr=0; fr<Fr; fr++) for (int k=0; k<Wid; k++) lene+=Specs[i][fr][k]*Specs[i][fr][k];
|
xue@1
|
718 ene[i]=lene;
|
xue@1
|
719 if (lene!=0)
|
xue@1
|
720 {
|
xue@1
|
721 double ilene=1.0/lene;
|
xue@1
|
722 for (int fr=0; fr<Fr; fr++) for (int k=0; k<Wid; k++) Specs[i][fr][k]=Specs[i][fr][k]*Specs[i][fr][k]*ilene;
|
xue@1
|
723 }
|
xue@1
|
724 }
|
xue@1
|
725
|
xue@1
|
726 double result=DoCutSpectrogramBlock2(spl, Specs, WID, 0, 0, 0, WID/wid, ene[31]);
|
xue@1
|
727
|
xue@1
|
728 //de-normalize
|
xue@1
|
729 for (int i=0, Fr=1, Wid=WID; Wid>=wid; i++, Fr*=2, Wid/=2)
|
xue@1
|
730 {
|
xue@1
|
731 double lene=ene[i];
|
xue@1
|
732 if (lene!=0)
|
xue@1
|
733 for (int fr=0; fr<Fr; fr++) for (int k=0; k<Wid; k++) Specs[i][fr][k]=sqrt(Specs[i][fr][k]*lene);
|
xue@1
|
734 }
|
xue@1
|
735
|
xue@1
|
736 double e[32]; for (int i=0; i<32; i++) e[i]=1;
|
xue@1
|
737 DoMixSpectrogramBlock2(spl, Spec, Specs, WID, 0, 0, 0, normmix, log2(WID/wid)+1, e);
|
xue@1
|
738 return result;
|
xue@1
|
739 }//MixSpectrogramBlock2
|
xue@1
|
740
|
xue@1
|
741 //---------------------------------------------------------------------------
|
xue@1
|
742 /*
|
xue@1
|
743 function MixSpectrogram: obtain composite spectrogram from multiresolutin spectrogram as pixel grid
|
xue@1
|
744
|
xue@1
|
745 This method deals with Fr (base) frames of WID samples. Each base frame may be divided into 2 1st-
|
xue@1
|
746 octave frames, 4 2nd-octave frames, ..., etc. The spectrogram calculated on base frame is given in
|
xue@1
|
747 Specs[0] (Fr frames); that of 1st octave is given in Specs[1] (2*Fr frames); etc. The method resamples
|
xue@1
|
748 the spectrograms of different frame width into a single spectrogram so that the entropy is maximized
|
xue@1
|
749 globally.
|
xue@1
|
750
|
xue@1
|
751 The output Spec is a spectrogram of apparent resolution WID at hop size wid. It is a redundant
|
xue@1
|
752 representation, with equal values occupying blocks of size WID/wid.
|
xue@1
|
753
|
xue@1
|
754 In: Specs[0][Fr][WID], Specs[1][Fr*2][WID/2], ..., Specs[Res-1] [Fr*(WID/wid)][wid], multiresolution
|
xue@1
|
755 spectrogram
|
xue@1
|
756 Out: Spec[Fr*(WID/wid)][WID], composite spectrogram as pixel grid
|
xue@1
|
757 cuts[Fr][wid][N=Wid/wid], tilings of small square blocks
|
xue@1
|
758 Returns 0.
|
xue@1
|
759 */
|
xue@1
|
760 double MixSpectrogram(double** Spec, double*** Specs, int Fr, int WID, int wid, bool norm, bool normmix, int*** cuts)
|
xue@1
|
761 {
|
xue@1
|
762 //totally Fr frames of WID samples
|
xue@1
|
763 //each frame is divided into wid VERTICAL parts
|
xue@1
|
764 bool createcuts=(cuts==0);
|
xue@1
|
765 if (createcuts)
|
xue@1
|
766 {
|
xue@1
|
767 cuts=new int**[Fr];
|
xue@1
|
768 memset(cuts, 0, sizeof(int**)*Fr);
|
xue@1
|
769 }
|
xue@1
|
770 int Res=log2(WID/wid)+1;
|
xue@1
|
771 double*** lSpecs=new double**[Res];
|
xue@1
|
772 for (int i=0; i<Fr; i++)
|
xue@1
|
773 {
|
xue@1
|
774 for (int j=0, nfr=1; j<Res; j++, nfr*=2) lSpecs[j]=&Specs[j][i*nfr];
|
xue@1
|
775 MixSpectrogramBlock(&Spec[i*WID/wid], lSpecs, WID, wid, norm, normmix, cuts[i]);
|
xue@1
|
776 }
|
xue@1
|
777
|
xue@1
|
778 delete[] lSpecs;
|
xue@1
|
779 if (createcuts) delete[] cuts;
|
xue@1
|
780 return 0;
|
xue@1
|
781 }//MixSpectrogram
|
xue@1
|
782
|
xue@1
|
783 /*
|
xue@1
|
784 function MixSpectrogram: obtain composite spectrogram from multiresolutin spectrogram as vectors
|
xue@1
|
785
|
xue@1
|
786 In: Specs[0][Fr][WID], Specs[1][Fr*2][WID/2], ..., Specs[Res-1] [Fr*(WID/wid)][wid], multiresolution
|
xue@1
|
787 spectrogram
|
xue@1
|
788 Out: spl[Fr][WID], Spec[Fr][WID], composite spectrogram as tiling and value vectors by frame.
|
xue@1
|
789
|
xue@1
|
790 Returns 0.
|
xue@1
|
791 */
|
xue@1
|
792 double MixSpectrogram(int** spl, double** Spec, double*** Specs, int Fr, int WID, int wid, bool norm, bool normmix)
|
xue@1
|
793 {
|
xue@1
|
794 //totally Fr frames of WID samples
|
xue@1
|
795 //each frame is divided into wid VERTICAL parts
|
xue@1
|
796 int Res=log2(WID/wid)+1;
|
xue@1
|
797 double*** lSpecs=new double**[Res];
|
xue@1
|
798 for (int i=0; i<Fr; i++)
|
xue@1
|
799 {
|
xue@1
|
800 for (int j=0, nfr=1; j<Res; j++, nfr*=2) lSpecs[j]=&Specs[j][i*nfr];
|
xue@1
|
801 MixSpectrogramBlock(spl[i], Spec[i], lSpecs, WID, wid, norm, normmix);
|
xue@1
|
802 // MixSpectrogramBlock2(spl[i], Spec[i], lSpecs, WID, wid, norm);
|
xue@1
|
803 }
|
xue@1
|
804
|
xue@1
|
805 delete[] lSpecs;
|
xue@1
|
806 return 0;
|
xue@1
|
807 }//MixSpectrogram
|
xue@1
|
808
|
xue@1
|
809 //---------------------------------------------------------------------------
|
xue@1
|
810 /*
|
xue@1
|
811 function VSplitSpec: split a spectrogram vertically into left and right halves.
|
xue@1
|
812
|
xue@1
|
813 In: Spec[X][Y]: spectrogram to split
|
xue@1
|
814 Out: lSpec[X][Y/2], rSpec[X][Y/2]: the two half spectrograms
|
xue@1
|
815
|
xue@1
|
816 No return value. Both lSpec and rSpec are allocated anew. The caller is responsible to free these
|
xue@1
|
817 buffers.
|
xue@1
|
818 */
|
xue@1
|
819 void VSplitSpec(int X, int Y, double** Spec, double**& lSpec, double**& rSpec)
|
xue@1
|
820 {
|
xue@1
|
821 lSpec=new double*[X/2], rSpec=new double*[X/2];
|
xue@1
|
822 for(int i=0; i<X/2; i++)
|
xue@1
|
823 lSpec[i]=Spec[i], rSpec[i]=Spec[i+X/2];
|
xue@1
|
824 }//VSplitSpec
|
xue@1
|
825
|
xue@1
|
826 /*
|
xue@1
|
827 function VSplitSpecs: split a multiresolution spectrogram vertically into left and right halves
|
xue@1
|
828
|
xue@1
|
829 A full spectrogram array is given in log2(N)+1 spectrograms, with the base spec of 1*N, 1st octave of
|
xue@1
|
830 2*(N/2), ..., last octave of N*1. When this array is split into two spectrogram arrays horizontally,
|
xue@1
|
831 the last spec (with the highest time resolution). Each of the two new arrays is given in log2(N)
|
xue@1
|
832 spectrograms.
|
xue@1
|
833
|
xue@1
|
834 In: Specs[nRes+1][][]: multiresolution spectrogram
|
xue@1
|
835 Out: lSpecs[nRes][][], rSpecs[nRes][][], the two half multiresolution spectrograms
|
xue@1
|
836
|
xue@1
|
837 This function allocates two 2nd order arrays of double*, which the caller is responsible to free.
|
xue@1
|
838 */
|
xue@1
|
839 void VSplitSpecs(int N, double*** Specs, double***& lSpecs, double***& rSpecs)
|
xue@1
|
840 {
|
xue@1
|
841 int nRes=log2(N); // new number of resolutions
|
xue@1
|
842 lSpecs=new double**[nRes], rSpecs=new double**[nRes];
|
xue@1
|
843 lSpecs[0]=new double*[nRes*N/2], rSpecs[0]=new double*[nRes*N/2]; for (int i=1; i<nRes; i++) lSpecs[i]=&lSpecs[0][i*N/2], rSpecs[i]=&rSpecs[0][i*N/2];
|
xue@1
|
844 for (int i=0, Fr=1; i<nRes; i++, Fr*=2)
|
xue@1
|
845 for (int j=0; j<Fr; j++)
|
xue@1
|
846 lSpecs[i][j]=Specs[i+1][j], rSpecs[i][j]=Specs[i+1][j+Fr];
|
xue@1
|
847 }//VSplitSpecs
|
xue@1
|
848
|
xue@1
|
849
|