forked from g0orx/wdsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfcomp.c
472 lines (437 loc) · 12.6 KB
/
cfcomp.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
/* cfcomp.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2017 Warren Pratt, NR0V
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The author can be reached by email at
*/
#include "comm.h"
void calc_cfcwindow (CFCOMP a)
{
int i;
double arg0, arg1, cgsum, igsum, coherent_gain, inherent_power_gain, wmult;
switch (a->wintype)
{
case 0:
arg0 = 2.0 * PI / (double)a->fsize;
cgsum = 0.0;
igsum = 0.0;
for (i = 0; i < a->fsize; i++)
{
a->window[i] = sqrt (0.54 - 0.46 * cos((double)i * arg0));
cgsum += a->window[i];
igsum += a->window[i] * a->window[i];
}
coherent_gain = cgsum / (double)a->fsize;
inherent_power_gain = igsum / (double)a->fsize;
wmult = 1.0 / sqrt (inherent_power_gain);
for (i = 0; i < a->fsize; i++)
a->window[i] *= wmult;
a->winfudge = sqrt (1.0 / coherent_gain);
break;
case 1:
arg0 = 2.0 * PI / (double)a->fsize;
cgsum = 0.0;
igsum = 0.0;
for (i = 0; i < a->fsize; i++)
{
arg1 = cos(arg0 * (double)i);
a->window[i] = sqrt (+0.21747
+ arg1 * (-0.45325
+ arg1 * (+0.28256
+ arg1 * (-0.04672))));
cgsum += a->window[i];
igsum += a->window[i] * a->window[i];
}
coherent_gain = cgsum / (double)a->fsize;
inherent_power_gain = igsum / (double)a->fsize;
wmult = 1.0 / sqrt (inherent_power_gain);
for (i = 0; i < a->fsize; i++)
a->window[i] *= wmult;
a->winfudge = sqrt (1.0 / coherent_gain);
break;
}
}
int fCOMPcompare (const void * a, const void * b)
{
if (*(double*)a < *(double*)b)
return -1;
else if (*(double*)a == *(double*)b)
return 0;
else
return 1;
}
void calc_comp (CFCOMP a)
{
int i, j;
double f, frac, fincr, fmax;
double* sary;
a->precomplin = pow (10.0, 0.05 * a->precomp);
a->prepeqlin = pow (10.0, 0.05 * a->prepeq);
fmax = 0.5 * a->rate;
for (i = 0; i < a->nfreqs; i++)
{
a->F[i] = max (a->F[i], 0.0);
a->F[i] = min (a->F[i], fmax);
a->G[i] = max (a->G[i], 0.0);
}
sary = (double *)malloc0 (3 * a->nfreqs * sizeof (double));
for (i = 0; i < a->nfreqs; i++)
{
sary[3 * i + 0] = a->F[i];
sary[3 * i + 1] = a->G[i];
sary[3 * i + 2] = a->E[i];
}
qsort (sary, a->nfreqs, 3 * sizeof (double), fCOMPcompare);
for (i = 0; i < a->nfreqs; i++)
{
a->F[i] = sary[3 * i + 0];
a->G[i] = sary[3 * i + 1];
a->E[i] = sary[3 * i + 2];
}
_aligned_free (sary);
a->fp[0] = 0.0;
a->fp[a->nfreqs + 1] = fmax;
a->gp[0] = a->G[0];
a->gp[a->nfreqs + 1] = a->G[a->nfreqs - 1];
a->ep[0] = a->E[0]; // cutoff?
a->ep[a->nfreqs + 1] = a->E[a->nfreqs - 1]; // cutoff?
for (i = 0, j = 1; i < a->nfreqs; i++, j++)
{
a->fp[j] = a->F[i];
a->gp[j] = a->G[i];
a->ep[j] = a->E[i];
}
fincr = a->rate / (double)a->fsize;
j = 0;
// print_impulse ("gp.txt", a->nfreqs+2, a->gp, 0, 0);
for (i = 0; i < a->msize; i++)
{
f = fincr * (double)i;
while (f >= a->fp[j + 1] && j < a->nfreqs) j++;
frac = (f - a->fp[j]) / (a->fp[j + 1] - a->fp[j]);
a->comp[i] = pow (10.0, 0.05 * (frac * a->gp[j + 1] + (1.0 - frac) * a->gp[j]));
a->peq[i] = pow (10.0, 0.05 * (frac * a->ep[j + 1] + (1.0 - frac) * a->ep[j]));
}
// print_impulse ("comp.txt", a->msize, a->comp, 0, 0);
}
void calc_cfcomp(CFCOMP a)
{
int i;
a->incr = a->fsize / a->ovrlp;
if (a->fsize > a->bsize)
a->iasize = a->fsize;
else
a->iasize = a->bsize + a->fsize - a->incr;
a->iainidx = 0;
a->iaoutidx = 0;
if (a->fsize > a->bsize)
{
if (a->bsize > a->incr) a->oasize = a->bsize;
else a->oasize = a->incr;
a->oainidx = (a->fsize - a->bsize - a->incr) % a->oasize;
}
else
{
a->oasize = a->bsize;
a->oainidx = a->fsize - a->incr;
}
a->init_oainidx = a->oainidx;
a->oaoutidx = 0;
a->msize = a->fsize / 2 + 1;
a->window = (double *)malloc0 (a->fsize * sizeof(double));
a->inaccum = (double *)malloc0 (a->iasize * sizeof(double));
a->forfftin = (double *)malloc0 (a->fsize * sizeof(double));
a->forfftout = (double *)malloc0 (a->msize * sizeof(complex));
a->mask = (double *)malloc0 (a->msize * sizeof(double));
a->revfftin = (double *)malloc0 (a->msize * sizeof(complex));
a->revfftout = (double *)malloc0 (a->fsize * sizeof(double));
a->save = (double **)malloc0(a->ovrlp * sizeof(double *));
for (i = 0; i < a->ovrlp; i++)
a->save[i] = (double *)malloc0(a->fsize * sizeof(double));
a->outaccum = (double *)malloc0(a->oasize * sizeof(double));
a->nsamps = 0;
a->saveidx = 0;
a->Rfor = fftw_plan_dft_r2c_1d(a->fsize, a->forfftin, (fftw_complex *)a->forfftout, FFTW_ESTIMATE);
a->Rrev = fftw_plan_dft_c2r_1d(a->fsize, (fftw_complex *)a->revfftin, a->revfftout, FFTW_ESTIMATE);
calc_cfcwindow(a);
a->pregain = (2.0 * a->winfudge) / (double)a->fsize;
a->postgain = 0.5 / ((double)a->ovrlp * a->winfudge);
a->fp = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
a->gp = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
a->ep = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
a->comp = (double *) malloc0 (a->msize * sizeof (double));
a->peq = (double *) malloc0 (a->msize * sizeof (double));
calc_comp (a);
a->gain = 0.0;
a->mmult = exp (-1.0 / (a->rate * a->ovrlp * a->mtau));
}
void decalc_cfcomp(CFCOMP a)
{
int i;
_aligned_free (a->peq);
_aligned_free (a->comp);
_aligned_free (a->ep);
_aligned_free (a->gp);
_aligned_free (a->fp);
fftw_destroy_plan(a->Rrev);
fftw_destroy_plan(a->Rfor);
_aligned_free(a->outaccum);
for (i = 0; i < a->ovrlp; i++)
_aligned_free(a->save[i]);
_aligned_free(a->save);
_aligned_free(a->revfftout);
_aligned_free(a->revfftin);
_aligned_free(a->mask);
_aligned_free(a->forfftout);
_aligned_free(a->forfftin);
_aligned_free(a->inaccum);
_aligned_free(a->window);
}
CFCOMP create_cfcomp (int run, int position, int peq_run, int size, double* in, double* out, int fsize, int ovrlp,
int rate, int wintype, int comp_method, int nfreqs, double precomp, double prepeq, double* F, double* G, double* E, double mtau)
{
CFCOMP a = (CFCOMP) malloc0 (sizeof (cfcomp));
a->run = run;
a->position = position;
a->peq_run = peq_run;
a->bsize = size;
a->in = in;
a->out = out;
a->fsize = fsize;
a->ovrlp = ovrlp;
a->rate = rate;
a->wintype = wintype;
a->comp_method = comp_method;
a->nfreqs = nfreqs;
a->precomp = precomp;
a->prepeq = prepeq;
a->mtau = mtau; // compression metering time constant
a->F = (double *)malloc0 (a->nfreqs * sizeof (double));
a->G = (double *)malloc0 (a->nfreqs * sizeof (double));
a->E = (double *)malloc0 (a->nfreqs * sizeof (double));
memcpy (a->F, F, a->nfreqs * sizeof (double));
memcpy (a->G, G, a->nfreqs * sizeof (double));
memcpy (a->E, E, a->nfreqs * sizeof (double));
calc_cfcomp (a);
return a;
}
void flush_cfcomp (CFCOMP a)
{
int i;
memset (a->inaccum, 0, a->iasize * sizeof (double));
for (i = 0; i < a->ovrlp; i++)
memset (a->save[i], 0, a->fsize * sizeof (double));
memset (a->outaccum, 0, a->oasize * sizeof (double));
a->nsamps = 0;
a->iainidx = 0;
a->iaoutidx = 0;
a->oainidx = a->init_oainidx;
a->oaoutidx = 0;
a->saveidx = 0;
a->gain = 0.0;
}
void destroy_cfcomp (CFCOMP a)
{
decalc_cfcomp (a);
_aligned_free (a->E);
_aligned_free (a->G);
_aligned_free (a->F);
_aligned_free (a);
}
void calc_mask (CFCOMP a)
{
int i;
double comp, mask;
switch (a->comp_method)
{
case 0:
{
double mag, test;
for (i = 0; i < a->msize; i++)
{
mag = sqrt (a->forfftout[2 * i + 0] * a->forfftout[2 * i + 0]
+ a->forfftout[2 * i + 1] * a->forfftout[2 * i + 1]);
comp = a->precomplin * a->comp[i];
test = comp * mag;
if (test > 1.0)
mask = 1.0 / mag;
else
mask = comp;
a->mask[i] = mask;
if (test > a->gain) a->gain = test;
else a->gain = a->mmult * a->gain;
}
break;
}
}
if (a->peq_run)
{
for (i = 0; i < a->msize; i++)
{
a->mask[i] *= a->prepeqlin * a->peq[i];
}
}
}
void xcfcomp (CFCOMP a, int pos)
{
if (a->run && pos == a->position)
{
int i, j, k, sbuff, sbegin;
for (i = 0; i < 2 * a->bsize; i += 2)
{
a->inaccum[a->iainidx] = a->in[i];
a->iainidx = (a->iainidx + 1) % a->iasize;
}
a->nsamps += a->bsize;
while (a->nsamps >= a->fsize)
{
for (i = 0, j = a->iaoutidx; i < a->fsize; i++, j = (j + 1) % a->iasize)
a->forfftin[i] = a->pregain * a->window[i] * a->inaccum[j];
a->iaoutidx = (a->iaoutidx + a->incr) % a->iasize;
a->nsamps -= a->incr;
fftw_execute (a->Rfor);
calc_mask(a);
for (i = 0; i < a->msize; i++)
{
a->revfftin[2 * i + 0] = a->mask[i] * a->forfftout[2 * i + 0];
a->revfftin[2 * i + 1] = a->mask[i] * a->forfftout[2 * i + 1];
}
fftw_execute (a->Rrev);
for (i = 0; i < a->fsize; i++)
a->save[a->saveidx][i] = a->postgain * a->window[i] * a->revfftout[i];
for (i = a->ovrlp; i > 0; i--)
{
sbuff = (a->saveidx + i) % a->ovrlp;
sbegin = a->incr * (a->ovrlp - i);
for (j = sbegin, k = a->oainidx; j < a->incr + sbegin; j++, k = (k + 1) % a->oasize)
{
if ( i == a->ovrlp)
a->outaccum[k] = a->save[sbuff][j];
else
a->outaccum[k] += a->save[sbuff][j];
}
}
a->saveidx = (a->saveidx + 1) % a->ovrlp;
a->oainidx = (a->oainidx + a->incr) % a->oasize;
}
for (i = 0; i < a->bsize; i++)
{
a->out[2 * i + 0] = a->outaccum[a->oaoutidx];
a->out[2 * i + 1] = 0.0;
a->oaoutidx = (a->oaoutidx + 1) % a->oasize;
}
}
else if (a->out != a->in)
memcpy (a->out, a->in, a->bsize * sizeof (complex));
}
void setBuffers_cfcomp (CFCOMP a, double* in, double* out)
{
a->in = in;
a->out = out;
}
void setSamplerate_cfcomp (CFCOMP a, int rate)
{
decalc_cfcomp (a);
a->rate = rate;
calc_cfcomp (a);
}
void setSize_cfcomp (CFCOMP a, int size)
{
decalc_cfcomp (a);
a->bsize = size;
calc_cfcomp (a);
}
/********************************************************************************************************
* *
* TXA Properties *
* *
********************************************************************************************************/
PORT
void SetTXACFCOMPRun (int channel, int run)
{
CFCOMP a = txa[channel].cfcomp.p;
if (a->run != run)
{
EnterCriticalSection (&ch[channel].csDSP);
a->run = run;
LeaveCriticalSection (&ch[channel].csDSP);
}
}
PORT
void SetTXACFCOMPPosition (int channel, int pos)
{
CFCOMP a = txa[channel].cfcomp.p;
if (a->position != pos)
{
EnterCriticalSection (&ch[channel].csDSP);
a->position = pos;
LeaveCriticalSection (&ch[channel].csDSP);
}
}
PORT
void SetTXACFCOMPprofile (int channel, int nfreqs, double* F, double* G, double *E)
{
CFCOMP a = txa[channel].cfcomp.p;
EnterCriticalSection (&ch[channel].csDSP);
a->nfreqs = nfreqs;
_aligned_free (a->E);
_aligned_free (a->F);
_aligned_free (a->G);
a->F = (double *)malloc0 (a->nfreqs * sizeof (double));
a->G = (double *)malloc0 (a->nfreqs * sizeof (double));
a->E = (double *)malloc0 (a->nfreqs * sizeof (double));
memcpy (a->F, F, a->nfreqs * sizeof (double));
memcpy (a->G, G, a->nfreqs * sizeof (double));
memcpy (a->E, E, a->nfreqs * sizeof (double));
_aligned_free (a->ep);
_aligned_free (a->gp);
_aligned_free (a->fp);
a->fp = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
a->gp = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
a->ep = (double *) malloc0 ((a->nfreqs + 2) * sizeof (double));
calc_comp(a);
LeaveCriticalSection (&ch[channel].csDSP);
}
PORT
void SetTXACFCOMPPrecomp (int channel, double precomp)
{
CFCOMP a = txa[channel].cfcomp.p;
if (a->precomp != precomp)
{
EnterCriticalSection (&ch[channel].csDSP);
a->precomp = precomp;
a->precomplin = pow (10.0, 0.05 * a->precomp);
LeaveCriticalSection (&ch[channel].csDSP);
}
}
PORT
void SetTXACFCOMPPeqRun (int channel, int run)
{
CFCOMP a = txa[channel].cfcomp.p;
if (a->peq_run != run)
{
EnterCriticalSection (&ch[channel].csDSP);
a->peq_run = run;
LeaveCriticalSection (&ch[channel].csDSP);
}
}
PORT
void SetTXACFCOMPPrePeq (int channel, double prepeq)
{
CFCOMP a = txa[channel].cfcomp.p;
EnterCriticalSection (&ch[channel].csDSP);
a->prepeq = prepeq;
a->prepeqlin = pow (10.0, 0.05 * a->prepeq);
LeaveCriticalSection (&ch[channel].csDSP);
}