comparison spandsp-0.0.6pre17/tests/line_model_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * line_model_tests.c - Tests for the telephone line model.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2004 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: line_model_tests.c,v 1.28 2009/09/23 16:02:59 steveu Exp $
26 */
27
28 /*! \page line_model_tests_page Telephony line model tests
29 \section line_model_tests_page_sec_1 What does it do?
30 ???.
31
32 \section line_model_tests_page_sec_2 How does it work?
33 ???.
34 */
35
36 #if defined(HAVE_CONFIG_H)
37 #include "config.h"
38 #endif
39
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <time.h>
46 #include <sndfile.h>
47
48 //#if defined(WITH_SPANDSP_INTERNALS)
49 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
50 //#endif
51
52 #include "spandsp.h"
53 #include "spandsp-sim.h"
54
55 #if !defined(NULL)
56 #define NULL (void *) 0
57 #endif
58
59 #define BLOCK_LEN 160
60
61 #define OUT_FILE_COMPLEXIFY "complexify.wav"
62 #define IN_FILE_NAME1 "line_model_test_in1.wav"
63 #define IN_FILE_NAME2 "line_model_test_in2.wav"
64 #define OUT_FILE_NAME1 "line_model_one_way_test_out.wav"
65 #define OUT_FILE_NAME2 "line_model_two_way_test_out.wav"
66
67 int channel_codec;
68 int rbs_pattern;
69
70 static void complexify_tests(void)
71 {
72 complexify_state_t *s;
73 complexf_t cc;
74 int16_t amp;
75 int i;
76 SNDFILE *outhandle;
77 int outframes;
78 int16_t out[40000];
79 awgn_state_t noise1;
80
81 if ((outhandle = sf_open_telephony_write(OUT_FILE_COMPLEXIFY, 2)) == NULL)
82 {
83 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_COMPLEXIFY);
84 exit(2);
85 }
86 awgn_init_dbm0(&noise1, 1234567, -10.0f);
87 s = complexify_init();
88 for (i = 0; i < 20000; i++)
89 {
90 amp = awgn(&noise1);
91 cc = complexify(s, amp);
92 out[2*i] = cc.re;
93 out[2*i + 1] = cc.im;
94 }
95 outframes = sf_writef_short(outhandle, out, 20000);
96 if (outframes != 20000)
97 {
98 fprintf(stderr, " Error writing audio file\n");
99 exit(2);
100 }
101 if (sf_close(outhandle))
102 {
103 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_COMPLEXIFY);
104 exit(2);
105 }
106 }
107 /*- End of function --------------------------------------------------------*/
108
109 static void test_one_way_model(int line_model_no, int speech_test)
110 {
111 one_way_line_model_state_t *model;
112 int16_t input1[BLOCK_LEN];
113 int16_t output1[BLOCK_LEN];
114 int16_t amp[2*BLOCK_LEN];
115 SNDFILE *inhandle1;
116 SNDFILE *outhandle;
117 int outframes;
118 int samples;
119 int i;
120 int j;
121 awgn_state_t noise1;
122
123 if ((model = one_way_line_model_init(line_model_no, -50, channel_codec, rbs_pattern)) == NULL)
124 {
125 fprintf(stderr, " Failed to create line model\n");
126 exit(2);
127 }
128
129 awgn_init_dbm0(&noise1, 1234567, -10.0f);
130
131 if (speech_test)
132 {
133 if ((inhandle1 = sf_open_telephony_read(IN_FILE_NAME1, 1)) == NULL)
134 {
135 fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME1);
136 exit(2);
137 }
138 }
139 else
140 {
141 inhandle1 = NULL;
142 }
143 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME1, 1)) == NULL)
144 {
145 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME1);
146 exit(2);
147 }
148 for (i = 0; i < 10000; i++)
149 {
150 if (speech_test)
151 {
152 samples = sf_readf_short(inhandle1, input1, BLOCK_LEN);
153 if (samples == 0)
154 break;
155 }
156 else
157 {
158 for (j = 0; j < BLOCK_LEN; j++)
159 input1[j] = awgn(&noise1);
160 samples = BLOCK_LEN;
161 }
162 for (j = 0; j < samples; j++)
163 {
164 one_way_line_model(model,
165 &output1[j],
166 &input1[j],
167 1);
168 amp[j] = output1[j];
169 }
170 outframes = sf_writef_short(outhandle, amp, samples);
171 if (outframes != samples)
172 {
173 fprintf(stderr, " Error writing audio file\n");
174 exit(2);
175 }
176 }
177 if (speech_test)
178 {
179 if (sf_close(inhandle1))
180 {
181 fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME1);
182 exit(2);
183 }
184 }
185 if (sf_close(outhandle))
186 {
187 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME1);
188 exit(2);
189 }
190 one_way_line_model_release(model);
191 }
192 /*- End of function --------------------------------------------------------*/
193
194 static void test_both_ways_model(int line_model_no, int speech_test)
195 {
196 both_ways_line_model_state_t *model;
197 int16_t input1[BLOCK_LEN];
198 int16_t input2[BLOCK_LEN];
199 int16_t output1[BLOCK_LEN];
200 int16_t output2[BLOCK_LEN];
201 int16_t amp[2*BLOCK_LEN];
202 SNDFILE *inhandle1;
203 SNDFILE *inhandle2;
204 SNDFILE *outhandle;
205 int outframes;
206 int samples;
207 int i;
208 int j;
209 awgn_state_t noise1;
210 awgn_state_t noise2;
211
212 if ((model = both_ways_line_model_init(line_model_no, -50, line_model_no + 1, -35, channel_codec, rbs_pattern)) == NULL)
213 {
214 fprintf(stderr, " Failed to create line model\n");
215 exit(2);
216 }
217
218 awgn_init_dbm0(&noise1, 1234567, -10.0f);
219 awgn_init_dbm0(&noise2, 1234567, -10.0f);
220
221 if (speech_test)
222 {
223 if ((inhandle1 = sf_open_telephony_read(IN_FILE_NAME1, 1)) == NULL)
224 {
225 fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME1);
226 exit(2);
227 }
228 if ((inhandle2 = sf_open_telephony_read(IN_FILE_NAME2, 1)) == NULL)
229 {
230 fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME2);
231 exit(2);
232 }
233 }
234 else
235 {
236 inhandle1 =
237 inhandle2 = NULL;
238 }
239 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME2, 2)) == NULL)
240 {
241 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME2);
242 exit(2);
243 }
244 for (i = 0; i < 10000; i++)
245 {
246 if (speech_test)
247 {
248 samples = sf_readf_short(inhandle1, input1, BLOCK_LEN);
249 if (samples == 0)
250 break;
251 samples = sf_readf_short(inhandle2, input2, samples);
252 if (samples == 0)
253 break;
254 }
255 else
256 {
257 for (j = 0; j < BLOCK_LEN; j++)
258 {
259 input1[j] = awgn(&noise1);
260 input2[j] = awgn(&noise2);
261 }
262 samples = BLOCK_LEN;
263 }
264 for (j = 0; j < samples; j++)
265 {
266 both_ways_line_model(model,
267 &output1[j],
268 &input1[j],
269 &output2[j],
270 &input2[j],
271 1);
272 amp[2*j] = output1[j];
273 amp[2*j + 1] = output2[j];
274 }
275 outframes = sf_writef_short(outhandle, amp, samples);
276 if (outframes != samples)
277 {
278 fprintf(stderr, " Error writing audio file\n");
279 exit(2);
280 }
281 }
282 if (speech_test)
283 {
284 if (sf_close(inhandle1))
285 {
286 fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME1);
287 exit(2);
288 }
289 if (sf_close(inhandle2))
290 {
291 fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME2);
292 exit(2);
293 }
294 }
295 if (sf_close(outhandle))
296 {
297 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME2);
298 exit(2);
299 }
300 both_ways_line_model_release(model);
301 }
302 /*- End of function --------------------------------------------------------*/
303
304 static void test_line_filter(int line_model_no)
305 {
306 float out;
307 double sumin;
308 double sumout;
309 int i;
310 int j;
311 int p;
312 int ptr;
313 int len;
314 swept_tone_state_t *s;
315 float filter[129];
316 int16_t buf[BLOCK_LEN];
317
318 s = swept_tone_init(NULL, 200.0f, 3900.0f, -10.0f, 120*SAMPLE_RATE, 0);
319 for (j = 0; j < 129; j++)
320 filter[j] = 0.0f;
321 ptr = 0;
322 for (;;)
323 {
324 if ((len = swept_tone(s, buf, BLOCK_LEN)) <= 0)
325 break;
326 sumin = 0.0;
327 sumout = 0.0;
328 for (i = 0; i < len; i++)
329 {
330 /* Add the sample in the filter buffer */
331 p = ptr;
332 filter[p] = buf[i];
333 if (++p == 129)
334 p = 0;
335 ptr = p;
336
337 /* Apply the filter */
338 out = 0.0f;
339 for (j = 0; j < 129; j++)
340 {
341 out += line_models[line_model_no][128 - j]*filter[p];
342 if (++p >= 129)
343 p = 0;
344 }
345 sumin += buf[i]*buf[i];
346 sumout += out*out;
347 }
348 /*endfor*/
349 printf("%7.1f %f\n", swept_tone_current_frequency(s), 10.0*log10(sumout/sumin));
350 }
351 /*endfor*/
352 swept_tone_free(s);
353 }
354 /*- End of function --------------------------------------------------------*/
355
356 int main(int argc, char *argv[])
357 {
358 int line_model_no;
359 int speech_test;
360 int log_audio;
361 int opt;
362
363 channel_codec = MUNGE_CODEC_NONE;
364 log_audio = FALSE;
365 line_model_no = 0;
366 rbs_pattern = 0;
367 speech_test = FALSE;
368 while ((opt = getopt(argc, argv, "c:lm:r:s:")) != -1)
369 {
370 switch (opt)
371 {
372 case 'c':
373 channel_codec = atoi(optarg);
374 break;
375 case 'l':
376 log_audio = TRUE;
377 break;
378 case 'm':
379 line_model_no = atoi(optarg);
380 break;
381 case 'r':
382 rbs_pattern = atoi(optarg);
383 break;
384 case 's':
385 speech_test = atoi(optarg);
386 break;
387 default:
388 //usage();
389 exit(2);
390 }
391 }
392 complexify_tests();
393 test_one_way_model(line_model_no, speech_test);
394 test_both_ways_model(line_model_no, speech_test);
395 test_line_filter(line_model_no);
396 }
397 /*- End of function --------------------------------------------------------*/
398 /*- End of file ------------------------------------------------------------*/

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