sd.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. % sd.m
  2. % David Rowe Aug 2012
  3. % Plots the spectal distorion between twofiles of LPCs. Used for LSP
  4. % quantisation tuning.
  5. function sd(raw_filename, dump_file_prefix, f)
  6. ak1_filename = sprintf("%s_ak.txt", dump_file_prefix);
  7. ak2_filename = sprintf("%s_ak_.txt", dump_file_prefix);
  8. ak1 = load(ak1_filename);
  9. ak2 = load(ak2_filename);
  10. [ak1_r, ak1_c] = size(ak1);
  11. [ak2_r, ak2_c] = size(ak1);
  12. frames = max([ak1_r ak2_r]);
  13. sd = zeros(1,frames);
  14. Ndft = 512;
  15. A1 = zeros(frames, Ndft);
  16. A2 = zeros(frames, Ndft);
  17. % initial helicopter view of all frames
  18. for i = 1:frames
  19. A1(i,:) = -20*log10(abs(fft(ak1(i,:),Ndft)));
  20. A2(i,:) = -20*log10(abs(fft(ak2(i,:),Ndft)));
  21. sd(i) = sum((A1(i,:) - A2(i,:)).^2)/Ndft;
  22. end
  23. printf("sd av %3.2f dB*dB\n", sum(sd)/frames);
  24. figure(1);
  25. clf;
  26. subplot(211)
  27. fs=fopen(raw_filename,"rb");
  28. s = fread(fs,Inf,"short");
  29. plot(s);
  30. subplot(212)
  31. plot(sd);
  32. % now enter single step mode so we can analyse each frame
  33. sn_name = strcat(dump_file_prefix,"_sn.txt");
  34. Sn = load(sn_name);
  35. lsp1_filename = sprintf("%s_lsp.txt", dump_file_prefix);
  36. lsp2_filename = sprintf("%s_lsp_.txt", dump_file_prefix);
  37. lsp1 = load(lsp1_filename);
  38. lsp2 = load(lsp2_filename);
  39. weights_filename = sprintf("%s_weights.txt", dump_file_prefix);
  40. if file_in_path(".",weights_filename)
  41. weights = load(weights_filename);
  42. end
  43. k = ' ';
  44. do
  45. figure(2);
  46. clf;
  47. subplot(211)
  48. s = [ Sn(2*f-1,:) Sn(2*f,:) ];
  49. size(s);
  50. plot(s);
  51. axis([1 length(s) -20000 20000]);
  52. subplot(212);
  53. plot((1:Ndft/2)*4000/(Ndft/2), A1(f,1:(Ndft/2)),";A1;r");
  54. axis([1 4000 -20 40]);
  55. hold on;
  56. plot((1:Ndft/2)*4000/(Ndft/2), A2(f,1:(Ndft/2)),";A2;");
  57. if file_in_path(".",weights_filename)
  58. plot(lsp1(f,:)*4000/pi, weights(f,:),";weights;g+");
  59. end
  60. for l=1:10
  61. plot([lsp1(f,l)*4000/pi lsp1(f,l)*4000/pi], [0 -10], 'r');
  62. plot([lsp2(f,l)*4000/pi lsp2(f,l)*4000/pi], [-10 -20], 'b');
  63. endfor
  64. plot(0,0,';lsp1;r');
  65. plot(0,0,';lsp2;b');
  66. sd_str = sprintf(";sd %3.2f dB*dB;", sd(f));
  67. plot(0,0,sd_str);
  68. hold off;
  69. % interactive menu
  70. printf("\rframe: %d menu: n-next b-back q-quit", f);
  71. fflush(stdout);
  72. k = kbhit();
  73. if (k == 'n')
  74. f = f + 1;
  75. endif
  76. if (k == 'b')
  77. f = f - 1;
  78. endif
  79. until (k == 'q')
  80. printf("\n");
  81. endfunction