sphear_fir_render.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_fir_render: render impulse responses throught the FIR filter A2B matrix
  6. %%
  7. %% usage:
  8. %% [] = sphear_fir_render(FIR, fmin, fmax, ymin, ymax, Nfft, plot_on, smooth)
  9. %%
  10. %% FIR: A2B matrix of FIR filters
  11. %% fmin, fmax: range of frequencies to use
  12. %% ymin, ymax: y range to plot
  13. %% Fs: sampling rate
  14. %% Nfft: size of fft used to calculate frequency response
  15. %%
  16. %% optional parameters:
  17. %%
  18. %% plot_on: plot frequency response of components, default is "epsc"
  19. %% can be one of 0, "eps", "epsc" (color), "png", "pngc" (color)
  20. %% smooth: number of samples to average, default is 0
  21. %%
  22. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  23. %% nando@ccrma.stanford.edu
  24. %%
  25. %% This program is free software: you can redistribute it and/or modify
  26. %% it under the terms of the GNU General Public License as published by
  27. %% the Free Software Foundation, either version 3 of the License, or
  28. %% (at your option) any later version.
  29. %%
  30. %% This program is distributed in the hope that it will be useful,
  31. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. %% GNU General Public License for more details.
  34. %%
  35. %% You should have received a copy of the GNU General Public License
  36. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  37. function [BFF] = sphear_fir_render(FIR, fmin, fmax, ymin, ymax, Fs, Nfft, plot_on, smooth)
  38. %% no smoothing or plotting by default
  39. if (!exist("smooth"))
  40. smooth = 0;
  41. end
  42. if (!exist("plot_on"))
  43. plot_on = "epsc";
  44. else
  45. if (plot_on == 1)
  46. plot_on = "epsc"
  47. end
  48. end
  49. for m = 1:16
  50. %% render b format components
  51. bf(m,:,:) = sphear_a_to_b(m, FIR, Nfft);
  52. %% calculate B format frequency responses
  53. for b = 1:4
  54. if (smooth > 0)
  55. bfft(m,b,:) = fastsmooth(20*log10(abs(fft(bf(m,b,:), Nfft))), smooth, 2, 1);
  56. else
  57. bfft(m,b,:) = 20*log10(abs(fft(bf(m,b,:), Nfft)));
  58. end
  59. end
  60. %% write soundfile with rendered B format signals
  61. %%
  62. %% if we write a 32 bit soundfile we need to scale by 256
  63. %% if we want the same amplitude as when writing a 16 bit
  64. %% per sample soundfile, otherwise we get nasty clipping
  65. wavwrite(reshape(bf(m,:,:),4,[])' / 256, Fs, 32, ["../data/current/bf/b-format-" sprintf("%02.0f", m) ".wav"]);
  66. %% plot if requested
  67. if (plot_on != 0)
  68. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  69. %% color plot
  70. wopts = 'k;W;';
  71. xopts = 'r;X;';
  72. yopts = 'b;Y;';
  73. zopts = 'g;Z;';
  74. copt = '-color';
  75. cext = "_color";
  76. else
  77. %% mono plot
  78. wopts = '-;W;';
  79. xopts = '--;X;';
  80. yopts = '-.;Y;';
  81. zopts = ':;Z;';
  82. copt = '-mono';
  83. cext = "_mono";
  84. end
  85. f = figure;
  86. hold on;
  87. fr = (1:Nfft/2) * Fs/Nfft;
  88. grid on;
  89. angle = 360 / 16 * (m - 1);
  90. %% we add the title as a subfigure in the paper
  91. %% title(sprintf("azimuth = %.1f degrees", angle));
  92. axis([fmin, fmax, ymin, ymax]);
  93. tics('y', [6, 3, 0, -3, -6, -9, -12, -15, -20, -25, -30, -40, -50]);
  94. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  95. ca=get (gcf, "currentaxes");
  96. set(ca,"fontweight","bold","linewidth",1.5)
  97. pbaspect([16, 9, 1])
  98. semilogx(fr, bfft(m,1,1:end/2), wopts, "linewidth", 1.5);
  99. semilogx(fr, bfft(m,2,1:end/2), xopts, "linewidth", 1.5);
  100. semilogx(fr, bfft(m,3,1:end/2), yopts, "linewidth", 1.5);
  101. semilogx(fr, bfft(m,4,1:end/2), zopts, "linewidth", 1.5);
  102. l = legend('W', 'X', 'Y', 'Z');
  103. legend(l, 'location', 'west');
  104. hold off;
  105. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  106. print(f, '-deps', '-tight', copt, ["../data/current/figures/sphear_bf_azimuth_" sprintf("%03.0f", angle) cext ".eps"]);
  107. else
  108. print(f, '-dpng', copt, ["../data/current/figures/sphear_bf_azimuth_" sprintf("%03.0f", angle) cext ".png"]);
  109. end
  110. end
  111. end
  112. end