-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.c
533 lines (480 loc) · 13.5 KB
/
fft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*****************************************************************
- fft.c
- subroutines doing fast fourier transform, correlation, etc
-
- Includes:
- fft() - FFT for complex sequence.
- fftr() - FFT for real sequence.
- cor() - cross-correlation of two seqs. using their spectra.
- crscrl() - cross-correlation of two seq., returns a portion.
- maxCor() - max cross-correlation.
- maxCorSlide() - max cross-correlation using a sliding window.
- conv() - convolve two time seq. in the time domain.
- amp() - integrate a time seq. between two points.
- acc() - auto-cc coefficient of two segments in an array.
- cumsum() - cummulative sum of a time seq.
- maver() - m-point moving average.
- diffrt() - differentiate the time sequence.
- sqr() - square of the time sequence.
- revers() - reverse the time sequence.
- coswndw() - generate a cosine taper window.
- filter() - high-pass filtering data in frequency domain.
- findMaxAbs() - find absolute maximum in an array.
- findMax() - find maximum in an array.
- rtrend() - remove trend (a+b*t).
- fltGauss() - apply the Gaussian filter.
- shiftSpec() - do time shift in freq. domain.
- specAdd() - spectrum addition.
- specMul() - spectrum multiplication.
- specScale() - multiply a constant to spectrum.
- specPwr() - compute auto-corr. using spectrum.
-
- Revision History
- Lupei Zhu 06/20/94 Initial revision
- Lupei Zhu 12/02/99 conv() now can handle ns>n
- Lupei Zhu 01/01/00 add more subroutines
- Lupei Zhu 09/02/04 add maxCor and maxCorSlide
- fix a bug w.r.t. delay
- Lupei Zhu 03/24/13 fix a bug in maxCorSlide().
- and a bug in cor().
******************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "Complex.h"
/*---------------------------------------------------------------
* fft()
* discrete fourier transform of complex sequence x[i], i=0,1,...,n-1.
*
* fft{x}[i] = dt*SUM x[k]*exp(-j*i*k*pi/n) over k=0 to n-1
* or
* inv_fft{x}[i] = (1/(n*dt)) SUM x[k]*exp(j*i*k*pi/n) over k=0 to n-1
* This should agree with analog Fourier transform in amplitude
* Input arguments:
* x (complex *) - array for FFT (IN/OUT)
* n (int) - dimension of x[], n=2^N, N>0.
* dt (float) - time sampling interval, forward (>0) or inverse (<0) FFT;
*--------------------------------------------------------------*/
void fft(complex *a, int n, float dt) {
int i, j, k, step, m;
complex u, w, t;
double pi;
pi = -PI;
if (dt<0.) pi = PI;
for (m=n/2,j=0,i=1; i<n-1; i++) {
for (k=m; k<=j; k/=2) j -= k;
j += k;
if(i<j) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
for (m=1,step=2; m<n; m=step, step*=2) {
for (u=One,w=cmplx(cos(pi/m),sin(pi/m)),j=0; j<m; j++) {
for (i=j; i<n; i+=step) {
k = i+m;
t = cmltp(a[k], u);
a[k] = cplus(a[i], cngtv(t));
a[i] = cplus(a[i], t);
}
u = cmltp(u, w);
}
}
if (dt<0.) dt=-1./(n*dt);
for (i=0; i<n; i++) a[i] = dmltp(dt, a[i]);
}
/*---------------------------------------------------------------
* fftr()
* fast fourier transform of real sequence
* Input arguments:
* x (complex *) - array for FFT (IN/OUT). For the forward
* transf., x is the real sequence stored as complex array;
* for the inverse transf., x is the half of spectrum f and
* f(n) is in Im(x[0]).
* n (int) - dimension of x[], n=2^N, N>0.
* dt (float) - forward (>0) or inverse (<0) FFT;
*--------------------------------------------------------------*/
void fftr(complex *x, int n, float dt) {
int i, j, n2;
float delw, w;
complex t, g, h, isg;
n2 = n/2;
delw = PI/n;
isg = IMAGE;
if (dt>0.) {
delw = -delw;
isg = cngtv(isg);
fft(x, n, dt);
}
x[0] = cmplx(x[0].x+x[0].y, x[0].x-x[0].y);
for (i=1, w=delw; i<n2; i++, w+=delw) {
j = n-i;
t = conjg(x[j]);
g = cplus(x[i], t);
h = cplus(x[i], cngtv(t));
h = cmltp(cmplx(cos(w), sin(w)), h);
x[i] = dmltp(0.5, cplus(g, cmltp(isg,h)));
x[j] = dmltp(0.5, cplus(conjg(g), cmltp(isg, conjg(h))));
}
x[n2] = conjg(x[n2]);
if (dt<0.) {
x[0] = dmltp(0.5, x[0]);
fft(x, n, dt);
}
}
void fftr_(float *x, int *n, float *dt) {
fftr((complex *) x, *n, *dt);
}
/*
cross-correlation, IFFT{data[w]*conjugate(src[w])} = int(data(tau)*src(t-tau),tau).
The output is shifted by pi in phase so that the zero-lag is at the middle (nft/2).
*/
void cor(
complex *src, /* In: source function */
complex *data, /* In: data */
/* Out: cross-correlation */
float dt, /* In: dt */
int nft /* In: number of pts */
)
{
int j;
float aa;
data[0]=cmplx(data[0].x*src[0].x, data[0].y*src[0].y);
for (aa=-1.,j=1; j<nft; j++) {
data[j]=cmltp(data[j], conjg(src[j]));
data[j]=dmltp(aa, data[j]);
aa = -aa;
}
fftr(data, nft, -dt);
}
/*
Convolving s[] with f[] in time domain, the result is
brought back in f[]. so the result will be good for the
case that s[] is shorter than f[]
*/
void conv(float *s, int ns, float *f, int n) {
int i,j,k,m;
float *g,*pt;
m = n+ns;
if ( (g=(float *) malloc(m*sizeof(float))) == NULL ) {
fprintf(stderr, "fail to malloc in conv()\n");
exit(-1);
}
for(i=0;i<ns;i++) g[i] = 0.;
for(pt=f,k=0;k<n;k++,i++,pt++){
g[i] = *pt;
for(*pt=0.,j=0;j<ns;j++) *pt += g[i-j]*s[j];
}
free(g);
}
void conv_(float *s, int *ns, float *f, int *n) {
conv(s, *ns, f, *n);
}
/*
cross-correlate rec with syn: sum(rec[i]*syn[j-i],i). Note no dt.
Only return an array of m+1 points of cross-correlation around the zero-lag.
m is an even number (2*k) and the dedays are -k, -k+1, ..., 0, ..., k-1, k;
positive delay means that the syn is earlier.
*/
float *crscrl(int npt,float *rec,float *syn,int m) {
int i,nft,nft2;
float *ss1,*ss2;
nft=2;while(nft<npt)nft*=2;nft2=nft;nft*=2;
ss1=(float *)calloc(nft, sizeof(float));
ss2=(float *)calloc(nft, sizeof(float));
memcpy((char *)ss1, (char *) rec, npt*sizeof(float));
memcpy((char *)ss2, (char *) syn, npt*sizeof(float));
for(i=npt;i<nft;i++) {ss1[i]=0.;ss2[i]=0.;}
fftr((complex *) ss1,nft2,1.);
fftr((complex *) ss2,nft2,1.);
cor((complex *) ss2, (complex *) ss1, 1., nft2);
nft2 -= m/2;
i = (m+1)*sizeof(float);
ss2=(float *)realloc(ss2, i);
memcpy(ss2, ss1+nft2, i);
free(ss1);
return(ss2);
}
/* data(t) = amp*syn(t-delay), return max. cross-correlation */
float maxCor(float *data, float *syn, int n, int max_shift, int *delay, float *amp) {
int i;
float *crs,c,dataAuto,synAuto;
for(dataAuto=0.,synAuto=0.,i=0;i<n;i++) {
dataAuto += data[i]*data[i];
synAuto += syn[i]*syn[i];
}
if (max_shift<0) max_shift = n;
crs = crscrl(n,data,syn,2*max_shift);
for(c=-FLT_MAX,i=0;i<=2*max_shift;i++) {
if (c<crs[i]) {
c=crs[i];
*delay=i;
}
}
free(crs);
*delay -= max_shift;
*amp = c/synAuto;
return(c/sqrt(dataAuto*synAuto));
}
/* data(t) = amp*syn(t-delay), return max. cross-correlation.
This one uses a sliding window of length lw */
float maxCorSlide(float *data, float *syn, int n, int lw, float overlap,int *delay, float *amp) {
int i, j, max_shift, delay0;
float cc, cc_best, amp0;
max_shift = (int) rint(lw*(1.-overlap));
cc_best = -FLT_MAX;
for(i=0;i+lw<n;i+=max_shift) {
for(j=0;j+lw<n;j+=max_shift) {
cc = maxCor(data+i,syn+j,lw,max_shift,&delay0,&0);
if (cc>cc_best) {
cc_best = cc;
*delay = delay0 + i-j;
*amp = amp0;
}
}
}
return cc_best;
}
/* integrate data[n] between t1 and t2 (non-dimensional times w.r.t to the beginning time)*/
float amp(float t1, float t2, float *data, int n) {
int i, it1, it2;
float dd, am;
if ( t1 < 0 ) t1=0;
if ( t2 > n-1 ) t2=n-1;
if ( t1 > n-1 || t2 < t1) return 0.;
it1 = floor(t1);
i = it1 + 1;
dd = i-t1;
am = dd*(dd*data[it1]+(2.-dd)*data[i]);
/* return data[it1]*dd + data[i]*(1.-dd);*/
it2 = ceil(t2);
while (i<it2) {
am += data[i]+data[i+1];
i++;
}
dd = i-t2;
am -= dd*(dd*data[i-1]+(2.-dd)*data[i]);
return 0.5*am;
}
/* compute auto-cc of two segments of an array. t1 and t2 are non-dimensional center times
of the segments measured w.r.t. the beginning time of the data */
float acc(float *data, int n, float t1, float t2, int half) {
int i;
float am1, am2, amc;
int it1 = floor(t1-half);
int it2 = floor(t2-half);
int len = 2*half;
if ( half<1 || it2<it1 || it1<0 || it2+len>n-1 ) return 0.;
for(am1=0.,am2=0.,amc=0.,i=0; i<len; i++) {
am1 += data[it1+i]*data[it1+i];
am2 += data[it2+i]*data[it2+i];
amc += data[it1+i]*data[it2+i];
}
return amc/sqrt(am1*am2);
}
/* cummulative sum of a(t) */
void cumsum(float *a, int n, float dt) {
int i;
float u;
for(u=0,i=0;i<n;i++) {
u+=a[i]*dt;
a[i] = u;
}
}
/* m-point moving average of a(t), m must be a positive odd integer */
void maver(float *a, int n, int m) {
int i, j, m2;
float aver, c, *b;
m2 = (m-1)/2;
c = 1./m;
b = (float *) calloc(n+m-1, sizeof(float));
memcpy(b+m2, a, n*sizeof(float));
for(aver=0.,j=m2;j<m;j++) {
aver += c*b[j];
}
a[0] = aver;
for(i=1;i<n;i++,j++) {
aver += c*(b[j]-b[j-m]);
a[i] = aver;
}
free(b);
}
/* differentiate of a(t) */
void diffrt(float *a, int n, float dt) {
int i;
for(i=n-1;i>0;i--) {
a[i] = (a[i]-a[i-1])/dt;
}
a[0]=0.;
}
/* square of a(t) */
void sqr(float *a, int n) {
int i;
for(i=0;i<n;i++) {
a[i] *= a[i];
}
}
/* reverse a(t) */
void revers(float *a, int n) {
int i, j;
float dum;
for(i=0,j=n-1;i<j;i++,j--) {
dum = a[i];
a[i] = a[j];
a[j] = dum;
}
}
/* return a symetric taper function of length n
f(i) = 0.5*(1-cos(i*pi/n1)), i=0, n1
f(i) = 1, i=n1, n/2
where n1=w*n, 0<w<0.5
*/
float *coswndw(int n, float w) {
int j, n1;
float t, dt, *wndw;
if ( (wndw=(float *)malloc(n*sizeof(float))) == NULL ) return NULL;
for(j=0;j<n;j++) wndw[j]=1.;
if (w>0.5) w=0.5;
n1 = rint(w*n); if (n1<1) n1=1;
t = 0.;
dt = PI/n1;
for(j=0;j<n1;j++,t+=dt) {
wndw[j]=wndw[n-j-1]=0.5*(1-cos(t));
}
return wndw;
}
/* windowing spectrum d[i], sgn=1 -> high-pass; sgn=-1 -> low-pass */
void filter(complex *d, int n, float f1, float f2, float dt, int sgn) {
int i, if1, if2;
float a;
dt = 0.5/dt/n;
if1 = rint(f1/dt);
if2 = rint(f2/dt); if (if2>=n) if2=n-1;
if (if2<=if1) {
fprintf(stderr, "filter freq. wrong f2<f1\n");
return;
}
dt = PI/(if2-if1);
for(a=0.,i=if1;i<if2;i++,a+=dt) d[i] = dmltp(0.5*(1-sgn*cos(a)), d[i]);
if (sgn<0) {
for(i=if2;i<n;i++) d[i] = Zero;
d[0].y = 0.;
} else {
d[0].x = 0.;
for(i=1;i<if1;i++) d[i] = Zero;
}
}
/* find max. value and location in an array */
int findMax(float *a, int n, float *amp) {
int i,shift=0;
*amp = 0.;
for(i=0;i<n;i++) {
if (a[i]>*amp) {
*amp = a[i];
shift = i;
}
}
return shift;
}
/* find max. absolute value and location in an array */
int findMaxAbs(float *a, int n, float *amp) {
int i,shift=0;
*amp = 0.;
for(i=0;i<n;i++) {
if (fabs(a[i])>fabs(*amp)) {
*amp = a[i];
shift = i;
}
}
return shift;
}
/* remove trend a*i + b */
void rtrend(float *y, int n) {
int i;
double a, b, a11, a12, a22, y1, y2;
y1 = y2 = 0.;
for(i=0;i<n;i++) {
y1 += i*y[i];
y2 += y[i];
}
a12 = 0.5*n*(n-1);
a11 = a12*(2*n-1)/3.;
a22 = n;
b = a11*a22-a12*a12;
a = (a22*y1-a12*y2)/b;
b = (a11*y2-a12*y1)/b;
for(i=0;i<n;i++) {
y[i] = y[i] - a*i - b;
}
}
/* multiply exp(-w^2/(4*gauss^2)) to spectrum u, which is equivalent to
convolve f(t)=(gauss/sqrt(pi))*exp(-(gauss*t)^2) to u(t).
f(t) has unit area. The input gauss is actually gauss*dt */
void fltGauss(complex *u, int n, float gauss) {
int j;
float w, delw, agg;
delw=PI/n;
for(w=delw,j=1;j<n;j++,w+=delw) {
agg = 0.5*w/gauss;
u[j] = dmltp(exp(-agg*agg),u[j]);
}
agg = 0.5*w/gauss;
u[0].y = (exp(-agg*agg))*u[0].y;
}
/* convolve f(t)=(gauss/sqrt(pi))*exp(-(gauss*t)^2) to u(t).
f(t) has unit area. The input gauss is actually gauss*dt */
void GaussLP(float *u, int n, float gauss) {
int i, nft2;
complex *v;
nft2 = 2; while (nft2<n) nft2 *= 2; nft2 /= 2;
v = (complex *) malloc(nft2*sizeof(complex));
for(i=0;i<nft2;i++) v[i] = Zero;
memcpy(v, u, n*sizeof(float));
fftr(v, nft2, 1.);
fltGauss(v, nft2, gauss);
fftr(v, nft2, -1.);
memcpy(u, v, n*sizeof(float));
}
void gausslp_(float *u, int *n, float *gauss) {
GaussLP(u, *n, *gauss);
}
/* compute f(t)=u(t-shift*dt), which is equ. to multiply
exp(-j*w*shift) to its spectrum u*/
void shiftSpec(complex *u, int n, float shift) {
int j;
float w, delw;
delw=PI/n;
for(w=delw,j=1;j<n;j++,w+=delw) {
u[j] = cmltp(cmplx(cos(w*shift),-sin(w*shift)),u[j]);
}
u[0].y = cos(w*shift)*u[0].y;
}
/* compute spectrum power which equals auto_correlation*dt */
float specPwr(complex *u, int n) {
int j;
float a, temp;
for(a=0.,j=0;j<n;j++) {
temp = ccabs(u[j]);
a += temp*temp;
}
return (a/n);
}
/* add spectra a=a+b */
void specAdd(complex *a, complex *b, int n) {
int j;
for(j=0;j<n;j++) a[j] = cplus(a[j], b[j]);
}
/* specMul a=a*b */
void specMul(complex *a, complex *b, int n) {
int j;
a[0]=cmplx(a[0].x*b[0].x, a[0].y*b[0].y);
for (j=1; j<n; j++) a[j]=cmltp(a[j], b[j]);
}
/* multiply spectrum a by a constant c */
void specScale(complex *a, float c, int n) {
int j;
for(j=0;j<n;j++) a[j] = dmltp(c, a[j]);
}