sphear_fir_render_directions.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. %%
  2. %% *SpHEAR Project
  3. %% sphear_fir_render_directions: render impulse responses throught the FIR filter A2B matrix
  4. %%
  5. %% usage:
  6. %% [] = sphear_fir_render_directions(FIR, fmin, fmax, N, Npre, Nfft, plot_on, smooth)
  7. %%
  8. %% FIR: A2B matrix of FIR filters
  9. %% fmin, fmax: range of frequencies to use
  10. %% N: number of samples to filter
  11. %% Npre: number of samples before the main impulse peak
  12. %% Nfft: size of FFT
  13. %%
  14. %% optional parameters:
  15. %%
  16. %% plot_on: plot frequency response of components, default is "epsc"
  17. %% can be one of 0, "eps", "epsc" (color), "png", "pngc" (color)
  18. %% smooth: number of samples to average, default is 0
  19. %%
  20. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  21. %% nando@ccrma.stanford.edu
  22. %%
  23. %% This program is free software: you can redistribute it and/or modify
  24. %% it under the terms of the GNU General Public License as published by
  25. %% the Free Software Foundation, either version 3 of the License, or
  26. %% (at your option) any later version.
  27. %%
  28. %% This program is distributed in the hope that it will be useful,
  29. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. %% GNU General Public License for more details.
  32. %%
  33. %% You should have received a copy of the GNU General Public License
  34. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  35. function [BFF] = sphear_fir_render_directions(FIR, fmin, fmax, N, Npre, Nfft, plot_on, smooth)
  36. %% no smoothing or plotting by default
  37. if (!exist("smooth"))
  38. smooth = 0;
  39. end
  40. if (!exist("plot_on"))
  41. plot_on = "epsc";
  42. else
  43. if (plot_on == 1)
  44. plot_on = "epsc"
  45. end
  46. end
  47. [ref, Fs] = wavread('../cal/2016.04.20/export/reference-001-1.wav');
  48. %% find highest peak location and define sample range
  49. [index, value] = findmaxpeak(ref);
  50. r_start = index - Npre;
  51. r_end = r_start + N -1;
  52. range = r_start:r_end;
  53. ref_fft = fft(tukeywin(size(range)(2), 0.02) .* ref(range), N);
  54. printf("reference: peak %f at %d, sample range [%d %d]\n", value, index, r_start, r_end);
  55. %% plot if requested
  56. if (plot_on != 0)
  57. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  58. %% color plot
  59. wopts = 'k;W;';
  60. popts = 'r;X;';
  61. dopts = 'b;Y;';
  62. copt = '-color';
  63. cext = "_color";
  64. else
  65. %% mono plot
  66. wopts = '-;W;';
  67. popts = '--;X;';
  68. dopts = '-.;Y;';
  69. copt = '-mono';
  70. cext = "_mono";
  71. end
  72. f = figure;
  73. hold on;
  74. grid on;
  75. axis([fmin, fmax, -30, 0]);
  76. tics('y', [6, 0, -3, -6, -9, -12, -15, -20, -25, -30]);
  77. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  78. idx = 1;
  79. for m = [1, 5, 9, 13, 3, 7, 11, 15]
  80. %% render b format components
  81. bf(idx,:,:) = sphear_a_to_b(m, FIR, range, N);
  82. %% calculate B format frequency responses
  83. for b = 1:4
  84. if (smooth > 0)
  85. bfft(idx,b,:) = fastsmooth(20*log10(abs(fft(bf(idx,b,:), Nfft))), smooth, 2, 1);
  86. else
  87. bfft(idx,b,:) = 20*log10(abs(fft(bf(idx,b,:), Nfft)));
  88. end
  89. end
  90. fr = (1:Nfft/2) * Fs/Nfft;
  91. %% W
  92. semilogx(fr, bfft(idx,1,1:end/2) * 10^(6/20), wopts);
  93. %% print principal and diagonal directions with different colors
  94. if (idx > 4)
  95. col = popts;
  96. else
  97. col = dopts;
  98. end
  99. if (rem(idx, 2) == 1)
  100. semilogx(fr, bfft(idx,2,1:end/2), col);
  101. else
  102. semilogx(fr, bfft(idx,3,1:end/2), col);
  103. end
  104. idx = idx + 1;
  105. end
  106. l = legend('W (+6dB)', 'XY principal', 'XY diagonal');
  107. legend(l, 'location', 'west');
  108. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  109. print(f, '-deps', '-tight', copt,["figures/sphear_directions.eps"]);
  110. else
  111. print(f, '-deps', copt, ["figures/sphear_directions.eps"]);
  112. end
  113. end
  114. end