1 /*---------------------------------------------------------------------------*\
2
3 FILE........: dump.c
4 AUTHOR......: David Rowe
5 DATE CREATED: 25/8/09
6
7 Routines to dump data to text files for Octave analysis.
8
9 \*---------------------------------------------------------------------------*/
10
11 /*
12 All rights reserved.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License version 2.1, as
16 published by the Free Software Foundation. This program is
17 distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 License for more details.
21
22 You should have received a copy of the GNU Lesser General Public License
23 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "defines.h"
27 #include "comp.h"
28 #include "dump.h"
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34
35 #ifdef DUMP
36 static int dumpon = 0;
37
38 static FILE *fsn = NULL;
39 static FILE *fsw = NULL;
40 static FILE *few = NULL;
41 static FILE *fsw_ = NULL;
42 static FILE *fsoftdec = NULL;
43 static FILE *fmodel = NULL;
44 static FILE *fqmodel = NULL;
45 static FILE *fpwb = NULL;
46 static FILE *fpw = NULL;
47 static FILE *frw = NULL;
48 static FILE *flsp = NULL;
49 static FILE *fweights = NULL;
50 static FILE *flsp_ = NULL;
51 static FILE *fmel = NULL;
52 static FILE *fmel_indexes = NULL;
53 static FILE *fphase = NULL;
54 static FILE *fphase_ = NULL;
55 static FILE *ffw = NULL;
56 static FILE *fe = NULL;
57 static FILE *fsq = NULL;
58 static FILE *fdec = NULL;
59 static FILE *fsnr = NULL;
60 static FILE *flpcsnr = NULL;
61 static FILE *fak = NULL;
62 static FILE *fak_ = NULL;
63 static FILE *fbg = NULL;
64 static FILE *fE = NULL;
65 static FILE *frk = NULL;
66 static FILE *fhephase = NULL;
67
68 static char prefix[MAX_STR];
69
dump_on(char p[])70 void dump_on(char p[]) {
71 dumpon = 1;
72 strcpy(prefix, p);
73 }
74
dump_off()75 void dump_off(){
76 if (fsn != NULL)
77 fclose(fsn);
78 if (fsw != NULL)
79 fclose(fsw);
80 if (fsw_ != NULL)
81 fclose(fsw_);
82 if (few != NULL)
83 fclose(few);
84 if (fmodel != NULL)
85 fclose(fmodel);
86 if (fsoftdec != NULL)
87 fclose(fsoftdec);
88 if (fqmodel != NULL)
89 fclose(fqmodel);
90 if (fpwb != NULL)
91 fclose(fpwb);
92 if (fpw != NULL)
93 fclose(fpw);
94 if (frw != NULL)
95 fclose(frw);
96 if (flsp != NULL)
97 fclose(flsp);
98 if (fweights != NULL)
99 fclose(fweights);
100 if (flsp_ != NULL)
101 fclose(flsp_);
102 if (fmel != NULL)
103 fclose(fmel);
104 if (fmel_indexes != NULL)
105 fclose(fmel_indexes);
106 if (fphase != NULL)
107 fclose(fphase);
108 if (fphase_ != NULL)
109 fclose(fphase_);
110 if (ffw != NULL)
111 fclose(ffw);
112 if (fe != NULL)
113 fclose(fe);
114 if (fsq != NULL)
115 fclose(fsq);
116 if (fdec != NULL)
117 fclose(fdec);
118 if (fsnr != NULL)
119 fclose(fsnr);
120 if (flpcsnr != NULL)
121 fclose(flpcsnr);
122 if (fak != NULL)
123 fclose(fak);
124 if (fak_ != NULL)
125 fclose(fak_);
126 if (fbg != NULL)
127 fclose(fbg);
128 if (fE != NULL)
129 fclose(fE);
130 if (frk != NULL)
131 fclose(frk);
132 if (fhephase != NULL)
133 fclose(fhephase);
134 }
135
dump_Sn(int m_pitch,float Sn[])136 void dump_Sn(int m_pitch, float Sn[]) {
137 int i;
138 char s[MAX_STR + 7];
139
140 if (!dumpon) return;
141
142 if (fsn == NULL) {
143 sprintf(s,"%s_sn.txt", prefix);
144 fsn = fopen(s, "wt");
145 assert(fsn != NULL);
146 }
147
148 /* split across two lines to avoid max line length problems */
149 /* reconstruct in Octave */
150
151 for(i=0; i<m_pitch/2; i++)
152 fprintf(fsn,"%f\t",Sn[i]);
153 fprintf(fsn,"\n");
154 for(i=m_pitch/2; i<m_pitch; i++)
155 fprintf(fsn,"%f\t",Sn[i]);
156 fprintf(fsn,"\n");
157 }
158
dump_Sw(COMP Sw[])159 void dump_Sw(COMP Sw[]) {
160 int i;
161 char s[MAX_STR + 7];
162
163 if (!dumpon) return;
164
165 if (fsw == NULL) {
166 sprintf(s,"%s_sw.txt", prefix);
167 fsw = fopen(s, "wt");
168 assert(fsw != NULL);
169 }
170
171 for(i=0; i<FFT_ENC/2; i++)
172 fprintf(fsw,"%f\t",
173 10.0*log10(Sw[i].real*Sw[i].real + Sw[i].imag*Sw[i].imag));
174 fprintf(fsw,"\n");
175 }
176
dump_Sw_(COMP Sw_[])177 void dump_Sw_(COMP Sw_[]) {
178 int i;
179 char s[MAX_STR + 8];
180
181 if (!dumpon) return;
182
183 if (fsw_ == NULL) {
184 sprintf(s,"%s_sw_.txt", prefix);
185 fsw_ = fopen(s, "wt");
186 assert(fsw_ != NULL);
187 }
188
189 for(i=0; i<FFT_ENC/2; i++)
190 fprintf(fsw_,"%f\t",
191 10.0*log10(Sw_[i].real*Sw_[i].real + Sw_[i].imag*Sw_[i].imag));
192 fprintf(fsw_,"\n");
193 }
194
dump_Ew(COMP Ew[])195 void dump_Ew(COMP Ew[]) {
196 int i;
197 char s[MAX_STR + 7];
198
199 if (!dumpon) return;
200
201 if (few == NULL) {
202 sprintf(s,"%s_ew.txt", prefix);
203 few = fopen(s, "wt");
204 assert(few != NULL);
205 }
206
207 for(i=0; i<FFT_ENC/2; i++)
208 fprintf(few,"%f\t",
209 10.0*log10(Ew[i].real*Ew[i].real + Ew[i].imag*Ew[i].imag));
210 fprintf(few,"\n");
211 }
212
dump_softdec(float * softdec,int n)213 void dump_softdec(float *softdec, int n)
214 {
215 int i;
216 char s[MAX_STR + 12];
217
218 if (!dumpon) return;
219
220 if (fsoftdec == NULL) {
221 sprintf(s,"%s_softdec.txt", prefix);
222 fsoftdec = fopen(s, "wt");
223 assert(fsoftdec != NULL);
224 }
225
226 for(i=0; i<n; i++)
227 fprintf(fsoftdec,"%f\t", softdec[i]);
228 fprintf(fsoftdec,"\n");
229 }
230
dump_model(MODEL * model)231 void dump_model(MODEL *model) {
232 int l;
233 char s[MAX_STR + 10];
234 char line[MAX_STR*10];
235
236 if (!dumpon) return;
237
238 if (fmodel == NULL) {
239 sprintf(s,"%s_model.txt", prefix);
240 fmodel = fopen(s, "wt");
241 assert(fmodel != NULL);
242 }
243
244 sprintf(line,"%12f %12d ", model->Wo, model->L);
245 for(l=1; l<=model->L; l++) {
246 sprintf(s,"%12f ",model->A[l]);
247 strcat(line, s);
248 assert(strlen(line) < MAX_STR*10);
249 }
250 for(l=model->L+1; l<=MAX_AMP; l++) {
251 sprintf(s,"%12f ", 0.0);
252 strcat(line,s);
253 assert(strlen(line) < MAX_STR*10);
254 }
255
256 sprintf(s,"%d\n",model->voiced);
257 strcat(line,s);
258 fprintf(fmodel,"%s",line);
259 }
260
dump_quantised_model(MODEL * model)261 void dump_quantised_model(MODEL *model) {
262 int l;
263 char s[MAX_STR + 11];
264 char line[4096];
265
266 if (!dumpon) return;
267
268 if (fqmodel == NULL) {
269 sprintf(s,"%s_qmodel.txt", prefix);
270 fqmodel = fopen(s, "wt");
271 assert(fqmodel != NULL);
272 }
273
274 sprintf(line,"%12f %12d ", model->Wo, model->L);
275 for(l=1; l<=model->L; l++) {
276 sprintf(s,"%12f ",model->A[l]);
277 strcat(line, s);
278 assert(strlen(line) < 4096);
279 }
280 for(l=model->L+1; l<=MAX_AMP; l++) {
281 sprintf(s,"%12f ", 0.0);
282 strcat(line, s);
283 assert(strlen(line) < 4096);
284 }
285
286 sprintf(s,"%d\n",model->voiced);
287 strcat(line, s);
288 fprintf(fqmodel, "%s", line);
289 }
290
dump_phase(float phase[],int L)291 void dump_phase(float phase[], int L) {
292 int l;
293 char s[MAX_STR + 10];
294
295 if (!dumpon) return;
296
297 if (fphase == NULL) {
298 sprintf(s,"%s_phase.txt", prefix);
299 fphase = fopen(s, "wt");
300 assert(fphase != NULL);
301 }
302
303 for(l=1; l<=L; l++)
304 fprintf(fphase,"%f\t",phase[l]);
305 for(l=L+1; l<=MAX_AMP; l++)
306 fprintf(fphase,"%f\t",0.0);
307 fprintf(fphase,"\n");
308 }
309
dump_phase_(float phase_[],int L)310 void dump_phase_(float phase_[], int L) {
311 int l;
312 char s[MAX_STR + 11];
313
314 if (!dumpon) return;
315
316 if (fphase_ == NULL) {
317 sprintf(s,"%s_phase_.txt", prefix);
318 fphase_ = fopen(s, "wt");
319 assert(fphase_ != NULL);
320 }
321
322 for(l=1; l<=L; l++)
323 fprintf(fphase_,"%f\t",phase_[l]);
324 for(l=L+1; l<MAX_AMP; l++)
325 fprintf(fphase_,"%f\t",0.0);
326 fprintf(fphase_,"\n");
327 }
328
329
dump_hephase(int ind[],int dim)330 void dump_hephase(int ind[], int dim) {
331 int m;
332 char s[MAX_STR + 12];
333
334 if (!dumpon) return;
335
336 if (fhephase == NULL) {
337 sprintf(s,"%s_hephase.txt", prefix);
338 fhephase = fopen(s, "wt");
339 assert(fhephase != NULL);
340 }
341
342 for(m=0; m<dim; m++)
343 fprintf(fhephase,"%d\t",ind[m]);
344 fprintf(fhephase,"\n");
345 }
346
347
dump_snr(float snr)348 void dump_snr(float snr) {
349 char s[MAX_STR + 8];
350
351 if (!dumpon) return;
352
353 if (fsnr == NULL) {
354 sprintf(s,"%s_snr.txt", prefix);
355 fsnr = fopen(s, "wt");
356 assert(fsnr != NULL);
357 }
358
359 fprintf(fsnr,"%f\n",snr);
360 }
361
dump_lpc_snr(float snr)362 void dump_lpc_snr(float snr) {
363 char s[MAX_STR + 12];
364
365 if (!dumpon) return;
366
367 if (flpcsnr == NULL) {
368 sprintf(s,"%s_lpc_snr.txt", prefix);
369 flpcsnr = fopen(s, "wt");
370 assert(flpcsnr != NULL);
371 }
372
373 fprintf(flpcsnr,"%f\n",snr);
374 }
375
376 /* Pw "before" post filter so we can plot before and after */
377
dump_Pwb(float Pwb[])378 void dump_Pwb(float Pwb[]) {
379 int i;
380 char s[MAX_STR + 8];
381
382 if (!dumpon) return;
383
384 if (fpwb == NULL) {
385 sprintf(s,"%s_pwb.txt", prefix);
386 fpwb = fopen(s, "wt");
387 assert(fpwb != NULL);
388 }
389
390 for(i=0; i<FFT_ENC/2; i++)
391 fprintf(fpwb,"%f\t",Pwb[i]);
392 fprintf(fpwb,"\n");
393 }
394
dump_Pw(float Pw[])395 void dump_Pw(float Pw[]) {
396 int i;
397 char s[MAX_STR +7];
398
399 if (!dumpon) return;
400
401 if (fpw == NULL) {
402 sprintf(s,"%s_pw.txt", prefix);
403 fpw = fopen(s, "wt");
404 assert(fpw != NULL);
405 }
406
407 for(i=0; i<FFT_ENC/2; i++)
408 fprintf(fpw,"%f\t",Pw[i]);
409 fprintf(fpw,"\n");
410 }
411
dump_Rw(float Rw[])412 void dump_Rw(float Rw[]) {
413 int i;
414 char s[MAX_STR + 7];
415
416 if (!dumpon) return;
417
418 if (frw == NULL) {
419 sprintf(s,"%s_rw.txt", prefix);
420 frw = fopen(s, "wt");
421 assert(frw != NULL);
422 }
423
424 for(i=0; i<FFT_ENC/2; i++)
425 fprintf(frw,"%f\t",Rw[i]);
426 fprintf(frw,"\n");
427 }
428
dump_weights(float w[],int order)429 void dump_weights(float w[], int order) {
430 int i;
431 char s[MAX_STR + 12];
432
433 if (!dumpon) return;
434
435 if (fweights == NULL) {
436 sprintf(s,"%s_weights.txt", prefix);
437 fweights = fopen(s, "wt");
438 assert(fweights != NULL);
439 }
440
441 for(i=0; i<order; i++)
442 fprintf(fweights,"%f\t", w[i]);
443 fprintf(fweights,"\n");
444 }
445
dump_lsp(float lsp[])446 void dump_lsp(float lsp[]) {
447 int i;
448 char s[MAX_STR + 8];
449
450 if (!dumpon) return;
451
452 if (flsp == NULL) {
453 sprintf(s,"%s_lsp.txt", prefix);
454 flsp = fopen(s, "wt");
455 assert(flsp != NULL);
456 }
457
458 for(i=0; i<10; i++)
459 fprintf(flsp,"%f\t",lsp[i]);
460 fprintf(flsp,"\n");
461 }
462
dump_lsp_(float lsp_[])463 void dump_lsp_(float lsp_[]) {
464 int i;
465 char s[MAX_STR + 9];
466
467 if (!dumpon) return;
468
469 if (flsp_ == NULL) {
470 sprintf(s,"%s_lsp_.txt", prefix);
471 flsp_ = fopen(s, "wt");
472 assert(flsp_ != NULL);
473 }
474
475 for(i=0; i<10; i++)
476 fprintf(flsp_,"%f\t",lsp_[i]);
477 fprintf(flsp_,"\n");
478 }
479
dump_mel(float mel[],int order)480 void dump_mel(float mel[], int order) {
481 int i;
482 char s[MAX_STR + 8];
483
484 if (!dumpon) return;
485
486 if (fmel == NULL) {
487 sprintf(s,"%s_mel.txt", prefix);
488 fmel = fopen(s, "wt");
489 assert(fmel != NULL);
490 }
491
492 for(i=0; i<order; i++)
493 fprintf(fmel,"%f\t",mel[i]);
494 fprintf(fmel,"\n");
495 }
496
dump_mel_indexes(int mel_indexes[],int order)497 void dump_mel_indexes(int mel_indexes[], int order) {
498 int i;
499 char s[MAX_STR + 16];
500
501 if (!dumpon) return;
502
503 if (fmel_indexes == NULL) {
504 sprintf(s,"%s_mel_indexes.txt", prefix);
505 fmel_indexes = fopen(s, "wt");
506 assert(fmel_indexes != NULL);
507 }
508
509 for(i=0; i<order; i++)
510 fprintf(fmel_indexes,"%d\t",mel_indexes[i]);
511 fprintf(fmel_indexes,"\n");
512 }
513
dump_ak(float ak[],int order)514 void dump_ak(float ak[], int order) {
515 int i;
516 char s[MAX_STR + 7];
517
518 if (!dumpon) return;
519
520 if (fak == NULL) {
521 sprintf(s,"%s_ak.txt", prefix);
522 fak = fopen(s, "wt");
523 assert(fak != NULL);
524 }
525
526 for(i=0; i<=order; i++)
527 fprintf(fak,"%f\t",ak[i]);
528 fprintf(fak,"\n");
529 }
530
dump_ak_(float ak_[],int order)531 void dump_ak_(float ak_[], int order) {
532 int i;
533 char s[MAX_STR + 8];
534
535 if (!dumpon) return;
536
537 if (fak_ == NULL) {
538 sprintf(s,"%s_ak_.txt", prefix);
539 fak_ = fopen(s, "wt");
540 assert(fak_ != NULL);
541 }
542
543 for(i=0; i<=order; i++)
544 fprintf(fak_,"%f\t",ak_[i]);
545 fprintf(fak_,"\n");
546 }
547
dump_Fw(COMP Fw[])548 void dump_Fw(COMP Fw[]) {
549 int i;
550 char s[MAX_STR + 7];
551
552 if (!dumpon) return;
553
554 if (ffw == NULL) {
555 sprintf(s,"%s_fw.txt", prefix);
556 ffw = fopen(s, "wt");
557 assert(ffw != NULL);
558 }
559
560 for(i=0; i<256; i++)
561 fprintf(ffw,"%f\t",Fw[i].real);
562 fprintf(ffw,"\n");
563 }
564
dump_e(float e_hz[])565 void dump_e(float e_hz[]) {
566 int i;
567 char s[MAX_STR + 6];
568
569 if (!dumpon) return;
570
571 if (fe == NULL) {
572 sprintf(s,"%s_e.txt", prefix);
573 fe = fopen(s, "wt");
574 assert(fe != NULL);
575 }
576
577 for(i=0; i<500/2; i++)
578 fprintf(fe,"%f\t",e_hz[i]);
579 fprintf(fe,"\n");
580 for(i=500/2; i<500; i++)
581 fprintf(fe,"%f\t",e_hz[i]);
582 fprintf(fe,"\n");
583 }
584
dump_sq(int m_pitch,float sq[])585 void dump_sq(int m_pitch, float sq[]) {
586 int i;
587 char s[MAX_STR + 7];
588
589 if (!dumpon) return;
590
591 if (fsq == NULL) {
592 sprintf(s,"%s_sq.txt", prefix);
593 fsq = fopen(s, "wt");
594 assert(fsq != NULL);
595 }
596
597 for(i=0; i<m_pitch/2; i++)
598 fprintf(fsq,"%f\t",sq[i]);
599 fprintf(fsq,"\n");
600 for(i=m_pitch/2; i<m_pitch; i++)
601 fprintf(fsq,"%f\t",sq[i]);
602 fprintf(fsq,"\n");
603 }
604
dump_dec(COMP Fw[])605 void dump_dec(COMP Fw[]) {
606 int i;
607 char s[MAX_STR + 8];
608
609 if (!dumpon) return;
610
611 if (fdec == NULL) {
612 sprintf(s,"%s_dec.txt", prefix);
613 fdec = fopen(s, "wt");
614 assert(fdec != NULL);
615 }
616
617 for(i=0; i<320/5; i++)
618 fprintf(fdec,"%f\t",Fw[i].real);
619 fprintf(fdec,"\n");
620 }
621
dump_bg(float e,float bg_est,float percent_uv)622 void dump_bg(float e, float bg_est, float percent_uv) {
623 char s[MAX_STR + 7];
624
625 if (!dumpon) return;
626
627 if (fbg == NULL) {
628 sprintf(s,"%s_bg.txt", prefix);
629 fbg = fopen(s, "wt");
630 assert(fbg != NULL);
631 }
632
633 fprintf(fbg,"%f\t%f\t%f\n", e, bg_est, percent_uv);
634 }
635
dump_E(float E)636 void dump_E(float E) {
637 char s[MAX_STR + 6];
638
639 if (!dumpon) return;
640
641 if (fE == NULL) {
642 sprintf(s,"%s_E.txt", prefix);
643 fE = fopen(s, "wt");
644 assert(fE != NULL);
645 }
646
647 fprintf(fE,"%f\n", 10.0*log10(E));
648 }
649
650 #if 0
651 void dump_Rk(float Rk[]) {
652 int i;
653 char s[MAX_STR];
654
655 if (!dumpon) return;
656
657 if (frk == NULL) {
658 sprintf(s,"%s_rk.txt", prefix);
659 frk = fopen(s, "wt");
660 assert(frk != NULL);
661 }
662
663 for(i=0; i<P_MAX; i++)
664 fprintf(frk,"%f\t",Rk[i]);
665 fprintf(frk,"\n");
666 }
667 #endif
668
669 #endif
670