fsk_tests.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * fsk_tests.c - Tests for the low speed FSK modem code (V.21, V.23, etc.).
  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 fsk_tests_page FSK modem tests
  26. \section fsk_tests_page_sec_1 What does it do?
  27. These tests allow either:
  28. - An FSK transmit modem to feed an FSK receive modem, of the same type,
  29. through a telephone line model. BER testing is then used to evaluate
  30. performance under various line conditions. This is effective for testing
  31. the basic performance of the receive modem. It is also the only test mode
  32. provided for evaluating the transmit modem.
  33. - An FSK receive modem is used to decode FSK audio, stored in a 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. \section fsk_tests_page_sec_2 How does it work?
  37. */
  38. #if defined(HAVE_CONFIG_H)
  39. #include "config.h"
  40. #endif
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <assert.h>
  46. #include <sndfile.h>
  47. #include "spandsp.h"
  48. #include "spandsp-sim.h"
  49. #define BLOCK_LEN 160
  50. #define OUTPUT_FILE_NAME "fsk.wav"
  51. char *decode_test_file = NULL;
  52. both_ways_line_model_state_t *model;
  53. int rx_bits = 0;
  54. bool cutoff_test_carrier = false;
  55. static void rx_status(void *user_data, int status)
  56. {
  57. printf("FSK rx status is %s (%d)\n", signal_status_to_str(status), status);
  58. }
  59. /*- End of function --------------------------------------------------------*/
  60. static void tx_status(void *user_data, int status)
  61. {
  62. printf("FSK tx status is %s (%d)\n", signal_status_to_str(status), status);
  63. }
  64. /*- End of function --------------------------------------------------------*/
  65. static void put_bit(void *user_data, int bit)
  66. {
  67. if (bit < 0)
  68. {
  69. rx_status(user_data, bit);
  70. return;
  71. }
  72. printf("Rx bit %d - %d\n", rx_bits++, bit);
  73. }
  74. /*- End of function --------------------------------------------------------*/
  75. static void cutoff_test_rx_status(void *user_data, int status)
  76. {
  77. printf("FSK rx status is %s (%d)\n", signal_status_to_str(status), status);
  78. switch (status)
  79. {
  80. case SIG_STATUS_CARRIER_UP:
  81. cutoff_test_carrier = true;
  82. break;
  83. case SIG_STATUS_CARRIER_DOWN:
  84. cutoff_test_carrier = false;
  85. break;
  86. }
  87. }
  88. /*- End of function --------------------------------------------------------*/
  89. static void cutoff_test_put_bit(void *user_data, int bit)
  90. {
  91. if (bit < 0)
  92. {
  93. cutoff_test_rx_status(user_data, bit);
  94. return;
  95. }
  96. }
  97. /*- End of function --------------------------------------------------------*/
  98. static void reporter(void *user_data, int reason, bert_results_t *results)
  99. {
  100. int channel;
  101. channel = (int) (intptr_t) user_data;
  102. switch (reason)
  103. {
  104. case BERT_REPORT_SYNCED:
  105. fprintf(stderr, "%d: BERT report synced\n", channel);
  106. break;
  107. case BERT_REPORT_UNSYNCED:
  108. fprintf(stderr, "%d: BERT report unsync'ed\n", channel);
  109. break;
  110. case BERT_REPORT_REGULAR:
  111. fprintf(stderr, "%d: BERT report regular - %d bits, %d bad bits, %d resyncs\n", channel, results->total_bits, results->bad_bits, results->resyncs);
  112. break;
  113. case BERT_REPORT_GT_10_2:
  114. fprintf(stderr, "%d: BERT report > 1 in 10^2\n", channel);
  115. break;
  116. case BERT_REPORT_LT_10_2:
  117. fprintf(stderr, "%d: BERT report < 1 in 10^2\n", channel);
  118. break;
  119. case BERT_REPORT_LT_10_3:
  120. fprintf(stderr, "%d: BERT report < 1 in 10^3\n", channel);
  121. break;
  122. case BERT_REPORT_LT_10_4:
  123. fprintf(stderr, "%d: BERT report < 1 in 10^4\n", channel);
  124. break;
  125. case BERT_REPORT_LT_10_5:
  126. fprintf(stderr, "%d: BERT report < 1 in 10^5\n", channel);
  127. break;
  128. case BERT_REPORT_LT_10_6:
  129. fprintf(stderr, "%d: BERT report < 1 in 10^6\n", channel);
  130. break;
  131. case BERT_REPORT_LT_10_7:
  132. fprintf(stderr, "%d: BERT report < 1 in 10^7\n", channel);
  133. break;
  134. default:
  135. fprintf(stderr, "%d: BERT report reason %d\n", channel, reason);
  136. break;
  137. }
  138. }
  139. /*- End of function --------------------------------------------------------*/
  140. int main(int argc, char *argv[])
  141. {
  142. fsk_tx_state_t *caller_tx;
  143. fsk_rx_state_t *caller_rx;
  144. fsk_tx_state_t *answerer_tx;
  145. fsk_rx_state_t *answerer_rx;
  146. bert_state_t caller_bert;
  147. bert_state_t answerer_bert;
  148. bert_results_t bert_results;
  149. power_meter_t caller_meter;
  150. power_meter_t answerer_meter;
  151. int16_t caller_amp[BLOCK_LEN];
  152. int16_t answerer_amp[BLOCK_LEN];
  153. int16_t caller_model_amp[BLOCK_LEN];
  154. int16_t answerer_model_amp[BLOCK_LEN];
  155. int16_t out_amp[2*BLOCK_LEN];
  156. SNDFILE *inhandle;
  157. SNDFILE *outhandle;
  158. int outframes;
  159. int i;
  160. int j;
  161. int samples;
  162. int test_bps;
  163. int noise_level;
  164. int noise_sweep;
  165. int bits_per_test;
  166. int line_model_no;
  167. int modem_under_test_1;
  168. int modem_under_test_2;
  169. int modems_set;
  170. int channel_codec;
  171. int rbs_pattern;
  172. int on_at;
  173. int off_at;
  174. int opt;
  175. bool log_audio;
  176. tone_gen_descriptor_t tone_desc;
  177. tone_gen_state_t tone_tx;
  178. channel_codec = MUNGE_CODEC_NONE;
  179. rbs_pattern = 0;
  180. line_model_no = 0;
  181. decode_test_file = NULL;
  182. noise_sweep = false;
  183. modem_under_test_1 = FSK_V21CH1;
  184. modem_under_test_2 = FSK_V21CH2;
  185. log_audio = false;
  186. modems_set = 0;
  187. while ((opt = getopt(argc, argv, "c:d:lm:nr:s:")) != -1)
  188. {
  189. switch (opt)
  190. {
  191. case 'c':
  192. channel_codec = atoi(optarg);
  193. break;
  194. case 'd':
  195. decode_test_file = optarg;
  196. break;
  197. case 'l':
  198. log_audio = true;
  199. break;
  200. case 'm':
  201. line_model_no = atoi(optarg);
  202. break;
  203. case 'n':
  204. noise_sweep = true;
  205. break;
  206. case 'r':
  207. rbs_pattern = atoi(optarg);
  208. break;
  209. case 's':
  210. switch (modems_set++)
  211. {
  212. case 0:
  213. modem_under_test_1 = atoi(optarg);
  214. break;
  215. case 1:
  216. modem_under_test_2 = atoi(optarg);
  217. break;
  218. }
  219. break;
  220. default:
  221. //usage();
  222. exit(2);
  223. break;
  224. }
  225. }
  226. if (modem_under_test_1 >= 0)
  227. printf("Modem channel 1 is '%s'\n", preset_fsk_specs[modem_under_test_1].name);
  228. if (modem_under_test_2 >= 0)
  229. printf("Modem channel 2 is '%s'\n", preset_fsk_specs[modem_under_test_2].name);
  230. outhandle = NULL;
  231. if (log_audio)
  232. {
  233. if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 2)) == NULL)
  234. {
  235. fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_FILE_NAME);
  236. exit(2);
  237. }
  238. }
  239. noise_level = -200;
  240. bits_per_test = 0;
  241. inhandle = NULL;
  242. memset(caller_amp, 0, sizeof(*caller_amp));
  243. memset(answerer_amp, 0, sizeof(*answerer_amp));
  244. memset(caller_model_amp, 0, sizeof(*caller_model_amp));
  245. memset(answerer_model_amp, 0, sizeof(*answerer_model_amp));
  246. power_meter_init(&caller_meter, 7);
  247. power_meter_init(&answerer_meter, 7);
  248. if (decode_test_file)
  249. {
  250. if ((inhandle = sf_open_telephony_read(decode_test_file, 1)) == NULL)
  251. {
  252. fprintf(stderr, " Cannot open audio file '%s'\n", decode_test_file);
  253. exit(2);
  254. }
  255. caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], FSK_FRAME_MODE_SYNC, put_bit, NULL);
  256. fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx);
  257. test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
  258. for (;;)
  259. {
  260. samples = sf_readf_short(inhandle, caller_model_amp, BLOCK_LEN);
  261. if (samples < BLOCK_LEN)
  262. break;
  263. for (i = 0; i < samples; i++)
  264. power_meter_update(&caller_meter, caller_model_amp[i]);
  265. fsk_rx(caller_rx, caller_model_amp, samples);
  266. }
  267. if (sf_close_telephony(inhandle))
  268. {
  269. fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file);
  270. exit(2);
  271. }
  272. fsk_rx_free(caller_rx);
  273. }
  274. else
  275. {
  276. printf("Test cutoff level\n");
  277. caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], FSK_FRAME_MODE_SYNC, cutoff_test_put_bit, NULL);
  278. fsk_rx_signal_cutoff(caller_rx, -30.0f);
  279. fsk_rx_set_modem_status_handler(caller_rx, cutoff_test_rx_status, (void *) &caller_rx);
  280. on_at = 0;
  281. for (i = -40; i < -25; i++)
  282. {
  283. tone_gen_descriptor_init(&tone_desc,
  284. 1500,
  285. i,
  286. 0,
  287. 0,
  288. 1,
  289. 0,
  290. 0,
  291. 0,
  292. true);
  293. tone_gen_init(&tone_tx, &tone_desc);
  294. for (j = 0; j < 10; j++)
  295. {
  296. samples = tone_gen(&tone_tx, caller_model_amp, 160);
  297. fsk_rx(caller_rx, caller_model_amp, samples);
  298. }
  299. if (cutoff_test_carrier)
  300. break;
  301. }
  302. on_at = i;
  303. off_at = 0;
  304. for ( ; i > -40; i--)
  305. {
  306. tone_gen_descriptor_init(&tone_desc,
  307. 1500,
  308. i,
  309. 0,
  310. 0,
  311. 1,
  312. 0,
  313. 0,
  314. 0,
  315. true);
  316. tone_gen_init(&tone_tx, &tone_desc);
  317. for (j = 0; j < 10; j++)
  318. {
  319. samples = tone_gen(&tone_tx, caller_model_amp, 160);
  320. fsk_rx(caller_rx, caller_model_amp, samples);
  321. }
  322. if (!cutoff_test_carrier)
  323. break;
  324. }
  325. off_at = i;
  326. printf("Carrier on at %d, off at %d\n", on_at, off_at);
  327. if (on_at < -29 || on_at > -26
  328. ||
  329. off_at < -35 || off_at > -31)
  330. {
  331. printf("Tests failed.\n");
  332. exit(2);
  333. }
  334. fsk_rx_free(caller_rx);
  335. printf("Test with BERT\n");
  336. test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
  337. if (modem_under_test_1 >= 0)
  338. {
  339. caller_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert);
  340. fsk_tx_set_modem_status_handler(caller_tx, tx_status, (void *) &caller_tx);
  341. answerer_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], FSK_FRAME_MODE_SYNC, (put_bit_func_t) bert_put_bit, &answerer_bert);
  342. fsk_rx_set_modem_status_handler(answerer_rx, rx_status, (void *) &answerer_rx);
  343. }
  344. if (modem_under_test_2 >= 0)
  345. {
  346. answerer_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert);
  347. fsk_tx_set_modem_status_handler(answerer_tx, tx_status, (void *) &answerer_tx);
  348. caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) bert_put_bit, &caller_bert);
  349. fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx);
  350. }
  351. test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
  352. bits_per_test = 500000;
  353. noise_level = -24;
  354. bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  355. bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1);
  356. bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  357. bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2);
  358. if ((model = both_ways_line_model_init(line_model_no,
  359. (float) noise_level,
  360. -15.0f,
  361. -15.0f,
  362. line_model_no,
  363. (float) noise_level,
  364. -15.0f,
  365. -15.0f,
  366. channel_codec,
  367. rbs_pattern)) == NULL)
  368. {
  369. fprintf(stderr, " Failed to create line model\n");
  370. exit(2);
  371. }
  372. for (;;)
  373. {
  374. samples = fsk_tx(caller_tx, caller_amp, BLOCK_LEN);
  375. for (i = 0; i < samples; i++)
  376. power_meter_update(&caller_meter, caller_amp[i]);
  377. samples = fsk_tx(answerer_tx, answerer_amp, BLOCK_LEN);
  378. for (i = 0; i < samples; i++)
  379. power_meter_update(&answerer_meter, answerer_amp[i]);
  380. both_ways_line_model(model,
  381. caller_model_amp,
  382. caller_amp,
  383. answerer_model_amp,
  384. answerer_amp,
  385. samples);
  386. //printf("Powers %10.5fdBm0 %10.5fdBm0\n", power_meter_current_dbm0(&caller_meter), power_meter_current_dbm0(&answerer_meter));
  387. fsk_rx(answerer_rx, caller_model_amp, samples);
  388. for (i = 0; i < samples; i++)
  389. out_amp[2*i] = caller_model_amp[i];
  390. for ( ; i < BLOCK_LEN; i++)
  391. out_amp[2*i] = 0;
  392. fsk_rx(caller_rx, answerer_model_amp, samples);
  393. for (i = 0; i < samples; i++)
  394. out_amp[2*i + 1] = answerer_model_amp[i];
  395. for ( ; i < BLOCK_LEN; i++)
  396. out_amp[2*i + 1] = 0;
  397. if (log_audio)
  398. {
  399. outframes = sf_writef_short(outhandle, out_amp, BLOCK_LEN);
  400. if (outframes != BLOCK_LEN)
  401. {
  402. fprintf(stderr, " Error writing audio file\n");
  403. exit(2);
  404. }
  405. }
  406. if (samples < BLOCK_LEN)
  407. {
  408. bert_result(&caller_bert, &bert_results);
  409. fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
  410. if (!noise_sweep)
  411. {
  412. if (bert_results.total_bits != bits_per_test - 43
  413. ||
  414. bert_results.bad_bits != 0
  415. ||
  416. bert_results.resyncs != 0)
  417. {
  418. printf("Tests failed.\n");
  419. exit(2);
  420. }
  421. }
  422. bert_result(&answerer_bert, &bert_results);
  423. fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
  424. if (!noise_sweep)
  425. {
  426. if (bert_results.total_bits != bits_per_test - 43
  427. ||
  428. bert_results.bad_bits != 0
  429. ||
  430. bert_results.resyncs != 0)
  431. {
  432. printf("Tests failed.\n");
  433. exit(2);
  434. }
  435. break;
  436. }
  437. /* Put a little silence between the chunks in the file. */
  438. memset(out_amp, 0, sizeof(out_amp));
  439. if (log_audio)
  440. {
  441. for (i = 0; i < 200; i++)
  442. outframes = sf_writef_short(outhandle, out_amp, BLOCK_LEN);
  443. }
  444. if (modem_under_test_1 >= 0)
  445. {
  446. caller_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert);
  447. fsk_tx_set_modem_status_handler(caller_tx, tx_status, (void *) &caller_tx);
  448. answerer_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], FSK_FRAME_MODE_SYNC, (put_bit_func_t) bert_put_bit, &answerer_bert);
  449. fsk_rx_set_modem_status_handler(answerer_rx, rx_status, (void *) &answerer_rx);
  450. }
  451. if (modem_under_test_2 >= 0)
  452. {
  453. answerer_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert);
  454. fsk_tx_set_modem_status_handler(answerer_tx, tx_status, (void *) &answerer_tx);
  455. caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) bert_put_bit, &caller_bert);
  456. fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx);
  457. }
  458. noise_level++;
  459. both_ways_line_model_free(model);
  460. if ((model = both_ways_line_model_init(line_model_no,
  461. (float) noise_level,
  462. line_model_no,
  463. -15.0f,
  464. -15.0f,
  465. noise_level,
  466. channel_codec,
  467. -15.0f,
  468. -15.0f,
  469. 0)) == NULL)
  470. {
  471. fprintf(stderr, " Failed to create line model\n");
  472. exit(2);
  473. }
  474. bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  475. bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1);
  476. bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
  477. bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2);
  478. }
  479. }
  480. bert_release(&caller_bert);
  481. bert_release(&answerer_bert);
  482. if (modem_under_test_1 >= 0)
  483. {
  484. fsk_tx_free(caller_tx);
  485. fsk_rx_free(answerer_rx);
  486. }
  487. if (modem_under_test_2 >= 0)
  488. {
  489. fsk_tx_free(answerer_tx);
  490. fsk_rx_free(caller_rx);
  491. }
  492. both_ways_line_model_free(model);
  493. printf("Tests passed.\n");
  494. }
  495. if (log_audio)
  496. {
  497. if (sf_close_telephony(outhandle))
  498. {
  499. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME);
  500. exit(2);
  501. }
  502. }
  503. return 0;
  504. }
  505. /*- End of function --------------------------------------------------------*/
  506. /*- End of file ------------------------------------------------------------*/