summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/codec2/dump.c
blob: af966e5005573fc228435fc4b015abdefa7ae029 (plain)
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*---------------------------------------------------------------------------*\

  FILE........: dump.c
  AUTHOR......: David Rowe
  DATE CREATED: 25/8/09

  Routines to dump data to text files for Octave analysis.

\*---------------------------------------------------------------------------*/

/*
  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  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 Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "defines.h"
#include "comp.h"
#include "dump.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef __EMBEDDED__
#include "gdb_stdio.h"
#define fprintf gdb_stdio_fprintf
#define fopen gdb_stdio_fopen
#define fclose gdb_stdio_fclose
#endif

#ifdef DUMP
static int dumpon = 0;

static FILE *fsn = NULL;
static FILE *fsw = NULL;
static FILE *few = NULL;
static FILE *fsw_ = NULL;
static FILE *fmodel = NULL;
static FILE *fqmodel = NULL;
static FILE *fpwb = NULL;
static FILE *fpw = NULL;
static FILE *frw = NULL;
static FILE *flsp = NULL;
static FILE *fweights = NULL;
static FILE *flsp_ = NULL;
static FILE *fmel = NULL;
static FILE *fphase = NULL;
static FILE *fphase_ = NULL;
static FILE *ffw = NULL;
static FILE *fe = NULL;
static FILE *fsq = NULL;
static FILE *fdec = NULL;
static FILE *fsnr = NULL;
static FILE *flpcsnr = NULL;
static FILE *fak = NULL;
static FILE *fak_ = NULL;
static FILE *fbg = NULL;
static FILE *fE = NULL;
static FILE *frk = NULL;
static FILE *fhephase = NULL;

static char  prefix[MAX_STR];

void dump_on(char p[]) {
    dumpon = 1;
    strcpy(prefix, p);
}

void dump_off(){
    if (fsn != NULL)
	fclose(fsn);
    if (fsw != NULL)
	fclose(fsw);
    if (fsw_ != NULL)
	fclose(fsw_);
    if (few != NULL)
	fclose(few);
    if (fmodel != NULL)
	fclose(fmodel);
    if (fqmodel != NULL)
	fclose(fqmodel);
    if (fpwb != NULL)
	fclose(fpwb);
    if (fpw != NULL)
	fclose(fpw);
    if (frw != NULL)
	fclose(frw);
    if (flsp != NULL)
	fclose(flsp);
    if (fweights != NULL)
	fclose(fweights);
    if (flsp_ != NULL)
	fclose(flsp_);
    if (fmel != NULL)
	fclose(fmel);
    if (fphase != NULL)
	fclose(fphase);
    if (fphase_ != NULL)
	fclose(fphase_);
    if (ffw != NULL)
	fclose(ffw);
    if (fe != NULL)
	fclose(fe);
    if (fsq != NULL)
	fclose(fsq);
    if (fdec != NULL)
	fclose(fdec);
    if (fsnr != NULL)
	fclose(fsnr);
    if (flpcsnr != NULL)
	fclose(flpcsnr);
    if (fak != NULL)
	fclose(fak);
    if (fak_ != NULL)
	fclose(fak_);
    if (fbg != NULL)
	fclose(fbg);
    if (fE != NULL)
	fclose(fE);
    if (frk != NULL)
	fclose(frk);
    if (fhephase != NULL)
	fclose(fhephase);
}

void dump_Sn(float Sn[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fsn == NULL) {
	sprintf(s,"%s_sn.txt", prefix);
	fsn = fopen(s, "wt");
	assert(fsn != NULL);
    }

    /* split across two lines to avoid max line length problems */
    /* reconstruct in Octave */

    for(i=0; i<M/2; i++)
	fprintf(fsn,"%f\t",Sn[i]);
    fprintf(fsn,"\n");
    for(i=M/2; i<M; i++)
	fprintf(fsn,"%f\t",Sn[i]);
    fprintf(fsn,"\n");
}

void dump_Sw(COMP Sw[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fsw == NULL) {
	sprintf(s,"%s_sw.txt", prefix);
	fsw = fopen(s, "wt");
	assert(fsw != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(fsw,"%f\t",
		10.0*log10(Sw[i].real*Sw[i].real + Sw[i].imag*Sw[i].imag));
    fprintf(fsw,"\n");
}

void dump_Sw_(COMP Sw_[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fsw_ == NULL) {
	sprintf(s,"%s_sw_.txt", prefix);
	fsw_ = fopen(s, "wt");
	assert(fsw_ != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(fsw_,"%f\t",
		10.0*log10(Sw_[i].real*Sw_[i].real + Sw_[i].imag*Sw_[i].imag));
    fprintf(fsw_,"\n");
}

void dump_Ew(COMP Ew[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (few == NULL) {
	sprintf(s,"%s_ew.txt", prefix);
	few = fopen(s, "wt");
	assert(few != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(few,"%f\t",
		10.0*log10(Ew[i].real*Ew[i].real + Ew[i].imag*Ew[i].imag));
    fprintf(few,"\n");
}

void dump_model(MODEL *model) {
    int l;
    char s[MAX_STR];
    char line[2048];

    if (!dumpon) return;

    if (fmodel == NULL) {
	sprintf(s,"%s_model.txt", prefix);
	fmodel = fopen(s, "wt");
	assert(fmodel != NULL);
    }

    sprintf(line,"%12f %12d ", model->Wo, model->L);
    for(l=1; l<=model->L; l++) {
	sprintf(s,"%12f ",model->A[l]);
        strcat(line, s);
    }
    for(l=model->L+1; l<=MAX_AMP; l++) {
	sprintf(s,"%12f ", 0.0);
        strcat(line,s);
    }

    sprintf(s,"%d\n",model->voiced);
    strcat(line,s);
    fprintf(fmodel,"%s",line);
}

void dump_quantised_model(MODEL *model) {
    int l;
    char s[MAX_STR];
    char line[2048];

    if (!dumpon) return;

    if (fqmodel == NULL) {
	sprintf(s,"%s_qmodel.txt", prefix);
	fqmodel = fopen(s, "wt");
	assert(fqmodel != NULL);
    }

    sprintf(line,"%12f %12d ", model->Wo, model->L);
    for(l=1; l<=model->L; l++) {
	sprintf(s,"%12f ",model->A[l]);
        strcat(line, s);
    }
    for(l=model->L+1; l<=MAX_AMP; l++) {
	sprintf(s,"%12f ", 0.0);
        strcat(line, s);
    }

    sprintf(s,"%d\n",model->voiced);
    strcat(line, s);
    fprintf(fqmodel, "%s", line);
}

void dump_phase(float phase[], int L) {
    int l;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fphase == NULL) {
	sprintf(s,"%s_phase.txt", prefix);
	fphase = fopen(s, "wt");
	assert(fphase != NULL);
    }

    for(l=1; l<=L; l++)
	fprintf(fphase,"%f\t",phase[l]);
    for(l=L+1; l<=MAX_AMP; l++)
	fprintf(fphase,"%f\t",0.0);
    fprintf(fphase,"\n");
}

void dump_phase_(float phase_[], int L) {
    int l;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fphase_ == NULL) {
	sprintf(s,"%s_phase_.txt", prefix);
	fphase_ = fopen(s, "wt");
	assert(fphase_ != NULL);
    }

    for(l=1; l<=L; l++)
	fprintf(fphase_,"%f\t",phase_[l]);
    for(l=L+1; l<MAX_AMP; l++)
	fprintf(fphase_,"%f\t",0.0);
    fprintf(fphase_,"\n");
}


void dump_hephase(int ind[], int dim) {
    int m;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fhephase == NULL) {
	sprintf(s,"%s_hephase.txt", prefix);
	fhephase = fopen(s, "wt");
	assert(fhephase != NULL);
    }

    for(m=0; m<dim; m++)
	fprintf(fhephase,"%d\t",ind[m]);
    fprintf(fhephase,"\n");
}


void dump_snr(float snr) {
    char s[MAX_STR];

    if (!dumpon) return;

    if (fsnr == NULL) {
	sprintf(s,"%s_snr.txt", prefix);
	fsnr = fopen(s, "wt");
	assert(fsnr != NULL);
    }

    fprintf(fsnr,"%f\n",snr);
}

void dump_lpc_snr(float snr) {
    char s[MAX_STR];

    if (!dumpon) return;

    if (flpcsnr == NULL) {
	sprintf(s,"%s_lpc_snr.txt", prefix);
	flpcsnr = fopen(s, "wt");
	assert(flpcsnr != NULL);
    }

    fprintf(flpcsnr,"%f\n",snr);
}

/* Pw "before" post filter so we can plot before and after */

void dump_Pwb(COMP Pwb[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fpwb == NULL) {
	sprintf(s,"%s_pwb.txt", prefix);
	fpwb = fopen(s, "wt");
	assert(fpwb != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(fpwb,"%f\t",Pwb[i].real);
    fprintf(fpwb,"\n");
}

void dump_Pw(COMP Pw[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fpw == NULL) {
	sprintf(s,"%s_pw.txt", prefix);
	fpw = fopen(s, "wt");
	assert(fpw != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(fpw,"%f\t",Pw[i].real);
    fprintf(fpw,"\n");
}

void dump_Rw(float Rw[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (frw == NULL) {
	sprintf(s,"%s_rw.txt", prefix);
	frw = fopen(s, "wt");
	assert(frw != NULL);
    }

    for(i=0; i<FFT_ENC/2; i++)
	fprintf(frw,"%f\t",Rw[i]);
    fprintf(frw,"\n");
}

void dump_weights(float w[], int order) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fweights == NULL) {
	sprintf(s,"%s_weights.txt", prefix);
	fweights = fopen(s, "wt");
	assert(fweights != NULL);
    }

    for(i=0; i<order; i++)
	fprintf(fweights,"%f\t", w[i]);
    fprintf(fweights,"\n");
}

void dump_lsp(float lsp[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (flsp == NULL) {
	sprintf(s,"%s_lsp.txt", prefix);
	flsp = fopen(s, "wt");
	assert(flsp != NULL);
    }

    for(i=0; i<10; i++)
	fprintf(flsp,"%f\t",lsp[i]);
    fprintf(flsp,"\n");
}

void dump_lsp_(float lsp_[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (flsp_ == NULL) {
	sprintf(s,"%s_lsp_.txt", prefix);
	flsp_ = fopen(s, "wt");
	assert(flsp_ != NULL);
    }

    for(i=0; i<10; i++)
	fprintf(flsp_,"%f\t",lsp_[i]);
    fprintf(flsp_,"\n");
}

void dump_mel(int mel[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fmel == NULL) {
	sprintf(s,"%s_mel.txt", prefix);
	fmel = fopen(s, "wt");
	assert(fmel != NULL);
    }

    for(i=0; i<10; i++)
	fprintf(fmel,"%d\t",mel[i]);
    fprintf(fmel,"\n");
}

void dump_ak(float ak[], int order) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fak == NULL) {
	sprintf(s,"%s_ak.txt", prefix);
	fak = fopen(s, "wt");
	assert(fak != NULL);
    }

    for(i=0; i<=order; i++)
	fprintf(fak,"%f\t",ak[i]);
    fprintf(fak,"\n");
}

void dump_ak_(float ak_[], int order) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fak_ == NULL) {
	sprintf(s,"%s_ak_.txt", prefix);
	fak_ = fopen(s, "wt");
	assert(fak_ != NULL);
    }

    for(i=0; i<=order; i++)
	fprintf(fak_,"%f\t",ak_[i]);
    fprintf(fak_,"\n");
}

void dump_Fw(COMP Fw[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (ffw == NULL) {
	sprintf(s,"%s_fw.txt", prefix);
	ffw = fopen(s, "wt");
	assert(ffw != NULL);
    }

    for(i=0; i<256; i++)
	fprintf(ffw,"%f\t",Fw[i].real);
    fprintf(ffw,"\n");
}

void dump_e(float e_hz[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fe == NULL) {
	sprintf(s,"%s_e.txt", prefix);
	fe = fopen(s, "wt");
	assert(fe != NULL);
    }

    for(i=0; i<500/2; i++)
	fprintf(fe,"%f\t",e_hz[i]);
    fprintf(fe,"\n");
    for(i=500/2; i<500; i++)
	fprintf(fe,"%f\t",e_hz[i]);
    fprintf(fe,"\n");
}

void dump_sq(float sq[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fsq == NULL) {
	sprintf(s,"%s_sq.txt", prefix);
	fsq = fopen(s, "wt");
	assert(fsq != NULL);
    }

    for(i=0; i<M/2; i++)
	fprintf(fsq,"%f\t",sq[i]);
    fprintf(fsq,"\n");
    for(i=M/2; i<M; i++)
	fprintf(fsq,"%f\t",sq[i]);
    fprintf(fsq,"\n");
}

void dump_dec(COMP Fw[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (fdec == NULL) {
	sprintf(s,"%s_dec.txt", prefix);
	fdec = fopen(s, "wt");
	assert(fdec != NULL);
    }

    for(i=0; i<320/5; i++)
	fprintf(fdec,"%f\t",Fw[i].real);
    fprintf(fdec,"\n");
}

void dump_bg(float e, float bg_est, float percent_uv) {
    char s[MAX_STR];

    if (!dumpon) return;

    if (fbg == NULL) {
	sprintf(s,"%s_bg.txt", prefix);
	fbg = fopen(s, "wt");
	assert(fbg != NULL);
    }

    fprintf(fbg,"%f\t%f\t%f\n", e, bg_est, percent_uv);
}

void dump_E(float E) {
    char s[MAX_STR];

    if (!dumpon) return;

    if (fE == NULL) {
	sprintf(s,"%s_E.txt", prefix);
	fE = fopen(s, "wt");
	assert(fE != NULL);
    }

    fprintf(fE,"%f\n", 10.0*log10(E));
}

void dump_Rk(float Rk[]) {
    int i;
    char s[MAX_STR];

    if (!dumpon) return;

    if (frk == NULL) {
	sprintf(s,"%s_rk.txt", prefix);
	frk = fopen(s, "wt");
	assert(frk != NULL);
    }

    for(i=0; i<P_MAX; i++)
	fprintf(frk,"%f\t",Rk[i]);
    fprintf(frk,"\n");
}

#endif