xue@1
|
1 //---------------------------------------------------------------------------
|
xue@1
|
2
|
xue@1
|
3 #include <math.h>
|
Chris@2
|
4 #include <string.h>
|
xue@1
|
5 #include "procedures.h"
|
xue@1
|
6 #include "matrix.h"
|
xue@1
|
7 #include "opt.h"
|
Chris@2
|
8 #include "sinest.h"
|
xue@1
|
9
|
xue@1
|
10 //---------------------------------------------------------------------------
|
xue@1
|
11 //TGMM methods
|
xue@1
|
12
|
xue@1
|
13 //method TGMM::TGMM: default constructor
|
xue@1
|
14 TGMM::TGMM()
|
xue@1
|
15 {
|
xue@1
|
16 p=0, m=dev=0;
|
xue@1
|
17 }//TGMM
|
xue@1
|
18
|
xue@1
|
19 //method GMM:~TGMM: default destructor
|
xue@1
|
20 TGMM::~TGMM()
|
xue@1
|
21 {
|
xue@1
|
22 ReleaseGMM(p, m, dev)
|
xue@1
|
23 };
|
xue@1
|
24
|
xue@1
|
25 //---------------------------------------------------------------------------
|
xue@1
|
26 //TFSpans methods
|
xue@1
|
27
|
xue@1
|
28 //method TTFSpans: default constructor
|
xue@1
|
29 TTFSpans::TTFSpans()
|
xue@1
|
30 {
|
xue@1
|
31 Count=0;
|
xue@1
|
32 Capacity=100;
|
xue@1
|
33 Items=new TTFSpan[Capacity];
|
xue@1
|
34 }//TTFSpans
|
xue@1
|
35
|
xue@1
|
36 //method ~TTFSpans: default destructor
|
xue@1
|
37 TTFSpans::~TTFSpans()
|
xue@1
|
38 {
|
xue@1
|
39 delete[] Items;
|
xue@1
|
40 }//~TTFSpans
|
xue@1
|
41
|
xue@1
|
42 /*
|
xue@1
|
43 method Add: add a new span to the list
|
xue@1
|
44
|
xue@1
|
45 In: ATFSpan: the new span to add
|
xue@1
|
46 */
|
xue@1
|
47 void TTFSpans::Add(TTFSpan& ATFSpan)
|
xue@1
|
48 {
|
xue@1
|
49 if (Count==Capacity)
|
xue@1
|
50 {
|
xue@1
|
51 int OldCapacity=Capacity;
|
xue@1
|
52 Capacity+=50;
|
xue@1
|
53 TTFSpan* NewItems=new TTFSpan[Capacity];
|
xue@1
|
54 memcpy(NewItems, Items, sizeof(TTFSpan)*OldCapacity);
|
xue@1
|
55 delete[] Items;
|
xue@1
|
56 Items=NewItems;
|
xue@1
|
57 }
|
xue@1
|
58 Items[Count]=ATFSpan;
|
xue@1
|
59 Count++;
|
xue@1
|
60 }//Add
|
xue@1
|
61
|
xue@1
|
62 /*
|
xue@1
|
63 method Clear: discard the current content without freeing memory.
|
xue@1
|
64 */
|
xue@1
|
65 void TTFSpans::Clear()
|
xue@1
|
66 {
|
xue@1
|
67 Count=0;
|
xue@1
|
68 }//Clear
|
xue@1
|
69
|
xue@1
|
70 /*
|
xue@1
|
71 method Delete: delete a span from current list
|
xue@1
|
72
|
xue@1
|
73 In: Index: index to the span to delete
|
xue@1
|
74 */
|
xue@1
|
75 int TTFSpans::Delete(int Index)
|
xue@1
|
76 {
|
xue@1
|
77 if (Index<0 || Index>=Count)
|
xue@1
|
78 return 0;
|
xue@1
|
79 memmove(&Items[Index], &Items[Index+1], sizeof(TTFSpan)*(Count-1-Index));
|
xue@1
|
80 Count--;
|
xue@1
|
81 return 1;
|
xue@1
|
82 }//Delete
|
xue@1
|
83
|
xue@1
|
84 //---------------------------------------------------------------------------
|
xue@1
|
85 //SpecTrack methods
|
xue@1
|
86
|
xue@1
|
87 /*
|
xue@1
|
88 method TSpecTrack::Add: adds a SpecPeak to the track.
|
xue@1
|
89
|
xue@1
|
90 In: APeak: the SpecPeak to add.
|
xue@1
|
91 */
|
xue@1
|
92 int TSpecTrack::Add(TSpecPeak& APeak)
|
xue@1
|
93 {
|
xue@1
|
94 if (Count>=Capacity)
|
xue@1
|
95 {
|
xue@1
|
96 Peaks=(TSpecPeak*)realloc(Peaks, sizeof(TSpecPeak)*(Capacity*2));
|
xue@1
|
97 Capacity*=2;
|
xue@1
|
98 }
|
xue@1
|
99 int ind=LocatePeak(APeak);
|
xue@1
|
100 if (ind<0)
|
xue@1
|
101 {
|
xue@1
|
102 InsertPeak(APeak, -ind-1);
|
xue@1
|
103 ind=-ind-1;
|
xue@1
|
104 }
|
xue@1
|
105
|
xue@1
|
106 int t=APeak.t;
|
xue@1
|
107 double f=APeak.f;
|
xue@1
|
108 if (Count==1) t1=t2=t, fmin=fmax=f;
|
xue@1
|
109 else
|
xue@1
|
110 {
|
xue@1
|
111 if (t<t1) t1=t;
|
xue@1
|
112 else if (t>t2) t2=t;
|
xue@1
|
113 if (f<fmin) fmin=f;
|
xue@1
|
114 else if (f>fmax) fmax=f;
|
xue@1
|
115 }
|
xue@1
|
116 return ind;
|
xue@1
|
117 }//Add
|
xue@1
|
118
|
xue@1
|
119 /*
|
xue@1
|
120 method TSpecTrack::TSpecTrack: creates a SpecTrack with an inital capacity.
|
xue@1
|
121
|
xue@1
|
122 In: ACapacity: initial capacity, i.e. the number SpecPeak's to allocate storage space for.
|
xue@1
|
123 */
|
xue@1
|
124 TSpecTrack::TSpecTrack(int ACapacity)
|
xue@1
|
125 {
|
xue@1
|
126 Count=0;
|
xue@1
|
127 Capacity=ACapacity;
|
xue@1
|
128 Peaks=new TSpecPeak[Capacity];
|
xue@1
|
129 }//TSpecTrack
|
xue@1
|
130
|
xue@1
|
131 //method TSpecTrack::~TSpecTrack: default destructor.
|
xue@1
|
132 TSpecTrack::~TSpecTrack()
|
xue@1
|
133 {
|
xue@1
|
134 delete[] Peaks;
|
xue@1
|
135 }//TSpecTrack
|
xue@1
|
136
|
xue@1
|
137 /*
|
xue@1
|
138 method InsertPeak: inserts a new SpecPeak into the track at a given index. Internal use only.
|
xue@1
|
139
|
xue@1
|
140 In: APeak: the SpecPeak to insert.
|
xue@1
|
141 index: the position in the list to place the new SpecPeak. Original SpecPeak's at and after this
|
xue@1
|
142 position are shifted by 1 posiiton.
|
xue@1
|
143 */
|
xue@1
|
144 void TSpecTrack::InsertPeak(TSpecPeak& APeak, int index)
|
xue@1
|
145 {
|
xue@1
|
146 memmove(&Peaks[index+1], &Peaks[index], sizeof(TSpecPeak)*(Count-index));
|
xue@1
|
147 Peaks[index]=APeak;
|
xue@1
|
148 Count++;
|
xue@1
|
149 }//InsertPeak
|
xue@1
|
150
|
xue@1
|
151 /*
|
xue@1
|
152 method TSpecTrack::LocatePeak: looks for a SpecPeak in the track that has the same time (t) as APeak.
|
xue@1
|
153
|
xue@1
|
154 In: APeak: a SpecPeak
|
xue@1
|
155
|
xue@1
|
156 Returns the index in this track of the first SpecPeak that has the same time stamp as APeak. However,
|
xue@1
|
157 if there is no peak with that time stamp, the method returns -1 if APeaks comes before the first
|
xue@1
|
158 SpecPeak, -2 if between 1st and 2nd SpecPeak's, -3 if between 2nd and 3rd SpecPeak's, etc.
|
xue@1
|
159 */
|
xue@1
|
160 int TSpecTrack::LocatePeak(TSpecPeak& APeak)
|
xue@1
|
161 {
|
xue@1
|
162 if (APeak.t<Peaks[0].t) return -1;
|
xue@1
|
163 if (APeak.t>Peaks[Count-1].t) return -Count-1;
|
xue@1
|
164
|
xue@1
|
165 if (APeak.t==Peaks[0].t) return 0;
|
xue@1
|
166 else if (APeak.t==Peaks[Count-1].t) return Count-1;
|
xue@1
|
167
|
xue@1
|
168 int a=0, b=Count-1, c=(a+b)/2;
|
xue@1
|
169 while (a<c)
|
xue@1
|
170 {
|
xue@1
|
171 if (APeak.t==Peaks[c].t) return c;
|
xue@1
|
172 else if (APeak.t<Peaks[c].t) {b=c; c=(a+b)/2;}
|
xue@1
|
173 else {a=c; c=(a+b)/2;}
|
xue@1
|
174 }
|
xue@1
|
175 return -a-2;
|
xue@1
|
176 }//LocatePeak
|
xue@1
|
177
|
xue@1
|
178 //---------------------------------------------------------------------------
|
xue@1
|
179 /*
|
xue@1
|
180 function: ACPower: AC power
|
xue@1
|
181
|
xue@1
|
182 In: data[Count]: a signal
|
xue@1
|
183
|
xue@1
|
184 Returns the power of its AC content.
|
xue@1
|
185 */
|
xue@1
|
186 double ACPower(double* data, int Count, void*)
|
xue@1
|
187 {
|
xue@1
|
188 if (Count<=0) return 0;
|
xue@1
|
189 double power=0, avg=0, tmp;
|
xue@1
|
190 for (int i=0; i<Count; i++)
|
xue@1
|
191 {
|
xue@1
|
192 tmp=*(data++);
|
xue@1
|
193 power+=tmp*tmp;
|
xue@1
|
194 avg+=tmp;
|
xue@1
|
195 }
|
xue@1
|
196 power=(power-avg*avg)/Count;
|
xue@1
|
197 return power;
|
xue@1
|
198 }//ACPower
|
xue@1
|
199
|
xue@1
|
200 //---------------------------------------------------------------------------
|
xue@1
|
201 /*
|
xue@1
|
202 function Add: vector addition
|
xue@1
|
203
|
xue@1
|
204 In: dest[Count], source[Count]: two vectors
|
xue@1
|
205 Out: dest[Count]: their sum
|
xue@1
|
206
|
xue@1
|
207 No return value.
|
xue@1
|
208 */
|
xue@1
|
209 void Add(double* dest, double* source, int Count)
|
xue@1
|
210 {
|
xue@1
|
211 for (int i=0; i<Count; i++) *(dest++)+=*(source++);
|
xue@1
|
212 }//Add
|
xue@1
|
213
|
xue@1
|
214 /*
|
xue@1
|
215 function Add: vector addition
|
xue@1
|
216
|
xue@1
|
217 In: addend[count], adder[count]: two vectors
|
xue@1
|
218 Out: out[count]: their sum
|
xue@1
|
219
|
xue@1
|
220 No return value.
|
xue@1
|
221 */
|
xue@1
|
222 void Add(double* out, double* addend, double* adder, int count)
|
xue@1
|
223 {
|
xue@1
|
224 for (int i=0; i<count; i++) *(out++)=*(addend++)+*(adder++);
|
xue@1
|
225 }//Add
|
xue@1
|
226
|
xue@1
|
227 //---------------------------------------------------------------------------
|
xue@1
|
228
|
xue@1
|
229 /*
|
xue@1
|
230 function ApplyWindow: applies window function to signal buffer.
|
xue@1
|
231
|
xue@1
|
232 In: Buffer[Size]: signal to be windowed
|
xue@1
|
233 Weight[Size]: the window
|
xue@1
|
234 Out: OutBuffer[Size]: windowed signal
|
xue@1
|
235
|
xue@1
|
236 No return value;
|
xue@1
|
237 */
|
xue@1
|
238 void ApplyWindow(double* OutBuffer, double* Buffer, double* Weights, int Size)
|
xue@1
|
239 {
|
xue@1
|
240 for (int i=0; i<Size; i++) *(OutBuffer++)=*(Buffer++)**(Weights++);
|
xue@1
|
241 }//ApplyWindow
|
xue@1
|
242
|
xue@1
|
243 //---------------------------------------------------------------------------
|
xue@1
|
244 /*
|
xue@1
|
245 function Avg: average
|
xue@1
|
246
|
xue@1
|
247 In: data[Count]: a data array
|
xue@1
|
248
|
xue@1
|
249 Returns the average of the array.
|
xue@1
|
250 */
|
xue@1
|
251 double Avg(double* data, int Count, void*)
|
xue@1
|
252 {
|
xue@1
|
253 if (Count<=0) return 0;
|
xue@1
|
254 double avg=0;
|
xue@1
|
255 for (int i=0; i<Count; i++) avg+=*(data++);
|
xue@1
|
256 avg/=Count;
|
xue@1
|
257 return avg;
|
xue@1
|
258 }//Avg
|
xue@1
|
259
|
xue@1
|
260 //---------------------------------------------------------------------------
|
xue@1
|
261 /*
|
xue@1
|
262 function AvgFilter: get slow-varying wave trace by averaging
|
xue@1
|
263
|
xue@1
|
264 In: data[Count]: input signal
|
xue@1
|
265 HWid: half the size of the averaging window
|
xue@1
|
266 Out: datout[Count]: the slow-varying part of data[].
|
xue@1
|
267
|
xue@1
|
268 No return value.
|
xue@1
|
269 */
|
xue@1
|
270 void AvgFilter(double* dataout, double* data, int Count, int HWid)
|
xue@1
|
271 {
|
xue@1
|
272 double sum=0;
|
xue@1
|
273
|
xue@1
|
274 dataout[0]=data[0];
|
xue@1
|
275
|
xue@1
|
276 for (int i=1; i<=HWid; i++)
|
xue@1
|
277 {
|
xue@1
|
278 sum+=data[2*i-1]+data[2*i];
|
xue@1
|
279 dataout[i]=sum/(2*i+1);
|
xue@1
|
280 }
|
xue@1
|
281
|
xue@1
|
282 for (int i=HWid+1; i<Count-HWid; i++)
|
xue@1
|
283 {
|
xue@1
|
284 sum=sum+data[i+HWid]-data[i-HWid-1];
|
xue@1
|
285 dataout[i]=sum/(2*HWid+1);
|
xue@1
|
286 }
|
xue@1
|
287
|
xue@1
|
288 for (int i=Count-HWid; i<Count; i++)
|
xue@1
|
289 {
|
xue@1
|
290 sum=sum-data[2*i-Count-1]-data[2*i-Count];
|
xue@1
|
291 dataout[i]=sum/(2*(Count-i)-1);
|
xue@1
|
292 }
|
xue@1
|
293 }//AvgFilter
|
xue@1
|
294
|
xue@1
|
295 //---------------------------------------------------------------------------
|
xue@1
|
296 /*
|
xue@1
|
297 function CalculateSpectrogram: computes the spectrogram of a signal
|
xue@1
|
298
|
xue@1
|
299 In: data[Count]: the time-domain signal
|
xue@1
|
300 start, end: start and end points marking the section for which the spectrogram is to be computed
|
xue@1
|
301 Wid, Offst: frame size and hop size
|
xue@1
|
302 Window: window function
|
xue@1
|
303 amp: a pre-amplifier
|
xue@1
|
304 half: specifies if the spectral values at Wid/2 are to be retried
|
xue@1
|
305 Out: Spec[][Wid/2] or Spec[][Wid/2+1]: amplitude spectrogram
|
xue@1
|
306 ph[][][Wid/2] or Ph[][Wid/2+1]: phase spectrogram
|
xue@1
|
307
|
xue@1
|
308 No return value. The caller is repsonse to arrange storage spance of output buffers.
|
xue@1
|
309 */
|
xue@1
|
310 void CalculateSpectrogram(double* data, int Count, int start, int end, int Wid, int Offst, double* Window, double** Spec, double** Ph, double amp, bool half)
|
xue@1
|
311 {
|
xue@1
|
312 AllocateFFTBuffer(Wid, fft, w, x);
|
xue@1
|
313
|
xue@1
|
314 int Fr=(end-start-Wid)/Offst+1;
|
xue@1
|
315
|
xue@1
|
316 for (int i=0; i<Fr; i++)
|
xue@1
|
317 {
|
xue@1
|
318 RFFTCW(&data[i*Offst+start], Window, 0, 0, log2(Wid), w, x);
|
xue@1
|
319
|
xue@1
|
320 if (Spec)
|
xue@1
|
321 {
|
xue@1
|
322 for (int j=0; j<Wid/2; j++)
|
xue@1
|
323 Spec[i][j]=sqrt(x[j].x*x[j].x+x[j].y*x[j].y)*amp;
|
xue@1
|
324 if (half)
|
xue@1
|
325 Spec[i][Wid/2]=sqrt(x[Wid/2].x*x[Wid/2].x+x[Wid/2].y*x[Wid/2].y)*amp;
|
xue@1
|
326 }
|
xue@1
|
327 if (Ph)
|
xue@1
|
328 {
|
xue@1
|
329 for (int j=0; j<=Wid/2; j++)
|
xue@1
|
330 Ph[i][j]=Atan2(x[j].y, x[j].x);
|
xue@1
|
331 if (half)
|
xue@1
|
332 Ph[i][Wid/2]=Atan2(x[Wid/2].y, x[Wid/2].x);
|
xue@1
|
333 }
|
xue@1
|
334 }
|
xue@1
|
335 FreeFFTBuffer(fft);
|
xue@1
|
336 }//CalculateSpectrogram
|
xue@1
|
337
|
xue@1
|
338 //---------------------------------------------------------------------------
|
xue@1
|
339 /*
|
xue@1
|
340 function Conv: simple convolution
|
xue@1
|
341
|
xue@1
|
342 In: in1[N1], in2[N2]: two sequences
|
xue@1
|
343 Out: out[N1+N2-1]: their convolution
|
xue@1
|
344
|
xue@1
|
345 No return value.
|
xue@1
|
346 */
|
xue@1
|
347 void Conv(double* out, int N1, double* in1, int N2, double* in2)
|
xue@1
|
348 {
|
xue@1
|
349 int N=N1+N1-1;
|
xue@1
|
350 memset(out, 0, sizeof(double)*N);
|
xue@1
|
351 for (int n1=0; n1<N1; n1++)
|
xue@1
|
352 for (int n2=0; n2<N2; n2++)
|
xue@1
|
353 out[n1+n2]+=in1[n1]*in2[n2];
|
xue@1
|
354 }//Conv
|
xue@1
|
355
|
xue@1
|
356 //---------------------------------------------------------------------------
|
xue@1
|
357 /*
|
xue@1
|
358 function Correlation: computes correlation coefficient of 2 vectors a & b, equals cos(aOb).
|
xue@1
|
359
|
xue@1
|
360 In: a[Count], b[Count]: two vectors
|
xue@1
|
361
|
xue@1
|
362 Returns their correlation coefficient.
|
xue@1
|
363 */
|
xue@1
|
364 double Correlation(double* a, double* b, int Count)
|
xue@1
|
365 {
|
xue@1
|
366 double aa=0, bb=0, ab=0;
|
xue@1
|
367 for (int i=0; i<Count; i++)
|
xue@1
|
368 {
|
xue@1
|
369 aa+=*a**a;
|
xue@1
|
370 bb+=*b**b;
|
xue@1
|
371 ab+=*(a++)**(b++);
|
xue@1
|
372 }
|
xue@1
|
373 return ab/sqrt(aa*bb);
|
xue@1
|
374 }//Correlation
|
xue@1
|
375
|
xue@1
|
376 //---------------------------------------------------------------------------
|
xue@1
|
377 /*
|
xue@1
|
378 function DCAmplitude: DC amplitude
|
xue@1
|
379
|
xue@1
|
380 In: data[Count]: a signal
|
xue@1
|
381
|
xue@1
|
382 Returns its DC amplitude (=AC amplitude without DC removing)
|
xue@1
|
383 */
|
xue@1
|
384 double DCAmplitude(double* data, int Count, void*)
|
xue@1
|
385 {
|
xue@1
|
386 if (Count<=0) return 0;
|
xue@1
|
387 double power=0, tmp;
|
xue@1
|
388 for (int i=0; i<Count; i++)
|
xue@1
|
389 {
|
xue@1
|
390 tmp=*(data++);
|
xue@1
|
391 power+=tmp*tmp;
|
xue@1
|
392 }
|
xue@1
|
393 power/=Count;
|
xue@1
|
394 return sqrt(2*power);
|
xue@1
|
395 }//DCAmplitude
|
xue@1
|
396
|
xue@1
|
397 /*
|
xue@1
|
398 function DCPower: DC power
|
xue@1
|
399
|
xue@1
|
400 In: data[Count]: a signal
|
xue@1
|
401
|
xue@1
|
402 Returns its DC power.
|
xue@1
|
403 */
|
xue@1
|
404 double DCPower(double* data, int Count, void*)
|
xue@1
|
405 {
|
xue@1
|
406 if (Count<=0) return 0;
|
xue@1
|
407 double power=0, tmp;
|
xue@1
|
408 for (int i=0; i<Count; i++)
|
xue@1
|
409 {
|
xue@1
|
410 tmp=*(data++);
|
xue@1
|
411 power+=tmp*tmp;
|
xue@1
|
412 }
|
xue@1
|
413 power/=Count;
|
xue@1
|
414 return power;
|
xue@1
|
415 }//DCPower
|
xue@1
|
416
|
xue@1
|
417 //---------------------------------------------------------------------------
|
xue@1
|
418 /*
|
xue@1
|
419 DCT: discrete cosine transform, direct computation. For fast DCT, see fft.cpp.
|
xue@1
|
420
|
xue@1
|
421 In: input[N]: a signal
|
xue@1
|
422 Out: output[N]: its DCT
|
xue@1
|
423
|
xue@1
|
424 No return value.
|
xue@1
|
425 */
|
xue@1
|
426 void DCT( double* output, double* input, int N)
|
xue@1
|
427 {
|
xue@1
|
428 double Wn;
|
xue@1
|
429
|
xue@1
|
430 for (int n=0; n<N; n++)
|
xue@1
|
431 {
|
xue@1
|
432 output[n]=0;
|
xue@1
|
433 Wn=n*M_PI/2/N;
|
xue@1
|
434 for (int k=0; k<N; k++)
|
xue@1
|
435 output[n]+=input[k]*cos((2*k+1)*Wn);
|
xue@1
|
436 if (n==0) output[n]*=1.4142135623730950488016887242097/N;
|
xue@1
|
437 else output[n]*=2.0/N;
|
xue@1
|
438 }
|
xue@1
|
439 }//DCT
|
xue@1
|
440
|
xue@1
|
441 /*
|
xue@1
|
442 function IDCT: inverse discrete cosine transform, direct computation. For fast IDCT, see fft.cpp.
|
xue@1
|
443
|
xue@1
|
444 In: input[N]: a signal
|
xue@1
|
445 Out: output[N]: its IDCT
|
xue@1
|
446
|
xue@1
|
447 No return value.
|
xue@1
|
448 */
|
xue@1
|
449 void IDCT(double* output, double* input, int N)
|
xue@1
|
450 {
|
xue@1
|
451 for (int k=0; k<N; k++)
|
xue@1
|
452 {
|
xue@1
|
453 double Wk=(2*k+1)*M_PI/2/N;
|
xue@1
|
454 output[k]=input[0]/1.4142135623730950488016887242097;
|
xue@1
|
455 for (int n=1; n<N; n++)
|
xue@1
|
456 output[k]+=input[n]*cos(n*Wk);
|
xue@1
|
457 }
|
xue@1
|
458 }//IDCT
|
xue@1
|
459
|
xue@1
|
460 //---------------------------------------------------------------------------
|
xue@1
|
461 /*
|
xue@1
|
462 function DeDC: removes DC component of a signal
|
xue@1
|
463
|
xue@1
|
464 In: data[Count]: the signal
|
xue@1
|
465 HWid: half of averaging window size
|
xue@1
|
466 Out: data[Count]: de-DC-ed signal
|
xue@1
|
467
|
xue@1
|
468 No return value.
|
xue@1
|
469 */
|
xue@1
|
470 void DeDC(double* data, int Count, int HWid)
|
xue@1
|
471 {
|
xue@1
|
472 double* data2=new double[Count];
|
xue@1
|
473 AvgFilter(data2, data, Count, HWid);
|
xue@1
|
474 for (int i=0; i<Count; i++)
|
xue@1
|
475 *(data++)-=*(data2++);
|
xue@1
|
476 delete[] data2;
|
xue@1
|
477 }//DeDC
|
xue@1
|
478
|
xue@1
|
479 /*
|
xue@1
|
480 function DeDC_static: removes DC component statically
|
xue@1
|
481
|
xue@1
|
482 In: data[Count]: the signal
|
xue@1
|
483 Out: data[Count]: DC-removed signal
|
xue@1
|
484
|
xue@1
|
485 No return value.
|
xue@1
|
486 */
|
xue@1
|
487 void DeDC_static(double* data, int Count)
|
xue@1
|
488 {
|
xue@1
|
489 double avg=Avg(data, Count, 0);
|
xue@1
|
490 for (int i=0; i<Count; i++) *(data++)-=avg;
|
xue@1
|
491 }//DeDC_static
|
xue@1
|
492
|
xue@1
|
493 //---------------------------------------------------------------------------
|
xue@1
|
494 /*
|
xue@1
|
495 function DoubleToInt: converts double-precision floating point array to integer array
|
xue@1
|
496
|
xue@1
|
497 In: in[Count]: the double array
|
xue@1
|
498 BytesPerSample: bytes per sample of destination integers
|
xue@1
|
499 Out: out[Count]: the integer array
|
xue@1
|
500
|
xue@1
|
501 No return value.
|
xue@1
|
502 */
|
xue@1
|
503 void DoubleToInt(void* out, int BytesPerSample, double* in, int Count)
|
xue@1
|
504 {
|
xue@1
|
505 if (BytesPerSample==1){unsigned char* out8=(unsigned char*)out; for (int k=0; k<Count; k++) *(out8++)=*(in++)+128.5;}
|
xue@1
|
506 else {__int16* out16=(__int16*)out; for (int k=0; k<Count; k++) *(out16++)=floor(*(in++)+0.5);}
|
xue@1
|
507 }//DoubleToInt
|
xue@1
|
508
|
xue@1
|
509 /*
|
xue@1
|
510 function DoubleToIntAdd: adds double-precision floating point array to integer array
|
xue@1
|
511
|
xue@1
|
512 In: in[Count]: the double array
|
xue@1
|
513 out[Count]: the integer array
|
xue@1
|
514 BytesPerSample: bytes per sample of destination integers
|
xue@1
|
515 Out: out[Count]: the sum of the two arrays
|
xue@1
|
516
|
xue@1
|
517 No return value.
|
xue@1
|
518 */
|
xue@1
|
519 void DoubleToIntAdd(void* out, int BytesPerSample, double* in, int Count)
|
xue@1
|
520 {
|
xue@1
|
521 if (BytesPerSample==1)
|
xue@1
|
522 {
|
xue@1
|
523 unsigned char* out8=(unsigned char*)out;
|
xue@1
|
524 for (int k=0; k<Count; k++){*out8=*out8+*in+128.5; out8++; in++;}
|
xue@1
|
525 }
|
xue@1
|
526 else
|
xue@1
|
527 {
|
xue@1
|
528 __int16* out16=(__int16*)out;
|
xue@1
|
529 for (int k=0; k<Count; k++){*out16=*out16+floor(*in+0.5); out16++; in++;}
|
xue@1
|
530 }
|
xue@1
|
531 }//DoubleToIntAdd
|
xue@1
|
532
|
xue@1
|
533 //---------------------------------------------------------------------------
|
xue@1
|
534 /*
|
xue@1
|
535 DPower: in-frame power variation
|
xue@1
|
536
|
xue@1
|
537 In: data[Count]: a signal
|
xue@1
|
538
|
xue@1
|
539 returns the different between AC powers of its first and second halves.
|
xue@1
|
540 */
|
xue@1
|
541 double DPower(double* data, int Count, void*)
|
xue@1
|
542 {
|
xue@1
|
543 double ene1=ACPower(data, Count/2, 0);
|
xue@1
|
544 double ene2=ACPower(&data[Count/2], Count/2, 0);
|
xue@1
|
545 return ene2-ene1;
|
xue@1
|
546 }//DPower
|
xue@1
|
547
|
xue@1
|
548 //---------------------------------------------------------------------------
|
xue@1
|
549 /*
|
xue@1
|
550 funciton Energy: energy
|
xue@1
|
551
|
xue@1
|
552 In: data[Count]: a signal
|
xue@1
|
553
|
xue@1
|
554 Returns its total energy
|
xue@1
|
555 */
|
xue@1
|
556 double Energy(double* data, int Count)
|
xue@1
|
557 {
|
xue@1
|
558 double result=0;
|
xue@1
|
559 for (int i=0; i<Count; i++) result+=data[i]*data[i];
|
xue@1
|
560 return result;
|
xue@1
|
561 }//Energy
|
xue@1
|
562
|
xue@1
|
563 //---------------------------------------------------------------------------
|
xue@1
|
564 /*
|
xue@1
|
565 function ExpOnsetFilter: onset filter with exponential impulse response h(t)=Aexp(-t/Tr)-Bexp(-t/Ta),
|
xue@1
|
566 A=1-exp(-1/Tr), B=1-exp(-1/Ta).
|
xue@1
|
567
|
xue@1
|
568 In: data[Count]: signal to filter
|
xue@1
|
569 Tr, Ta: time constants of h(t)
|
xue@1
|
570 Out: dataout[Count]: filtered signal, normalized by multiplying a factor.
|
xue@1
|
571
|
xue@1
|
572 Returns the normalization factor. Identical data and dataout is allowed.
|
xue@1
|
573 */
|
xue@1
|
574 double ExpOnsetFilter(double* dataout, double* data, int Count, double Tr, double Ta)
|
xue@1
|
575 {
|
xue@1
|
576 double FA=0, FB=0;
|
xue@1
|
577 double EA=exp(-1.0/Tr), EB=exp(-1.0/Ta);
|
xue@1
|
578 double A=1-EA, B=1-EB;
|
xue@1
|
579 double NormFactor=1/sqrt((1-EA)*(1-EA)/(1-EA*EA)+(1-EB)*(1-EB)/(1-EB*EB)-2*(1-EA)*(1-EB)/(1-EA*EB));
|
xue@1
|
580 for (int i=0; i<Count; i++)
|
xue@1
|
581 {
|
xue@1
|
582 FA=FA*EA+*data;
|
xue@1
|
583 FB=FB*EB+*(data++);
|
xue@1
|
584 *(dataout++)=(A*FA-B*FB)*NormFactor;
|
xue@1
|
585 }
|
xue@1
|
586 return NormFactor;
|
xue@1
|
587 }//ExpOnsetFilter
|
xue@1
|
588
|
xue@1
|
589 /*
|
xue@1
|
590 function ExpOnsetFilter_balanced: exponential onset filter without starting step response. It
|
xue@1
|
591 extends the input signal at the front end by bal*Ta samples by repeating the its value at 0, then
|
xue@1
|
592 applies the onset filter on the extended signal instead.
|
xue@1
|
593
|
xue@1
|
594 In: data[Count]: signal to filter
|
xue@1
|
595 Tr, Ta: time constants to the impulse response of onset filter, see ExpOnsetFilter().
|
xue@1
|
596 bal: balancing factor
|
xue@1
|
597 Out: dataout[Count]: filtered signal, normalized by multiplying a factor.
|
xue@1
|
598
|
xue@1
|
599 Returns the normalization factor. Identical data and dataout is allowed.
|
xue@1
|
600 */
|
xue@1
|
601 double ExpOnsetFilter_balanced(double* dataout, double* data, int Count, double Tr, double Ta, int bal)
|
xue@1
|
602 {
|
xue@1
|
603 double* tmpdata=new double[int(Count+bal*Ta)];
|
xue@1
|
604 double* ltmpdata=tmpdata;
|
xue@1
|
605 for (int i=0; i<bal*Ta; i++) *(ltmpdata++)=data[0];
|
xue@1
|
606 memcpy(ltmpdata, data, sizeof(double)*Count);
|
xue@1
|
607 double result=ExpOnsetFilter(tmpdata, tmpdata, bal*Ta+Count, Tr, Ta);
|
xue@1
|
608 memcpy(dataout, ltmpdata, sizeof(double)*Count);
|
xue@1
|
609 delete[] tmpdata;
|
xue@1
|
610 return result;
|
xue@1
|
611 }//ExpOnsetFilter_balanced
|
xue@1
|
612
|
xue@1
|
613 //---------------------------------------------------------------------------
|
xue@1
|
614 /*
|
xue@1
|
615 function ExtractLinearComponent: Legendre linear component
|
xue@1
|
616
|
xue@1
|
617 In: data[Count+1]: a signal
|
xue@1
|
618 Out: dataout[Count+1]: its Legendre linear component, optional.
|
xue@1
|
619
|
xue@1
|
620 Returns the coefficient to the linear component.
|
xue@1
|
621 */
|
xue@1
|
622 double ExtractLinearComponent(double* dataout, double* data, int Count)
|
xue@1
|
623 {
|
xue@1
|
624 double tmp=0;
|
xue@1
|
625 int N=Count*2;
|
xue@1
|
626 for (int n=0; n<=Count; n++) tmp+=n**(data++);
|
xue@1
|
627 tmp=tmp*24/N/(N+1)/(N+2);
|
xue@1
|
628 if (dataout)
|
xue@1
|
629 for (int n=0; n<=Count; n++) *(dataout++)=tmp*n;
|
xue@1
|
630 return tmp;
|
xue@1
|
631 }//ExtractLinearComponent
|
xue@1
|
632
|
xue@1
|
633 //---------------------------------------------------------------------------
|
xue@1
|
634 /*
|
xue@1
|
635 function FFTConv: fast convolution of two series by FFT overlap-add. In an overlap-add scheme it is
|
xue@1
|
636 assumed that one of the convolvends is short compared to the other one, which can be potentially
|
xue@1
|
637 infinitely long. The long convolvend is devided into short segments, each of which is convolved with
|
xue@1
|
638 the short convolvend, the results of which are then assembled into the final result. The minimal delay
|
xue@1
|
639 from input to output is the amount of overlap, which is the size of the short convolvend minus 1.
|
xue@1
|
640
|
xue@1
|
641 In: source1[size1]: convolvend
|
xue@1
|
642 source2[size2]: second convolvend
|
xue@1
|
643 zero: position of first point in convoluton result, relative to main output buffer.
|
xue@1
|
644 pre_buffer[-zero]: buffer hosting values to be overlap-added to the start of the result.
|
xue@1
|
645 Out: dest[size1]: the middle part of convolution result
|
xue@1
|
646 pre_buffer[-zero]: now updated by adding beginning part of the convolution result
|
xue@1
|
647 post_buffer[size2+zero]: end part of the convolution result
|
xue@1
|
648
|
xue@1
|
649 No return value. Identical dest and source1 allowed.
|
xue@1
|
650
|
xue@1
|
651 The convolution result has length size1+size2 (counting one trailing zero). If zero lies in the range
|
xue@1
|
652 between -size2 and 0, then the first -zero samples are added to pre_buffer[], next size1 samples are
|
xue@1
|
653 saved to dest[], and the last size2+zero sampled are saved to post_buffer[]; if not, the middle size1
|
xue@1
|
654 samples are saved to dest[], while pre_buffer[] and post_buffer[] are not used.
|
xue@1
|
655 */
|
xue@1
|
656 void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, int zero, double* pre_buffer, double* post_buffer)
|
xue@1
|
657 {
|
xue@1
|
658 int order=log2(size2-1)+1+1;
|
xue@1
|
659 int Wid=1<<order;
|
xue@1
|
660 int HWid=Wid/2;
|
xue@1
|
661 int Fr=size1/HWid;
|
xue@1
|
662 int res=size1-HWid*Fr;
|
xue@1
|
663 bool trunc=false;
|
xue@1
|
664 if (zero<-size2+1 || zero>0) zero=-size2/2, trunc=true;
|
xue@1
|
665 if (pre_buffer==NULL || (post_buffer==NULL && size2+zero!=0)) trunc=true;
|
xue@1
|
666
|
xue@1
|
667 AllocateFFTBuffer(Wid, fft, w, x1);
|
xue@1
|
668 int* hbitinv=CreateBitInvTable(order-1);
|
xue@1
|
669 cdouble* x2=new cdouble[Wid];
|
xue@1
|
670 double* tmp=new double[HWid];
|
xue@1
|
671 memset(tmp, 0, sizeof(double)*HWid);
|
xue@1
|
672
|
xue@1
|
673 memcpy(fft, source2, sizeof(double)*size2);
|
xue@1
|
674 memset(&fft[size2], 0, sizeof(double)*(Wid-size2));
|
xue@1
|
675 RFFTC(fft, 0, 0, order, w, x2, hbitinv);
|
xue@1
|
676
|
xue@1
|
677 double r1, r2, i1, i2;
|
xue@1
|
678 int ind, ind_;
|
xue@1
|
679 for (int i=0; i<Fr; i++)
|
xue@1
|
680 {
|
xue@1
|
681 memcpy(fft, &source1[i*HWid], sizeof(double)*HWid);
|
xue@1
|
682 memset(&fft[HWid], 0, sizeof(double)*HWid);
|
xue@1
|
683
|
xue@1
|
684 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
685
|
xue@1
|
686 for (int j=0; j<Wid; j++)
|
xue@1
|
687 {
|
xue@1
|
688 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
689 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
690 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
691 }
|
xue@1
|
692 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
693 for (int j=0; j<HWid; j++) tmp[j]+=fft[j];
|
xue@1
|
694
|
xue@1
|
695 ind=i*HWid+zero; //(i+1)*HWid<=size1
|
xue@1
|
696 ind_=ind+HWid; //ind_=(i+1)*HWid+zero<=size1
|
xue@1
|
697 if (ind<0)
|
xue@1
|
698 {
|
xue@1
|
699 if (!trunc)
|
xue@1
|
700 memdoubleadd(pre_buffer, tmp, -ind);
|
xue@1
|
701 memcpy(dest, &tmp[-ind], sizeof(double)*(HWid+ind));
|
xue@1
|
702 }
|
xue@1
|
703 else
|
xue@1
|
704 memcpy(&dest[ind], tmp, sizeof(double)*HWid);
|
xue@1
|
705 memcpy(tmp, &fft[HWid], sizeof(double)*HWid);
|
xue@1
|
706 }
|
xue@1
|
707
|
xue@1
|
708 if (res>0)
|
xue@1
|
709 {
|
xue@1
|
710 memcpy(fft, &source1[Fr*HWid], sizeof(double)*res);
|
xue@1
|
711 memset(&fft[res], 0, sizeof(double)*(Wid-res));
|
xue@1
|
712
|
xue@1
|
713 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
714
|
xue@1
|
715 for (int j=0; j<Wid; j++)
|
xue@1
|
716 {
|
xue@1
|
717 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
718 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
719 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
720 }
|
xue@1
|
721 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
722 for (int j=0; j<HWid; j++)
|
xue@1
|
723 tmp[j]+=fft[j];
|
xue@1
|
724
|
xue@1
|
725 ind=Fr*HWid+zero; //Fr*HWid=size1-res, ind=size1-res+zero<size1
|
xue@1
|
726 ind_=ind+HWid; //ind_=size1 -res+zero+HWid
|
xue@1
|
727 if (ind<0)
|
xue@1
|
728 {
|
xue@1
|
729 if (!trunc)
|
xue@1
|
730 memdoubleadd(pre_buffer, tmp, -ind);
|
xue@1
|
731 memcpy(dest, &tmp[-ind], sizeof(double)*(HWid+ind));
|
xue@1
|
732 }
|
xue@1
|
733 else if (ind_>size1)
|
xue@1
|
734 {
|
xue@1
|
735 memcpy(&dest[ind], tmp, sizeof(double)*(size1-ind));
|
xue@1
|
736 if (!trunc && post_buffer)
|
xue@1
|
737 {
|
xue@1
|
738 if (ind_>size1+size2+zero)
|
xue@1
|
739 memcpy(post_buffer, &tmp[size1-ind], sizeof(double)*(size2+zero));
|
xue@1
|
740 else
|
xue@1
|
741 memcpy(post_buffer, &tmp[size1-ind], sizeof(double)*(ind_-size1));
|
xue@1
|
742 }
|
xue@1
|
743 }
|
xue@1
|
744 else
|
xue@1
|
745 memcpy(&dest[ind], tmp, sizeof(double)*HWid);
|
xue@1
|
746 memcpy(tmp, &fft[HWid], sizeof(double)*HWid);
|
xue@1
|
747 Fr++;
|
xue@1
|
748 }
|
xue@1
|
749
|
xue@1
|
750 ind=Fr*HWid+zero;
|
xue@1
|
751 ind_=ind+HWid;
|
xue@1
|
752
|
xue@1
|
753 if (ind<size1)
|
xue@1
|
754 {
|
xue@1
|
755 if (ind_>size1)
|
xue@1
|
756 {
|
xue@1
|
757 memcpy(&dest[ind], tmp, sizeof(double)*(size1-ind));
|
xue@1
|
758 if (!trunc && post_buffer)
|
xue@1
|
759 {
|
xue@1
|
760 if (ind_>size1+size2+zero)
|
xue@1
|
761 memcpy(post_buffer, &tmp[size1-ind], sizeof(double)*(size2+zero));
|
xue@1
|
762 else
|
xue@1
|
763 memcpy(post_buffer, &tmp[size1-ind], sizeof(double)*(ind_-size1));
|
xue@1
|
764 }
|
xue@1
|
765 }
|
xue@1
|
766 else
|
xue@1
|
767 memcpy(&dest[ind], tmp, sizeof(double)*HWid);
|
xue@1
|
768 }
|
xue@1
|
769 else //ind>=size1 => ind_>=size1+size2+zero
|
xue@1
|
770 {
|
xue@1
|
771 if (!trunc && post_buffer)
|
xue@1
|
772 memcpy(&post_buffer[ind-size1], tmp, sizeof(double)*(size1+size2+zero-ind));
|
xue@1
|
773 }
|
xue@1
|
774
|
xue@1
|
775 FreeFFTBuffer(fft);
|
xue@1
|
776 delete[] x2;
|
xue@1
|
777 delete[] tmp;
|
xue@1
|
778 delete[] hbitinv;
|
xue@1
|
779 }//FFTConv
|
xue@1
|
780
|
xue@1
|
781 /*
|
xue@1
|
782 function FFTConv: the simplified version using two output buffers instead of three. This is almost
|
xue@1
|
783 equivalent to setting zero=-size2 in the three-output-buffer version (so that post_buffer no longer
|
xue@1
|
784 exists), except that this version requires size2 (renamed HWid) be a power of 2, and pre_buffer point
|
xue@1
|
785 to the END of the storage (so that pre_buffer=dest automatically connects the two buffers in a
|
xue@1
|
786 continuous memory block).
|
xue@1
|
787
|
xue@1
|
788 In: source1[size1]: convolvend
|
xue@1
|
789 source2[HWid]: second convolved, HWid be a power of 2
|
xue@1
|
790 pre_buffer[-HWid:-1]: buffer hosting values to be overlap-added to the start of the result.
|
xue@1
|
791 Out: dest[size1]: main output buffer, now hosting end part of the result (after HWid samples).
|
xue@1
|
792 pre_buffer[-HWid:-1]: now updated by added the start of the result
|
xue@1
|
793
|
xue@1
|
794 No return value.
|
xue@1
|
795 */
|
xue@1
|
796 void FFTConv(double* dest, double* source1, int size1, double* source2, int HWid, double* pre_buffer)
|
xue@1
|
797 {
|
xue@1
|
798 int Wid=HWid*2;
|
xue@1
|
799 int order=log2(Wid);
|
xue@1
|
800 int Fr=size1/HWid;
|
xue@1
|
801 int res=size1-HWid*Fr;
|
xue@1
|
802
|
xue@1
|
803 AllocateFFTBuffer(Wid, fft, w, x1);
|
xue@1
|
804 cdouble *x2=new cdouble[Wid];
|
xue@1
|
805 double *tmp=new double[HWid];
|
xue@1
|
806 int* hbitinv=CreateBitInvTable(order-1);
|
xue@1
|
807
|
xue@1
|
808 memcpy(fft, source2, sizeof(double)*HWid);
|
xue@1
|
809 memset(&fft[HWid], 0, sizeof(double)*HWid);
|
xue@1
|
810 RFFTC(fft, 0, 0, order, w, x2, hbitinv);
|
xue@1
|
811
|
xue@1
|
812 double r1, r2, i1, i2;
|
xue@1
|
813 for (int i=0; i<Fr; i++)
|
xue@1
|
814 {
|
xue@1
|
815 memcpy(fft, &source1[i*HWid], sizeof(double)*HWid);
|
xue@1
|
816 memset(&fft[HWid], 0, sizeof(double)*HWid);
|
xue@1
|
817
|
xue@1
|
818 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
819
|
xue@1
|
820 for (int j=0; j<Wid; j++)
|
xue@1
|
821 {
|
xue@1
|
822 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
823 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
824 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
825 }
|
xue@1
|
826 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
827
|
xue@1
|
828 if (i==0)
|
xue@1
|
829 {
|
xue@1
|
830 if (pre_buffer!=NULL)
|
xue@1
|
831 {
|
xue@1
|
832 double* destl=&pre_buffer[-HWid+1];
|
xue@1
|
833 for (int j=0; j<HWid-1; j++) destl[j]+=fft[j];
|
xue@1
|
834 }
|
xue@1
|
835 }
|
xue@1
|
836 else
|
xue@1
|
837 {
|
xue@1
|
838 for (int j=0; j<HWid-1; j++) tmp[j+1]+=fft[j];
|
xue@1
|
839 memcpy(&dest[(i-1)*HWid], tmp, sizeof(double)*HWid);
|
xue@1
|
840 }
|
xue@1
|
841 memcpy(tmp, &fft[HWid-1], sizeof(double)*HWid);
|
xue@1
|
842 }
|
xue@1
|
843
|
xue@1
|
844 if (res>0)
|
xue@1
|
845 {
|
xue@1
|
846 if (Fr==0) memset(tmp, 0, sizeof(double)*HWid);
|
xue@1
|
847
|
xue@1
|
848 memcpy(fft, &source1[Fr*HWid], sizeof(double)*res);
|
xue@1
|
849 memset(&fft[res], 0, sizeof(double)*(Wid-res));
|
xue@1
|
850
|
xue@1
|
851 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
852 for (int j=0; j<Wid; j++)
|
xue@1
|
853 {
|
xue@1
|
854 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
855 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
856 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
857 }
|
xue@1
|
858 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
859
|
xue@1
|
860 if (Fr==0)
|
xue@1
|
861 {
|
xue@1
|
862 if (pre_buffer!=NULL)
|
xue@1
|
863 {
|
xue@1
|
864 double* destl=&pre_buffer[-HWid+1];
|
xue@1
|
865 for (int j=0; j<HWid-1; j++) destl[j]+=fft[j];
|
xue@1
|
866 }
|
xue@1
|
867 }
|
xue@1
|
868 else
|
xue@1
|
869 {
|
xue@1
|
870 for (int j=0; j<HWid-1; j++) tmp[j+1]+=fft[j];
|
xue@1
|
871 memcpy(&dest[(Fr-1)*HWid], tmp, sizeof(double)*HWid);
|
xue@1
|
872 }
|
xue@1
|
873
|
xue@1
|
874 memcpy(&dest[Fr*HWid], &fft[HWid-1], sizeof(double)*res);
|
xue@1
|
875 }
|
xue@1
|
876 else
|
xue@1
|
877 memcpy(&dest[(Fr-1)*HWid], tmp, sizeof(double)*HWid);
|
xue@1
|
878
|
xue@1
|
879 FreeFFTBuffer(fft);
|
xue@1
|
880 delete[] x2; delete[] tmp; delete[] hbitinv;
|
xue@1
|
881 }//FFTConv
|
xue@1
|
882
|
xue@1
|
883 /*
|
xue@1
|
884 function FFTConv: fast convolution of two series by FFT overlap-add. Same as the three-output-buffer
|
xue@1
|
885 version above but using integer output buffers as well as integer source1.
|
xue@1
|
886
|
xue@1
|
887 In: source1[size1]: convolvend
|
xue@1
|
888 bps: bytes per sample of integer units in source1[].
|
xue@1
|
889 source2[size2]: second convolvend
|
xue@1
|
890 zero: position of first point in convoluton result, relative to main output buffer.
|
xue@1
|
891 pre_buffer[-zero]: buffer hosting values to be overlap-added to the start of the result.
|
xue@1
|
892 Out: dest[size1]: the middle part of convolution result
|
xue@1
|
893 pre_buffer[-zero]: now updated by adding beginning part of the convolution result
|
xue@1
|
894 post_buffer[size2+zero]: end part of the convolution result
|
xue@1
|
895
|
xue@1
|
896 No return value. Identical dest and source1 allowed.
|
xue@1
|
897 */
|
xue@1
|
898 void FFTConv(unsigned char* dest, unsigned char* source1, int bps, int size1, double* source2, int size2, int zero, unsigned char* pre_buffer, unsigned char* post_buffer)
|
xue@1
|
899 {
|
xue@1
|
900 int order=log2(size2-1)+1+1;
|
xue@1
|
901 int Wid=1<<order;
|
xue@1
|
902 int HWid=Wid/2;
|
xue@1
|
903 int Fr=size1/HWid;
|
xue@1
|
904 int res=size1-HWid*Fr;
|
xue@1
|
905 bool trunc=false;
|
xue@1
|
906 if (zero<-size2+1 || zero>0) zero=-size2/2, trunc=true;
|
xue@1
|
907 if (pre_buffer==NULL || (post_buffer==NULL && size2+zero!=0)) trunc=true;
|
xue@1
|
908
|
xue@1
|
909 AllocateFFTBuffer(Wid, fft, w, x1);
|
xue@1
|
910 cdouble* x2=new cdouble[Wid];
|
xue@1
|
911 double* tmp=new double[HWid];
|
xue@1
|
912 memset(tmp, 0, sizeof(double)*HWid);
|
xue@1
|
913 int* hbitinv=CreateBitInvTable(order-1);
|
xue@1
|
914
|
xue@1
|
915 memcpy(fft, source2, sizeof(double)*size2);
|
xue@1
|
916 memset(&fft[size2], 0, sizeof(double)*(Wid-size2));
|
xue@1
|
917 RFFTC(fft, 0, 0, order, w, x2, hbitinv);
|
xue@1
|
918
|
xue@1
|
919 double r1, r2, i1, i2;
|
xue@1
|
920 int ind, ind_;
|
xue@1
|
921 for (int i=0; i<Fr; i++)
|
xue@1
|
922 {
|
xue@1
|
923 IntToDouble(fft, &source1[i*HWid*bps], bps, HWid);
|
xue@1
|
924 memset(&fft[HWid], 0, sizeof(double)*HWid);
|
xue@1
|
925
|
xue@1
|
926 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
927
|
xue@1
|
928 for (int j=0; j<Wid; j++)
|
xue@1
|
929 {
|
xue@1
|
930 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
931 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
932 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
933 }
|
xue@1
|
934 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
935 for (int j=0; j<HWid; j++) tmp[j]+=fft[j];
|
xue@1
|
936
|
xue@1
|
937 ind=i*HWid+zero; //(i+1)*HWid<=size1
|
xue@1
|
938 ind_=ind+HWid; //ind_=(i+1)*HWid+zero<=size1
|
xue@1
|
939 if (ind<0)
|
xue@1
|
940 {
|
xue@1
|
941 if (!trunc)
|
xue@1
|
942 DoubleToIntAdd(pre_buffer, bps, tmp, -ind);
|
xue@1
|
943 DoubleToInt(dest, bps, &tmp[-ind], HWid+ind);
|
xue@1
|
944 }
|
xue@1
|
945 else
|
xue@1
|
946 DoubleToInt(&dest[ind*bps], bps, tmp, HWid);
|
xue@1
|
947 memcpy(tmp, &fft[HWid], sizeof(double)*HWid);
|
xue@1
|
948 }
|
xue@1
|
949
|
xue@1
|
950 if (res>0)
|
xue@1
|
951 {
|
xue@1
|
952 IntToDouble(fft, &source1[Fr*HWid*bps], bps, res);
|
xue@1
|
953 memset(&fft[res], 0, sizeof(double)*(Wid-res));
|
xue@1
|
954
|
xue@1
|
955 RFFTC(fft, 0, 0, order, w, x1, hbitinv);
|
xue@1
|
956
|
xue@1
|
957 for (int j=0; j<Wid; j++)
|
xue@1
|
958 {
|
xue@1
|
959 r1=x1[j].x, r2=x2[j].x, i1=x1[j].y, i2=x2[j].y;
|
xue@1
|
960 x1[j].x=r1*r2-i1*i2;
|
xue@1
|
961 x1[j].y=r1*i2+r2*i1;
|
xue@1
|
962 }
|
xue@1
|
963 CIFFTR(x1, order, w, fft, hbitinv);
|
xue@1
|
964 for (int j=0; j<HWid; j++)
|
xue@1
|
965 tmp[j]+=fft[j];
|
xue@1
|
966
|
xue@1
|
967 ind=Fr*HWid+zero; //Fr*HWid=size1-res, ind=size1-res+zero<size1
|
xue@1
|
968 ind_=ind+HWid; //ind_=size1 -res+zero+HWid
|
xue@1
|
969 if (ind<0)
|
xue@1
|
970 {
|
xue@1
|
971 if (!trunc)
|
xue@1
|
972 DoubleToIntAdd(pre_buffer, bps, tmp, -ind);
|
xue@1
|
973 DoubleToInt(dest, bps, &tmp[-ind], HWid+ind);
|
xue@1
|
974 }
|
xue@1
|
975 else if (ind_>size1)
|
xue@1
|
976 {
|
xue@1
|
977 DoubleToInt(&dest[ind*bps], bps, tmp, size1-ind);
|
xue@1
|
978 if (!trunc && post_buffer)
|
xue@1
|
979 {
|
xue@1
|
980 if (ind_>size1+size2+zero)
|
xue@1
|
981 DoubleToInt(post_buffer, bps, &tmp[size1-ind], size2+zero);
|
xue@1
|
982 else
|
xue@1
|
983 DoubleToInt(post_buffer, bps, &tmp[size1-ind], ind_-size1);
|
xue@1
|
984 }
|
xue@1
|
985 }
|
xue@1
|
986 else
|
xue@1
|
987 DoubleToInt(&dest[ind*bps], bps, tmp, HWid);
|
xue@1
|
988 memcpy(tmp, &fft[HWid], sizeof(double)*HWid);
|
xue@1
|
989 Fr++;
|
xue@1
|
990 }
|
xue@1
|
991
|
xue@1
|
992 ind=Fr*HWid+zero;
|
xue@1
|
993 ind_=ind+HWid;
|
xue@1
|
994
|
xue@1
|
995 if (ind<size1)
|
xue@1
|
996 {
|
xue@1
|
997 if (ind_>size1)
|
xue@1
|
998 {
|
xue@1
|
999 DoubleToInt(&dest[ind*bps], bps, tmp, size1-ind);
|
xue@1
|
1000 if (!trunc && post_buffer)
|
xue@1
|
1001 {
|
xue@1
|
1002 if (ind_>size1+size2+zero)
|
xue@1
|
1003 DoubleToInt(post_buffer, bps, &tmp[size1-ind], size2+zero);
|
xue@1
|
1004 else
|
xue@1
|
1005 DoubleToInt(post_buffer, bps, &tmp[size1-ind], ind_-size1);
|
xue@1
|
1006 }
|
xue@1
|
1007 }
|
xue@1
|
1008 else
|
xue@1
|
1009 DoubleToInt(&dest[ind*bps], bps, tmp, HWid);
|
xue@1
|
1010 }
|
xue@1
|
1011 else //ind>=size1 => ind_>=size1+size2+zero
|
xue@1
|
1012 {
|
xue@1
|
1013 if (!trunc && post_buffer)
|
xue@1
|
1014 DoubleToInt(&post_buffer[(ind-size1)*bps], bps, tmp, size1+size2+zero-ind);
|
xue@1
|
1015 }
|
xue@1
|
1016
|
xue@1
|
1017 FreeFFTBuffer(fft);
|
xue@1
|
1018 delete[] x2;
|
xue@1
|
1019 delete[] tmp;
|
xue@1
|
1020 delete[] hbitinv;
|
xue@1
|
1021 }//FFTConv
|
xue@1
|
1022
|
xue@1
|
1023 //---------------------------------------------------------------------------
|
xue@1
|
1024 /*
|
xue@1
|
1025 function FFTFilter: FFT with cosine-window overlap-add: This FFT filter is not an actural LTI system,
|
xue@1
|
1026 but an block processing with overlap-add. In this function the blocks are overlapped by 50% and summed
|
xue@1
|
1027 up with Hann windowing.
|
xue@1
|
1028
|
xue@1
|
1029 In: data[Count]: input data
|
xue@1
|
1030 Wid: DFT size
|
xue@1
|
1031 On, Off: cut-off frequencies of FFT filter. On<Off: band-pass; On>Off: band-stop.
|
xue@1
|
1032 Out: dataout[Count]: filtered data
|
xue@1
|
1033
|
xue@1
|
1034 No return value. Identical data and dataout allowed
|
xue@1
|
1035 */
|
xue@1
|
1036 void FFTFilter(double* dataout, double* data, int Count, int Wid, int On, int Off)
|
xue@1
|
1037 {
|
xue@1
|
1038 int Order=log2(Wid);
|
xue@1
|
1039 int HWid=Wid/2;
|
xue@1
|
1040 int Fr=(Count-Wid)/HWid+1;
|
xue@1
|
1041 AllocateFFTBuffer(Wid, ldata, w, x);
|
xue@1
|
1042
|
xue@1
|
1043 double* win=new double[Wid];
|
xue@1
|
1044 for (int i=0; i<Wid; i++) win[i]=sqrt((1-cos(2*M_PI*i/Wid))/2);
|
xue@1
|
1045 double* tmpdata=new double[HWid];
|
xue@1
|
1046 memset(tmpdata, 0, HWid*sizeof(double));
|
xue@1
|
1047
|
xue@1
|
1048 for (int i=0; i<Fr; i++)
|
xue@1
|
1049 {
|
xue@1
|
1050 memcpy(ldata, &data[i*HWid], Wid*sizeof(double));
|
xue@1
|
1051 if (i>0)
|
xue@1
|
1052 for (int k=0; k<HWid; k++)
|
xue@1
|
1053 ldata[k]=ldata[k]*win[k];
|
xue@1
|
1054 for (int k=HWid; k<Wid; k++)
|
xue@1
|
1055 ldata[k]=ldata[k]*win[k];
|
xue@1
|
1056
|
xue@1
|
1057 RFFTC(ldata, NULL, NULL, Order, w, x, 0);
|
xue@1
|
1058
|
xue@1
|
1059 if (On<Off) //band pass: keep [On, Off) and set other bins to zero
|
xue@1
|
1060 {
|
xue@1
|
1061 memset(x, 0, On*sizeof(cdouble));
|
xue@1
|
1062 if (On>=1)
|
xue@1
|
1063 memset(&x[Wid-On+1], 0, (On-1)*sizeof(cdouble));
|
xue@1
|
1064 if (Off*2<=Wid)
|
xue@1
|
1065 memset(&x[Off], 0, (Wid-Off*2+1)*sizeof(cdouble));
|
xue@1
|
1066 }
|
xue@1
|
1067 else //band stop: set [Off, On) to zero.
|
xue@1
|
1068 {
|
xue@1
|
1069 memset(&x[Off], 0, sizeof(cdouble)*(On-Off));
|
xue@1
|
1070 memset(&x[Wid-On+1], 0, sizeof(double)*(On-Off));
|
xue@1
|
1071 }
|
xue@1
|
1072
|
xue@1
|
1073 CIFFTR(x, Order, w, ldata);
|
xue@1
|
1074
|
xue@1
|
1075 if (i>0) for (int k=0; k<HWid; k++) ldata[k]=ldata[k]*win[k];
|
xue@1
|
1076 for (int k=HWid; k<Wid; k++) ldata[k]=ldata[k]*win[k];
|
xue@1
|
1077
|
xue@1
|
1078 memcpy(&dataout[i*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
1079 for (int k=0; k<HWid; k++) dataout[i*HWid+k]+=ldata[k];
|
xue@1
|
1080 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
1081 }
|
xue@1
|
1082
|
xue@1
|
1083 memcpy(&dataout[Fr*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
1084 memset(&dataout[Fr*HWid+HWid], 0, (Count-Fr*HWid-HWid)*sizeof(double));
|
xue@1
|
1085
|
xue@1
|
1086 delete[] win;
|
xue@1
|
1087 delete[] tmpdata;
|
xue@1
|
1088 FreeFFTBuffer(ldata);
|
xue@1
|
1089 }//FFTFilter
|
xue@1
|
1090
|
xue@1
|
1091 /*
|
xue@1
|
1092 funtion FFTFilterOLA: FFTFilter with overlap-add support. This is a true LTI filter whose impulse
|
xue@1
|
1093 response is constructed using IFFT. The filtering is implemented by fast convolution.
|
xue@1
|
1094
|
xue@1
|
1095 In: data[Count]: input data
|
xue@1
|
1096 Wid: FFT size
|
xue@1
|
1097 On, Off: cut-off frequencies, in bins, of the filter
|
xue@1
|
1098 pre_buffer[Wid]: buffer hosting sampled to be added with the start of output
|
xue@1
|
1099 Out: dataout[Count]: main output buffer, hosting the last $Count samples of output.
|
xue@1
|
1100 pre_buffer[Wid]: now updated by adding the first Wid samples of output
|
xue@1
|
1101
|
xue@1
|
1102 No return value. The complete output contains Count+Wid effective samples (including final 0); firt
|
xue@1
|
1103 $Wid are added to pre_buffer[], next Count samples saved to dataout[].
|
xue@1
|
1104 */
|
xue@1
|
1105 void FFTFilterOLA(double* dataout, double* data, int Count, int Wid, int On, int Off, double* pre_buffer)
|
xue@1
|
1106 {
|
xue@1
|
1107 AllocateFFTBuffer(Wid, spec, w, x);
|
xue@1
|
1108 memset(x, 0, sizeof(cdouble)*Wid);
|
xue@1
|
1109 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2);
|
xue@1
|
1110 CIFFTR(x, log2(Wid), w, spec);
|
xue@1
|
1111 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL);
|
xue@1
|
1112 FreeFFTBuffer(spec);
|
xue@1
|
1113 }//FFTFilterOLA
|
xue@1
|
1114 //version for integer input and output, where BytesPerSample specifies the integer format.
|
xue@1
|
1115 void FFTFilterOLA(unsigned char* dataout, unsigned char* data, int BytesPerSample, int Count, int Wid, int On, int Off, unsigned char* pre_buffer)
|
xue@1
|
1116 {
|
xue@1
|
1117 AllocateFFTBuffer(Wid, spec, w, x);
|
xue@1
|
1118 memset(x, 0, sizeof(cdouble)*Wid);
|
xue@1
|
1119 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2);
|
xue@1
|
1120 CIFFTR(x, log2(Wid), w, spec);
|
xue@1
|
1121 FFTConv(dataout, data, BytesPerSample, Count, spec, Wid, -Wid, pre_buffer, NULL);
|
xue@1
|
1122 FreeFFTBuffer(spec);
|
xue@1
|
1123 }//FFTFilterOLA
|
xue@1
|
1124
|
xue@1
|
1125 /*
|
xue@1
|
1126 function FFTFilterOLA: FFT filter with overlap-add support.
|
xue@1
|
1127
|
xue@1
|
1128 In: data[Count]: input data
|
xue@1
|
1129 amp[0:HWid]: amplitude response
|
xue@1
|
1130 ph[0:HWid]: phase response, where ph[0]=ph[HWid]=0;
|
xue@1
|
1131 pre_buffer[Wid]: buffer hosting sampled to be added to the beginning of the output
|
xue@1
|
1132 Out: dataout[Count]: main output buffer, hosting the middle $Count samples of output.
|
xue@1
|
1133 pre_buffer[Wid]: now updated by adding the first Wid/2 samples of output
|
xue@1
|
1134
|
xue@1
|
1135 No return value.
|
xue@1
|
1136 */
|
xue@1
|
1137 void FFTFilterOLA(double* dataout, double* data, int Count, double* amp, double* ph, int Wid, double* pre_buffer)
|
xue@1
|
1138 {
|
xue@1
|
1139 int HWid=Wid/2;
|
xue@1
|
1140 AllocateFFTBuffer(Wid, spec, w, x);
|
xue@1
|
1141 x[0].x=amp[0], x[0].y=0;
|
xue@1
|
1142 for (int i=1; i<HWid; i++)
|
xue@1
|
1143 {
|
xue@1
|
1144 x[i].x=x[Wid-i].x=amp[i]*cos(ph[i]);
|
xue@1
|
1145 x[i].y=amp[i]*sin(ph[i]);
|
xue@1
|
1146 x[Wid-i].y=-x[i].y;
|
xue@1
|
1147 }
|
xue@1
|
1148 x[HWid].x=amp[HWid], x[HWid].y=0;
|
xue@1
|
1149 CIFFTR(x, log2(Wid), w, spec);
|
xue@1
|
1150 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL);
|
xue@1
|
1151 FreeFFTBuffer(spec);
|
xue@1
|
1152 }//FFTFilterOLA
|
xue@1
|
1153
|
xue@1
|
1154 /*
|
xue@1
|
1155 function FFTMask: masks a band of a signal with noise
|
xue@1
|
1156
|
xue@1
|
1157 In: data[Count]: input signal
|
xue@1
|
1158 DigiOn, DigiOff: cut-off frequences of the band to mask
|
xue@1
|
1159 maskcoef: masking noise amplifier. If set to 1 than the mask level is set to the highest signal
|
xue@1
|
1160 level in the masking band.
|
xue@1
|
1161 Out: dataout[Count]: output data
|
xue@1
|
1162
|
xue@1
|
1163 No return value.
|
xue@1
|
1164 */
|
xue@1
|
1165 double FFTMask(double* dataout, double* data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef)
|
xue@1
|
1166 {
|
xue@1
|
1167 int Order=log2(Wid);
|
xue@1
|
1168 int HWid=Wid/2;
|
xue@1
|
1169 int Fr=(Count-Wid)/HWid+1;
|
xue@1
|
1170 int On=Wid*DigiOn, Off=Wid*DigiOff;
|
xue@1
|
1171 AllocateFFTBuffer(Wid, ldata, w, x);
|
xue@1
|
1172
|
xue@1
|
1173 double* winhann=new double[Wid];
|
xue@1
|
1174 double* winhamm=new double[Wid];
|
xue@1
|
1175 for (int i=0; i<Wid; i++)
|
xue@1
|
1176 {winhamm[i]=0.54-0.46*cos(2*M_PI*i/Wid); winhann[i]=(1-cos(2*M_PI*i/Wid))/2/winhamm[i];}
|
xue@1
|
1177 double* tmpdata=new double[HWid];
|
xue@1
|
1178 memset(tmpdata, 0, HWid*sizeof(double));
|
xue@1
|
1179 double max, randfi;
|
xue@1
|
1180
|
xue@1
|
1181 max=0;
|
xue@1
|
1182 for (int i=0; i<Fr; i++)
|
xue@1
|
1183 {
|
xue@1
|
1184 memcpy(ldata, &data[i*HWid], Wid*sizeof(double));
|
xue@1
|
1185 if (i>0)
|
xue@1
|
1186 for (int k=0; k<HWid; k++)
|
xue@1
|
1187 ldata[k]=ldata[k]*winhamm[k];
|
xue@1
|
1188 for (int k=HWid; k<Wid; k++)
|
xue@1
|
1189 ldata[k]=ldata[k]*winhamm[k];
|
xue@1
|
1190
|
xue@1
|
1191 RFFTC(ldata, ldata, NULL, Order, w, x, 0);
|
xue@1
|
1192
|
xue@1
|
1193 for (int k=On; k<Off; k++)
|
xue@1
|
1194 {
|
xue@1
|
1195 x[k].x=x[Wid-k].x=x[k].y=x[Wid-k].y=0;
|
xue@1
|
1196 if (max<ldata[k]) max=ldata[k];
|
xue@1
|
1197 }
|
xue@1
|
1198
|
xue@1
|
1199 CIFFTR(x, Order, w, ldata);
|
xue@1
|
1200
|
xue@1
|
1201 if (i>0)
|
xue@1
|
1202 for (int k=0; k<HWid; k++) ldata[k]=ldata[k]*winhann[k];
|
xue@1
|
1203 for (int k=HWid; k<Wid; k++) ldata[k]=ldata[k]*winhann[k];
|
xue@1
|
1204
|
xue@1
|
1205 for (int k=0; k<HWid; k++) tmpdata[k]+=ldata[k];
|
xue@1
|
1206 memcpy(&dataout[i*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
1207 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
1208 }
|
xue@1
|
1209 memcpy(&dataout[Fr*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
1210
|
xue@1
|
1211 max*=maskcoef;
|
xue@1
|
1212
|
xue@1
|
1213 for (int i=0; i<Wid; i++)
|
xue@1
|
1214 winhann[i]=winhann[i]*winhamm[i];
|
xue@1
|
1215
|
xue@1
|
1216 for (int i=0; i<Fr; i++)
|
xue@1
|
1217 {
|
xue@1
|
1218 memset(x, 0, sizeof(cdouble)*Wid);
|
xue@1
|
1219 for (int k=On; k<Off; k++)
|
xue@1
|
1220 {
|
xue@1
|
1221 randfi=rand()*M_PI*2/RAND_MAX;
|
xue@1
|
1222 x[k].x=x[Wid-k].x=max*cos(randfi);
|
xue@1
|
1223 x[k].y=max*sin(randfi);
|
xue@1
|
1224 x[Wid-k].y=-x[k].y;
|
xue@1
|
1225 }
|
xue@1
|
1226
|
xue@1
|
1227 CIFFTR(x, Order, w, ldata);
|
xue@1
|
1228
|
xue@1
|
1229 if (i>0)
|
xue@1
|
1230 for (int k=0; k<HWid; k++)
|
xue@1
|
1231 ldata[k]=ldata[k]*winhann[k];
|
xue@1
|
1232 for (int k=HWid; k<Wid; k++)
|
xue@1
|
1233 ldata[k]=ldata[k]*winhann[k];
|
xue@1
|
1234
|
xue@1
|
1235 for (int k=0; k<Wid; k++) dataout[i*HWid+k]+=ldata[k];
|
xue@1
|
1236 }
|
xue@1
|
1237
|
xue@1
|
1238 memset(&dataout[Fr*HWid+HWid], 0, (Count-Fr*HWid-HWid)*sizeof(double));
|
xue@1
|
1239
|
xue@1
|
1240 delete[] winhann;
|
xue@1
|
1241 delete[] winhamm;
|
xue@1
|
1242 delete[] tmpdata;
|
xue@1
|
1243 FreeFFTBuffer(ldata);
|
xue@1
|
1244
|
xue@1
|
1245 return max;
|
xue@1
|
1246 }//FFTMask
|
xue@1
|
1247
|
xue@1
|
1248 //---------------------------------------------------------------------------
|
xue@1
|
1249 /*
|
xue@1
|
1250 function FindInc: find the element in ordered list data that is closest to value.
|
xue@1
|
1251
|
xue@1
|
1252 In: data[Count]: a ordered list
|
xue@1
|
1253 value: the value to locate in the list
|
xue@1
|
1254
|
xue@1
|
1255 Returns the index of the element in the sorted list which is closest to $value.
|
xue@1
|
1256 */
|
xue@1
|
1257 int FindInc(double value, double* data, int Count)
|
xue@1
|
1258 {
|
xue@1
|
1259 if (value>=data[Count-1]) return Count-1;
|
xue@1
|
1260 if (value<data[0]) return 0;
|
xue@1
|
1261 int end=InsertInc(value, data, Count, false);
|
xue@1
|
1262 if (fabs(value-data[end-1])<fabs(value-data[end])) return end-1;
|
xue@1
|
1263 else return end;
|
xue@1
|
1264 }//FindInc
|
xue@1
|
1265
|
xue@1
|
1266 //---------------------------------------------------------------------------
|
xue@1
|
1267 /*
|
xue@1
|
1268 function Gaussian: Gaussian function
|
xue@1
|
1269
|
xue@1
|
1270 In: Vector[Dim]: a vector
|
xue@1
|
1271 Mean[Dim]: mean of Gaussian function
|
xue@1
|
1272 Dev[Fim]: diagonal autocorrelation matrix of Gaussian function
|
xue@1
|
1273
|
xue@1
|
1274 Returns the value of Gaussian function at Vector[].
|
xue@1
|
1275 */
|
xue@1
|
1276 double Gaussian(int Dim, double* Vector, double* Mean, double* Dev)
|
xue@1
|
1277 {
|
xue@1
|
1278 double bmt=0, tmp;
|
xue@1
|
1279 for (int dim=0; dim<Dim; dim++)
|
xue@1
|
1280 {
|
xue@1
|
1281 tmp=Vector[dim]-Mean[dim];
|
xue@1
|
1282 bmt+=tmp*tmp/Dev[dim];
|
xue@1
|
1283 }
|
xue@1
|
1284 bmt=-bmt/2;
|
xue@1
|
1285 tmp=log(Dev[0]);
|
xue@1
|
1286 for (int dim=1; dim<Dim; dim++) tmp+=log(Dev[dim]);
|
xue@1
|
1287 bmt-=tmp/2;
|
xue@1
|
1288 bmt-=Dim*log(M_PI*2)/2;
|
xue@1
|
1289 bmt=exp(bmt);
|
xue@1
|
1290 return bmt;
|
xue@1
|
1291 }//Gaussian
|
xue@1
|
1292
|
xue@1
|
1293
|
xue@1
|
1294 //---------------------------------------------------------------------------
|
xue@1
|
1295 /*
|
xue@1
|
1296 function Hamming: calculates the amplitude spectrum of Hamming window at a given frequency
|
xue@1
|
1297
|
xue@1
|
1298 In: f: frequency
|
xue@1
|
1299 T: size of Hamming window
|
xue@1
|
1300
|
xue@1
|
1301 Returns the amplitude spectrum at specified frequency.
|
xue@1
|
1302 */
|
xue@1
|
1303 double Hamming(double f, double T)
|
xue@1
|
1304 {
|
xue@1
|
1305 double omg0=2*M_PI/T;
|
xue@1
|
1306 double omg=f*2*M_PI;
|
xue@1
|
1307 cdouble c1, c2, c3;
|
xue@1
|
1308 cdouble nj(0, -1);
|
xue@1
|
1309 cdouble pj(0, 1);
|
xue@1
|
1310 double a=0.54, b=0.46;
|
xue@1
|
1311
|
xue@1
|
1312 cdouble c=1.0-exp(nj*T*omg);
|
xue@1
|
1313 double half=0.5;
|
xue@1
|
1314
|
xue@1
|
1315 if (fabs(omg)<1e-100)
|
xue@1
|
1316 c1=a*T;
|
xue@1
|
1317 else
|
xue@1
|
1318 c1=a*c/(pj*omg);
|
xue@1
|
1319
|
xue@1
|
1320 if (fabs(omg+omg0)<1e-100)
|
xue@1
|
1321 c2=b*0.5*T;
|
xue@1
|
1322 else
|
xue@1
|
1323 c2=c*b*half/(nj*cdouble(omg+omg0));
|
xue@1
|
1324
|
xue@1
|
1325 if (fabs(omg-omg0)<1e-100)
|
xue@1
|
1326 c3=b*0.5*T;
|
xue@1
|
1327 else
|
xue@1
|
1328 c3=b*c*half/(nj*cdouble(omg-omg0));
|
xue@1
|
1329
|
xue@1
|
1330 c=c1+c2+c3;
|
xue@1
|
1331 return abs(c);
|
xue@1
|
1332 }//Hamming*/
|
xue@1
|
1333
|
xue@1
|
1334 //---------------------------------------------------------------------------
|
xue@1
|
1335 /*
|
xue@1
|
1336 function HannSq: computes the square norm of Hann window spectrum (window-size-normalized)
|
xue@1
|
1337
|
xue@1
|
1338 In: x: frequency, in bins
|
xue@1
|
1339 N: size of Hann window
|
xue@1
|
1340
|
xue@1
|
1341 Return the square norm.
|
xue@1
|
1342 */
|
xue@1
|
1343 double HannSq(double x, double N)
|
xue@1
|
1344 {
|
xue@1
|
1345 double re, im;
|
xue@1
|
1346 double pim=M_PI*x;
|
xue@1
|
1347 double pimf=pim/N;
|
xue@1
|
1348 double pif=M_PI/N;
|
xue@1
|
1349
|
xue@1
|
1350 double sinpim=sin(pim);
|
xue@1
|
1351 double sinpimf=sin(pimf);
|
xue@1
|
1352 double sinpimplus1f=sin(pimf+pif);
|
xue@1
|
1353 double sinpimminus1f=sin(pimf-pif);
|
xue@1
|
1354
|
xue@1
|
1355 double spmdivbyspmf, spmdivbyspmpf, spmdivbyspmmf;
|
xue@1
|
1356
|
xue@1
|
1357 if (sinpimf==0)
|
xue@1
|
1358 spmdivbyspmf=N, spmdivbyspmpf=spmdivbyspmmf=0;
|
xue@1
|
1359 else if (sinpimplus1f==0)
|
xue@1
|
1360 spmdivbyspmpf=-N, spmdivbyspmf=spmdivbyspmmf=0;
|
xue@1
|
1361 else if (sinpimminus1f==0)
|
xue@1
|
1362 spmdivbyspmmf=-N, spmdivbyspmf=spmdivbyspmpf=0;
|
xue@1
|
1363 else
|
xue@1
|
1364 spmdivbyspmf=sinpim/sinpimf, spmdivbyspmpf=sinpim/sinpimplus1f, spmdivbyspmmf=sinpim/sinpimminus1f;
|
xue@1
|
1365
|
xue@1
|
1366 re=0.5*spmdivbyspmf-0.25*cos(pif)*(spmdivbyspmpf+spmdivbyspmmf);
|
xue@1
|
1367 im=0.25*sin(pif)*(-spmdivbyspmpf+spmdivbyspmmf);
|
xue@1
|
1368
|
xue@1
|
1369 return (re*re+im*im)/(N*N);
|
xue@1
|
1370 }//HannSq
|
xue@1
|
1371
|
xue@1
|
1372 /*
|
xue@1
|
1373 function Hann: computes the Hann window amplitude spectrum (window-size-normalized).
|
xue@1
|
1374
|
xue@1
|
1375 In: x: frequency, in bins
|
xue@1
|
1376 N: size of Hann window
|
xue@1
|
1377
|
xue@1
|
1378 Return the amplitude spectrum evaluated at x. Maximum 0.5 is reached at x=0. Time 2 to normalize
|
xue@1
|
1379 maximum to 1.
|
xue@1
|
1380 */
|
xue@1
|
1381 double Hann(double x, double N)
|
xue@1
|
1382 {
|
xue@1
|
1383 double pim=M_PI*x;
|
xue@1
|
1384 double pif=M_PI/N;
|
xue@1
|
1385 double pimf=pif*x;
|
xue@1
|
1386
|
xue@1
|
1387 double sinpim=sin(pim);
|
xue@1
|
1388 double tanpimf=tan(pimf);
|
xue@1
|
1389 double tanpimplus1f=tan(pimf+pif);
|
xue@1
|
1390 double tanpimminus1f=tan(pimf-pif);
|
xue@1
|
1391
|
xue@1
|
1392 double spmdivbyspmf, spmdivbyspmpf, spmdivbyspmmf;
|
xue@1
|
1393
|
xue@1
|
1394 if (fabs(tanpimf)<1e-10)
|
xue@1
|
1395 spmdivbyspmf=N, spmdivbyspmpf=spmdivbyspmmf=0;
|
xue@1
|
1396 else if (fabs(tanpimplus1f)<1e-10)
|
xue@1
|
1397 spmdivbyspmpf=-N, spmdivbyspmf=spmdivbyspmmf=0;
|
xue@1
|
1398 else if (fabs(tanpimminus1f)<1e-10)
|
xue@1
|
1399 spmdivbyspmmf=-N, spmdivbyspmf=spmdivbyspmpf=0;
|
xue@1
|
1400 else
|
xue@1
|
1401 spmdivbyspmf=sinpim/tanpimf, spmdivbyspmpf=sinpim/tanpimplus1f, spmdivbyspmmf=sinpim/tanpimminus1f;
|
xue@1
|
1402
|
xue@1
|
1403 double result=0.5*spmdivbyspmf-0.25*(spmdivbyspmpf+spmdivbyspmmf);
|
xue@1
|
1404
|
xue@1
|
1405 return result/N;
|
xue@1
|
1406 }//HannC
|
xue@1
|
1407
|
xue@1
|
1408 /*
|
xue@1
|
1409 function HxPeak2: fine spectral peak detection. This does detection and high-precision LSE estimation
|
xue@1
|
1410 in one go. However, since in practise most peaks are spurious, LSE estimation is not necessary on
|
xue@1
|
1411 them. Accordingly, HxPeak2 is deprecated in favour of faster but coarser peak picking methods, such as
|
xue@1
|
1412 QIFFT, which leaves fine estimation to a later stage of processing.
|
xue@1
|
1413
|
xue@1
|
1414 In: F, dF, ddF: pointers to functions that compute LSE peak energy for, plus its 1st and 2nd
|
xue@1
|
1415 derivatives against, a given frequency.
|
xue@1
|
1416 params: pointer to a data structure (l_hx) hosting input data fed to F, dF, and ddF
|
xue@1
|
1417 (st, en): frequency range, in bins, to search for peaks in
|
xue@1
|
1418 epf: convergence detection threshold
|
xue@1
|
1419 Out: hps[return value]: peak frequencies
|
xue@1
|
1420 vps[return value]; peak amplitudes
|
xue@1
|
1421
|
xue@1
|
1422 Returns the number of peaks detected.
|
xue@1
|
1423 */
|
xue@1
|
1424 int HxPeak2(double*& hps, double*& vhps, double (*F)(double, void*), double (*dF)(double, void*), double(*ddF)(double, void*), void* params, double st, double en, double epf)
|
xue@1
|
1425 {
|
xue@1
|
1426 struct l_hx {int N; union {double B; struct {int k1; int k2;};}; cdouble* x; double dhxpeak; double hxpeak;} *p=(l_hx *)params;
|
xue@1
|
1427 int dfshift=int(&((l_hx*)0)->dhxpeak);
|
xue@1
|
1428 int fshift=int(&((l_hx*)0)->hxpeak);
|
xue@1
|
1429 double B=p->B;
|
xue@1
|
1430 int count=0;
|
xue@1
|
1431
|
xue@1
|
1432 int den=ceil(en), dst=floor(st);
|
xue@1
|
1433 if (den-dst<3) den++, dst--;
|
xue@1
|
1434 if (den-dst<3) den++, dst--;
|
xue@1
|
1435 if (dst<1) dst=1;
|
xue@1
|
1436
|
xue@1
|
1437 double step=0.5;
|
xue@1
|
1438 int num=(den-dst)/step+1;
|
xue@1
|
1439 bool allochps=false, allocvhps=false;
|
xue@1
|
1440 if (hps==NULL) allochps=true, hps=new double[num];
|
xue@1
|
1441 if (vhps==NULL) allocvhps=true, vhps=new double[num];
|
xue@1
|
1442
|
xue@1
|
1443 {
|
xue@1
|
1444 double* inp=new double[num];
|
xue@1
|
1445 for (int i=0; i<num; i++)
|
xue@1
|
1446 {
|
xue@1
|
1447 double lf=dst+step*i;
|
xue@1
|
1448 p->k1=ceil(lf-B); if (p->k1<0) p->k1=0;
|
xue@1
|
1449 p->k2=floor(lf+B); if (p->k2>=p->N/2) p->k2=p->N/2-1;
|
xue@1
|
1450 inp[i]=F(lf, params);
|
xue@1
|
1451 }
|
xue@1
|
1452
|
xue@1
|
1453 for (int i=1; i<num-1; i++)
|
xue@1
|
1454 {
|
xue@1
|
1455 if (inp[i]>=inp[i-1] && inp[i]>=inp[i+1]) //inp[i]=F(dst+step*i)
|
xue@1
|
1456 {
|
xue@1
|
1457 if (inp[i]==inp[i-1] && inp[i]==inp[i+1]) continue;
|
xue@1
|
1458 double fa=dst+step*(i-1), fb=dst+step*(i+1);
|
xue@1
|
1459 double ff=dst+step*i;
|
xue@1
|
1460 p->k1=ceil(ff-B); if (p->k1<0) p->k1=0;
|
xue@1
|
1461 p->k2=floor(ff+B); if (p->k2>=p->N/2) p->k2=p->N/2-1;
|
xue@1
|
1462
|
xue@1
|
1463 double tmp=Newton1dmax(ff, fa, fb, ddF, params, dfshift, fshift, dF, dfshift, epf);
|
xue@1
|
1464
|
xue@1
|
1465 //although we have selected inp[i] to be a local maximum, different truncation
|
xue@1
|
1466 // of local spectrum implies it may not hold as the truncation of inp[i] is
|
xue@1
|
1467 // used for recalculating inp[i-1] and inp[i+1] in init_Newton method. In this
|
xue@1
|
1468 // case we retry the sub-maximal frequency to see if it becomes a local maximum
|
xue@1
|
1469 // when the spectrum is truncated to centre on it.
|
xue@1
|
1470
|
xue@1
|
1471 if (tmp==-0.5 || tmp==-0.7) //y(fa)<=y(ff)<y(fb) or y(ff)<y(fa)<y(fb)
|
xue@1
|
1472 {
|
xue@1
|
1473 tmp=Newton1dmax(fb, ff, 2*fb-ff, ddF, params, dfshift, fshift, dF, dfshift, epf);
|
xue@1
|
1474 if (tmp==-0.5 || tmp==-0.7) continue;
|
xue@1
|
1475 /*
|
xue@1
|
1476 double ff2=(ff+fb)/2;
|
xue@1
|
1477 p->k1=ceil(ff2-B); if (p->k1<0) p->k1=0;
|
xue@1
|
1478 p->k2=floor(ff2+B); if (p->k2>=p->N/2) p->k2=p->N/2-1;
|
xue@1
|
1479 tmp=Newton1dmax(ff2, ff, fb, ddF, params, dfshift, fshift, dF, dfshift, epf);
|
xue@1
|
1480 p->k1=ceil(ff-B); if (p->k1<0) p->k1=0;
|
xue@1
|
1481 p->k2=floor(ff+B); if (p->k2>=p->N/2) p->k2=p->N/2-1; */
|
xue@1
|
1482 }
|
xue@1
|
1483 else if (tmp==-0.6 || tmp==-0.8) //y(fb)<=y(ff)<y(fa)
|
xue@1
|
1484 {
|
xue@1
|
1485 tmp=Newton1dmax(fa, 2*fa-ff, ff, ddF, params, dfshift, fshift, dF, dfshift, epf);
|
xue@1
|
1486 if (tmp==-0.6 || tmp==-0.8) continue;
|
xue@1
|
1487 }
|
xue@1
|
1488 if (tmp<0 /*tmp==-0.5 || tmp==-0.6 || tmp==-1 || tmp==-2 || tmp==-3*/)
|
xue@1
|
1489 {
|
xue@1
|
1490 Search1Dmax(ff, params, F, dst+step*(i-1), dst+step*(i+1), &vhps[count], epf);
|
xue@1
|
1491 }
|
xue@1
|
1492 else
|
xue@1
|
1493 {
|
xue@1
|
1494 vhps[count]=p->hxpeak;
|
xue@1
|
1495 }
|
xue@1
|
1496 if (ff>=st && ff<=en && ff>dst+step*(i-0.99) && ff<dst+step*(i+0.99))
|
xue@1
|
1497 {
|
xue@1
|
1498 // if (count==0 || fabs(tmp-hps[count-1])>0.1)
|
xue@1
|
1499 // {
|
xue@1
|
1500 hps[count]=ff;
|
xue@1
|
1501 count++;
|
xue@1
|
1502 // }
|
xue@1
|
1503 }
|
xue@1
|
1504 }
|
xue@1
|
1505 }
|
xue@1
|
1506 delete[] inp;
|
xue@1
|
1507 }
|
xue@1
|
1508
|
xue@1
|
1509 if (allochps) hps=(double*)realloc(hps, sizeof(double)*count);
|
xue@1
|
1510 if (allocvhps) vhps=(double*)realloc(vhps, sizeof(double)*count);
|
xue@1
|
1511 return count;
|
xue@1
|
1512 }//HxPeak2
|
xue@1
|
1513
|
xue@1
|
1514 //---------------------------------------------------------------------------
|
xue@1
|
1515 /*
|
xue@1
|
1516 function InsertDec: inserts value into sorted decreasing list
|
xue@1
|
1517
|
xue@1
|
1518 In: data[Count]: a sorted decreasing list.
|
xue@1
|
1519 value: the value to be added
|
xue@1
|
1520 Out: data[Count]: the list with $value inserted if the latter is larger than its last entry, in which
|
xue@1
|
1521 case the original last entry is discarded.
|
xue@1
|
1522
|
xue@1
|
1523 Returns the index where $value is located in data[], or -1 if $value is smaller than or equal to
|
xue@1
|
1524 data[Count-1].
|
xue@1
|
1525 */
|
xue@1
|
1526 int InsertDec(int value, int* data, int Count)
|
xue@1
|
1527 {
|
xue@1
|
1528 if (Count<=0) return -1;
|
xue@1
|
1529 if (value<=data[Count-1]) return -1;
|
xue@1
|
1530 if (value>data[0])
|
xue@1
|
1531 {
|
xue@1
|
1532 memmove(&data[1], &data[0], sizeof(int)*(Count-1));
|
xue@1
|
1533 data[0]=value;
|
xue@1
|
1534 return 0;
|
xue@1
|
1535 }
|
xue@1
|
1536
|
xue@1
|
1537 //now Count>=2
|
xue@1
|
1538 int head=0, end=Count-1, mid;
|
xue@1
|
1539
|
xue@1
|
1540 //D(head)>=value>D(end)
|
xue@1
|
1541 while (end-head>1)
|
xue@1
|
1542 {
|
xue@1
|
1543 mid=(head+end)/2;
|
xue@1
|
1544 if (value<=data[mid]) head=mid;
|
xue@1
|
1545 else end=mid;
|
xue@1
|
1546 }
|
xue@1
|
1547
|
xue@1
|
1548 //D(head=end-1)>=value>D(end)
|
xue@1
|
1549 memmove(&data[end+1], &data[end], sizeof(int)*(Count-end-1));
|
xue@1
|
1550 data[end]=value;
|
xue@1
|
1551 return end;
|
xue@1
|
1552 }//InsertDec
|
xue@1
|
1553 //the double version
|
xue@1
|
1554 int InsertDec(double value, double* data, int Count)
|
xue@1
|
1555 {
|
xue@1
|
1556 if (Count<=0) return -1;
|
xue@1
|
1557 if (value<=data[Count-1]) return -1;
|
xue@1
|
1558 if (value>data[0])
|
xue@1
|
1559 {
|
xue@1
|
1560 memmove(&data[1], &data[0], sizeof(double)*(Count-1));
|
xue@1
|
1561 data[0]=value;
|
xue@1
|
1562 return 0;
|
xue@1
|
1563 }
|
xue@1
|
1564
|
xue@1
|
1565 //now Count>=2
|
xue@1
|
1566 int head=0, end=Count-1, mid;
|
xue@1
|
1567
|
xue@1
|
1568 //D(head)>=value>D(end)
|
xue@1
|
1569 while (end-head>1)
|
xue@1
|
1570 {
|
xue@1
|
1571 mid=(head+end)/2;
|
xue@1
|
1572 if (value<=data[mid]) head=mid;
|
xue@1
|
1573 else end=mid;
|
xue@1
|
1574 }
|
xue@1
|
1575
|
xue@1
|
1576 //D(head=end-1)>=value>D(end)
|
xue@1
|
1577 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1578 data[end]=value;
|
xue@1
|
1579 return end;
|
xue@1
|
1580 }//InsertDec
|
xue@1
|
1581
|
xue@1
|
1582 /*
|
xue@1
|
1583 function InsertDec: inserts value and attached integer into sorted decreasing list
|
xue@1
|
1584
|
xue@1
|
1585 In: data[Count]: a sorted decreasing list
|
xue@1
|
1586 indices[Count]: a list of integers attached to entries of data[]
|
xue@1
|
1587 value, index: the value to be added and its attached integer
|
xue@1
|
1588 Out: data[Count], indices[Count]: the lists with $value and $index inserted if $value is larger than
|
xue@1
|
1589 the last entry of data[], in which case the original last entries are discarded.
|
xue@1
|
1590
|
xue@1
|
1591 Returns the index where $value is located in data[], or -1 if $value is smaller than or equal to
|
xue@1
|
1592 data[Count-1].
|
xue@1
|
1593 */
|
xue@1
|
1594 int InsertDec(double value, int index, double* data, int* indices, int Count)
|
xue@1
|
1595 {
|
xue@1
|
1596 if (Count<=0) return -1;
|
xue@1
|
1597 if (value<=data[Count-1]) return -1;
|
xue@1
|
1598 if (value>data[0])
|
xue@1
|
1599 {
|
xue@1
|
1600 memmove(&data[1], data, sizeof(double)*(Count-1));
|
xue@1
|
1601 memmove(&indices[1], indices, sizeof(int)*(Count-1));
|
xue@1
|
1602 data[0]=value, indices[0]=index;
|
xue@1
|
1603 return 0;
|
xue@1
|
1604 }
|
xue@1
|
1605
|
xue@1
|
1606 //now Count>=2
|
xue@1
|
1607 int head=0, end=Count-1, mid;
|
xue@1
|
1608
|
xue@1
|
1609 //D(head)>=value>D(end)
|
xue@1
|
1610 while (end-head>1)
|
xue@1
|
1611 {
|
xue@1
|
1612 mid=(head+end)/2;
|
xue@1
|
1613 if (value<=data[mid]) head=mid;
|
xue@1
|
1614 else end=mid;
|
xue@1
|
1615 }
|
xue@1
|
1616
|
xue@1
|
1617 //D(head=end-1)>=value>D(end)
|
xue@1
|
1618 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1619 memmove(&indices[end+1], &indices[end], sizeof(int)*(Count-end-1));
|
xue@1
|
1620 data[end]=value, indices[end]=index;
|
xue@1
|
1621 return end;
|
xue@1
|
1622 }//InsertDec
|
xue@1
|
1623
|
xue@1
|
1624 /*
|
xue@1
|
1625 InsertInc: inserts value into sorted increasing list.
|
xue@1
|
1626
|
xue@1
|
1627 In: data[Count]: a sorted increasing list.
|
xue@1
|
1628 Capacity: maximal size of data[]
|
xue@1
|
1629 value: the value to be added
|
xue@1
|
1630 Compare: pointer to function that compare two values
|
xue@1
|
1631 Out: data[Count]: the list with $value inserted. If the original list is full (Count=Capacity) then
|
xue@1
|
1632 either $value, or the last entry of data[], whichever is larger, is discarded.
|
xue@1
|
1633
|
xue@1
|
1634 Returns the index where $value is located in data[], or -1 if it is not inserted, which happens if
|
xue@1
|
1635 Count=Capacity and $value is larger than or equal to the last entry in data[Capacity].
|
xue@1
|
1636 */
|
xue@1
|
1637 int InsertInc(void* value, void** data, int Count, int Capacity, int (*Compare)(void*, void*))
|
xue@1
|
1638 {
|
xue@1
|
1639 if (Capacity<=0) return -1;
|
xue@1
|
1640 if (Count>Capacity) Count=Capacity;
|
xue@1
|
1641
|
xue@1
|
1642 //Compare(A,B)<0 if A<B, =0 if A=B, >0 if A>B
|
xue@1
|
1643 int PosToInsert;
|
xue@1
|
1644 if (Count==0) PosToInsert=0;
|
xue@1
|
1645 else if (Compare(value, data[Count-1])>0) PosToInsert=Count;
|
xue@1
|
1646 else if (Compare(value, data[0])<0) PosToInsert=0;
|
xue@1
|
1647 else
|
xue@1
|
1648 {
|
xue@1
|
1649 //now Count>=2
|
xue@1
|
1650 int head=0, end=Count-1, mid;
|
xue@1
|
1651
|
xue@1
|
1652 //D(head)<=value<D(end)
|
xue@1
|
1653 while (end-head>1)
|
xue@1
|
1654 {
|
xue@1
|
1655 mid=(head+end)/2;
|
xue@1
|
1656 if (Compare(value, data[mid])>=0) head=mid;
|
xue@1
|
1657 else end=mid;
|
xue@1
|
1658 }
|
xue@1
|
1659 //D(head=end-1)<=value<D(end)
|
xue@1
|
1660 PosToInsert=end;
|
xue@1
|
1661 }
|
xue@1
|
1662
|
xue@1
|
1663 if (Count<Capacity)
|
xue@1
|
1664 {
|
xue@1
|
1665 memmove(&data[PosToInsert+1], &data[PosToInsert], sizeof(void*)*(Count-PosToInsert));
|
xue@1
|
1666 data[PosToInsert]=value;
|
xue@1
|
1667 }
|
xue@1
|
1668 else //Count==Capacity
|
xue@1
|
1669 {
|
xue@1
|
1670 if (PosToInsert>=Capacity) return -1;
|
xue@1
|
1671 memmove(&data[PosToInsert+1], &data[PosToInsert], sizeof(void*)*(Count-PosToInsert-1));
|
xue@1
|
1672 data[PosToInsert]=value;
|
xue@1
|
1673 }
|
xue@1
|
1674 return PosToInsert;
|
xue@1
|
1675 }//InsertInc
|
xue@1
|
1676
|
xue@1
|
1677 /*
|
xue@1
|
1678 function InsertInc: inserts value into sorted increasing list
|
xue@1
|
1679
|
xue@1
|
1680 In: data[Count]: a sorted increasing list.
|
xue@1
|
1681 value: the value to be added
|
xue@1
|
1682 doinsert: specifies whether the actually insertion is to be performed
|
xue@1
|
1683 Out: data[Count]: the list with $value inserted if the latter is smaller than its last entry, in which
|
xue@1
|
1684 case the original last entry of data[] is discarded.
|
xue@1
|
1685
|
xue@1
|
1686 Returns the index where $value is located in data[], or -1 if value is larger than or equal to
|
xue@1
|
1687 data[Count-1].
|
xue@1
|
1688 */
|
xue@1
|
1689 int InsertInc(double value, double* data, int Count, bool doinsert)
|
xue@1
|
1690 {
|
xue@1
|
1691 if (Count<=0) return -1;
|
xue@1
|
1692 if (value>=data[Count-1]) return -1;
|
xue@1
|
1693 if (value<data[0])
|
xue@1
|
1694 {
|
xue@1
|
1695 memmove(&data[1], &data[0], sizeof(double)*(Count-1));
|
xue@1
|
1696 if (doinsert) data[0]=value;
|
xue@1
|
1697 return 0;
|
xue@1
|
1698 }
|
xue@1
|
1699
|
xue@1
|
1700 //now Count>=2
|
xue@1
|
1701 int head=0, end=Count-1, mid;
|
xue@1
|
1702
|
xue@1
|
1703 //D(head)<=value<D(end)
|
xue@1
|
1704 while (end-head>1)
|
xue@1
|
1705 {
|
xue@1
|
1706 mid=(head+end)/2;
|
xue@1
|
1707 if (value>=data[mid]) head=mid;
|
xue@1
|
1708 else end=mid;
|
xue@1
|
1709 }
|
xue@1
|
1710
|
xue@1
|
1711 //D(head=end-1)<=value<D(end)
|
xue@1
|
1712 if (doinsert)
|
xue@1
|
1713 {
|
xue@1
|
1714 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1715 data[end]=value;
|
xue@1
|
1716 }
|
xue@1
|
1717 return end;
|
xue@1
|
1718 }//InsertInc
|
xue@1
|
1719 //version where data[] is int.
|
xue@1
|
1720 int InsertInc(double value, int* data, int Count, bool doinsert)
|
xue@1
|
1721 {
|
xue@1
|
1722 if (Count<=0) return -1;
|
xue@1
|
1723 if (value>=data[Count-1]) return -1;
|
xue@1
|
1724 if (value<data[0])
|
xue@1
|
1725 {
|
xue@1
|
1726 memmove(&data[1], &data[0], sizeof(int)*(Count-1));
|
xue@1
|
1727 if (doinsert) data[0]=value;
|
xue@1
|
1728 return 0;
|
xue@1
|
1729 }
|
xue@1
|
1730
|
xue@1
|
1731 //now Count>=2
|
xue@1
|
1732 int head=0, end=Count-1, mid;
|
xue@1
|
1733
|
xue@1
|
1734 //D(head)<=value<D(end)
|
xue@1
|
1735 while (end-head>1)
|
xue@1
|
1736 {
|
xue@1
|
1737 mid=(head+end)/2;
|
xue@1
|
1738 if (value>=data[mid]) head=mid;
|
xue@1
|
1739 else end=mid;
|
xue@1
|
1740 }
|
xue@1
|
1741
|
xue@1
|
1742 //D(head=end-1)<=value<D(end)
|
xue@1
|
1743 if (doinsert)
|
xue@1
|
1744 {
|
xue@1
|
1745 memmove(&data[end+1], &data[end], sizeof(int)*(Count-end-1));
|
xue@1
|
1746 data[end]=value;
|
xue@1
|
1747 }
|
xue@1
|
1748 return end;
|
xue@1
|
1749 }//InsertInc
|
xue@1
|
1750
|
xue@1
|
1751 /*
|
xue@1
|
1752 function InsertInc: inserts value and attached integer into sorted increasing list
|
xue@1
|
1753
|
xue@1
|
1754 In: data[Count]: a sorted increasing list
|
xue@1
|
1755 indices[Count]: a list of integers attached to entries of data[]
|
xue@1
|
1756 value, index: the value to be added and its attached integer
|
xue@1
|
1757 Out: data[Count], indices[Count]: the lists with $value and $index inserted if $value is smaller than
|
xue@1
|
1758 the last entry of data[], in which case the original last entries are discarded.
|
xue@1
|
1759
|
xue@1
|
1760 Returns the index where $value is located in data[], or -1 if $value is larger than or equal to
|
xue@1
|
1761 data[Count-1].
|
xue@1
|
1762 */
|
xue@1
|
1763 int InsertInc(double value, int index, double* data, int* indices, int Count)
|
xue@1
|
1764 {
|
xue@1
|
1765 if (Count<=0) return -1;
|
xue@1
|
1766 if (value>=data[Count-1]) return -1;
|
xue@1
|
1767 if (value<data[0])
|
xue@1
|
1768 {
|
xue@1
|
1769 memmove(&data[1], data, sizeof(double)*(Count-1));
|
xue@1
|
1770 memmove(&indices[1], indices, sizeof(int)*(Count-1));
|
xue@1
|
1771 data[0]=value, indices[0]=index;
|
xue@1
|
1772 return 0;
|
xue@1
|
1773 }
|
xue@1
|
1774
|
xue@1
|
1775 //now Count>=2
|
xue@1
|
1776 int head=0, end=Count-1, mid;
|
xue@1
|
1777
|
xue@1
|
1778 //D(head)>=value>D(end)
|
xue@1
|
1779 while (end-head>1)
|
xue@1
|
1780 {
|
xue@1
|
1781 mid=(head+end)/2;
|
xue@1
|
1782 if (value>=data[mid]) head=mid;
|
xue@1
|
1783 else end=mid;
|
xue@1
|
1784 }
|
xue@1
|
1785
|
xue@1
|
1786 //D(head=end-1)>=value>D(end)
|
xue@1
|
1787 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1788 memmove(&indices[end+1], &indices[end], sizeof(int)*(Count-end-1));
|
xue@1
|
1789 data[end]=value, indices[end]=index;
|
xue@1
|
1790 return end;
|
xue@1
|
1791 }//InsertInc
|
xue@1
|
1792 //version where indices[] is double-precision floating point.
|
xue@1
|
1793 int InsertInc(double value, double index, double* data, double* indices, int Count)
|
xue@1
|
1794 {
|
xue@1
|
1795 if (Count<=0) return -1;
|
xue@1
|
1796 if (value>=data[Count-1]) return -1;
|
xue@1
|
1797 if (value<data[0])
|
xue@1
|
1798 {
|
xue@1
|
1799 memmove(&data[1], data, sizeof(double)*(Count-1));
|
xue@1
|
1800 memmove(&indices[1], indices, sizeof(double)*(Count-1));
|
xue@1
|
1801 data[0]=value, indices[0]=index;
|
xue@1
|
1802 return 0;
|
xue@1
|
1803 }
|
xue@1
|
1804
|
xue@1
|
1805 //now Count>=2
|
xue@1
|
1806 int head=0, end=Count-1, mid;
|
xue@1
|
1807
|
xue@1
|
1808 //D(head)>=value>D(end)
|
xue@1
|
1809 while (end-head>1)
|
xue@1
|
1810 {
|
xue@1
|
1811 mid=(head+end)/2;
|
xue@1
|
1812 if (value>=data[mid]) head=mid;
|
xue@1
|
1813 else end=mid;
|
xue@1
|
1814 }
|
xue@1
|
1815
|
xue@1
|
1816 //D(head=end-1)>=value>D(end)
|
xue@1
|
1817 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1818 memmove(&indices[end+1], &indices[end], sizeof(double)*(Count-end-1));
|
xue@1
|
1819 data[end]=value, indices[end]=index;
|
xue@1
|
1820 return end;
|
xue@1
|
1821 }//InsertInc
|
xue@1
|
1822
|
xue@1
|
1823 /*
|
xue@1
|
1824 function InsertIncApp: inserts value into flexible-length sorted increasing list
|
xue@1
|
1825
|
xue@1
|
1826 In: data[Count]: a sorted increasing list.
|
xue@1
|
1827 value: the value to be added
|
xue@1
|
1828 Out: data[Count+1]: the list with $value inserted.
|
xue@1
|
1829
|
xue@1
|
1830 Returns the index where $value is located in data[], or -1 if Count<0. data[] must have Count+1
|
xue@1
|
1831 storage units on calling.
|
xue@1
|
1832 */
|
xue@1
|
1833 int InsertIncApp(double value, double* data, int Count)
|
xue@1
|
1834 {
|
xue@1
|
1835 if (Count<0) return -1;
|
xue@1
|
1836 if (Count==0){data[0]=value; return 0;}
|
xue@1
|
1837 if (value>=data[Count-1]){data[Count]=value; return Count;}
|
xue@1
|
1838 if (value<data[0])
|
xue@1
|
1839 {
|
xue@1
|
1840 memmove(&data[1], &data[0], sizeof(double)*Count);
|
xue@1
|
1841 data[0]=value;
|
xue@1
|
1842 return 0;
|
xue@1
|
1843 }
|
xue@1
|
1844
|
xue@1
|
1845 //now Count>=2
|
xue@1
|
1846 int head=0, end=Count-1, mid;
|
xue@1
|
1847
|
xue@1
|
1848 //D(head)<=value<D(end)
|
xue@1
|
1849 while (end-head>1)
|
xue@1
|
1850 {
|
xue@1
|
1851 mid=(head+end)/2;
|
xue@1
|
1852 if (value>=data[mid]) head=mid;
|
xue@1
|
1853 else end=mid;
|
xue@1
|
1854 }
|
xue@1
|
1855
|
xue@1
|
1856 //D(head=end-1)<=value<D(end)
|
xue@1
|
1857 memmove(&data[end+1], &data[end], sizeof(double)*(Count-end));
|
xue@1
|
1858 data[end]=value;
|
xue@1
|
1859
|
xue@1
|
1860 return end;
|
xue@1
|
1861 }//InsertIncApp
|
xue@1
|
1862
|
xue@1
|
1863 //---------------------------------------------------------------------------
|
xue@1
|
1864 /*
|
xue@1
|
1865 function InstantFreq; calculates instantaneous frequency from spectrum, evaluated at bin k
|
xue@1
|
1866
|
xue@1
|
1867 In: x[hwid]: spectrum with scale 2hwid
|
xue@1
|
1868 k: reference frequency, in bins
|
xue@1
|
1869 mode: must be 1.
|
xue@1
|
1870
|
xue@1
|
1871 Returns an instantaneous frequency near bin k.
|
xue@1
|
1872 */
|
xue@1
|
1873 double InstantFreq(int k, int hwid, cdouble* x, int mode)
|
xue@1
|
1874 {
|
xue@1
|
1875 double result;
|
xue@1
|
1876 switch(mode)
|
xue@1
|
1877 {
|
xue@1
|
1878 //mode 1: the phase vocoder method, based on J. Brown, where the spectrogram
|
xue@1
|
1879 // MUST be calculated using rectangular window
|
xue@1
|
1880 case 1:
|
xue@1
|
1881 {
|
xue@1
|
1882 if (k<1) k=1;
|
xue@1
|
1883 if (k>hwid-2) k=hwid-2;
|
xue@1
|
1884 double hr=0.5*(x[k].x-0.5*(x[k+1].x+x[k-1].x)), hi=0.5*(x[k].y-0.5*(x[k+1].y+x[k-1].y));
|
xue@1
|
1885 double ph0=Atan2(hi, hr);
|
xue@1
|
1886 double c=cos(M_PI/hwid), s=sin(M_PI/hwid);
|
xue@1
|
1887 hr=0.5*(x[k].x-0.5*(x[k+1].x*c-x[k+1].y*s+x[k-1].x*c+x[k-1].y*s));
|
xue@1
|
1888 hi=0.5*(x[k].y-0.5*(x[k+1].y*c+x[k+1].x*s+x[k-1].y*c-x[k-1].x*s));
|
xue@1
|
1889 double ph1=Atan2(hi, hr);
|
xue@1
|
1890 result=(ph1-ph0)/(2*M_PI);
|
xue@1
|
1891 if (result<-0.5) result+=1;
|
xue@1
|
1892 if (result>0.5) result-=1;
|
xue@1
|
1893 result+=k*0.5/hwid;
|
xue@1
|
1894 break;
|
xue@1
|
1895 }
|
xue@1
|
1896 case 2:
|
xue@1
|
1897 break;
|
xue@1
|
1898 }
|
xue@1
|
1899 return result;
|
xue@1
|
1900 }//InstantFreq
|
xue@1
|
1901
|
xue@1
|
1902 /*
|
xue@1
|
1903 function InstantFreq; calculates "frequency spectrum", a sequence of frequencies evaluated at DFT bins
|
xue@1
|
1904
|
xue@1
|
1905 In: x[hwid]: spectrum with scale 2hwid
|
xue@1
|
1906 mode: must be 1.
|
xue@1
|
1907 Out: freqspec[hwid]: the frequency spectrum
|
xue@1
|
1908
|
xue@1
|
1909 No return value.
|
xue@1
|
1910 */
|
xue@1
|
1911 void InstantFreq(double* freqspec, int hwid, cdouble* x, int mode)
|
xue@1
|
1912 {
|
xue@1
|
1913 for (int i=0; i<hwid; i++)
|
xue@1
|
1914 freqspec[i]=InstantFreq(i, hwid, x, mode);
|
xue@1
|
1915 }//InstantFreq
|
xue@1
|
1916
|
xue@1
|
1917 //---------------------------------------------------------------------------
|
xue@1
|
1918 /*
|
xue@1
|
1919 function IntToDouble: copy content of integer array to double array
|
xue@1
|
1920
|
xue@1
|
1921 In: in: pointer to integer array
|
xue@1
|
1922 BytesPerSample: number of bytes each integer takes
|
xue@1
|
1923 Count: size of integer array, in integers
|
xue@1
|
1924 Out: vector out[Count].
|
xue@1
|
1925
|
xue@1
|
1926 No return value.
|
xue@1
|
1927
|
xue@1
|
1928 This version is currently commented out in favour of the version implemented in QuickSpec.cpp which
|
xue@1
|
1929 supports 24-bit integers.
|
xue@1
|
1930 *//*
|
xue@1
|
1931 void IntToDouble(double* out, void* in, int BytesPerSample, int Count)
|
xue@1
|
1932 {
|
xue@1
|
1933 if (BytesPerSample==1){unsigned char* in8=(unsigned char*)in; for (int k=0; k<Count; k++) *(out++)=*(in8++)-128.0;}
|
xue@1
|
1934 else {__int16* in16=(__int16*)in; for (int k=0; k<Count; k++) *(out++)=*(in16++);}
|
xue@1
|
1935 }//IntToDouble*/
|
xue@1
|
1936
|
xue@1
|
1937 //---------------------------------------------------------------------------
|
xue@1
|
1938 /*
|
xue@1
|
1939 function IPHannC: inner product with Hann window spectrum
|
xue@1
|
1940
|
xue@1
|
1941 In: x[N]: spectrum
|
xue@1
|
1942 f: reference frequency
|
xue@1
|
1943 K1, K2: spectral truncation bounds
|
xue@1
|
1944
|
xue@1
|
1945 Returns the absolute value of the inner product of x[K1:K2] with the corresponding band of the
|
xue@1
|
1946 spectrum of a sinusoid at frequency f.
|
xue@1
|
1947 */
|
xue@1
|
1948 double IPHannC(double f, cdouble* x, int N, int K1, int K2)
|
xue@1
|
1949 {
|
xue@1
|
1950 int M; double c[4], iH2;
|
xue@1
|
1951 windowspec(wtHann, N, &M, c, &iH2);
|
xue@1
|
1952 return abs(IPWindowC(f, x, N, M, c, iH2, K1, K2));
|
xue@1
|
1953 }//IPHannC
|
xue@1
|
1954
|
xue@1
|
1955
|
xue@1
|
1956 //---------------------------------------------------------------------------
|
xue@1
|
1957 /*
|
xue@1
|
1958 function lse: linear regression y=ax+b
|
xue@1
|
1959
|
xue@1
|
1960 In: x[Count], y[Count]: input points
|
xue@1
|
1961 Out: a, b: LSE estimation of coefficients in y=ax+b
|
xue@1
|
1962
|
xue@1
|
1963 No return value.
|
xue@1
|
1964 */
|
xue@1
|
1965 void lse(double* x, double* y, int Count, double& a, double& b)
|
xue@1
|
1966 {
|
xue@1
|
1967 double sx=0, sy=0, sxx=0, sxy=0;
|
xue@1
|
1968 for (int i=0; i<Count; i++)
|
xue@1
|
1969 {
|
xue@1
|
1970 sx+=x[i];
|
xue@1
|
1971 sy+=y[i];
|
xue@1
|
1972 sxx+=x[i]*x[i];
|
xue@1
|
1973 sxy+=x[i]*y[i];
|
xue@1
|
1974 }
|
xue@1
|
1975 b=(sxx*sy-sx*sxy)/(Count*sxx-sx*sx);
|
xue@1
|
1976 a=(sy-Count*b)/sx;
|
xue@1
|
1977 }//lse
|
xue@1
|
1978
|
xue@1
|
1979 //--------------------------------------------------------------------------
|
xue@1
|
1980 /*
|
xue@1
|
1981 memdoubleadd: vector addition
|
xue@1
|
1982
|
xue@1
|
1983 In: dest[count], source[count]: addends
|
xue@1
|
1984 Out: dest[count]: sum
|
xue@1
|
1985
|
xue@1
|
1986 No return value.
|
xue@1
|
1987 */
|
xue@1
|
1988 void memdoubleadd(double* dest, double* source, int count)
|
xue@1
|
1989 {
|
xue@1
|
1990 for (int i=0; i<count; i++){*dest=*dest+*source; dest++; source++;}
|
xue@1
|
1991 }//memdoubleadd
|
xue@1
|
1992
|
xue@1
|
1993 //--------------------------------------------------------------------------
|
xue@1
|
1994 /*
|
xue@1
|
1995 function Mel: converts frequency in Hz to frequency in mel.
|
xue@1
|
1996
|
xue@1
|
1997 In: f: frequency, in Hz
|
xue@1
|
1998
|
xue@1
|
1999 Returns the frequency measured on mel scale.
|
xue@1
|
2000 */
|
xue@1
|
2001 double Mel(double f)
|
xue@1
|
2002 {
|
xue@1
|
2003 return 1127.01048*log(1+f/700);
|
xue@1
|
2004 }//Mel
|
xue@1
|
2005
|
xue@1
|
2006 /*
|
xue@1
|
2007 function InvMel: converts frequency in mel to frequency in Hz.
|
xue@1
|
2008
|
xue@1
|
2009 In: f: frequency, in mel.
|
xue@1
|
2010
|
xue@1
|
2011 Returns the frequency in Hz.
|
xue@1
|
2012 */
|
xue@1
|
2013 double InvMel(double mel)
|
xue@1
|
2014 {
|
xue@1
|
2015 return 700*(exp(mel/1127.01048)-1);
|
xue@1
|
2016 }//InvMel
|
xue@1
|
2017
|
xue@1
|
2018 /*
|
xue@1
|
2019 function MFCC: calculates MFCC.
|
xue@1
|
2020
|
xue@1
|
2021 In: Data[FrameWidth]: data
|
xue@1
|
2022 NumBands: number of frequency bands on mel scale
|
xue@1
|
2023 Bands[3*NumBands]: mel frequency bands, comes as $NumBands triples, each containing the lower,
|
xue@1
|
2024 middle and high frequencies, in bins, of one band, from which a weighting window is created to
|
xue@1
|
2025 weight individual bins.
|
xue@1
|
2026 Ceps_Order: number of MFC coefficients (i.e. DCT coefficients)
|
xue@1
|
2027 W, X: FFT buffers
|
xue@1
|
2028 Out: C[Ceps_Order]: MFCC
|
xue@1
|
2029 Amps[NumBands]: log spectrum on MF bands
|
xue@1
|
2030
|
xue@1
|
2031 No return value. Use MFCCPrepareBands() to retrieve Bands[].
|
xue@1
|
2032 */
|
xue@1
|
2033 void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double* Data, double* Bands, double* C, double* Amps, cdouble* W, cdouble* X)
|
xue@1
|
2034 {
|
xue@1
|
2035 double tmp, b2s, b2c, b2e;
|
xue@1
|
2036
|
xue@1
|
2037 RFFTC(Data, 0, 0, log2(FrameWidth), W, X, 0);
|
xue@1
|
2038 for (int i=0; i<=FrameWidth/2; i++) Amps[i]=X[i].x*X[i].x+X[i].y*X[i].y;
|
xue@1
|
2039
|
xue@1
|
2040 for (int i=0; i<NumBands; i++)
|
xue@1
|
2041 {
|
xue@1
|
2042 tmp=0;
|
xue@1
|
2043 b2s=Bands[3*i], b2c=Bands[3*i+1], b2e=Bands[3*i+2];
|
xue@1
|
2044
|
xue@1
|
2045 for (int j=ceil(b2s); j<ceil(b2c); j++)
|
xue@1
|
2046 tmp+=Amps[j]*(j-b2s)/(b2c-b2s);
|
xue@1
|
2047 for (int j=ceil(b2c); j<b2e; j++)
|
xue@1
|
2048 tmp+=Amps[j]*(b2e-j)/(b2e-b2c);
|
xue@1
|
2049
|
xue@1
|
2050 if (tmp<3.7200759760208359629596958038631e-44)
|
xue@1
|
2051 Amps[i]=-100;
|
xue@1
|
2052 else
|
xue@1
|
2053 Amps[i]=log(tmp);
|
xue@1
|
2054 }
|
xue@1
|
2055
|
xue@1
|
2056 for (int i=0; i<Ceps_Order; i++)
|
xue@1
|
2057 {
|
xue@1
|
2058 tmp=Amps[0]*cos(M_PI*(i+1)/2/NumBands);
|
xue@1
|
2059 for (int j=1; j<NumBands; j++)
|
xue@1
|
2060 tmp+=Amps[j]*cos(M_PI*(i+0.5)*(j+0.5)/NumBands);
|
xue@1
|
2061 C[i]=tmp;
|
xue@1
|
2062 }
|
xue@1
|
2063 }//MFCC
|
xue@1
|
2064
|
xue@1
|
2065 /*
|
xue@1
|
2066 function MFCCPrepareBands: returns a array of OVERLAPPING bands given in triples, whose 1st and 3rd
|
xue@1
|
2067 entries are the start and end of a band, in bins, and the 2nd is a middle frequency.
|
xue@1
|
2068
|
xue@1
|
2069 In: SamplesPerSec: sampling rate
|
xue@1
|
2070 NumberOfBins: FFT size
|
xue@1
|
2071 NumberOfBands: number of mel-frequency bands
|
xue@1
|
2072
|
xue@1
|
2073 Returns pointer to the array of triples.
|
xue@1
|
2074 */
|
xue@1
|
2075 double* MFCCPrepareBands(int NumberOfBands, int SamplesPerSec, int NumberOfBins)
|
xue@1
|
2076 {
|
xue@1
|
2077 double* Bands=new double[NumberOfBands*3];
|
xue@1
|
2078 double naqfreq=SamplesPerSec/2.0; //naqvist freq
|
xue@1
|
2079 double binwid=SamplesPerSec*1.0/NumberOfBins;
|
xue@1
|
2080 double naqmel=Mel(naqfreq);
|
xue@1
|
2081 double b=naqmel/(NumberOfBands+1);
|
xue@1
|
2082
|
xue@1
|
2083 Bands[0]=0;
|
xue@1
|
2084 Bands[1]=InvMel(b)/binwid;
|
xue@1
|
2085 Bands[2]=InvMel(b*2)/binwid;
|
xue@1
|
2086 for (int i=1; i<NumberOfBands; i++)
|
xue@1
|
2087 {
|
xue@1
|
2088 Bands[3*i]=Bands[3*i-2];
|
xue@1
|
2089 Bands[3*i+1]=Bands[3*i-1];
|
xue@1
|
2090 Bands[3*i+2]=InvMel(b*(i+2))/binwid;
|
xue@1
|
2091 }
|
xue@1
|
2092 return Bands;
|
xue@1
|
2093 }//MFCCPrepareBands
|
xue@1
|
2094
|
xue@1
|
2095 //---------------------------------------------------------------------------
|
xue@1
|
2096 /*
|
xue@1
|
2097 function Multi: vector-constant multiplication
|
xue@1
|
2098
|
xue@1
|
2099 In: data[count]: a vector
|
xue@1
|
2100 mul: a constant
|
xue@1
|
2101 Out: data[count]: their product
|
xue@1
|
2102
|
xue@1
|
2103 No return value.
|
xue@1
|
2104 */
|
xue@1
|
2105 void Multi(double* data, int count, double mul)
|
xue@1
|
2106 {
|
xue@1
|
2107 for (int i=0; i<count; i++){*data=*data*mul; data++;}
|
xue@1
|
2108 }//Multi
|
xue@1
|
2109
|
xue@1
|
2110 /*
|
xue@1
|
2111 function Multi: vector-constant multiplication
|
xue@1
|
2112
|
xue@1
|
2113 In: in[count]: a vector
|
xue@1
|
2114 mul: a constant
|
xue@1
|
2115 Out: out[count]: their product
|
xue@1
|
2116
|
xue@1
|
2117 No return value.
|
xue@1
|
2118 */
|
xue@1
|
2119 void Multi(double* out, double* in, int count, double mul)
|
xue@1
|
2120 {
|
xue@1
|
2121 for (int i=0; i<count; i++) *(out++)=*(in++)*mul;
|
xue@1
|
2122 }//Multi
|
xue@1
|
2123
|
xue@1
|
2124 /*
|
xue@1
|
2125 function Multi: vector-constant multiply-addition
|
xue@1
|
2126
|
xue@1
|
2127 In: in[count], adder[count]: vectors
|
xue@1
|
2128 mul: a constant
|
xue@1
|
2129 Out: out[count]: in[]+adder[]*mul
|
xue@1
|
2130
|
xue@1
|
2131 No return value.
|
xue@1
|
2132 */
|
xue@1
|
2133 void MultiAdd(double* out, double* in, double* adder, int count, double mul)
|
xue@1
|
2134 {
|
xue@1
|
2135 for (int i=0; i<count; i++) *(out++)=*(in++)+*(adder++)*mul;
|
xue@1
|
2136 }//MultiAdd
|
xue@1
|
2137
|
xue@1
|
2138 //---------------------------------------------------------------------------
|
xue@1
|
2139 /*
|
xue@1
|
2140 function NearestPeak: finds a peak value in an array that is nearest to a given index
|
xue@1
|
2141
|
xue@1
|
2142 In: data[count]: an array
|
xue@1
|
2143 anindex: an index
|
xue@1
|
2144
|
xue@1
|
2145 Returns the index to a peak of data[] that is closest to anindex. In case of two cloest indices,
|
xue@1
|
2146 returns the index to the higher peak of the two.
|
xue@1
|
2147 */
|
xue@1
|
2148 int NearestPeak(double* data, int count, int anindex)
|
xue@1
|
2149 {
|
xue@1
|
2150 int upind=anindex, downind=anindex;
|
xue@1
|
2151 if (anindex<1) anindex=1;
|
xue@1
|
2152 if (anindex>count-2) anindex=count-2;
|
xue@1
|
2153
|
xue@1
|
2154 if (data[anindex]>data[anindex-1] && data[anindex]>data[anindex+1]) return anindex;
|
xue@1
|
2155
|
xue@1
|
2156 if (data[anindex]<data[anindex-1])
|
xue@1
|
2157 while (downind>0 && data[downind-1]>data[downind]) downind--;
|
xue@1
|
2158 if (data[anindex]<data[anindex+1])
|
xue@1
|
2159 while (upind<count-1 && data[upind]<data[upind+1]) upind++;
|
xue@1
|
2160
|
xue@1
|
2161 if (upind==anindex) return downind;
|
xue@1
|
2162 if (downind==anindex) return upind;
|
xue@1
|
2163
|
xue@1
|
2164 if (anindex-downind<upind-anindex) return downind;
|
xue@1
|
2165 else if (anindex-downind>upind-anindex) return upind;
|
xue@1
|
2166 else if (data[upind]<data[downind]) return downind;
|
xue@1
|
2167 else return upind;
|
xue@1
|
2168 }//NearestPeak
|
xue@1
|
2169
|
xue@1
|
2170 //---------------------------------------------------------------------------
|
xue@1
|
2171 /*
|
xue@1
|
2172 function NegativeExp: fits the curve y=1-x^lmd.
|
xue@1
|
2173
|
xue@1
|
2174 In: x[Count], y[Count]: sample points to fit, x[0]=0, x[Count-1]=1, y[0]=1, y[Count-1]=0
|
xue@1
|
2175 Out: lmd: coefficient of y=1-x^lmd.
|
xue@1
|
2176
|
xue@1
|
2177 Returns rms fitting error.
|
xue@1
|
2178 */
|
xue@1
|
2179 double NegativeExp(double* x, double* y, int Count, double& lmd, int sample, double step, double eps, int maxiter)
|
xue@1
|
2180 {
|
xue@1
|
2181 lmd=0;
|
xue@1
|
2182 for (int i=1; i<Count-1; i++)
|
xue@1
|
2183 {
|
xue@1
|
2184 if (y[i]<1)
|
xue@1
|
2185 lmd+=log(1-y[i])/log(x[i]);
|
xue@1
|
2186 else
|
xue@1
|
2187 lmd+=-50/log(x[i]);
|
xue@1
|
2188 }
|
xue@1
|
2189 lmd/=(Count-2);
|
xue@1
|
2190
|
xue@1
|
2191 //lmd has been initialized
|
xue@1
|
2192 //coming up will be the recursive calculation of lmd by lgg
|
xue@1
|
2193
|
xue@1
|
2194 int iter=0;
|
xue@1
|
2195 double laste, lastdel, e=0, del=0, tmp;
|
xue@1
|
2196 do
|
xue@1
|
2197 {
|
xue@1
|
2198 iter++;
|
xue@1
|
2199 laste=e;
|
xue@1
|
2200 lastdel=del;
|
xue@1
|
2201 e=0, del=0;
|
xue@1
|
2202 for (int i=1; i<Count-1; i+=sample)
|
xue@1
|
2203 {
|
xue@1
|
2204 tmp=pow(x[i], lmd);
|
xue@1
|
2205 e=e+(y[i]+tmp-1)*(y[i]+tmp-1);
|
xue@1
|
2206 del=del+(y[i]+tmp-1)*tmp*log(x[i]);
|
xue@1
|
2207 }
|
xue@1
|
2208 if (laste && e>laste) lmd+=step*lastdel, step/=2;
|
xue@1
|
2209 else lmd+=-step*sample*del;
|
xue@1
|
2210 }
|
xue@1
|
2211 while (e && iter<=maxiter && (!laste || fabs(laste-e)/e>eps));
|
xue@1
|
2212 return sqrt(e/Count);
|
xue@1
|
2213 }//NegativeExp
|
xue@1
|
2214
|
xue@1
|
2215 //---------------------------------------------------------------------------
|
xue@1
|
2216 /*
|
xue@1
|
2217 function: NL: noise level, calculated on 5% of total frames with least energy
|
xue@1
|
2218
|
xue@1
|
2219 In: data[Count]:
|
xue@1
|
2220 Wid: window width for power level estimation
|
xue@1
|
2221
|
xue@1
|
2222 Returns noise level, in rms.
|
xue@1
|
2223 */
|
xue@1
|
2224 double NL(double* data, int Count, int Wid)
|
xue@1
|
2225 {
|
xue@1
|
2226 int Fr=Count/Wid;
|
xue@1
|
2227 int Num=Fr/20+1;
|
xue@1
|
2228 double* ene=new double[Num], tmp;
|
xue@1
|
2229 for (int i=0; i<Num; i++) ene[i]=1e30;
|
xue@1
|
2230 for (int i=0; i<Fr; i++)
|
xue@1
|
2231 {
|
xue@1
|
2232 tmp=DCPower(&data[i*Wid], Wid, 0);
|
xue@1
|
2233 InsertInc(tmp, ene, Num);
|
xue@1
|
2234 }
|
xue@1
|
2235 double result=Avg(ene, Num, 0);
|
xue@1
|
2236 delete[] ene;
|
xue@1
|
2237 result=sqrt(result);
|
xue@1
|
2238 return result;
|
xue@1
|
2239 }//NL
|
xue@1
|
2240
|
xue@1
|
2241 //---------------------------------------------------------------------------
|
xue@1
|
2242 /*
|
xue@1
|
2243 function Normalize: normalizes data to [-Maxi, Maxi], without zero shift
|
xue@1
|
2244
|
xue@1
|
2245 In: data[Count]: data to be normalized
|
xue@1
|
2246 Maxi: destination maximal absolute value
|
xue@1
|
2247 Out: data[Count]: normalized data
|
xue@1
|
2248
|
xue@1
|
2249 Returns the original maximal absolute value.
|
xue@1
|
2250 */
|
xue@1
|
2251 double Normalize(double* data, int Count, double Maxi)
|
xue@1
|
2252 {
|
xue@1
|
2253 double max=0;
|
xue@1
|
2254 double* ldata=data;
|
xue@1
|
2255 for (int i=0; i<Count; i++)
|
xue@1
|
2256 {
|
xue@1
|
2257 if (*ldata>max) max=*ldata;
|
xue@1
|
2258 else if (-*ldata>max) max=-*ldata;
|
xue@1
|
2259 ldata++;
|
xue@1
|
2260 }
|
xue@1
|
2261 if (max>0)
|
xue@1
|
2262 {
|
xue@1
|
2263 Maxi=Maxi/max;
|
xue@1
|
2264 for (int i=0; i<Count; i++) *(data++)*=Maxi;
|
xue@1
|
2265 }
|
xue@1
|
2266 return max;
|
xue@1
|
2267 }//Normalize
|
xue@1
|
2268
|
xue@1
|
2269 /*
|
xue@1
|
2270 function Normalize2: normalizes data to a specified Euclidian norm
|
xue@1
|
2271
|
xue@1
|
2272 In: data[Count]: data to normalize
|
xue@1
|
2273 Norm: destination Euclidian norm
|
xue@1
|
2274 Out: data[Count]: normalized data.
|
xue@1
|
2275
|
xue@1
|
2276 Returns the original Euclidian norm.
|
xue@1
|
2277 */
|
xue@1
|
2278 double Normalize2(double* data, int Count, double Norm)
|
xue@1
|
2279 {
|
xue@1
|
2280 double norm=0;
|
xue@1
|
2281 for (int i=0; i<Count; i++) norm+=data[i]*data[i];
|
xue@1
|
2282 norm=sqrt(norm);
|
xue@1
|
2283 double mul=norm/Norm;
|
xue@1
|
2284 if (mul!=0) for (int i=0; i<Count; i++) data[i]/=mul;
|
xue@1
|
2285 return norm;
|
xue@1
|
2286 }//Normalize2
|
xue@1
|
2287
|
xue@1
|
2288 //---------------------------------------------------------------------------
|
xue@1
|
2289 /*
|
xue@1
|
2290 function PhaseSpan: computes the unwrapped phase variation across the Nyquist range
|
xue@1
|
2291
|
xue@1
|
2292 In: data[Count]: time-domain data
|
xue@1
|
2293 aparams: a fftparams structure
|
xue@1
|
2294
|
xue@1
|
2295 Returns the difference between unwrapped phase angles at 0 and Nyquist frequency.
|
xue@1
|
2296 */
|
xue@1
|
2297 double PhaseSpan(double* data, int Count, void* aparams)
|
xue@1
|
2298 {
|
xue@1
|
2299 int Pad=1;
|
xue@1
|
2300 fftparams* params=(fftparams*)aparams;
|
xue@1
|
2301 double* Arg=new double[Count*Pad];
|
xue@1
|
2302 AllocateFFTBuffer(Count*Pad, Amp, w, x);
|
xue@1
|
2303 memset(Amp, 0, sizeof(double)*Count*Pad);
|
xue@1
|
2304 memcpy(&Amp[Count*(Pad-1)/2], data, sizeof(double)*Count);
|
xue@1
|
2305 ApplyWindow(Amp, Amp, params->Amp, Count);
|
xue@1
|
2306 RFFTC(Amp, Amp, Arg, log2(Count*Pad), w, x, 0);
|
xue@1
|
2307
|
xue@1
|
2308 SmoothPhase(Arg, Count*Pad/2+1);
|
xue@1
|
2309 double result=Arg[Count*Pad/2]-Arg[0];
|
xue@1
|
2310 delete[] Arg;
|
xue@1
|
2311 FreeFFTBuffer(Amp);
|
xue@1
|
2312 return result;
|
xue@1
|
2313 }//PhaseSpan
|
xue@1
|
2314
|
xue@1
|
2315 //---------------------------------------------------------------------------
|
xue@1
|
2316 /*
|
xue@1
|
2317 function PolyFit: least square polynomial fitting y=sum(i){a[i]*x^i}
|
xue@1
|
2318
|
xue@1
|
2319 In: x[N], y[N]: sample points
|
xue@1
|
2320 P: order of polynomial, P<N
|
xue@1
|
2321 Out: a[P+1]: coefficients of polynomial
|
xue@1
|
2322
|
xue@1
|
2323 No return value.
|
xue@1
|
2324 */
|
xue@1
|
2325 void PolyFit(int P, double* a, int N, double* x, double* y)
|
xue@1
|
2326 {
|
xue@1
|
2327 Alloc2(P+1, P+1, aa);
|
xue@1
|
2328 double ai0, bi, *bb=new double[P+1], *s=new double[N], *r=new double[N];
|
xue@1
|
2329 aa[0][0]=N; bi=0; for (int i=0; i<N; i++) s[i]=1, r[i]=y[i], bi+=y[i]; bb[0]=bi;
|
xue@1
|
2330
|
xue@1
|
2331 for (int i=1; i<=P; i++)
|
xue@1
|
2332 {
|
xue@1
|
2333 ai0=bi=0; for (int j=0; j<N; j++) {s[j]*=x[j], r[j]*=x[j]; ai0+=s[j], bi+=r[j];}
|
xue@1
|
2334 for (int j=0; j<=i; j++) aa[i-j][j]=ai0; bb[i]=bi;
|
xue@1
|
2335 }
|
xue@1
|
2336 for (int i=P+1; i<=2*P; i++)
|
xue@1
|
2337 {
|
xue@1
|
2338 ai0=0; for (int j=0; j<N; j++) {s[j]*=x[j]; ai0+=s[j];}
|
xue@1
|
2339 for (int j=i-P; j<=P; j++) aa[i-j][j]=ai0;
|
xue@1
|
2340 }
|
xue@1
|
2341 GESCP(P+1, a, aa, bb);
|
xue@1
|
2342 DeAlloc2(aa); delete[] bb; delete[] s; delete[] r;
|
xue@1
|
2343 }//PolyFit
|
xue@1
|
2344
|
xue@1
|
2345 //---------------------------------------------------------------------------
|
xue@1
|
2346 /*
|
xue@1
|
2347 function Pow: vector power function
|
xue@1
|
2348
|
xue@1
|
2349 In: data[Count]: a vector
|
xue@1
|
2350 ex: expontnet
|
xue@1
|
2351 Out: data[Count]: point-wise $ex-th power of data[]
|
xue@1
|
2352
|
xue@1
|
2353 No return value.
|
xue@1
|
2354 */
|
xue@1
|
2355 void Pow(double* data, int Count, double ex)
|
xue@1
|
2356 {
|
xue@1
|
2357 for (int i=0; i<Count; i++)
|
xue@1
|
2358 data[i]=pow(data[i], ex);
|
xue@1
|
2359 }//Power
|
xue@1
|
2360
|
xue@1
|
2361 //---------------------------------------------------------------------------
|
xue@1
|
2362 /*
|
xue@1
|
2363 Rectify: semi-wave rectification
|
xue@1
|
2364
|
xue@1
|
2365 In: data[Count]: data to rectify
|
xue@1
|
2366 th: rectification threshold: values below th are rectified to th
|
xue@1
|
2367 Out: data[Count]: recitified data
|
xue@1
|
2368
|
xue@1
|
2369 Returns number of preserved (i.e. not rectified) samples.
|
xue@1
|
2370 */
|
xue@1
|
2371 int Rectify(double* data, int Count, double th)
|
xue@1
|
2372 {
|
xue@1
|
2373 int Result=0;
|
xue@1
|
2374 for (int i=0; i<Count; i++)
|
xue@1
|
2375 {
|
xue@1
|
2376 if (data[i]<=th) data[i]=th;
|
xue@1
|
2377 else Result++;
|
xue@1
|
2378 }
|
xue@1
|
2379 return Result;
|
xue@1
|
2380 }//Rectify
|
xue@1
|
2381
|
xue@1
|
2382 //---------------------------------------------------------------------------
|
xue@1
|
2383 /*
|
xue@1
|
2384 function Res: minimum absolute residue.
|
xue@1
|
2385
|
xue@1
|
2386 In: in: a number
|
xue@1
|
2387 mod: modulus
|
xue@1
|
2388
|
xue@1
|
2389 Returns the minimal absolute residue of $in devided by $mod.
|
xue@1
|
2390 */
|
xue@1
|
2391 double Res(double in, double mod)
|
xue@1
|
2392 {
|
xue@1
|
2393 int i=in/mod;
|
xue@1
|
2394 in=in-i*mod;
|
xue@1
|
2395 if (in>mod/2) in-=mod;
|
xue@1
|
2396 if (in<-mod/2) in+=mod;
|
xue@1
|
2397 return in;
|
xue@1
|
2398 }//Res
|
xue@1
|
2399
|
xue@1
|
2400 //---------------------------------------------------------------------------
|
xue@1
|
2401 /*
|
xue@1
|
2402 function Romberg: Romberg algorithm for numerical integration
|
xue@1
|
2403
|
xue@1
|
2404 In: f: function to integrate
|
xue@1
|
2405 params: extra argument for calling f
|
xue@1
|
2406 a, b: integration boundaries
|
xue@1
|
2407 n: depth of sampling
|
xue@1
|
2408
|
xue@1
|
2409 Returns the integral of f(*, params) over [a, b].
|
xue@1
|
2410 */
|
xue@1
|
2411 double Romberg(int n, double(*f)(double, void*), double a, double b, void* params)
|
xue@1
|
2412 {
|
xue@1
|
2413 int np=1;
|
xue@1
|
2414 double* r1=new double[n+1];.
|
xue@1
|
2415 double* r2=new double[n+1];
|
xue@1
|
2416 double h=b-a, *swp;
|
xue@1
|
2417 r1[1]=h*(f(a, params)+f(b, params))/2;
|
xue@1
|
2418 for (int i=2; i<=n; i++)
|
xue@1
|
2419 {
|
xue@1
|
2420 double akh=a+0.5*h; r2[1]=f(akh, params);
|
xue@1
|
2421 for (int k=2; k<=np; k++) {akh+=h; r2[1]+=f(akh, params);} //akh=a+(k-0.5)h
|
xue@1
|
2422 r2[1]=0.5*(r1[1]+h*r2[1]);
|
xue@1
|
2423 double fj=4;
|
xue@1
|
2424 for (int j=2; j<=i; j++) {r2[j]=(fj*r2[j-1]-r1[j-1])/(fj-1); fj*=4;} //fj=4^(j-1)
|
xue@1
|
2425 h/=2; np*=2;
|
xue@1
|
2426 swp=r1; r1=r2; r2=swp;
|
xue@1
|
2427 }
|
xue@1
|
2428 h=r1[n];
|
xue@1
|
2429 delete[] r1;
|
xue@1
|
2430 delete[] r2;
|
xue@1
|
2431 return h;
|
xue@1
|
2432 }//Romberg
|
xue@1
|
2433
|
xue@1
|
2434 /*
|
xue@1
|
2435 function Romberg: Romberg algorithm for numerical integration, may return before specified depth on
|
xue@1
|
2436 convergence.
|
xue@1
|
2437
|
xue@1
|
2438 In: f: function to integrate
|
xue@1
|
2439 params: extra argument for calling f
|
xue@1
|
2440 a, b: integration boundaries
|
xue@1
|
2441 n: depth of sampling
|
xue@1
|
2442 ep: convergence test threshold
|
xue@1
|
2443
|
xue@1
|
2444 Returns the integral of f(*, params) over [a, b].
|
xue@1
|
2445 */
|
xue@1
|
2446 double Romberg(double(*f)(double, void*), double a, double b, void* params, int n, double ep)
|
xue@1
|
2447 {
|
xue@1
|
2448 int i, np=1;
|
xue@1
|
2449 double* r1=new double[n+1];
|
xue@1
|
2450 double* r2=new double[n+1];
|
xue@1
|
2451 double h=b-a, *swp;
|
xue@1
|
2452 r1[1]=h*(f(a, params)+f(b, params))/2;
|
xue@1
|
2453 bool mep=false;
|
xue@1
|
2454 for (i=2; i<=n; i++)
|
xue@1
|
2455 {
|
xue@1
|
2456 double akh=a+0.5*h; r2[1]=f(akh, params);
|
xue@1
|
2457 for (int k=2; k<=np; k++) {akh+=h; r2[1]+=f(akh, params);} //akh=a+(k-0.5)h
|
xue@1
|
2458 r2[1]=0.5*(r1[1]+h*r2[1]);
|
xue@1
|
2459 double fj=4;
|
xue@1
|
2460 for (int j=2; j<=i; j++) {r2[j]=(fj*r2[j-1]-r1[j-1])/(fj-1); fj*=4;} //fj=4^(j-1)
|
xue@1
|
2461
|
xue@1
|
2462 if (fabs(r2[i]-r1[i-1])<ep)
|
xue@1
|
2463 {
|
xue@1
|
2464 if (mep) break;
|
xue@1
|
2465 else mep=true;
|
xue@1
|
2466 }
|
xue@1
|
2467 else mep=false;
|
xue@1
|
2468
|
xue@1
|
2469 h/=2; np*=2;
|
xue@1
|
2470 swp=r1; r1=r2; r2=swp;
|
xue@1
|
2471 }
|
xue@1
|
2472 if (i<=n) h=r2[i];
|
xue@1
|
2473 else h=r1[n];
|
xue@1
|
2474 delete[] r1;
|
xue@1
|
2475 delete[] r2;
|
xue@1
|
2476 return h;
|
xue@1
|
2477 }//Romberg
|
xue@1
|
2478
|
xue@1
|
2479 //---------------------------------------------------------------------------
|
xue@1
|
2480 //analog and digital sinc functions
|
xue@1
|
2481
|
xue@1
|
2482 //sinca(0)=1, sincd(0)=N, sinca(1)=sincd(1)=0.
|
xue@1
|
2483 /*
|
xue@1
|
2484 function sinca: analog sinc function.
|
xue@1
|
2485
|
xue@1
|
2486 In: x: frequency
|
xue@1
|
2487
|
xue@1
|
2488 Returns sinc(x)=sin(pi*x)/(pi*x), sinca(0)=1, sinca(1)=0
|
xue@1
|
2489 */
|
xue@1
|
2490 double sinca(double x)
|
xue@1
|
2491 {
|
xue@1
|
2492 if (x==0) return 1;
|
xue@1
|
2493 return sin(M_PI*x)/(M_PI*x);
|
xue@1
|
2494 }//sinca
|
xue@1
|
2495
|
xue@1
|
2496 /*
|
xue@1
|
2497 function sincd_unn: unnormalized discrete sinc function
|
xue@1
|
2498
|
xue@1
|
2499 In: x: frequency
|
xue@1
|
2500 N: scale (window size, DFT size)
|
xue@1
|
2501
|
xue@1
|
2502 Returns sinc(x)=sin(pi*x)/sin(pi*x/N), sincd(0)=N, sincd(1)=0.
|
xue@1
|
2503 */
|
xue@1
|
2504 double sincd_unn(double x, int N)
|
xue@1
|
2505 {
|
xue@1
|
2506 if (x==0) return N;
|
xue@1
|
2507 return sin(M_PI*x)/sin(M_PI*x/N);
|
xue@1
|
2508 }//sincd
|
xue@1
|
2509
|
xue@1
|
2510 //---------------------------------------------------------------------------
|
xue@1
|
2511 /*
|
xue@1
|
2512 SmoothPhase: phase unwrapping on module mpi*PI, 2PI by default
|
xue@1
|
2513
|
xue@1
|
2514 In: Arg[Count]: phase angles to unwrap
|
xue@1
|
2515 mpi: unwrapping modulus, in pi's
|
xue@1
|
2516 Out: Arg[Count]: unwrapped phase
|
xue@1
|
2517
|
xue@1
|
2518 Returns the amount of unwrap, in pi's, of the last phase angle
|
xue@1
|
2519 */
|
xue@1
|
2520 double SmoothPhase(double* Arg, int Count, int mpi)
|
xue@1
|
2521 {
|
xue@1
|
2522 double m2pi=mpi*M_PI;
|
xue@1
|
2523 for (int i=1; i<Count-1; i++)
|
xue@1
|
2524 Arg[i]=Arg[i-1]+Res(Arg[i]-Arg[i-1], m2pi);
|
xue@1
|
2525 double tmp=Res(Arg[Count-1]-Arg[Count-2], m2pi);
|
xue@1
|
2526 double result=(Arg[Count-1]-Arg[Count-2]-tmp)/m2pi;
|
xue@1
|
2527 Arg[Count-1]=Arg[Count-2]+tmp;
|
xue@1
|
2528
|
xue@1
|
2529 return result;
|
xue@1
|
2530 }//SmoothPhase
|
xue@1
|
2531
|
xue@1
|
2532 //---------------------------------------------------------------------------
|
xue@1
|
2533 //the stiff string partial frequency model f[m]=mf[1]*sqrt(1+B(m*m-1)).
|
xue@1
|
2534
|
xue@1
|
2535 /*
|
xue@1
|
2536 StiffB: computes stiffness coefficient from fundamental and another partial frequency based on the
|
xue@1
|
2537 stiff string partial frequency model f[m]=mf[1]*sqrt(1+B(m*m-1)).
|
xue@1
|
2538
|
xue@1
|
2539 In: f0: fundamental frequency
|
xue@1
|
2540 m: 1-based partial index
|
xue@1
|
2541 fm: frequency of partial m
|
xue@1
|
2542
|
xue@1
|
2543 Returns stiffness coefficient B.
|
xue@1
|
2544 */
|
xue@1
|
2545 double StiffB(double f0, double fm, int m)
|
xue@1
|
2546 {
|
xue@1
|
2547 double f2=fm/m/f0;
|
xue@1
|
2548 return (f2*f2-1)/(m*m-1);
|
xue@1
|
2549 }//StiffB
|
xue@1
|
2550
|
xue@1
|
2551 //StiffF: partial frequency of a stiff string
|
xue@1
|
2552 /*
|
xue@1
|
2553 StiffFm: computes a partial frequency from fundamental frequency and partial index based on the stiff
|
xue@1
|
2554 string partial frequency model f[m]=mf[1]*sqrt(1+B(m*m-1)).
|
xue@1
|
2555
|
xue@1
|
2556 In: f0: fundamental frequency
|
xue@1
|
2557 m: 1-based partial index
|
xue@1
|
2558 B: stiffness coefficient
|
xue@1
|
2559
|
xue@1
|
2560 Returns frequency of the m-th partial.
|
xue@1
|
2561 */
|
xue@1
|
2562 double StiffFm(double f0, int m, double B)
|
xue@1
|
2563 {
|
xue@1
|
2564 return m*f0*sqrt(1+B*(m*m-1));
|
xue@1
|
2565 }//StiffFm
|
xue@1
|
2566
|
xue@1
|
2567 /*
|
xue@1
|
2568 StiffF0: computes fundamental frequency from another partial frequency and stiffness coefficient based
|
xue@1
|
2569 on the stiff string partial frequency model f[m]=mf[1]*sqrt(1+B(m*m-1)).
|
xue@1
|
2570
|
xue@1
|
2571 In: m: 1-based partial index
|
xue@1
|
2572 fm: frequency of partial m
|
xue@1
|
2573 B: stiffness coefficient
|
xue@1
|
2574
|
xue@1
|
2575 Returns the fundamental frequency.
|
xue@1
|
2576 */
|
xue@1
|
2577 double StiffF0(double fm, int m, double B)
|
xue@1
|
2578 {
|
xue@1
|
2579 return fm/m/sqrt(1+B*(m*m-1));
|
xue@1
|
2580 }//StiffF0
|
xue@1
|
2581
|
xue@1
|
2582 /*
|
xue@1
|
2583 StiffM: computes 1-based partial index from partial frequency, fundamental frequency and stiffness
|
xue@1
|
2584 coefficient based on the stiff string partial frequency model f[m]=mf[1]*sqrt(1+B(m*m-1)).
|
xue@1
|
2585
|
xue@1
|
2586 In: f0: fundamental freqency
|
xue@1
|
2587 fm: a partial frequency
|
xue@1
|
2588 B: stiffness coefficient
|
xue@1
|
2589
|
xue@1
|
2590 Returns the 1-based partial index which will generate the specified partial frequency from the model
|
xue@1
|
2591 which, however, does not have to be an integer in this function.
|
xue@1
|
2592 */
|
xue@1
|
2593 double StiffM(double f0, double fm, double B)
|
xue@1
|
2594 {
|
xue@1
|
2595 if (B<1e-14) return fm/f0;
|
xue@1
|
2596 double b1=B-1, ff=fm/f0;
|
xue@1
|
2597 double delta=b1*b1+4*B*ff*ff;
|
xue@1
|
2598 if (delta<0)
|
xue@1
|
2599 return sqrt(b1/2/B);
|
xue@1
|
2600 else
|
xue@1
|
2601 return sqrt((b1+sqrt(delta))/2/B);
|
xue@1
|
2602 }//StiffMd
|
xue@1
|
2603
|
xue@1
|
2604 //---------------------------------------------------------------------------
|
xue@1
|
2605 /*
|
xue@1
|
2606 TFFilter: time-frequency filtering with Hann-windowed overlap-add.
|
xue@1
|
2607
|
xue@1
|
2608 In: data[Count]: input data
|
xue@1
|
2609 Spans: time-frequency spans
|
xue@1
|
2610 wt, windp: type and extra parameter of FFT window
|
xue@1
|
2611 Sps: sampling rate
|
xue@1
|
2612 TOffst: optional offset applied to all time values in Spans, set to Spans timing of of data[0].
|
xue@1
|
2613 Pass: set to pass T-F content covered by Spans, clear to stop T-F content covered by Spans
|
xue@1
|
2614 Out: dataout[Count]: filtered data
|
xue@1
|
2615
|
xue@1
|
2616 No return value. Identical data and dataout allowed.
|
xue@1
|
2617 */
|
xue@1
|
2618 void TFFilter(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst)
|
xue@1
|
2619 {
|
xue@1
|
2620 int HWid=Wid/2;
|
xue@1
|
2621 int Fr=Count/HWid-1;
|
xue@1
|
2622 int Order=log2(Wid);
|
xue@1
|
2623
|
xue@1
|
2624 int** lspan=new int*[Fr];
|
xue@1
|
2625 double* lxspan=new double[Fr];
|
xue@1
|
2626
|
xue@1
|
2627 lspan[0]=new int[Fr*Wid];
|
xue@1
|
2628 for (int i=1; i<Fr; i++)
|
xue@1
|
2629 lspan[i]=&lspan[0][i*Wid];
|
xue@1
|
2630
|
xue@1
|
2631 //fill local filter span table
|
xue@1
|
2632 if (Pass)
|
xue@1
|
2633 memset(lspan[0], 0, sizeof(int)*Fr*Wid);
|
xue@1
|
2634 else
|
xue@1
|
2635 for (int i=0; i<Fr; i++)
|
xue@1
|
2636 for (int j=0; j<Wid; j++)
|
xue@1
|
2637 lspan[i][j]=1;
|
xue@1
|
2638
|
xue@1
|
2639 for (int i=0; i<Spans->Count; i++)
|
xue@1
|
2640 {
|
xue@1
|
2641 int lx1, lx2, ly1, ly2;
|
xue@1
|
2642 lx1=(Spans->Items[i].T1-TOffst)/HWid-1;
|
xue@1
|
2643 lx2=(Spans->Items[i].T2-1-TOffst)/HWid;
|
xue@1
|
2644 ly1=Spans->Items[i].F1*2/Sps*HWid+0.001;
|
xue@1
|
2645 ly2=Spans->Items[i].F2*2/Sps*HWid+1;
|
xue@1
|
2646 if (lx1<0) lx1=0;
|
xue@1
|
2647 if (lx2>=Fr) lx2=Fr-1;
|
xue@1
|
2648 if (ly1<0) ly1=0;
|
xue@1
|
2649 if (ly1>HWid) ly1=HWid;
|
xue@1
|
2650 if (Pass)
|
xue@1
|
2651 for (int x=lx1; x<=lx2; x++)
|
xue@1
|
2652 for (int y=ly1; y<=ly2; y++)
|
xue@1
|
2653 lspan[x][y]=1;
|
xue@1
|
2654 else
|
xue@1
|
2655 for (int x=lx1; x<=lx2; x++)
|
xue@1
|
2656 for (int y=ly1; y<=ly2; y++)
|
xue@1
|
2657 lspan[x][y]=0;
|
xue@1
|
2658 }
|
xue@1
|
2659 for (int i=0; i<Fr; i++)
|
xue@1
|
2660 {
|
xue@1
|
2661 lxspan[i]=0;
|
xue@1
|
2662 for (int j=0; j<=HWid; j++)
|
xue@1
|
2663 {
|
xue@1
|
2664 if (lspan[i][j])
|
xue@1
|
2665 lxspan[i]=lxspan[i]+1;
|
xue@1
|
2666 }
|
xue@1
|
2667 lxspan[i]/=(HWid+1);
|
xue@1
|
2668 }
|
xue@1
|
2669 double* winf=NewWindow(wt, Wid, 0, &windp);
|
xue@1
|
2670 double* wini=NewWindow(wtHann, Wid, NULL, NULL);
|
xue@1
|
2671 for (int i=0; i<Wid; i++)
|
xue@1
|
2672 if (winf[i]!=0) wini[i]=wini[i]/winf[i];
|
xue@1
|
2673 AllocateFFTBuffer(Wid, ldata, w, x);
|
xue@1
|
2674 double* tmpdata=new double[HWid];
|
xue@1
|
2675 memset(tmpdata, 0, HWid*sizeof(double));
|
xue@1
|
2676
|
xue@1
|
2677 for (int i=0; i<Fr; i++)
|
xue@1
|
2678 {
|
xue@1
|
2679 if (lxspan[i]==0)
|
xue@1
|
2680 {
|
xue@1
|
2681 memcpy(&dataout[i*HWid], tmpdata, sizeof(double)*HWid);
|
xue@1
|
2682 memset(tmpdata, 0, sizeof(double)*HWid);
|
xue@1
|
2683 continue;
|
xue@1
|
2684 }
|
xue@1
|
2685 if (lxspan[i]==1)
|
xue@1
|
2686 {
|
xue@1
|
2687 memcpy(ldata, &data[i*HWid], Wid*sizeof(double));
|
xue@1
|
2688 if (i>0)
|
xue@1
|
2689 for (int k=0; k<HWid; k++)
|
xue@1
|
2690 ldata[k]=ldata[k]*winf[k]*wini[k];
|
xue@1
|
2691 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2692 ldata[k]=ldata[k]*winf[k]*wini[k];
|
xue@1
|
2693
|
xue@1
|
2694 memcpy(&dataout[i*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
2695 for (int k=0; k<HWid; k++)
|
xue@1
|
2696 dataout[i*HWid+k]+=ldata[k];
|
xue@1
|
2697 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
2698 continue;
|
xue@1
|
2699 }
|
xue@1
|
2700 memcpy(ldata, &data[i*HWid], Wid*sizeof(double));
|
xue@1
|
2701 if (i>0)
|
xue@1
|
2702 for (int k=0; k<HWid; k++)
|
xue@1
|
2703 ldata[k]=ldata[k]*winf[k];
|
xue@1
|
2704 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2705 ldata[k]=ldata[k]*winf[k];
|
xue@1
|
2706
|
xue@1
|
2707 RFFTC(ldata, NULL, NULL, Order, w, x, 0);
|
xue@1
|
2708
|
xue@1
|
2709 if (!lspan[i][0]) x[0].x=x[0].y=0;
|
xue@1
|
2710 for (int j=1; j<=HWid; j++)
|
xue@1
|
2711 if (!lspan[i][j]) x[j].x=x[Wid-j].x=x[j].y=x[Wid-j].y=0;
|
xue@1
|
2712
|
xue@1
|
2713 CIFFTR(x, Order, w, ldata);
|
xue@1
|
2714
|
xue@1
|
2715 if (i>0)
|
xue@1
|
2716 for (int k=0; k<HWid; k++)
|
xue@1
|
2717 ldata[k]=ldata[k]*wini[k];
|
xue@1
|
2718 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2719 ldata[k]=ldata[k]*wini[k];
|
xue@1
|
2720
|
xue@1
|
2721 memcpy(&dataout[i*HWid], tmpdata, HWid*sizeof(double));
|
xue@1
|
2722 for (int k=0; k<HWid; k++)
|
xue@1
|
2723 dataout[i*HWid+k]+=ldata[k];
|
xue@1
|
2724 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
2725 }
|
xue@1
|
2726 memcpy(&dataout[Fr*HWid], tmpdata, sizeof(double)*HWid);
|
xue@1
|
2727 memset(&dataout[Fr*HWid+HWid], 0, sizeof(double)*(Count-Fr*HWid-HWid));
|
xue@1
|
2728
|
xue@1
|
2729 FreeFFTBuffer(ldata);
|
xue@1
|
2730 delete[] lspan[0];
|
xue@1
|
2731 delete[] lspan;
|
xue@1
|
2732 delete[] lxspan;
|
xue@1
|
2733 delete[] tmpdata;
|
xue@1
|
2734 delete[] winf;
|
xue@1
|
2735 delete[] wini;
|
xue@1
|
2736 }//TFFilter
|
xue@1
|
2737 //version on integer data, where BytesPerSample specified the integer format.
|
xue@1
|
2738 void TFFilter(void* data, void* dataout, int BytesPerSample, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst)
|
xue@1
|
2739 {
|
xue@1
|
2740 int HWid=Wid/2;
|
xue@1
|
2741 int Fr=Count/HWid-1;
|
xue@1
|
2742 int Order=log2(Wid);
|
xue@1
|
2743
|
xue@1
|
2744 int** lspan=new int*[Fr];
|
xue@1
|
2745 double* lxspan=new double[Fr];
|
xue@1
|
2746
|
xue@1
|
2747 lspan[0]=new int[Fr*Wid];
|
xue@1
|
2748 for (int i=1; i<Fr; i++)
|
xue@1
|
2749 lspan[i]=&lspan[0][i*Wid];
|
xue@1
|
2750
|
xue@1
|
2751 //fill local filter span table
|
xue@1
|
2752 if (Pass)
|
xue@1
|
2753 memset(lspan[0], 0, sizeof(int)*Fr*Wid);
|
xue@1
|
2754 else
|
xue@1
|
2755 for (int i=0; i<Fr; i++)
|
xue@1
|
2756 for (int j=0; j<Wid; j++)
|
xue@1
|
2757 lspan[i][j]=1;
|
xue@1
|
2758
|
xue@1
|
2759 for (int i=0; i<Spans->Count; i++)
|
xue@1
|
2760 {
|
xue@1
|
2761 int lx1, lx2, ly1, ly2;
|
xue@1
|
2762 lx1=(Spans->Items[i].T1-TOffst)/HWid-1;
|
xue@1
|
2763 lx2=(Spans->Items[i].T2-1-TOffst)/HWid;
|
xue@1
|
2764 ly1=Spans->Items[i].F1*2/Sps*HWid+0.001;
|
xue@1
|
2765 ly2=Spans->Items[i].F2*2/Sps*HWid+1;
|
xue@1
|
2766 if (lx1<0) lx1=0;
|
xue@1
|
2767 if (lx2>=Fr) lx2=Fr-1;
|
xue@1
|
2768 if (ly1<0) ly1=0;
|
xue@1
|
2769 if (ly1>HWid) ly1=HWid;
|
xue@1
|
2770 if (Pass)
|
xue@1
|
2771 for (int x=lx1; x<=lx2; x++)
|
xue@1
|
2772 for (int y=ly1; y<=ly2; y++)
|
xue@1
|
2773 lspan[x][y]=1;
|
xue@1
|
2774 else
|
xue@1
|
2775 for (int x=lx1; x<=lx2; x++)
|
xue@1
|
2776 for (int y=ly1; y<=ly2; y++)
|
xue@1
|
2777 lspan[x][y]=0;
|
xue@1
|
2778 }
|
xue@1
|
2779 for (int i=0; i<Fr; i++)
|
xue@1
|
2780 {
|
xue@1
|
2781 lxspan[i]=0;
|
xue@1
|
2782 for (int j=0; j<=HWid; j++)
|
xue@1
|
2783 {
|
xue@1
|
2784 if (lspan[i][j])
|
xue@1
|
2785 lxspan[i]=lxspan[i]+1;
|
xue@1
|
2786 }
|
xue@1
|
2787 lxspan[i]/=(HWid+1);
|
xue@1
|
2788 }
|
xue@1
|
2789 double* winf=NewWindow(wt, Wid, 0, &windp);
|
xue@1
|
2790 double* wini=NewWindow(wtHann, Wid, NULL, NULL);
|
xue@1
|
2791 for (int i=0; i<Wid; i++)
|
xue@1
|
2792 if (winf[i]!=0) wini[i]=wini[i]/winf[i];
|
xue@1
|
2793 AllocateFFTBuffer(Wid, ldata, w, x);
|
xue@1
|
2794 double* tmpdata=new double[HWid];
|
xue@1
|
2795 memset(tmpdata, 0, HWid*sizeof(double));
|
xue@1
|
2796
|
xue@1
|
2797 for (int i=0; i<Fr; i++)
|
xue@1
|
2798 {
|
xue@1
|
2799 if (lxspan[i]==0)
|
xue@1
|
2800 {
|
xue@1
|
2801 DoubleToInt(&((char*)dataout)[i*HWid*BytesPerSample], BytesPerSample, tmpdata, HWid);
|
xue@1
|
2802 memset(tmpdata, 0, sizeof(double)*HWid);
|
xue@1
|
2803 continue;
|
xue@1
|
2804 }
|
xue@1
|
2805 if (lxspan[i]==1)
|
xue@1
|
2806 {
|
xue@1
|
2807 IntToDouble(ldata, &((char*)data)[i*HWid*BytesPerSample], BytesPerSample, Wid);
|
xue@1
|
2808 if (i>0)
|
xue@1
|
2809 for (int k=0; k<HWid; k++)
|
xue@1
|
2810 ldata[k]=ldata[k]*winf[k]*wini[k];
|
xue@1
|
2811 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2812 ldata[k]=ldata[k]*winf[k]*wini[k];
|
xue@1
|
2813
|
xue@1
|
2814 for (int k=0; k<HWid; k++) tmpdata[k]+=ldata[k];
|
xue@1
|
2815 DoubleToInt(&((char*)dataout)[i*HWid*BytesPerSample], BytesPerSample, tmpdata, HWid);
|
xue@1
|
2816 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
2817 continue;
|
xue@1
|
2818 }
|
xue@1
|
2819 IntToDouble(ldata, &((char*)data)[i*HWid*BytesPerSample], BytesPerSample, Wid);
|
xue@1
|
2820 if (i>0)
|
xue@1
|
2821 for (int k=0; k<HWid; k++)
|
xue@1
|
2822 ldata[k]=ldata[k]*winf[k];
|
xue@1
|
2823 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2824 ldata[k]=ldata[k]*winf[k];
|
xue@1
|
2825
|
xue@1
|
2826 RFFTC(ldata, NULL, NULL, Order, w, x, 0);
|
xue@1
|
2827
|
xue@1
|
2828 if (!lspan[i][0]) x[0].x=x[0].y=0;
|
xue@1
|
2829 for (int j=1; j<=HWid; j++)
|
xue@1
|
2830 if (!lspan[i][j]) x[j].x=x[Wid-j].x=x[j].y=x[Wid-j].y=0;
|
xue@1
|
2831
|
xue@1
|
2832 CIFFTR(x, Order, w, ldata);
|
xue@1
|
2833
|
xue@1
|
2834 if (i>0)
|
xue@1
|
2835 for (int k=0; k<HWid; k++)
|
xue@1
|
2836 ldata[k]=ldata[k]*wini[k];
|
xue@1
|
2837 for (int k=HWid; k<Wid; k++)
|
xue@1
|
2838 ldata[k]=ldata[k]*wini[k];
|
xue@1
|
2839
|
xue@1
|
2840
|
xue@1
|
2841 for (int k=0; k<HWid; k++)
|
xue@1
|
2842 tmpdata[k]+=ldata[k];
|
xue@1
|
2843 DoubleToInt(&((char*)dataout)[i*HWid*BytesPerSample], BytesPerSample, tmpdata, HWid);
|
xue@1
|
2844 memcpy(tmpdata, &ldata[HWid], HWid*sizeof(double));
|
xue@1
|
2845 }
|
xue@1
|
2846 DoubleToInt(&((char*)dataout)[Fr*HWid*BytesPerSample], BytesPerSample, tmpdata, HWid);
|
xue@1
|
2847 memset(&((char*)dataout)[(Fr*HWid+HWid)*BytesPerSample], 0, BytesPerSample*(Count-Fr*HWid-HWid));
|
xue@1
|
2848
|
xue@1
|
2849 FreeFFTBuffer(ldata);
|
xue@1
|
2850
|
xue@1
|
2851 delete[] lspan[0];
|
xue@1
|
2852 delete[] lspan;
|
xue@1
|
2853 delete[] lxspan;
|
xue@1
|
2854 delete[] tmpdata;
|
xue@1
|
2855 delete[] winf;
|
xue@1
|
2856 delete[] wini;
|
xue@1
|
2857 }//TFFilter
|
xue@1
|
2858
|
xue@1
|
2859
|
xue@1
|
2860
|