-
Notifications
You must be signed in to change notification settings - Fork 13
/
cfirfb_prepare.c
196 lines (176 loc) · 4.69 KB
/
cfirfb_prepare.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
// cfirfb_prepare.c - complex FIR-filterbank preparation functions
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "chapro.h"
/***********************************************************/
// expand real to complex
static void
rc(float *x, int n)
{
int i, j, jr, ji;
for (i = 0; i < n; i++) {
j = (n - 1) - i;
jr = 2 * j;
ji = 2 * j + 1;
x[jr] = x[j];
x[ji] = 0;
}
}
// generate analytic signal
static void
anasig(float *x, int n)
{
int i;
rc(x, n);
cha_fft(x, n);
for (i = 2; i < n; i++) {
x[i] *= 2;
x[i + n] = 0;
}
cha_ifft(x, n);
}
/***********************************************************/
// compute FIR-filterbank coefficients
static __inline void
fir_filterbank(float *bb, double *cf, int nc, int nw, int wt, double sr)
{
double p, w, a = 0.16;
float *ww, *bk, *xx, *yy;
int j, k, kk, nt, nf, ns, *be;
nt = nw * 2;
nf = nw + 1;
ns = nf * 2;
be = (int *) calloc(nc + 1, sizeof(int));
ww = (float *) calloc(nw, sizeof(float));
xx = (float *) calloc(ns, sizeof(float));
yy = (float *) calloc(ns, sizeof(float));
// window
for (j = 0; j < nw; j++) {
p = M_PI * (2.0 * j - nw) / nw;
if (wt == 0) {
w = 0.54 + 0.46 * cos(p); // Hamming
} else {
w = (1 - a + cos(p) + a * cos(2 * p)) / 2; // Blackman
}
ww[j] = (float) w;
}
// frequency bands
be[0] = 0;
for (k = 1; k < nc; k++) {
kk = round(nf * cf[k - 1] * (2 / sr));
be[k] = (kk > nf) ? nf : kk;
}
be[nc] = nf;
// channel tranfer functions
fzero(xx, ns);
xx[nw / 2] = 1;
cha_fft_rc(xx, nt);
for (k = 0; k < nc; k++) {
bk = bb + k * nw;
fzero(yy, ns);
fcopy(yy + be[k] * 2, xx + be[k] * 2, (be[k + 1] - be[k]) * 2);
cha_fft_cr(yy, nt);
// apply window to iFFT of bandpass
for (j = 0; j < nw; j++) {
yy[j] *= ww[j];
}
fcopy(bk, yy, nw);
}
free(be);
free(ww);
free(xx);
free(yy);
}
// Fourier-transform FIR coefficients for short chunk (cs < nw)
static __inline void
fir_transform_sc(float *bb, float *hh, int nc, int nw, int cs)
{
float *bk, *hk;
int j, k, nk, ns, nt;
nk = nw / cs;
nt = cs * 2;
ns = nt * 2;
bk = (float *) calloc(nw * 2, sizeof(float));
for (k = 0; k < nc; k++) {
fcopy(bk, bb + k * nw, nw);
anasig(bk, nw);
for (j = 0; j < nk; j++) {
hk = hh + (k * nk + j) * ns;
fcopy(hk, bk + j * nt, nt);
cha_fft(hk, nt);
}
}
free(bk);
}
// Fourier-transform FIR coefficients for long chunk (cs >= nw)
static __inline void
fir_transform_lc(float *bb, float *hh, int nc, int nw, int cs)
{
float *hk;
int k, ns, nt;
nt = nw * 2;
ns = nt * 2;
for (k = 0; k < nc; k++) {
hk = hh + k * ns;
fcopy(hk, bb + k * nw, nw);
anasig(hk, nt);
cha_fft(hk, nt);
}
}
/***********************************************************/
// save bandwidth
static __inline void
save_bw(CHA_PTR cp, double *cf, int nc)
{
float *fck, *bwk, pcf;
int i;
fck = (float *) cha_allocate(cp, nc, sizeof(float), _fc);
bwk = (float *) cha_allocate(cp, nc, sizeof(float), _bw);
pcf = 0;
for (i = 0; i < nc; i++) {
fck[i] = (float) (cf[i] + pcf) / 2000;
bwk[i] = (float) (cf[i] - pcf) / 1000;
pcf = (float) cf[i];
}
}
/***********************************************************/
FUNC(int)
cha_cfirfb_prepare(CHA_PTR cp, double *cf, int nc, double sr,
int nw, int wt, int cs)
{
float *bb, *hh;
int ns, nt, nh;
if (cs <= 0) {
return (1);
}
cha_prepare(cp);
CHA_IVAR[_cs] = cs;
CHA_DVAR[_fs] = sr / 1000;
// allocate window buffers
CHA_IVAR[_nw] = nw;
CHA_IVAR[_nc] = nc;
nt = nw * 2;
ns = nt + 2;
nh = (cs < nw) ? (cs * 4) * (nw / cs) : (nw * 4);
cha_allocate(cp, ns * 2, sizeof(float), _ffxx);
cha_allocate(cp, ns * 2, sizeof(float), _ffyy);
cha_allocate(cp, nc * (nw + cs) * 2, sizeof(float), _ffzz);
cha_allocate(cp, nc * nh, sizeof(float), _ffhh);
// save bandwidths
save_bw(cp, cf, nc);
// compute FIR-filterbank coefficients
bb = calloc(nc * nw, sizeof(float));
fir_filterbank(bb, cf, nc, nw, wt, sr);
// Fourier-transform FIR coefficients
hh = cp[_ffhh];
if (cs < nw) { // short chunk
fir_transform_sc(bb, hh, nc, nw, cs);
} else { // long chunk
fir_transform_lc(bb, hh, nc, nw, cs);
}
free(bb);
// allocate chunk buffer
cha_allocate(cp, nc * cs * 2, sizeof(float), _cc);
return (0);
}