g168_tests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g168tests.c - Tests of the "test equipment" (filters, etc.) specified
  5. * in G.168. This code is only for checking out the tools,
  6. * not for testing an echo cancellor.
  7. *
  8. * Written by Steve Underwood <steveu@coppice.org>
  9. *
  10. * Copyright (C) 2001 Steve Underwood
  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 General Public License version 2, as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #if defined(HAVE_CONFIG_H)
  28. #include "config.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sndfile.h>
  34. #include "spandsp.h"
  35. #include "spandsp/g168models.h"
  36. #include "spandsp-sim.h"
  37. typedef struct
  38. {
  39. const char *name;
  40. int max;
  41. int cur;
  42. float gain;
  43. SNDFILE *handle;
  44. int16_t signal[SAMPLE_RATE];
  45. } signal_source_t;
  46. signal_source_t local_css;
  47. signal_source_t far_css;
  48. static void signal_load(signal_source_t *sig, const char *name)
  49. {
  50. sig->handle = sf_open_telephony_read(name, 1);
  51. sig->name = name;
  52. sig->max = sf_readf_short(sig->handle, sig->signal, SAMPLE_RATE);
  53. if (sig->max < 0)
  54. {
  55. fprintf(stderr, " Error reading sound file '%s'\n", sig->name);
  56. exit(2);
  57. }
  58. }
  59. /*- End of function --------------------------------------------------------*/
  60. static void signal_free(signal_source_t *sig)
  61. {
  62. if (sf_close_telephony(sig->handle))
  63. {
  64. fprintf(stderr, " Cannot close sound file '%s'\n", sig->name);
  65. exit(2);
  66. }
  67. }
  68. /*- End of function --------------------------------------------------------*/
  69. static void signal_restart(signal_source_t *sig, float gain)
  70. {
  71. sig->cur = 0;
  72. sig->gain = powf(10.0f, gain/20.0f);
  73. }
  74. /*- End of function --------------------------------------------------------*/
  75. static int16_t signal_amp(signal_source_t *sig)
  76. {
  77. int16_t tx;
  78. tx = sig->signal[sig->cur++]*sig->gain;
  79. if (sig->cur >= sig->max)
  80. sig->cur = 0;
  81. return tx;
  82. }
  83. /*- End of function --------------------------------------------------------*/
  84. int main(int argc, char *argv[])
  85. {
  86. int f;
  87. int i;
  88. int16_t amp[8000];
  89. int16_t value;
  90. int signal;
  91. float power[10];
  92. tone_gen_descriptor_t tone_desc;
  93. tone_gen_state_t tone_state;
  94. awgn_state_t noise_source;
  95. fir32_state_t line_model_d2;
  96. fir32_state_t line_model_d3;
  97. fir32_state_t line_model_d4;
  98. fir32_state_t line_model_d5;
  99. fir32_state_t line_model_d6;
  100. fir32_state_t line_model_d7;
  101. fir32_state_t line_model_d8;
  102. fir32_state_t line_model_d9;
  103. fir_float_state_t level_measurement_bp;
  104. signal_load(&local_css, "sound_c1_8k.wav");
  105. signal_load(&far_css, "sound_c3_8k.wav");
  106. fir32_create(&line_model_d2,
  107. line_model_d2_coeffs,
  108. sizeof(line_model_d2_coeffs)/sizeof(int32_t));
  109. fir32_create(&line_model_d3,
  110. line_model_d3_coeffs,
  111. sizeof(line_model_d3_coeffs)/sizeof(int32_t));
  112. fir32_create(&line_model_d4,
  113. line_model_d4_coeffs,
  114. sizeof(line_model_d4_coeffs)/sizeof(int32_t));
  115. fir32_create(&line_model_d5,
  116. line_model_d5_coeffs,
  117. sizeof(line_model_d5_coeffs)/sizeof(int32_t));
  118. fir32_create(&line_model_d6,
  119. line_model_d6_coeffs,
  120. sizeof(line_model_d6_coeffs)/sizeof(int32_t));
  121. fir32_create(&line_model_d7,
  122. line_model_d7_coeffs,
  123. sizeof(line_model_d7_coeffs)/sizeof(int32_t));
  124. fir32_create(&line_model_d8,
  125. line_model_d8_coeffs,
  126. sizeof(line_model_d8_coeffs)/sizeof(int32_t));
  127. fir32_create(&line_model_d9,
  128. line_model_d9_coeffs,
  129. sizeof(line_model_d9_coeffs)/sizeof(int32_t));
  130. fir_float_create(&level_measurement_bp,
  131. level_measurement_bp_coeffs,
  132. sizeof(level_measurement_bp_coeffs)/sizeof(float));
  133. for (f = 10; f < 4000; f++)
  134. {
  135. tone_gen_descriptor_init(&tone_desc,
  136. f,
  137. -10,
  138. 0,
  139. 0,
  140. 1,
  141. 0,
  142. 0,
  143. 0,
  144. true);
  145. tone_gen_init(&tone_state, &tone_desc);
  146. tone_gen(&tone_state, amp, 8000);
  147. for (i = 0; i < 10; i++)
  148. power[i] = 0.0f;
  149. for (i = 0; i < 800; i++)
  150. {
  151. signal = fir32(&line_model_d2, amp[i]);
  152. power[0] += ((signal*signal - power[0])/32.0f);
  153. signal = fir32(&line_model_d3, amp[i]);
  154. power[1] += ((signal*signal - power[1])/32.0f);
  155. signal = fir32(&line_model_d4, amp[i]);
  156. power[2] += ((signal*signal - power[2])/32.0f);
  157. signal = fir32(&line_model_d5, amp[i]);
  158. power[3] += ((signal*signal - power[3])/32.0f);
  159. signal = fir32(&line_model_d6, amp[i]);
  160. power[4] += ((signal*signal - power[4])/32.0f);
  161. signal = fir32(&line_model_d7, amp[i]);
  162. power[5] += ((signal*signal - power[5])/32.0f);
  163. signal = fir32(&line_model_d8, amp[i]);
  164. power[6] += ((signal*signal - power[6])/32.0f);
  165. signal = fir32(&line_model_d9, amp[i]);
  166. power[7] += ((signal*signal - power[7])/32.0f);
  167. signal = fir_float(&level_measurement_bp, amp[i]);
  168. power[8] += ((signal*signal - power[8])/32.0f);
  169. signal = amp[i];
  170. power[9] += ((signal*signal - power[9])/32.0f);
  171. }
  172. printf("%d %f %f %f %f %f %f %f %f %f %f\n",
  173. f,
  174. sqrt(power[0])*LINE_MODEL_D2_GAIN,
  175. sqrt(power[1])*LINE_MODEL_D3_GAIN,
  176. sqrt(power[2])*LINE_MODEL_D4_GAIN,
  177. sqrt(power[3])*LINE_MODEL_D5_GAIN,
  178. sqrt(power[4])*LINE_MODEL_D6_GAIN,
  179. sqrt(power[5])*LINE_MODEL_D7_GAIN,
  180. sqrt(power[6])*LINE_MODEL_D8_GAIN,
  181. sqrt(power[7])*LINE_MODEL_D9_GAIN,
  182. sqrt(power[8]),
  183. sqrt(power[9]));
  184. }
  185. awgn_init_dbm0(&noise_source, 1234567, -20.0f);
  186. for (i = 0; i < 10; i++)
  187. power[i] = 0.0f;
  188. signal_restart(&local_css, 0.0f);
  189. signal_restart(&far_css, 0.0f);
  190. for (i = 0; i < SAMPLE_RATE; i++)
  191. {
  192. value = signal_amp(&local_css);
  193. //value = awgn(&noise_source);
  194. signal = fir32(&line_model_d2, value);
  195. power[0] += ((signal*signal - power[0])/32.0f);
  196. signal = fir32(&line_model_d3, value);
  197. power[1] += ((signal*signal - power[1])/32.0f);
  198. signal = fir32(&line_model_d4, value);
  199. power[2] += ((signal*signal - power[2])/32.0f);
  200. signal = fir32(&line_model_d5, value);
  201. power[3] += ((signal*signal - power[3])/32.0f);
  202. signal = fir32(&line_model_d6, value);
  203. power[4] += ((signal*signal - power[4])/32.0f);
  204. signal = fir32(&line_model_d7, value);
  205. power[5] += ((signal*signal - power[5])/32.0f);
  206. signal = fir32(&line_model_d8, value);
  207. power[6] += ((signal*signal - power[6])/32.0f);
  208. signal = fir32(&line_model_d9, value);
  209. power[7] += ((signal*signal - power[7])/32.0f);
  210. signal = fir_float(&level_measurement_bp, value);
  211. power[8] += ((signal*signal - power[8])/32.0f);
  212. signal = value;
  213. power[9] += ((signal*signal - power[9])/32.0f);
  214. }
  215. for (i = 0; i < 10; i++)
  216. power[i] = 0.0f;
  217. for (i = 0; i < SAMPLE_RATE; i++)
  218. {
  219. value = signal_amp(&local_css);
  220. //value = awgn(&noise_source);
  221. signal = fir32(&line_model_d2, value);
  222. power[0] += ((signal*signal - power[0])/32.0f);
  223. signal = fir32(&line_model_d3, value);
  224. power[1] += ((signal*signal - power[1])/32.0f);
  225. signal = fir32(&line_model_d4, value);
  226. power[2] += ((signal*signal - power[2])/32.0f);
  227. signal = fir32(&line_model_d5, value);
  228. power[3] += ((signal*signal - power[3])/32.0f);
  229. signal = fir32(&line_model_d6, value);
  230. power[4] += ((signal*signal - power[4])/32.0f);
  231. signal = fir32(&line_model_d7, value);
  232. power[5] += ((signal*signal - power[5])/32.0f);
  233. signal = fir32(&line_model_d8, value);
  234. power[6] += ((signal*signal - power[6])/32.0f);
  235. signal = fir32(&line_model_d9, value);
  236. power[7] += ((signal*signal - power[7])/32.0f);
  237. signal = fir_float(&level_measurement_bp, value);
  238. power[8] += ((signal*signal - power[8])/32.0f);
  239. signal = value;
  240. power[9] += ((signal*signal - power[9])/32.0f);
  241. }
  242. printf("%d %f %f %f %f %f %f %f %f %f %f\n",
  243. 0,
  244. sqrt(power[0])*LINE_MODEL_D2_GAIN,
  245. sqrt(power[1])*LINE_MODEL_D3_GAIN,
  246. sqrt(power[2])*LINE_MODEL_D4_GAIN,
  247. sqrt(power[3])*LINE_MODEL_D5_GAIN,
  248. sqrt(power[4])*LINE_MODEL_D6_GAIN,
  249. sqrt(power[5])*LINE_MODEL_D7_GAIN,
  250. sqrt(power[6])*LINE_MODEL_D8_GAIN,
  251. sqrt(power[7])*LINE_MODEL_D9_GAIN,
  252. sqrt(power[8]),
  253. sqrt(power[9]));
  254. printf("%d %f %f %f %f %f %f %f %f %f %f\n",
  255. 0,
  256. sqrt(power[0]),
  257. sqrt(power[1]),
  258. sqrt(power[2]),
  259. sqrt(power[3]),
  260. sqrt(power[4]),
  261. sqrt(power[5]),
  262. sqrt(power[6]),
  263. sqrt(power[7]),
  264. sqrt(power[8]),
  265. sqrt(power[9]));
  266. printf("%d %f %f %f %f %f %f %f %f %f %f\n",
  267. 0,
  268. sqrt(power[0])/sqrt(power[9]),
  269. sqrt(power[1])/sqrt(power[9]),
  270. sqrt(power[2])/sqrt(power[9]),
  271. sqrt(power[3])/sqrt(power[9]),
  272. sqrt(power[4])/sqrt(power[9]),
  273. sqrt(power[5])/sqrt(power[9]),
  274. sqrt(power[6])/sqrt(power[9]),
  275. sqrt(power[7])/sqrt(power[9]),
  276. sqrt(power[8]),
  277. sqrt(power[9]));
  278. printf("%d %f %f %f %f %f %f %f %f %f %f\n",
  279. 0,
  280. sqrt(power[0])*LINE_MODEL_D2_GAIN/sqrt(power[9]),
  281. sqrt(power[1])*LINE_MODEL_D3_GAIN/sqrt(power[9]),
  282. sqrt(power[2])*LINE_MODEL_D4_GAIN/sqrt(power[9]),
  283. sqrt(power[3])*LINE_MODEL_D5_GAIN/sqrt(power[9]),
  284. sqrt(power[4])*LINE_MODEL_D6_GAIN/sqrt(power[9]),
  285. sqrt(power[5])*LINE_MODEL_D7_GAIN/sqrt(power[9]),
  286. sqrt(power[6])*LINE_MODEL_D8_GAIN/sqrt(power[9]),
  287. sqrt(power[7])*LINE_MODEL_D9_GAIN/sqrt(power[9]),
  288. sqrt(power[8]),
  289. sqrt(power[9]));
  290. for (i = 0; i < (int) (sizeof(css_c1)/sizeof(css_c1[0])); i++)
  291. printf("%d\n", css_c1[i]);
  292. printf("\n");
  293. for (i = 0; i < (int) (sizeof(css_c1)/sizeof(css_c3[0])); i++)
  294. printf("%d\n", css_c3[i]);
  295. signal_free(&local_css);
  296. signal_free(&far_css);
  297. fir32_free(&line_model_d2);
  298. fir32_free(&line_model_d3);
  299. fir32_free(&line_model_d4);
  300. fir32_free(&line_model_d5);
  301. fir32_free(&line_model_d6);
  302. fir32_free(&line_model_d7);
  303. fir32_free(&line_model_d8);
  304. fir32_free(&line_model_d9);
  305. fir_float_free(&level_measurement_bp);
  306. return 0;
  307. }
  308. /*- End of function --------------------------------------------------------*/
  309. /*- End of file ------------------------------------------------------------*/