1
0

sphear_fir_render.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. %%
  2. %% *SpHEAR Project
  3. %% sphear_fir_render: render impulse responses throught the FIR filter A2B matrix
  4. %%
  5. %% usage:
  6. %% [] = sphear_fir_render(FIR, fmin, fmax, N, Npre, Nfft, Fs, smooth, plt)
  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 used to calculate frequency response
  13. %% Fs: sampling rate
  14. %% smooth: number of samples to average
  15. %% plt: plot response
  16. %%
  17. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  18. %% nando@ccrma.stanford.edu
  19. %%
  20. %% This program is free software: you can redistribute it and/or modify
  21. %% it under the terms of the GNU General Public License as published by
  22. %% the Free Software Foundation, either version 3 of the License, or
  23. %% (at your option) any later version.
  24. %%
  25. %% This program is distributed in the hope that it will be useful,
  26. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. %% GNU General Public License for more details.
  29. %%
  30. %% You should have received a copy of the GNU General Public License
  31. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. function [BFF] = sphear_fir_render(FIR, fmin, fmax, N, Npre, Nfft, Fs, smooth, plt)
  33. [ref, Fs] = wavread('../cal/2016.04.20/export/reference-001-1.wav');
  34. %% find highest peak location and define sample range
  35. [index, value] = findmaxpeak(ref);
  36. r_start = index - Npre;
  37. r_end = r_start + N -1;
  38. range = r_start:r_end;
  39. ref_fft = fft(ref(range), N);
  40. printf("reference: peak %f at %d, sample range [%d %d]\n", value, index, r_start, r_end);
  41. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  42. writefile = 1;
  43. idx = 1;
  44. for m = 1:16
  45. %% render b format components
  46. bf(idx,:,:) = sphear_a_to_b(m, FIR, range, N);
  47. %% calculate B format frequency responses
  48. for b = 1:4
  49. bfft(idx,b,:) = fastsmooth(20*log10(abs(fft(bf(idx,b,:), Nfft))), smooth, 2, 1);
  50. end
  51. %% write soundfile with rendered B format signals
  52. if (writefile == 1)
  53. %% if we write a 32 bit soundfile we need to scale by
  54. %% 256 if want the same amplitude as when writing a
  55. %% 16 bit sample soundfile...
  56. wavwrite(reshape(bf(idx,:,:),4,[])' / 256, Fs, 32, ["../cal/2016.04.20/export/b-format-" sprintf("%02.0f", m) ".wav"]);
  57. end
  58. if (plt == 1)
  59. f = figure;
  60. hold on;
  61. fr = (1:Nfft/2) * Fs/Nfft;
  62. grid on;
  63. angle = 360 / 16 * (m - 1);
  64. title(sprintf("azimuth = %05.1f degrees", angle));
  65. axis([fmin, fmax, -45, 12]);
  66. tics('y', [6, 0, -2.5, -5, -7.5, -10, -12.5, -15, -17.5, -20, -22.5, -25, -30, -40, -50]);
  67. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  68. semilogx(fr, bfft(idx,1,1:end/2), 'k;W;');
  69. semilogx(fr, bfft(idx,2,1:end/2), 'r;X;');
  70. semilogx(fr, bfft(idx,3,1:end/2), 'b;Y;');
  71. semilogx(fr, bfft(idx,4,1:end/2), 'g;Z;');
  72. l = legend('W', 'X', 'Y', 'Z');
  73. legend(l, 'location', 'southwest');
  74. hold off;
  75. print(f, '-deps','-color',["figures/sphear_b_vs_angle_" sprintf("%03.0f", angle) ".eps"]);
  76. end
  77. idx = idx + 1;
  78. end
  79. end
  80. function [bf] = sphear_a_to_b(m, FIR, range, N);
  81. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  82. %% read in capsule signals and apply FIR filter
  83. for c = 1:4
  84. cs(c,:) = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(c)) ".wav"]);
  85. cr(c,:) = tukeywin(size(range)(2), 0.0) .* cs(c,:)'(range);
  86. for b = 1:4
  87. bs(c,b,:) = fftfilt(reshape(FIR(b,c,:), 1, [])', cr(c,:), N);
  88. end
  89. end
  90. %% add filtered capsule output to b format components
  91. for b = 1:4
  92. bf(b,:) = bs(1,b,:) + bs(2,b,:) + bs(3,b,:) + bs(4,b,:);
  93. end
  94. end
  95. function [i, v] = findmaxpeak(signal);
  96. [peaks, locs] = findpeaks(signal, "DoubleSided");
  97. [~, index] = max(peaks);
  98. i = locs(index);
  99. v = peaks(index);
  100. end