1
0

sphear_fir_render.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_fir_render: render impulse responses throught the FIR filter A2B matrix
  5. %%
  6. %% usage:
  7. %% [] = sphear_fir_render(FIR, fmin, fmax, ymin, ymax, N, Npre, Nfft, plot_on, smooth)
  8. %%
  9. %% FIR: A2B matrix of FIR filters
  10. %% fmin, fmax: range of frequencies to use
  11. %% ymin, ymax: y range to plot
  12. %% N: number of samples to filter
  13. %% Npre: number of samples before the main impulse peak
  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, N, Npre, 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. [ref, Fs] = wavread('../cal/2016.04.20/export/reference-001-1.wav');
  50. %% find highest peak location and define sample range
  51. [index, value] = findmaxpeak(ref);
  52. r_start = index - Npre;
  53. r_end = r_start + N -1;
  54. range = r_start:r_end;
  55. ref_fft = fft(ref(range), N);
  56. printf("reference: peak %f at %d, sample range [%d %d], sampling rate: %d\n",
  57. value, index, r_start, r_end, Fs);
  58. for m = 1:16
  59. %% render b format components
  60. bf(m,:,:) = sphear_a_to_b(m, FIR, range, N);
  61. %% calculate B format frequency responses
  62. for b = 1:4
  63. if (smooth > 0)
  64. bfft(m,b,:) = fastsmooth(20*log10(abs(fft(bf(m,b,:), Nfft))), smooth, 2, 1);
  65. else
  66. bfft(m,b,:) = 20*log10(abs(fft(bf(m,b,:), Nfft)));
  67. end
  68. end
  69. %% write soundfile with rendered B format signals
  70. %%
  71. %% if we write a 32 bit soundfile we need to scale by 256
  72. %% if we want the same amplitude as when writing a 16 bit
  73. %% per sample soundfile, otherwise we get nasty clipping
  74. wavwrite(reshape(bf(m,:,:),4,[])' / 256, Fs, 32, ["../cal/2016.04.20/export/b-format-" sprintf("%02.0f", m) ".wav"]);
  75. %% plot if requested
  76. if (plot_on != 0)
  77. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  78. %% color plot
  79. wopts = 'k;W;';
  80. xopts = 'r;X;';
  81. yopts = 'b;Y;';
  82. zopts = 'g;Z;';
  83. copt = '-color';
  84. cext = "_color";
  85. else
  86. %% mono plot
  87. wopts = '-;W;';
  88. xopts = '--;X;';
  89. yopts = '-.;Y;';
  90. zopts = ':;Z;';
  91. copt = '-mono';
  92. cext = "_mono";
  93. end
  94. f = figure;
  95. hold on;
  96. fr = (1:Nfft/2) * Fs/Nfft;
  97. grid on;
  98. angle = 360 / 16 * (m - 1);
  99. %% we add the title as a subfigure in the paper
  100. %% title(sprintf("azimuth = %.1f degrees", angle));
  101. axis([fmin, fmax, ymin, ymax]);
  102. tics('y', [6, 3, 0, -3, -6, -9, -12, -15, -20, -25, -30, -40, -50]);
  103. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  104. ca=get (gcf, "currentaxes");
  105. set(ca,"fontweight","bold","linewidth",1.5)
  106. pbaspect([16, 9, 1])
  107. semilogx(fr, bfft(m,1,1:end/2), wopts, "linewidth", 1.5);
  108. semilogx(fr, bfft(m,2,1:end/2), xopts, "linewidth", 1.5);
  109. semilogx(fr, bfft(m,3,1:end/2), yopts, "linewidth", 1.5);
  110. semilogx(fr, bfft(m,4,1:end/2), zopts, "linewidth", 1.5);
  111. l = legend('W', 'X', 'Y', 'Z');
  112. legend(l, 'location', 'west');
  113. hold off;
  114. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  115. print(f, '-deps', '-tight', copt, ["figures/sphear_bf_azimuth_" sprintf("%03.0f", angle) cext ".eps"]);
  116. else
  117. print(f, '-dpng', copt, ["figures/sphear_bf_azimuth_" sprintf("%03.0f", angle) cext ".png"]);
  118. end
  119. end
  120. end
  121. end