sphear_fir_render_directions.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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('../data/current/ref/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 = '-';
  67. popts = '--';
  68. dopts = ':';
  69. copt = '-mono';
  70. cext = "_mono";
  71. end
  72. f = figure;
  73. hold on;
  74. grid on;
  75. axis([fmin, fmax, -40, 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. ca=get (gcf, "currentaxes");
  79. set(ca,"fontweight","bold","linewidth",1.5)
  80. idx = 1;
  81. for m = [1, 5, 9, 13, 3, 7, 11, 15]
  82. %% render b format components
  83. bf(idx,:,:) = sphear_a_to_b(m, FIR, range, N);
  84. %% calculate B format frequency responses
  85. for b = 1:4
  86. if (smooth > 0)
  87. bfft(idx,b,:) = fastsmooth(20*log10(abs(fft(bf(idx,b,:), Nfft))), smooth, 2, 1);
  88. else
  89. bfft(idx,b,:) = 20*log10(abs(fft(bf(idx,b,:), Nfft)));
  90. end
  91. end
  92. fr = (1:Nfft/2) * Fs/Nfft;
  93. %% W
  94. p1 = semilogx(fr, bfft(idx,1,1:end/2) * 10^(-6/20), wopts, "linewidth", 1.5);
  95. %% print principal and diagonal directions with different colors
  96. if (rem(idx, 2) == 1)
  97. if (idx > 4)
  98. p2 = semilogx(fr, bfft(idx,2,1:end/2) * 10^(6/20), dopts, "linewidth", 1.5);
  99. else
  100. p3 = semilogx(fr, bfft(idx,2,1:end/2) * 10^(6/20), popts, "linewidth", 1.5);
  101. end
  102. else
  103. if (idx > 4)
  104. p2 = semilogx(fr, bfft(idx,3,1:end/2) * 10^(6/20), dopts, "linewidth", 1.5);
  105. else
  106. p3 = semilogx(fr, bfft(idx,3,1:end/2) * 10^(6/20), popts, "linewidth", 1.5);
  107. end
  108. end
  109. idx = idx + 1;
  110. end
  111. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  112. l = legend([p1, p2, p3],
  113. {"W (+12dB for clarity)", "XY principal directions", "XY diagonal directions"});
  114. legend(l, 'location', 'southwest');
  115. else
  116. %% there seems to be bug with legends in mono, the order of
  117. %% the legends seems to be reversed
  118. l = legend([p1, p2, p3],
  119. {"XY principal directions", "W (+12dB for clarity)", "XY diagonal directions"});
  120. legend(l, 'location', 'southwest');
  121. end
  122. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  123. print(f, '-deps', '-tight', copt,["figures/sphear_directions.eps"]);
  124. else
  125. print(f, '-deps', copt, ["figures/sphear_directions.eps"]);
  126. end
  127. end
  128. end