xue@1
|
1 //---------------------------------------------------------------------------
|
xue@1
|
2
|
xue@1
|
3 #include <math.h>
|
xue@1
|
4 #include <memory.h>
|
xue@1
|
5 #include "QuickSpec.h"
|
xue@1
|
6
|
xue@1
|
7
|
xue@1
|
8 //---------------------------------------------------------------------------
|
xue@1
|
9 /*
|
xue@1
|
10 method TQuickSpectrogram::TQuickSpectrogram:
|
xue@1
|
11
|
xue@1
|
12 In: AParent: pointer argument for calling G, if G is specified
|
xue@1
|
13 AnId: integer argument for calling G, if G is specified
|
xue@1
|
14 G: pointer to a function that supplies buffers used for FFT, 0 by default
|
xue@1
|
15 Ausex: set if complete complex spectrogram is to be buffered and accessible
|
xue@1
|
16 Auseph: set if phase spectrogram is to be buffered and accessible
|
xue@1
|
17 */
|
xue@1
|
18 __fastcall TQuickSpectrogram::TQuickSpectrogram(void* AParent, int AnId, bool Ausex, bool Auseph, GetBuf G)
|
xue@1
|
19 {
|
xue@1
|
20 memset(this, 0, sizeof(TQuickSpectrogram));
|
xue@1
|
21 Parent=AParent;
|
xue@1
|
22 Id=AnId;
|
xue@1
|
23 usex=Ausex;
|
xue@1
|
24 useph=Auseph;
|
xue@1
|
25 GetFFTBuffers=G;
|
xue@1
|
26 BufferSize=QSpec_BufferSize;
|
xue@1
|
27 fwt=wtRectangle;
|
xue@1
|
28 Wid=1024;
|
xue@1
|
29 Offst=512;
|
xue@1
|
30 }//TQuickSpectrogram
|
xue@1
|
31
|
xue@1
|
32 //TQuickSpectrogram::~TQuickSpectrogram
|
xue@1
|
33 __fastcall TQuickSpectrogram::~TQuickSpectrogram()
|
xue@1
|
34 {
|
xue@1
|
35 FreeBuffers();
|
xue@1
|
36 free8(fw);
|
xue@1
|
37 free8(fwin);
|
xue@1
|
38 free(fhbi);
|
xue@1
|
39 }//~TQuickSpectrogram
|
xue@1
|
40
|
xue@1
|
41 //---------------------------------------------------------------------------
|
xue@1
|
42 /*
|
xue@1
|
43 method TQuickSpectrogram::A: accesses amplitude spectrogram by frame
|
xue@1
|
44
|
xue@1
|
45 In: fr: frame index, 0-based
|
xue@1
|
46
|
xue@1
|
47 Returns pointer to amplitude spectrum of the fr'th frame, NULL if N/A
|
xue@1
|
48 */
|
xue@1
|
49 QSPEC_FORMAT* __fastcall TQuickSpectrogram::A(int fr)
|
xue@1
|
50 {
|
xue@1
|
51 if (Capacity==0) SetFrCapacity((DataLength-Wid)/Offst+2);
|
xue@1
|
52 if (fr<0 || fr>=Capacity) return NULL;
|
xue@1
|
53 if (Frame[fr]<0 || !Valid[fr]) CalculateSpectrum(fr);
|
xue@1
|
54 return fA[Frame[fr]];
|
xue@1
|
55 }//A
|
xue@1
|
56
|
xue@1
|
57 //---------------------------------------------------------------------------
|
xue@1
|
58 /*
|
xue@1
|
59 method TQuickSpectrogram::AddBuffer: increases internal buffer by BufferSize frames. Allocated buffers
|
xue@1
|
60 beyond Capacity frames will not be indexed or used by TQuickSpectrogram.
|
xue@1
|
61 */
|
xue@1
|
62 void TQuickSpectrogram::AddBuffer()
|
xue@1
|
63 {
|
xue@1
|
64 int base=1, Dim=Wid/2+1;
|
xue@1
|
65 if (usex) base+=2;
|
xue@1
|
66 if (useph) base+=1;
|
xue@1
|
67 QSPEC_FORMAT* newbuffer=(QSPEC_FORMAT*)malloc(sizeof(QSPEC_FORMAT)*Dim*BufferSize*base);
|
xue@1
|
68 int fr0=BufferCount*BufferSize;
|
xue@1
|
69 for (int i=0; i<BufferSize; i++)
|
xue@1
|
70 {
|
xue@1
|
71 int fr=fr0+i;
|
xue@1
|
72 if (fr<Capacity)
|
xue@1
|
73 {
|
xue@1
|
74 fA[fr]=&newbuffer[i*Dim];
|
xue@1
|
75 int base=1;
|
xue@1
|
76 if (usex) fSpec[fr]=(cmplx<QSPEC_FORMAT>*)&newbuffer[(BufferSize+i*2)*Dim], base+=2;
|
xue@1
|
77 if (useph) fPh[fr]=&newbuffer[(BufferSize*base+i)*Dim];
|
xue@1
|
78 }
|
xue@1
|
79 else break;
|
xue@1
|
80 }
|
xue@1
|
81 BufferCount++;
|
xue@1
|
82 }//AddBuffer
|
xue@1
|
83
|
xue@1
|
84 /*
|
xue@1
|
85 method TQuickSpectrogram::AddBuffer: increase internal buffer by a multiple of BufferSize so that
|
xue@1
|
86 it will be enough to host another AddFrCount frames.
|
xue@1
|
87 */
|
xue@1
|
88 void TQuickSpectrogram::AddBuffer(int AddFrCount)
|
xue@1
|
89 {
|
xue@1
|
90 while (FrCount+AddFrCount>BufferSize*BufferCount) AddBuffer();
|
xue@1
|
91 }//AddBuffer
|
xue@1
|
92
|
xue@1
|
93 //---------------------------------------------------------------------------
|
xue@1
|
94 /*
|
xue@1
|
95 function IntToDouble: copy content of integer array to double array
|
xue@1
|
96
|
xue@1
|
97 In: in: pointer to integer array
|
xue@1
|
98 BytesPerSample: number of bytes each integer takes
|
xue@1
|
99 Count: size of integer array, in integers
|
xue@1
|
100 Out: vector out[Count].
|
xue@1
|
101
|
xue@1
|
102 No return value.
|
xue@1
|
103 */
|
xue@1
|
104 void IntToDouble(double* out, void* in, int BytesPerSample, int Count)
|
xue@1
|
105 {
|
xue@1
|
106 if (BytesPerSample==1){unsigned char* in8=(unsigned char*)in; for (int k=0; k<Count; k++) *(out++)=*(in8++)-128.0;}
|
xue@1
|
107 else if (BytesPerSample==2) {__int16* in16=(__int16*)in; for (int k=0; k<Count; k++) *(out++)=*(in16++);}
|
xue@1
|
108 else {__pint24 in24=(__pint24)in; for (int k=0; k<Count; k++) *(out++)=*(in24++);}
|
xue@1
|
109 }//IntToDouble
|
xue@1
|
110
|
xue@1
|
111 /*
|
xue@1
|
112 function CalculateSpectrum: calculate spectrum of a signal in integer format
|
xue@1
|
113
|
xue@1
|
114 In: Data[Wid]: integer array hosting waveform data
|
xue@1
|
115 BytesPerSample: number of bytes each integer in Data[] takes
|
xue@1
|
116 win[Wid]: window function used for computing spectrum
|
xue@1
|
117 w[Wid/2], x[Wid], hbi[Wid/2]: FFT buffers
|
xue@1
|
118 Out: x[Wid]: complex spectrum
|
xue@1
|
119 Amp[Wid/2+1]: amplitude spectrum
|
xue@1
|
120 Arg[Wid/2+1]: phase spectrum, optional
|
xue@1
|
121
|
xue@1
|
122 No return value.
|
xue@1
|
123 */
|
xue@1
|
124 void CalculateSpectrum(void* Data, int BytesPerSample, double* win, QSPEC_FORMAT* Amp, QSPEC_FORMAT* Arg, int Wid, cdouble* w, cdouble* x, int* hbi)
|
xue@1
|
125 {
|
xue@1
|
126 if (BytesPerSample==2) RFFTCW((__int16*)Data, win, 0, 0, log2(Wid), w, x, hbi);
|
xue@1
|
127 else {IntToDouble((double*)x, Data, BytesPerSample, Wid); RFFTCW((double*)x, win, 0, 0, log2(Wid), w, x, hbi);}
|
xue@1
|
128 for (int j=0; j<=Wid/2; j++)
|
xue@1
|
129 {
|
xue@1
|
130 Amp[j]=sqrt(x[j].x*x[j].x+x[j].y*x[j].y);
|
xue@1
|
131 if (Arg) Arg[j]=(x[j].y==0 && x[j].x==0)?0:atan2(x[j].y, x[j].x);
|
xue@1
|
132 }
|
xue@1
|
133 }//CalculateSpectrum
|
xue@1
|
134
|
xue@1
|
135 /*
|
xue@1
|
136 function CalculateSpectrum: calculate spectrum of a signal in integer format, allowing the signal
|
xue@1
|
137 length $eff be shorter than the DFT size Wid.
|
xue@1
|
138
|
xue@1
|
139 In: Data[eff]: integer array hosting waveform data
|
xue@1
|
140 BytesPerSample: number of bytes each integer in Data[] takes
|
xue@1
|
141 win[Wid]: window function used for computing spectrum
|
xue@1
|
142 w[Wid/2], x[Wid], hbi[Wid/2]: FFT buffers
|
xue@1
|
143 Out: x[Wid]: complex spectrum
|
xue@1
|
144 Amp[Wid/2+1]: amplitude spectrum
|
xue@1
|
145 Arg[Wid/2+1]: phase spectrum, optional
|
xue@1
|
146
|
xue@1
|
147 No return value.
|
xue@1
|
148 */
|
xue@1
|
149 void CalculateSpectrum(void* Data, int BytesPerSample, double* win, QSPEC_FORMAT* Amp, QSPEC_FORMAT* Arg, int Wid, int eff, cdouble* w, cdouble* x, int* hbi)
|
xue@1
|
150 {
|
xue@1
|
151 if (eff<=0)
|
xue@1
|
152 {
|
xue@1
|
153 memset(Amp, 0, sizeof(double)*(Wid/2+1));
|
xue@1
|
154 if (Arg) memset(Arg, 0, sizeof(double)*(Wid/2+1));
|
xue@1
|
155 }
|
xue@1
|
156 else if (eff<Wid)
|
xue@1
|
157 {
|
xue@1
|
158 double* doublex=(double*)x;
|
xue@1
|
159 IntToDouble(doublex, Data, BytesPerSample, eff); memset(&doublex[eff], 0, sizeof(double)*(Wid-eff));
|
xue@1
|
160 RFFTCW(doublex, win, 0, 0, log2(Wid), w, x, hbi);
|
xue@1
|
161 for (int j=0; j<=Wid/2; j++)
|
xue@1
|
162 {
|
xue@1
|
163 Amp[j]=sqrt(x[j].x*x[j].x+x[j].y*x[j].y);
|
xue@1
|
164 if (Arg) Arg[j]=(x[j].y==0 && x[j].x==0)?0:atan2(x[j].y, x[j].x);
|
xue@1
|
165 }
|
xue@1
|
166 }
|
xue@1
|
167 else
|
xue@1
|
168 CalculateSpectrum(Data, BytesPerSample, win, Amp, Arg, Wid, w, x, hbi);
|
xue@1
|
169 }//CalculateSpectrum
|
xue@1
|
170
|
xue@1
|
171 /*
|
xue@1
|
172 method TQuickSpectrogram::CalculateSpectrum: computes spectrogram at fr'th frame.
|
xue@1
|
173
|
xue@1
|
174 In: fr: index to the frame whose spectrum is to be computed. fr must be between 0 and Capacity-1.
|
xue@1
|
175 */
|
xue@1
|
176 void __fastcall TQuickSpectrogram::CalculateSpectrum(int fr)
|
xue@1
|
177 {
|
xue@1
|
178 cdouble *w, *x;
|
xue@1
|
179 double* win;
|
xue@1
|
180 int* hbi;
|
xue@1
|
181
|
xue@1
|
182 //obtain FFT buffers win (window function), w (twiddle factors), x (data buffer),
|
xue@1
|
183 //hbi (half-size bit-inversed integer table)
|
xue@1
|
184 if (GetFFTBuffers) //then use external buffers provided through GetFFTBuffers
|
xue@1
|
185 GetFFTBuffers(Id, w, x, win, hbi, Parent);
|
xue@1
|
186 else //then use internal buffers
|
xue@1
|
187 {
|
xue@1
|
188 if (Wid!=fWid)
|
xue@1
|
189 { //then update internal buffers to the new window size
|
xue@1
|
190 free8(fw); free(fhbi);
|
xue@1
|
191 fw=(cdouble*)malloc8(sizeof(cdouble)*Wid*1.5); SetTwiddleFactors(Wid, fw);
|
xue@1
|
192 fx=&fw[Wid/2];
|
xue@1
|
193 fhbi=CreateBitInvTable(log2(Wid)-1);
|
xue@1
|
194 }
|
xue@1
|
195 if (Wid!=fWid || WinType!=fwt || WinParam!=fwdp)
|
xue@1
|
196 { //then update internal window function to the new window type
|
xue@1
|
197 fwin=NewWindow8(WinType, Wid, 0, &WinParam);
|
xue@1
|
198 fwt=WinType; fwdp=WinParam;
|
xue@1
|
199 }
|
xue@1
|
200 fWid=Wid;
|
xue@1
|
201
|
xue@1
|
202 //pick up the internal buffers
|
xue@1
|
203 w=fw, x=fx, win=fwin, hbi=fhbi;
|
xue@1
|
204 }
|
xue@1
|
205
|
xue@1
|
206 //obtain the index of this frame in internal storage
|
xue@1
|
207 if (Frame[fr]<0) {AddBuffer(1); Frame[fr]=FrCount; FrCount++;}
|
xue@1
|
208 int realfr=Frame[fr];
|
xue@1
|
209
|
xue@1
|
210 //obtain the pointer to this frame's phase spectrum
|
xue@1
|
211 QSPEC_FORMAT *lph=useph?fPh[realfr]:NULL;
|
xue@1
|
212 //ontain the pointer to this frame's complex spectrum
|
xue@1
|
213 cmplx<QSPEC_FORMAT>* lX=usex?fSpec[realfr]:NULL;
|
xue@1
|
214 //choose the buffer actually used for FFT - use lX if it is specified as complex double array
|
xue@1
|
215 //because it saves unnecessary data copying operations
|
xue@1
|
216 cdouble *lx=(usex && sizeof(QSPEC_FORMAT)==sizeof(double))?(cdouble*)lX:x;
|
xue@1
|
217
|
xue@1
|
218 //Actual FFT
|
xue@1
|
219 if (fr*Offst+Wid<=DataLength)
|
xue@1
|
220 ::CalculateSpectrum(&((char*)Data)[fr*Offst*BytesPerSample], BytesPerSample, win, fA[realfr], lph, Wid, w, lx, hbi);
|
xue@1
|
221 else
|
xue@1
|
222 ::CalculateSpectrum(&((char*)Data)[fr*Offst*BytesPerSample], BytesPerSample, win, fA[realfr], lph, Wid, DataLength-fr*Offst, w, x, hbi);
|
xue@1
|
223
|
xue@1
|
224 //optional data copy from x to lX
|
xue@1
|
225 if (usex && lx==x) for (int i=0; i<Wid/2+1; i++) lX[i]=x[i];
|
xue@1
|
226
|
xue@1
|
227 //tag this frame as computed and valid
|
xue@1
|
228 Valid[fr]=1;
|
xue@1
|
229 }//CalculateSpectrum
|
xue@1
|
230
|
xue@1
|
231 //---------------------------------------------------------------------------
|
xue@1
|
232 /*
|
xue@1
|
233 method TQuickSpectrogram::FreeBuffers: discards all computed spectra and free all internal buffers.
|
xue@1
|
234 This returns the TQuickSpectrogram to its initial state before any frame is accessed. After calling
|
xue@1
|
235 FreeBuffers() all frames will be recomputed when they are accessed.
|
xue@1
|
236 */
|
xue@1
|
237 void TQuickSpectrogram::FreeBuffers()
|
xue@1
|
238 {
|
xue@1
|
239 if (fA)
|
xue@1
|
240 {
|
xue@1
|
241 for (int i=0; i<BufferCount; i++) free(fA[i*BufferSize]);
|
xue@1
|
242 FrCount=BufferCount=Capacity=0;
|
xue@1
|
243 free(Frame); free(Valid);
|
xue@1
|
244 free(fA);
|
xue@1
|
245 Frame=Valid=0, fA=0;
|
xue@1
|
246 if (useph) {free(fPh); fPh=0;}
|
xue@1
|
247 if (usex) {free(fSpec); fSpec=0;}
|
xue@1
|
248 }
|
xue@1
|
249 }//FreeBuffers
|
xue@1
|
250
|
xue@1
|
251 //---------------------------------------------------------------------------
|
xue@1
|
252 /*
|
xue@1
|
253 method TQuickSpectrogram::Invalidate: renders all frames that have overlap with interval [From, To],
|
xue@1
|
254 measured in samples, as invalid. Invalid frames are recomputed when they are accessed again.
|
xue@1
|
255
|
xue@1
|
256 In: [From, To]: an interval spectrogram over which needs to be updated.
|
xue@1
|
257
|
xue@1
|
258 Returns the number of allocated frames affected, no matter if they were valid.
|
xue@1
|
259 */
|
xue@1
|
260 int TQuickSpectrogram::Invalidate(int From, int To)
|
xue@1
|
261 {
|
xue@1
|
262 int result=0;
|
xue@1
|
263 if (Frame)
|
xue@1
|
264 {
|
xue@1
|
265 int fr1=ceil((From-Wid+1.0)/Offst), fr2=floor(1.0*To/Offst);
|
xue@1
|
266 if (fr1<0) fr1=0;
|
xue@1
|
267 if (fr2>=Capacity) fr2=Capacity-1;
|
xue@1
|
268 for (int fr=fr1; fr<=fr2; fr++) if (Frame[fr]>=0) Valid[fr]=false, result++;
|
xue@1
|
269 }
|
xue@1
|
270 return result;
|
xue@1
|
271 }//Invalidate
|
xue@1
|
272
|
xue@1
|
273 //---------------------------------------------------------------------------
|
xue@1
|
274 /*
|
xue@1
|
275 method TQuickSpectrogram::Ph: accesses phase spectrogram by frame
|
xue@1
|
276
|
xue@1
|
277 In: fr: frame index, 0-based
|
xue@1
|
278
|
xue@1
|
279 Returns pointer to phase spectrum of the fr'th frame, NULL if N/A
|
xue@1
|
280 */
|
xue@1
|
281 QSPEC_FORMAT* __fastcall TQuickSpectrogram::Ph(int fr)
|
xue@1
|
282 {
|
xue@1
|
283 if (Capacity==0) SetFrCapacity((DataLength-Wid)/Offst+2);
|
xue@1
|
284 if (fr<0 || fr>=Capacity) return NULL;
|
xue@1
|
285 if (Frame[fr]<0 || !Valid[fr]) CalculateSpectrum(fr);
|
xue@1
|
286 return fPh[Frame[fr]];
|
xue@1
|
287 }//Ph
|
xue@1
|
288
|
xue@1
|
289 //---------------------------------------------------------------------------
|
xue@1
|
290 /*
|
xue@1
|
291 method TQuickSpectrogram::SetFrCapacity: sets the capacity, i.e. the maximal number of frames handled
|
xue@1
|
292 by this TQuickSpectrogram.
|
xue@1
|
293
|
xue@1
|
294 In: AnFrCapacity: the new Capacity, in frames
|
xue@1
|
295
|
xue@1
|
296 This method should not be called to set Capacity to a smaller value.
|
xue@1
|
297 */
|
xue@1
|
298 void TQuickSpectrogram::SetFrCapacity(int AnFrCapacity)
|
xue@1
|
299 {
|
xue@1
|
300 //adjusting the size of index and validity arrays
|
xue@1
|
301 Frame=(int*)realloc(Frame, sizeof(int)*AnFrCapacity);
|
xue@1
|
302 Valid=(int*)realloc(Valid, sizeof(int)*AnFrCapacity);
|
xue@1
|
303
|
xue@1
|
304 //
|
xue@1
|
305 fA=(QSPEC_FORMAT**)realloc(fA, sizeof(QSPEC_FORMAT*)*AnFrCapacity);
|
xue@1
|
306 if (usex) fSpec=(cmplx<QSPEC_FORMAT>**)realloc(fSpec, sizeof(cmplx<QSPEC_FORMAT>*)*AnFrCapacity);
|
xue@1
|
307 if (useph) fPh=(QSPEC_FORMAT**)realloc(fPh, sizeof(QSPEC_FORMAT*)*AnFrCapacity);
|
xue@1
|
308 if (AnFrCapacity>Capacity)
|
xue@1
|
309 {
|
xue@1
|
310 memset(&Frame[Capacity], 0xFF, sizeof(int)*(AnFrCapacity-Capacity));
|
xue@1
|
311 memset(&Valid[Capacity], 0x00, sizeof(int)*(AnFrCapacity-Capacity));
|
xue@1
|
312
|
xue@1
|
313 if (Capacity<BufferCount*BufferSize)
|
xue@1
|
314 {
|
xue@1
|
315 for (int fr=Capacity; fr<AnFrCapacity; fr++)
|
xue@1
|
316 {
|
xue@1
|
317 int bufferno=fr/BufferSize;
|
xue@1
|
318 if (bufferno<BufferCount)
|
xue@1
|
319 {
|
xue@1
|
320 QSPEC_FORMAT* thisbuffer=fA[BufferSize*bufferno];
|
xue@1
|
321 int lfr=fr%BufferSize, base=1, Dim=Wid/2+1;
|
xue@1
|
322 fA[fr]=&thisbuffer[lfr*Dim];
|
xue@1
|
323 if (usex) fSpec[fr]=(cmplx<QSPEC_FORMAT>*)(&thisbuffer[(BufferSize+lfr*2)*Dim]), base+=2;
|
xue@1
|
324 if (useph) fPh[fr]=&thisbuffer[(BufferSize*base+lfr)*Dim];
|
xue@1
|
325 }
|
xue@1
|
326 else break;
|
xue@1
|
327 }
|
xue@1
|
328 }
|
xue@1
|
329 }
|
xue@1
|
330 Capacity=AnFrCapacity;
|
xue@1
|
331 }//SetFrCapacity
|
xue@1
|
332
|
xue@1
|
333 //---------------------------------------------------------------------------
|
xue@1
|
334 /*
|
xue@1
|
335 method TQuickSpectrogram::Ph: accesses complex spectrogram by frame
|
xue@1
|
336
|
xue@1
|
337 In: fr: frame index, 0-based
|
xue@1
|
338
|
xue@1
|
339 Returns pointer to complex spectrum of the fr'th frame, NULL if N/A
|
xue@1
|
340 */
|
xue@1
|
341 cmplx<QSPEC_FORMAT>* __fastcall TQuickSpectrogram::Spec(int fr)
|
xue@1
|
342 {
|
xue@1
|
343 if (Capacity==0) SetFrCapacity((DataLength-Wid)/Offst+2);
|
xue@1
|
344 if (fr<0 || fr>=Capacity) return NULL;
|
xue@1
|
345 if (Frame[fr]<0 || !Valid[fr]) CalculateSpectrum(fr);
|
xue@1
|
346 return fSpec[Frame[fr]];
|
xue@1
|
347 }//Spec
|
xue@1
|
348
|
xue@1
|
349
|