v29_tests.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * v29_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2003 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 v29_tests_page V.29 modem tests
  26. \section v29_tests_page_sec_1 What does it do?
  27. These tests test one way paths, as V.29 is a half-duplex modem. They allow either:
  28. - A V.29 transmit modem to feed a V.29 receive modem through a telephone line
  29. model. BER testing is then used to evaluate performance under various line
  30. conditions. This is effective for testing the basic performance of the
  31. receive modem. It is also the only test mode provided for evaluating the
  32. transmit modem.
  33. - A V.29 receive modem is used to decode V.29 audio, stored in an audio file.
  34. This is good way to evaluate performance with audio recorded from other
  35. models of modem, and with real world problematic telephone lines.
  36. If the appropriate GUI environment exists, the tests are built such that a visual
  37. display of modem status is maintained.
  38. \section v29_tests_page_sec_2 How is it used?
  39. */
  40. #if defined(HAVE_CONFIG_H)
  41. #include "config.h"
  42. #endif
  43. #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
  44. #define ENABLE_GUI
  45. #endif
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <fcntl.h>
  49. #include <unistd.h>
  50. #include <string.h>
  51. #include <sndfile.h>
  52. #include <signal.h>
  53. #if defined(HAVE_FENV_H)
  54. #define __USE_GNU
  55. #include <fenv.h>
  56. #endif
  57. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  58. #include "spandsp.h"
  59. #include "spandsp-sim.h"
  60. #if defined(ENABLE_GUI)
  61. #include "modem_monitor.h"
  62. #include "line_model_monitor.h"
  63. #endif
  64. #define BLOCK_LEN 160
  65. #define OUT_FILE_NAME "v29.wav"
  66. char *decode_test_file = NULL;
  67. bool use_gui = false;
  68. int symbol_no = 0;
  69. int rx_bits = 0;
  70. bert_state_t bert;
  71. one_way_line_model_state_t *line_model;
  72. #if defined(ENABLE_GUI)
  73. qam_monitor_t *qam_monitor;
  74. #endif
  75. bert_results_t latest_results;
  76. static void reporter(void *user_data, int reason, bert_results_t *results)
  77. {
  78. switch (reason)
  79. {
  80. case BERT_REPORT_REGULAR:
  81. fprintf(stderr, "BERT report regular - %d bits, %d bad bits, %d resyncs\n", results->total_bits, results->bad_bits, results->resyncs);
  82. memcpy(&latest_results, results, sizeof(latest_results));
  83. break;
  84. default:
  85. fprintf(stderr, "BERT report %s\n", bert_event_to_str(reason));
  86. break;
  87. }
  88. }
  89. /*- End of function --------------------------------------------------------*/
  90. static void v29_rx_status(void *user_data, int status)
  91. {
  92. v29_rx_state_t *s;
  93. int i;
  94. int len;
  95. #if defined(SPANDSP_USE_FIXED_POINT)
  96. complexi16_t *coeffs;
  97. #else
  98. complexf_t *coeffs;
  99. #endif
  100. printf("V.29 rx status is %s (%d)\n", signal_status_to_str(status), status);
  101. s = (v29_rx_state_t *) user_data;
  102. switch (status)
  103. {
  104. case SIG_STATUS_TRAINING_SUCCEEDED:
  105. printf("Training succeeded\n");
  106. if ((len = v29_rx_equalizer_state(s, &coeffs)))
  107. {
  108. printf("Equalizer:\n");
  109. for (i = 0; i < len; i++)
  110. #if defined(SPANDSP_USE_FIXED_POINT)
  111. printf("%3d (%15.5f, %15.5f)\n", i, coeffs[i].re/V29_CONSTELLATION_SCALING_FACTOR, coeffs[i].im/V29_CONSTELLATION_SCALING_FACTOR);
  112. #else
  113. printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
  114. #endif
  115. }
  116. break;
  117. }
  118. }
  119. /*- End of function --------------------------------------------------------*/
  120. static void v29putbit(void *user_data, int bit)
  121. {
  122. if (bit < 0)
  123. {
  124. v29_rx_status(user_data, bit);
  125. return;
  126. }
  127. if (decode_test_file)
  128. printf("Rx bit %d - %d\n", rx_bits++, bit);
  129. else
  130. bert_put_bit(&bert, bit);
  131. }
  132. /*- End of function --------------------------------------------------------*/
  133. static void v29_tx_status(void *user_data, int status)
  134. {
  135. printf("V.29 tx status is %s (%d)\n", signal_status_to_str(status), status);
  136. }
  137. /*- End of function --------------------------------------------------------*/
  138. static int v29getbit(void *user_data)
  139. {
  140. return bert_get_bit(&bert);
  141. }
  142. /*- End of function --------------------------------------------------------*/
  143. #if defined(SPANDSP_USE_FIXED_POINT)
  144. static void qam_report(void *user_data, const complexi16_t *constel, const complexi16_t *target, int symbol)
  145. #else
  146. static void qam_report(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol)
  147. #endif
  148. {
  149. int i;
  150. int len;
  151. #if defined(SPANDSP_USE_FIXED_POINT)
  152. complexi16_t *coeffs;
  153. #else
  154. complexf_t *coeffs;
  155. #endif
  156. complexf_t constel_point;
  157. complexf_t target_point;
  158. float fpower;
  159. v29_rx_state_t *rx;
  160. static float smooth_power = 0.0f;
  161. static int update_interval = 100;
  162. rx = (v29_rx_state_t *) user_data;
  163. if (constel)
  164. {
  165. constel_point.re = constel->re/V29_CONSTELLATION_SCALING_FACTOR;
  166. constel_point.im = constel->im/V29_CONSTELLATION_SCALING_FACTOR;
  167. target_point.re = target->re/V29_CONSTELLATION_SCALING_FACTOR,
  168. target_point.im = target->im/V29_CONSTELLATION_SCALING_FACTOR,
  169. fpower = (constel_point.re - target_point.re)*(constel_point.re - target_point.re)
  170. + (constel_point.im - target_point.im)*(constel_point.im - target_point.im);
  171. smooth_power = 0.95f*smooth_power + 0.05f*fpower;
  172. #if defined(ENABLE_GUI)
  173. if (use_gui)
  174. {
  175. qam_monitor_update_constel(qam_monitor, &constel_point);
  176. qam_monitor_update_carrier_tracking(qam_monitor, v29_rx_carrier_frequency(rx));
  177. //qam_monitor_update_carrier_tracking(qam_monitor, (fpower) ? fpower : 0.001f);
  178. qam_monitor_update_symbol_tracking(qam_monitor, v29_rx_symbol_timing_correction(rx));
  179. }
  180. #endif
  181. printf("%8d [%8.4f, %8.4f] [%8.4f, %8.4f] %2x %8.4f %8.4f %9.4f %7.3f %7.4f\n",
  182. symbol_no,
  183. constel_point.re,
  184. constel_point.im,
  185. target_point.re,
  186. target_point.im,
  187. symbol,
  188. fpower,
  189. smooth_power,
  190. v29_rx_carrier_frequency(rx),
  191. v29_rx_signal_power(rx),
  192. v29_rx_symbol_timing_correction(rx));
  193. symbol_no++;
  194. if (--update_interval <= 0)
  195. {
  196. if ((len = v29_rx_equalizer_state(rx, &coeffs)))
  197. {
  198. printf("Equalizer A:\n");
  199. for (i = 0; i < len; i++)
  200. #if defined(SPANDSP_USE_FIXED_POINT)
  201. printf("%3d (%15.5f, %15.5f)\n", i, coeffs[i].re/V29_CONSTELLATION_SCALING_FACTOR, coeffs[i].im/V29_CONSTELLATION_SCALING_FACTOR);
  202. #else
  203. printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
  204. #endif
  205. #if defined(ENABLE_GUI)
  206. if (use_gui)
  207. {
  208. #if defined(SPANDSP_USE_FIXED_POINT)
  209. qam_monitor_update_int_equalizer(qam_monitor, coeffs, len);
  210. #else
  211. qam_monitor_update_equalizer(qam_monitor, coeffs, len);
  212. #endif
  213. }
  214. #endif
  215. }
  216. update_interval = 100;
  217. }
  218. }
  219. }
  220. /*- End of function --------------------------------------------------------*/
  221. #if defined(HAVE_FENV_H)
  222. static void sigfpe_handler(int sig_num, siginfo_t *info, void *data)
  223. {
  224. switch (sig_num)
  225. {
  226. case SIGFPE:
  227. switch (info->si_code)
  228. {
  229. case FPE_INTDIV:
  230. fprintf(stderr, "integer divide by zero at %p\n", info->si_addr);
  231. break;
  232. case FPE_INTOVF:
  233. fprintf(stderr, "integer overflow at %p\n", info->si_addr);
  234. break;
  235. case FPE_FLTDIV:
  236. fprintf(stderr, "FP divide by zero at %p\n", info->si_addr);
  237. break;
  238. case FPE_FLTOVF:
  239. fprintf(stderr, "FP overflow at %p\n", info->si_addr);
  240. break;
  241. case FPE_FLTUND:
  242. fprintf(stderr, "FP underflow at %p\n", info->si_addr);
  243. break;
  244. case FPE_FLTRES:
  245. fprintf(stderr, "FP inexact result at %p\n", info->si_addr);
  246. break;
  247. case FPE_FLTINV:
  248. fprintf(stderr, "FP invalid operation at %p\n", info->si_addr);
  249. break;
  250. case FPE_FLTSUB:
  251. fprintf(stderr, "subscript out of range at %p\n", info->si_addr);
  252. break;
  253. }
  254. break;
  255. default:
  256. fprintf(stderr, "Unexpected signal %d\n", sig_num);
  257. break;
  258. }
  259. exit(2);
  260. }
  261. /*- End of function --------------------------------------------------------*/
  262. static void fpe_trap_setup(void)
  263. {
  264. struct sigaction trap;
  265. sigemptyset(&trap.sa_mask);
  266. trap.sa_flags = SA_SIGINFO;
  267. trap.sa_sigaction = sigfpe_handler;
  268. sigaction(SIGFPE, &trap, NULL);
  269. //feenableexcept(FE_DIVBYZERO | FE_INEXACT | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW);
  270. //feenableexcept(FE_ALL_EXCEPT);
  271. feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  272. }
  273. /*- End of function --------------------------------------------------------*/
  274. #endif
  275. int main(int argc, char *argv[])
  276. {
  277. v29_rx_state_t *rx;
  278. v29_tx_state_t *tx;
  279. bert_results_t bert_results;
  280. int16_t gen_amp[BLOCK_LEN];
  281. int16_t amp[BLOCK_LEN];
  282. SNDFILE *inhandle;
  283. SNDFILE *outhandle;
  284. int outframes;
  285. int samples;
  286. int test_bps;
  287. int noise_level;
  288. int signal_level;
  289. int bits_per_test;
  290. int line_model_no;
  291. int block_no;
  292. int channel_codec;
  293. int rbs_pattern;
  294. int opt;
  295. bool tep;
  296. bool log_audio;
  297. logging_state_t *logging;
  298. channel_codec = MUNGE_CODEC_NONE;
  299. rbs_pattern = 0;
  300. test_bps = 9600;
  301. tep = false;
  302. line_model_no = 0;
  303. decode_test_file = NULL;
  304. use_gui = false;
  305. noise_level = -70;
  306. signal_level = -13;
  307. bits_per_test = 50000;
  308. log_audio = false;
  309. while ((opt = getopt(argc, argv, "b:B:c:d:glm:n:r:s:t")) != -1)
  310. {
  311. switch (opt)
  312. {
  313. case 'b':
  314. test_bps = atoi(optarg);
  315. if (test_bps != 9600 && test_bps != 7200 && test_bps != 4800)
  316. {
  317. fprintf(stderr, "Invalid bit rate specified\n");
  318. exit(2);
  319. }
  320. break;
  321. case 'B':
  322. bits_per_test = atoi(optarg);
  323. break;
  324. case 'c':
  325. channel_codec = atoi(optarg);
  326. break;
  327. case 'd':
  328. decode_test_file = optarg;
  329. break;
  330. case 'g':
  331. #if defined(ENABLE_GUI)
  332. use_gui = true;
  333. #else
  334. fprintf(stderr, "Graphical monitoring not available\n");
  335. exit(2);
  336. #endif
  337. break;
  338. case 'l':
  339. log_audio = true;
  340. break;
  341. case 'm':
  342. line_model_no = atoi(optarg);
  343. break;
  344. case 'n':
  345. noise_level = atoi(optarg);
  346. break;
  347. case 'r':
  348. rbs_pattern = atoi(optarg);
  349. break;
  350. case 's':
  351. signal_level = atoi(optarg);
  352. break;
  353. case 't':
  354. tep = true;
  355. break;
  356. default:
  357. //usage();
  358. exit(2);
  359. break;
  360. }
  361. }
  362. inhandle = NULL;
  363. outhandle = NULL;
  364. #if defined(HAVE_FENV_H)
  365. fpe_trap_setup();
  366. #endif
  367. if (log_audio)
  368. {
  369. if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
  370. {
  371. fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME);
  372. exit(2);
  373. }
  374. }
  375. if (decode_test_file)
  376. {
  377. /* We will decode the audio from a file. */
  378. tx = NULL;
  379. if ((inhandle = sf_open_telephony_read(decode_test_file, 1)) == NULL)
  380. {
  381. fprintf(stderr, " Cannot open audio file '%s'\n", decode_test_file);
  382. exit(2);
  383. }
  384. }
  385. else
  386. {
  387. /* We will generate V.29 audio, and add some noise to it. */
  388. tx = v29_tx_init(NULL, test_bps, tep, v29getbit, NULL);
  389. logging = v29_tx_get_logging_state(tx);
  390. span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  391. span_log_set_tag(logging, "V.29-tx");
  392. v29_tx_power(tx, signal_level);
  393. v29_tx_set_modem_status_handler(tx, v29_tx_status, (void *) tx);
  394. #if defined(SPANDSP_EXPOSE_INTERNAL_STRUCTURES)
  395. /* Move the carrier off a bit */
  396. tx->carrier_phase_rate = dds_phase_ratef(1710.0f);
  397. tx->carrier_phase = 0;
  398. #endif
  399. bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  400. bert_set_report(&bert, 10000, reporter, NULL);
  401. if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL)
  402. {
  403. fprintf(stderr, " Failed to create line model\n");
  404. exit(2);
  405. }
  406. }
  407. rx = v29_rx_init(NULL, test_bps, v29putbit, NULL);
  408. logging = v29_rx_get_logging_state(rx);
  409. span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  410. span_log_set_tag(logging, "V.29-rx");
  411. v29_rx_signal_cutoff(rx, -45.5f);
  412. v29_rx_set_modem_status_handler(rx, v29_rx_status, (void *) rx);
  413. v29_rx_set_qam_report_handler(rx, qam_report, (void *) rx);
  414. #if defined(SPANDSP_EXPOSE_INTERNAL_STRUCTURES)
  415. /* Rotate the starting phase */
  416. rx->carrier_phase = 0x80000000;
  417. #endif
  418. #if defined(ENABLE_GUI)
  419. if (use_gui)
  420. {
  421. qam_monitor = qam_monitor_init(6.0f, V29_CONSTELLATION_SCALING_FACTOR, NULL);
  422. if (!decode_test_file)
  423. {
  424. start_line_model_monitor(129);
  425. line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len);
  426. }
  427. }
  428. #endif
  429. memset(&latest_results, 0, sizeof(latest_results));
  430. for (block_no = 0; ; block_no++)
  431. {
  432. if (decode_test_file)
  433. {
  434. samples = sf_readf_short(inhandle, amp, BLOCK_LEN);
  435. #if defined(ENABLE_GUI)
  436. if (use_gui)
  437. qam_monitor_update_audio_level(qam_monitor, amp, samples);
  438. #endif
  439. if (samples == 0)
  440. break;
  441. }
  442. else
  443. {
  444. samples = v29_tx(tx, gen_amp, BLOCK_LEN);
  445. #if defined(ENABLE_GUI)
  446. if (use_gui)
  447. qam_monitor_update_audio_level(qam_monitor, gen_amp, samples);
  448. #endif
  449. if (samples == 0)
  450. {
  451. /* Push a little silence through, to ensure all the data bits get out of the buffers */
  452. vec_zeroi16(amp, BLOCK_LEN);
  453. v29_rx(rx, amp, BLOCK_LEN);
  454. /* Note that we might get a few bad bits as the carrier shuts down. */
  455. bert_result(&bert, &bert_results);
  456. fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
  457. fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
  458. /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report
  459. should be error free, though the final report will generally contain bits errors as the carrier was dying. The total
  460. number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred
  461. rapidly at the start of transmission, the last report should have occurred at not much less than the total number of
  462. bits we sent. */
  463. if (bert_results.total_bits < bits_per_test
  464. ||
  465. latest_results.total_bits < bits_per_test - 100
  466. ||
  467. latest_results.bad_bits != 0)
  468. {
  469. break;
  470. }
  471. memset(&latest_results, 0, sizeof(latest_results));
  472. signal_level--;
  473. v29_tx_restart(tx, test_bps, tep);
  474. v29_tx_power(tx, signal_level);
  475. v29_rx_restart(rx, test_bps, false);
  476. #if defined(SPANDSP_EXPOSE_INTERNAL_STRUCTURES)
  477. rx->eq_put_step = rand()%(48*10/3);
  478. #endif
  479. bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  480. bert_set_report(&bert, 10000, reporter, NULL);
  481. one_way_line_model_free(line_model);
  482. if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL)
  483. {
  484. fprintf(stderr, " Failed to create line model\n");
  485. exit(2);
  486. }
  487. }
  488. if (log_audio)
  489. {
  490. outframes = sf_writef_short(outhandle, gen_amp, samples);
  491. if (outframes != samples)
  492. {
  493. fprintf(stderr, " Error writing audio file\n");
  494. exit(2);
  495. }
  496. }
  497. one_way_line_model(line_model, amp, gen_amp, samples);
  498. }
  499. #if defined(ENABLE_GUI)
  500. if (use_gui && !decode_test_file)
  501. line_model_monitor_line_spectrum_update(amp, samples);
  502. #endif
  503. v29_rx(rx, amp, samples);
  504. }
  505. if (!decode_test_file)
  506. {
  507. bert_result(&bert, &bert_results);
  508. fprintf(stderr, "At completion:\n");
  509. fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
  510. fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
  511. one_way_line_model_free(line_model);
  512. if (signal_level > -43)
  513. {
  514. printf("Tests failed.\n");
  515. exit(2);
  516. }
  517. printf("Tests passed.\n");
  518. }
  519. v29_rx_free(rx);
  520. if (tx)
  521. v29_tx_free(tx);
  522. bert_release(&bert);
  523. #if defined(ENABLE_GUI)
  524. if (use_gui)
  525. qam_wait_to_end(qam_monitor);
  526. #endif
  527. if (decode_test_file)
  528. {
  529. if (sf_close_telephony(inhandle))
  530. {
  531. fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file);
  532. exit(2);
  533. }
  534. }
  535. if (log_audio)
  536. {
  537. if (sf_close_telephony(outhandle))
  538. {
  539. fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
  540. exit(2);
  541. }
  542. }
  543. return 0;
  544. }
  545. /*- End of function --------------------------------------------------------*/
  546. /*- End of file ------------------------------------------------------------*/