2
0

tlspsens.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tlspsens.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 31 May 2012
  5. Testing bit error sensitivity of LSP bits, first step in devising an unequal
  6. error protection scheme.
  7. \*---------------------------------------------------------------------------*/
  8. /*
  9. Copyright (C) 2012 David Rowe
  10. All rights reserved.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU Lesser General Public License version 2, as
  13. published by the Free Software Foundation. This program is
  14. distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. License for more details.
  18. You should have received a copy of the GNU Lesser General Public License
  19. along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <assert.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include "defines.h"
  27. #include "comp.h"
  28. #include "codec2.h"
  29. #include "quantise.h"
  30. #include "interp.h"
  31. #include "codec2_internal.h"
  32. float run_a_test(char raw_file_name[], int bit_to_corrupt)
  33. {
  34. FILE *fin;
  35. short buf[N];
  36. struct CODEC2 *c2;
  37. kiss_fft_cfg fft_fwd_cfg;
  38. MODEL model;
  39. float ak[LPC_ORD+1];
  40. float lsps[LPC_ORD], e;
  41. int lsp_indexes[LPC_ORD], found_bit;
  42. float snr, snr_sum;
  43. int frames, i, mask, index;
  44. c2 = codec2_create(CODEC2_MODE_2400);
  45. fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL);
  46. fin = fopen(raw_file_name, "rb");
  47. assert(fin != NULL);
  48. /* find bit we are corrupting */
  49. found_bit = 0;
  50. for(i=0; i<LSP_SCALAR_INDEXES; i++) {
  51. if (!found_bit) {
  52. if (bit_to_corrupt > lsp_bits(i))
  53. bit_to_corrupt -= lsp_bits(i);
  54. else {
  55. index = i;
  56. mask = (1 << bit_to_corrupt);
  57. printf(" index: %d bit: %d mask: 0x%x ", index, bit_to_corrupt, mask);
  58. found_bit = 1;
  59. }
  60. }
  61. }
  62. assert(found_bit == 1);
  63. /* OK test a sample file, flipping bit */
  64. snr_sum = 0.0;
  65. frames = 0;
  66. while(fread(buf, sizeof(short), N, fin) == N) {
  67. analyse_one_frame(c2, &model, buf);
  68. e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
  69. encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
  70. /* find and flip bit we are testing */
  71. lsp_indexes[index] ^= mask;
  72. /* decode LSPs and measure SNR */
  73. decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD);
  74. check_lsp_order(lsps, LPC_ORD);
  75. bw_expand_lsps(lsps, LPC_ORD);
  76. lsp_to_lpc(lsps, ak, LPC_ORD);
  77. aks_to_M2(fft_fwd_cfg, ak, LPC_ORD, &model, e, &snr, 0, 0, 1, 1, LPCPF_BETA, LPCPF_GAMMA);
  78. snr_sum += snr;
  79. frames++;
  80. }
  81. codec2_destroy(c2);
  82. fclose(fin);
  83. return snr_sum/frames;
  84. }
  85. int main(int argc, char *argv[]) {
  86. int i;
  87. int total_lsp_bits = 0;
  88. float snr;
  89. if (argc != 2) {
  90. printf("usage: %s RawFile\n", argv[0]);
  91. exit(1);
  92. }
  93. for(i=0; i<LPC_ORD; i++)
  94. total_lsp_bits += lsp_bits(i);
  95. for(i=0; i<total_lsp_bits; i++) {
  96. snr = run_a_test(argv[1], i);
  97. printf("%d %5.2f\n", i, snr);
  98. }
  99. return 0;
  100. }