-
Notifications
You must be signed in to change notification settings - Fork 13
/
tst_cifio.c
297 lines (261 loc) · 6.91 KB
/
tst_cifio.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
#ifndef ARDUINO
// tst_cifio.c - test gammatone-filterbank i/o with impulse signal
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <sigpro.h>
#include "chapro.h"
#define DATA_HDR "tst_cifio_data.h"
//#include DATA_HDR
typedef struct {
char *ifn, *ofn, cs, mat;
double rate;
float *iwav, *owav;
int32_t *siz;
int32_t iod, nwav, nsmp, mseg, nseg, oseg, pseg;
void **out;
} I_O;
static double target_delay = 4;
static struct {
char *ifn, *ofn, mat, tone_io;
double gn;
int ds;
} args;
static CHA_CLS cls;
/***********************************************************/
// initialize io
static void
usage()
{
fprintf(stdout, "usage: tst_cifio [-options]\n");
fprintf(stdout, "options\n");
fprintf(stdout, "-c N compress with gain=N (dB) [0]\n");
fprintf(stdout, "-d N set downsample factor to N [24]\n");
fprintf(stdout, "-h print help\n");
fprintf(stdout, "-k N compression kneepoint=N (dB) [0]\n");
fprintf(stdout, "-t tone response [default is impulse]\n");
fprintf(stdout, "-v print version\n");
exit(0);
}
static void
version()
{
fprintf(stdout, "%s\n", cha_version());
exit(0);
}
static void
parse_args(int ac, char *av[])
{
args.ds = 0;
args.gn = 0;
args.tone_io = 0;
while (ac > 1) {
if (av[1][0] == '-') {
if (av[1][1] == 'c') {
args.gn = atof(av[2]);
ac--;
av++;
} else if (av[1][1] == 'd') {
args.ds = atoi(av[2]);
ac--;
av++;
} else if (av[1][1] == 'h') {
usage();
} else if (av[1][1] == 't') {
args.tone_io = 1;
} else if (av[1][1] == 'v') {
version();
}
ac--;
av++;
} else {
break;
}
}
}
static void
init_wav(I_O *io)
{
float f, p;
int i;
/* second impulse input */
io->nwav = round(io->rate);
io->iwav = (float *) calloc(io->nwav, sizeof(float));
fprintf(stdout, "filterbank i/o with ");
if (args.tone_io == 0) {
fprintf(stdout, "impulse: \n");
io->ofn = "test/cifio_impulse.mat";
io->iwav[0] = 1;
} else {
fprintf(stdout, "tone: \n");
f = 1000;
p = (float) ((2 * M_PI * f) / io->rate);
io->ofn = "test/cifio_tone.mat";
for (i = 0; i < io->nwav; i++) {
io->iwav[i] = (float) sin(i * p);
}
}
io->nsmp = io->nwav;
io->mseg = 1;
io->nseg = 1;
io->owav = (float *) calloc(io->nsmp, sizeof(float));
}
static void
write_wave(I_O *io)
{
char *ft;
float r[1], *x, *y;
int n;
static VAR *vl;
ft = "MAT";
fprintf(stdout, "%s output: %s\n", ft, io->ofn);
remove(io->ofn);
n = io->nwav;
x = io->iwav;
y = io->owav;
r[0] = (float) io->rate;
vl = sp_var_alloc(3);
sp_var_add(vl, "rate", r, 1, 1, "f4");
sp_var_add(vl, "x", x, n, 1, "f4");
sp_var_add(vl, "y", y, n, 1, "f4");
sp_mat_save(io->ofn, vl);
sp_var_clear(vl);
}
/***********************************************************/
// specify filterbank center frequencies and bandwidths
static double
cgtfb_init(CHA_CLS *cls, double sr, int nm, int cpo)
{
float lfbw, fmid = 1000;
int i, nh, nc;
lfbw = fmid / nm;
nh = (int) floor(log2((float)sr / 2000) * cpo);
nc = nh + nm;
cls->nc = nc;
for (i = 0; i < (nm - 1); i++) {
cls->fc[i] = lfbw * (i + 1);
cls->bw[i] = lfbw;
}
cls->fc[nm - 1] = fmid;
cls->bw[nm - 1] = fmid * (pow(2.0, 0.5 / cpo) - (nm - 0.5) / nm);
for (i = nm; i < nc; i++) {
cls->fc[i] = fmid * pow(2.0, (i - nm + 1.0) / cpo);
cls->bw[i] = fmid * (pow(2.0, (i - nm + 1.5) / cpo) - pow(2.0, (i - nm + 0.5) / cpo));
}
return (400 / lfbw);
}
// CSL prescription
static void
compressor_init(CHA_CLS *cls, double gn)
{
int k, nc;
// set compression mode
cls->cm = 1;
// loop over filterbank channel
nc = cls->nc;
for (k = 0; k < nc; k++) {
cls->Lcs[k] = 0;
cls->Lcm[k] = 50;
cls->Lce[k] = 100;
cls->Lmx[k] = 120;
cls->Gcs[k] = (float) gn;
cls->Gcm[k] = (float) gn / 2;
cls->Gce[k] = 0;
cls->Gmx[k] = 90;
}
}
/***********************************************************/
// prepare filterbank
static void
prepare_filterbank(CHA_PTR cp)
{
double gd, *fc, *bw;
float z[256], p[256], g[64];
int nc, d[32];
static double sr = 24000; // sampling rate (Hz)
static int cs = 32; // chunk size
static int nm = 5; // number of frequency bands below 1 kHz
static int po = 3; // number of bands per octave above 1 kHz
static int no = 4; // gammatone filter order
gd = target_delay = cgtfb_init(&cls, sr, nm, po);
// prepare filterbank
nc = cls.nc;
fc = cls.fc;
bw = cls.bw;
cha_ciirfb_design(z, p, g, d, nc, fc, bw, sr, gd);
cha_ciirfb_prepare(cp, z, p, g, d, nc, no, sr, cs);
}
// prepare signal processing
static void
prepare(I_O *io, CHA_PTR cp)
{
double fs, gd, sr;
static double lr = 2e-5; // signal-level reference (Pa)
static double gn = 0; // flat suppressor gain (dB)
static int ds = 24; // downsample factor
prepare_filterbank(cp);
fs = CHA_DVAR[_fs];
gd = target_delay;
sr = fs * 1000;
if (args.ds) ds = args.ds;
if (args.gn) gn = args.gn;
// prepare compressor
compressor_init(&cls, gn);
cha_icmp_prepare(cp, &cls, sr, lr, ds);
// initialize waveform
io->rate = sr;
io->ifn = args.ifn;
io->ofn = args.ofn;
init_wav(io);
// generate C code from prepared data
//cha_data_gen(cp, DATA_HDR);
// report
fprintf(stdout, "CHA I/O simulation: sampling rate=%.0f kHz, ", fs);
fprintf(stdout, "filterbank gd=%.1f ms; ", gd);
fprintf(stdout, "compression: gain=%.0f, ds=%d\n", gn, ds);
}
// process signal
static void
process(I_O *io, CHA_PTR cp)
{
float *x, *y, *z;
int j, cs, ns, nk;
// next line switches to compiled data
//cp = (CHA_PTR) cha_data;
// initialize i/o pointers
x = io->iwav;
y = io->owav;
z = CHA_CB;
ns = io->nsmp;
// process gammatone filterbank
cs = CHA_IVAR[_cs]; // chunk size
nk = ns / cs; // number of chunks
for (j = 0; j < nk; j++) {
cha_ciirfb_analyze(cp, x + j * cs, z, cs);
cha_icmp_process(cp, z, z, cs);
cha_ciirfb_synthesize(cp, z, y + j * cs, cs);
}
}
// clean up io
static void
cleanup(I_O *io, CHA_PTR cp)
{
write_wave(io);
cha_cleanup(cp);
}
/***********************************************************/
int
main(int ac, char *av[])
{
static I_O io;
static void *cp[NPTR] = {0};
parse_args(ac, av);
prepare(&io, cp);
process(&io, cp);
cleanup(&io, cp);
return (0);
}
#endif