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