2
0

modem_echo_tests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * modem_echo_tests.c
  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. /*! \page modem_echo_can_tests_page Line echo cancellation for modems tests
  26. \section modem_echo_can_tests_page_sec_1 What does it do?
  27. Currently the echo cancellation tests only provide simple exercising of the
  28. cancellor in the way it might be used for line echo cancellation. The test code
  29. is in echotests.c.
  30. The goal is to test the echo cancellor again the G.16X specs. Clearly, that also
  31. means the goal for the cancellor itself is to comply with those specs. Right
  32. now, the only aspect of these tests implemented is the line impulse response
  33. models in g168tests.c.
  34. \section modem_echo_can_tests_page_sec_2 How does it work?
  35. The current test consists of feeding an audio file of real speech to the echo
  36. cancellor as the transmit signal. A very simple model of a telephone line is
  37. used to simulate a simple echo from the transmit signal. A second audio file of
  38. real speech is also used to simulate a signal received form the far end of the
  39. line. This is gated so it is only placed for one second every 10 seconds,
  40. simulating the double talk condition. The resulting echo cancelled signal can
  41. either be store in a file for further analysis, or played back as the data is
  42. processed.
  43. A number of modified versions of this test have been performed. The signal level
  44. of the two speech sources has been varied. Several simple models of the
  45. telephone line have been used. Although the current cancellor design has known
  46. limitations, it seems stable for all these test conditions. No instability has
  47. been observed in the current version due to arithmetic overflow when the speech
  48. is very loud (with earlier versions, well, ....:) ). The lack of saturating
  49. arithmetic in general purpose CPUs is a huge disadvantage here, as software
  50. saturation logic would cause a major slow down. Floating point would be good,
  51. but is not usable in the Linux kernel. Anyway, the bottom line seems to be the
  52. current design is genuinely useful, if imperfect.
  53. \section modem_echo_can_tests_page_sec_2 How do I use it?
  54. Build the tests with the command "./build". Currently there is no proper make
  55. setup, or way to build individual tests. "./build" will built all the tests
  56. which currently exist for the DSP functions. The echo cancellation test assumes
  57. there are two audio files containing mono, 16 bit signed PCM speech data, sampled
  58. at 8kHz. These should be called local_sound.wav and far_sound.wav. A third wave
  59. file will be produced. This very crudely starts with the first 256 bytes from
  60. the local_sound.wav file, followed by the results of the echo cancellation. The
  61. resulting audio is also played to the /dev/dsp device. A printf near the end of
  62. echo_tests.c is commented out with a \#if. If this is enabled, detailed
  63. information about the results of the echo cancellation will be written to
  64. stdout. By saving this into a file, Grace (recommended), GnuPlot, or some other
  65. plotting package may be used to graphically display the functioning of the
  66. cancellor.
  67. */
  68. #if defined(HAVE_CONFIG_H)
  69. #include "config.h"
  70. #endif
  71. #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
  72. #define ENABLE_GUI
  73. #endif
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <fcntl.h>
  77. #include <string.h>
  78. #include <time.h>
  79. #include <sndfile.h>
  80. #if defined(HAVE_MATH_H)
  81. #define GEN_CONST
  82. #endif
  83. #include "spandsp.h"
  84. #include "spandsp/g168models.h"
  85. #include "spandsp-sim.h"
  86. #if defined(ENABLE_GUI)
  87. #include "echo_monitor.h"
  88. #endif
  89. #if !defined(NULL)
  90. #define NULL (void *) 0
  91. #endif
  92. typedef struct
  93. {
  94. const char *name;
  95. int max;
  96. int cur;
  97. SNDFILE *handle;
  98. int16_t signal[8000];
  99. } signal_source_t;
  100. signal_source_t local_css;
  101. fir32_state_t line_model;
  102. SNDFILE *resulthandle;
  103. int16_t residue_sound[8000];
  104. int residue_cur = 0;
  105. int do_codec_munge = true;
  106. int use_gui = false;
  107. static const int16_t tone_1khz[] = {10362, 7327, 0, -7327, -10362, -7327, 0, 7327};
  108. static inline void put_residue(int16_t tx, int16_t residue)
  109. {
  110. int outframes;
  111. residue_sound[residue_cur++] = tx;
  112. residue_sound[residue_cur++] = residue;
  113. if (residue_cur >= 8000)
  114. {
  115. residue_cur >>= 1;
  116. outframes = sf_writef_short(resulthandle, residue_sound, residue_cur);
  117. if (outframes != residue_cur)
  118. {
  119. fprintf(stderr, " Error writing residue sound\n");
  120. exit(2);
  121. }
  122. residue_cur = 0;
  123. }
  124. }
  125. /*- End of function --------------------------------------------------------*/
  126. static void signal_load(signal_source_t *sig, const char *name)
  127. {
  128. sig->name = name;
  129. if ((sig->handle = sf_open_telephony_read(sig->name, 1)) == NULL)
  130. {
  131. fprintf(stderr, " Cannot open sound file '%s'\n", sig->name);
  132. exit(2);
  133. }
  134. sig->max = sf_readf_short(sig->handle, sig->signal, 8000);
  135. if (sig->max < 0)
  136. {
  137. fprintf(stderr, " Error reading sound file '%s'\n", sig->name);
  138. exit(2);
  139. }
  140. }
  141. /*- End of function --------------------------------------------------------*/
  142. static void signal_free(signal_source_t *sig)
  143. {
  144. if (sf_close_telephony(sig->handle))
  145. {
  146. fprintf(stderr, " Cannot close sound file '%s'\n", sig->name);
  147. exit(2);
  148. }
  149. }
  150. /*- End of function --------------------------------------------------------*/
  151. static void signal_restart(signal_source_t *sig)
  152. {
  153. sig->cur = 0;
  154. }
  155. /*- End of function --------------------------------------------------------*/
  156. static int16_t signal_amp(signal_source_t *sig)
  157. {
  158. int16_t tx;
  159. tx = sig->signal[sig->cur++];
  160. if (sig->cur >= sig->max)
  161. sig->cur = 0;
  162. return tx;
  163. }
  164. /*- End of function --------------------------------------------------------*/
  165. static inline int16_t codec_munger(int16_t amp)
  166. {
  167. if (do_codec_munge)
  168. return alaw_to_linear(linear_to_alaw(amp));
  169. return amp;
  170. }
  171. /*- End of function --------------------------------------------------------*/
  172. static void channel_model_create(int model)
  173. {
  174. static const int32_t *line_models[] =
  175. {
  176. line_model_d2_coeffs,
  177. line_model_d3_coeffs,
  178. line_model_d4_coeffs,
  179. line_model_d5_coeffs,
  180. line_model_d6_coeffs,
  181. line_model_d7_coeffs,
  182. line_model_d8_coeffs,
  183. line_model_d9_coeffs
  184. };
  185. static int line_model_sizes[] =
  186. {
  187. sizeof(line_model_d2_coeffs)/sizeof(int32_t),
  188. sizeof(line_model_d3_coeffs)/sizeof(int32_t),
  189. sizeof(line_model_d4_coeffs)/sizeof(int32_t),
  190. sizeof(line_model_d5_coeffs)/sizeof(int32_t),
  191. sizeof(line_model_d6_coeffs)/sizeof(int32_t),
  192. sizeof(line_model_d7_coeffs)/sizeof(int32_t),
  193. sizeof(line_model_d8_coeffs)/sizeof(int32_t),
  194. sizeof(line_model_d9_coeffs)/sizeof(int32_t)
  195. };
  196. fir32_create(&line_model, line_models[model], line_model_sizes[model]);
  197. }
  198. /*- End of function --------------------------------------------------------*/
  199. static int16_t channel_model(int16_t local, int16_t far)
  200. {
  201. int16_t echo;
  202. int16_t rx;
  203. /* Channel modelling is merely simulating the effects of A-law distortion
  204. and using one of the echo models from G.168 */
  205. /* The local tx signal will have gone through an A-law munging before
  206. it reached the line's analogue area where the echo occurs. */
  207. echo = fir32(&line_model, codec_munger(local/8));
  208. /* The far end signal will have been through an A-law munging, although
  209. this should not affect things. */
  210. rx = echo + codec_munger(far);
  211. /* This mixed echo and far end signal will have been through an A-law munging when it came back into
  212. the digital network. */
  213. rx = codec_munger(rx);
  214. return rx;
  215. }
  216. /*- End of function --------------------------------------------------------*/
  217. int main(int argc, char *argv[])
  218. {
  219. modem_echo_can_state_t *ctx;
  220. //awgn_state_t local_noise_source;
  221. awgn_state_t far_noise_source;
  222. int i;
  223. int clean;
  224. int16_t rx;
  225. int16_t tx;
  226. int line_model_no;
  227. time_t now;
  228. power_meter_t power_before;
  229. power_meter_t power_after;
  230. float unadapted_output_power;
  231. float unadapted_echo_power;
  232. float adapted_output_power;
  233. float adapted_echo_power;
  234. #if defined(ENABLE_GUI)
  235. int16_t amp[2];
  236. #endif
  237. line_model_no = 0;
  238. use_gui = false;
  239. for (i = 1; i < argc; i++)
  240. {
  241. if (strcmp(argv[i], "-g") == 0)
  242. {
  243. use_gui = true;
  244. continue;
  245. }
  246. line_model_no = atoi(argv[1]);
  247. }
  248. time(&now);
  249. ctx = modem_echo_can_init(256);
  250. awgn_init_dbm0(&far_noise_source, 7162534, -50.0f);
  251. signal_load(&local_css, "sound_c1_8k.wav");
  252. if ((resulthandle = sf_open_telephony_write("modem_echo.wav", 2)) == NULL)
  253. {
  254. fprintf(stderr, " Failed to open result file\n");
  255. exit(2);
  256. }
  257. #if defined(ENABLE_GUI)
  258. if (use_gui)
  259. start_echo_can_monitor(256);
  260. #endif
  261. channel_model_create(line_model_no);
  262. #if defined(ENABLE_GUI)
  263. if (use_gui)
  264. echo_can_monitor_line_model_update(line_model.coeffs, line_model.taps);
  265. #endif
  266. modem_echo_can_flush(ctx);
  267. power_meter_init(&power_before, 5);
  268. power_meter_init(&power_after, 5);
  269. /* Measure the echo power before adaption */
  270. modem_echo_can_adaption_mode(ctx, false);
  271. for (i = 0; i < 8000*5; i++)
  272. {
  273. tx = tone_1khz[i & 7];
  274. rx = channel_model(tx, 0);
  275. clean = modem_echo_can_update(ctx, tx, rx);
  276. power_meter_update(&power_before, rx);
  277. power_meter_update(&power_after, clean);
  278. }
  279. unadapted_output_power = power_meter_current_dbm0(&power_before);
  280. unadapted_echo_power = power_meter_current_dbm0(&power_after);
  281. printf("Pre-adaption: output power %10.5fdBm0, echo power %10.5fdBm0\n", unadapted_output_power, unadapted_echo_power);
  282. /* Converge the canceller */
  283. signal_restart(&local_css);
  284. modem_echo_can_adaption_mode(ctx, true);
  285. for (i = 0; i < 800*2; i++)
  286. {
  287. clean = modem_echo_can_update(ctx, 0, 0);
  288. put_residue(0, clean);
  289. }
  290. for (i = 0; i < 8000*50; i++)
  291. {
  292. tx = signal_amp(&local_css);
  293. rx = channel_model(tx, 0);
  294. clean = modem_echo_can_update(ctx, tx, rx);
  295. power_meter_update(&power_before, rx);
  296. power_meter_update(&power_after, clean);
  297. #if 0
  298. if (i%800 == 0)
  299. printf("Powers %10.5fdBm0 %10.5fdBm0\n", power_meter_current_dbm0(&power_before), power_meter_current_dbm0(&power_after));
  300. #endif
  301. put_residue(tx, clean);
  302. #if defined(ENABLE_GUI)
  303. if (use_gui)
  304. {
  305. echo_can_monitor_can_update(ctx->fir_taps16, 256);
  306. amp[0] = tx;
  307. echo_can_monitor_line_spectrum_update(amp, 1);
  308. }
  309. #endif
  310. }
  311. /* Now lets see how well adapted we are */
  312. modem_echo_can_adaption_mode(ctx, false);
  313. for (i = 0; i < 8000*5; i++)
  314. {
  315. tx = tone_1khz[i & 7];
  316. rx = channel_model(tx, 0);
  317. clean = modem_echo_can_update(ctx, tx, rx);
  318. power_meter_update(&power_before, rx);
  319. power_meter_update(&power_after, clean);
  320. }
  321. adapted_output_power = power_meter_current_dbm0(&power_before);
  322. adapted_echo_power = power_meter_current_dbm0(&power_after);
  323. printf("Post-adaption: output power %10.5fdBm0, echo power %10.5fdBm0\n", adapted_output_power, adapted_echo_power);
  324. if (fabsf(adapted_output_power - unadapted_output_power) > 0.1f
  325. ||
  326. adapted_echo_power > unadapted_echo_power - 30.0f)
  327. {
  328. printf("Tests failed.\n");
  329. exit(2);
  330. }
  331. modem_echo_can_free(ctx);
  332. signal_free(&local_css);
  333. if (sf_close_telephony(resulthandle))
  334. {
  335. fprintf(stderr, " Cannot close speech file '%s'\n", "result_sound.wav");
  336. exit(2);
  337. }
  338. #if defined(ENABLE_GUI)
  339. if (use_gui)
  340. echo_can_monitor_wait_to_end();
  341. #endif
  342. printf("Tests passed.\n");
  343. return 0;
  344. }
  345. /*- End of function --------------------------------------------------------*/
  346. /*- End of file ------------------------------------------------------------*/