annotate intercom/aec_test.cpp @ 6:22a74b01a099 default tip

implement more meaningful test program
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:14:50 +0200
parents 13be24d74cd2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 /* aec_test.cpp
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 *
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 * Copyright (C) DFS Deutsche Flugsicherung (2004). All Rights Reserved.
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 *
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 * Test stub for Acoustic Echo Cancellation NLMS-pw algorithm
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 * Author: Andre Adrian, DFS Deutsche Flugsicherung
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 * <Andre.Adrian@dfs.de>
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 *
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 * fortune says:
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 * It's never as good as it feels, and it's never as bad as it seems.
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 *
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 * compile
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 c++ -DWIDEB=2 -O2 -o aec_test aec_test.cpp aec.cpp tcp.cpp -lm
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 *
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 * Version 1.3 set/get ambient in dB
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 #include <stdio.h>
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 #include <stdlib.h>
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 #include <math.h>
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 #include <string.h>
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 #include "tcp.h"
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 #include "aec.h"
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 #define TAPS (40*WIDEB*8)
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 typedef signed short MONO;
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 float dB2q(float dB)
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 {
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 /* Dezibel to Ratio */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 return powf(10.0f, dB / 20.0f);
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 }
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 float q2dB(float q)
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 {
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 /* Ratio to Dezibel */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 return 20.0f * log10f(q);
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 }
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
42 /* Read a raw audio files (8KHz sample frequency, 16bit PCM, mono)
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 * from stdin, echo cancel it and write it to stdout
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 int main(int argc, char *argv[])
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 {
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
47 MONO inbuf_speaker[TAPS], inbuf_mic[TAPS], outbuf[TAPS];
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 float visualize;
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
50 fprintf(stderr, "usage: aec_test [ambient in dB] speaker.raw mic.raw out.raw\n");
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 AEC aec;
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
54 // ambient in dB
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
55 aec.setambient(MAXPCM*dB2q(0.0));
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
56
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
57 FILE *fspeaker = fopen("speaker.raw", "rb");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
58 if (!fspeaker) {
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
59 fprintf(stderr, "Failed to open 'speaker.raw', exit.\n");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
60 exit(EXIT_FAILURE);
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
61 }
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
62
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
63 FILE *fmic = fopen("mic.raw", "rb");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
64 if (!fmic) {
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
65 fprintf(stderr, "Failed to open 'mic.raw', exit.\n");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
66 exit(EXIT_FAILURE);
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 }
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
69 FILE *fout = fopen("out.raw", "wb");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
70 if (!fout) {
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
71 fprintf(stderr, "Failed to open 'out.raw', exit.\n");
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
72 exit(EXIT_FAILURE);
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
73 }
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
74
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
75 int taps_speaker, taps_mic;
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 float ambient;
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
77 while (true) {
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 int i;
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
79
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
80 taps_speaker = fread(inbuf_speaker, sizeof(MONO), TAPS, fspeaker);
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
81 taps_mic = fread(inbuf_mic, sizeof(MONO), TAPS, fmic);
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
82
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
83 if (taps_speaker != TAPS || taps_mic != TAPS)
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
84 break;
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
85
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
86
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
87 for (i = 0; i < TAPS; ++i) {
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
88 int s0 = inbuf_mic[i]; /* left channel microphone */
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
89 int s1 = inbuf_speaker[i]; /* right channel speaker */
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 /* and do NLMS */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 s0 = aec.doAEC(s0, s1);
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 /* output one internal variable */
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 // visualize = 16 * aec.hangover;
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 visualize = 32000 * aec.stepsize;
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
98 outbuf[i] = s0; /* right channel echo cancelled mic */
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 }
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
101 fwrite(outbuf, sizeof(MONO), TAPS, fout);
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 }
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 ambient = aec.getambient();
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 float ambientdB = q2dB(ambient / 32767.0f);
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB);
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106
6
22a74b01a099 implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
107 return EXIT_SUCCESS;
2
13be24d74cd2 import intercom-0.4.1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.