sphear_fir_render.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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)
  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. %% Fs: sampling rate
  13. %% plt: plot response
  14. %%
  15. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  16. %% nando@ccrma.stanford.edu
  17. %%
  18. %% This program is free software: you can redistribute it and/or modify
  19. %% it under the terms of the GNU General Public License as published by
  20. %% the Free Software Foundation, either version 3 of the License, or
  21. %% (at your option) any later version.
  22. %%
  23. %% This program is distributed in the hope that it will be useful,
  24. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. %% GNU General Public License for more details.
  27. %%
  28. %% You should have received a copy of the GNU General Public License
  29. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. function [BFF] = sphear_fir_render(FIR, fmin, fmax, N, Npre, Fs, plt)
  31. [ref, Fs] = wavread('../cal/2016.04.20/export/reference-001-1.wav');
  32. %% find highest peak location and define sample range
  33. [index, value] = findmaxpeak(ref);
  34. r_start = index - Npre;
  35. r_end = r_start + N -1;
  36. range = r_start:r_end;
  37. ref_fft = fft(ref(range), N);
  38. printf("reference: peak %f at %d, sample range [%d %d]\n", value, index, r_start, r_end);
  39. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  40. raw = 0;
  41. writefile = 1;
  42. minindex = N;
  43. maxpeak = 0;
  44. for m = 1:16
  45. c1r = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(1)) ".wav"]);
  46. c1 = c1r(range);
  47. [index, peak] = findmaxpeak(c1r);
  48. maxpeak = max(maxpeak, abs(peak));
  49. minindex = min(minindex, (index - r_start));
  50. w1 = fftfilt(reshape(FIR(1,1,:), 1, [])', c1);
  51. x1 = fftfilt(reshape(FIR(2,1,:), 1, [])', c1);
  52. y1 = fftfilt(reshape(FIR(3,1,:), 1, [])', c1);
  53. z1 = fftfilt(reshape(FIR(4,1,:), 1, [])', c1);
  54. c2r = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(2)) ".wav"]);
  55. c2 = c2r(range);
  56. [index, peak] = findmaxpeak(c2r);
  57. maxpeak = max(maxpeak, peak);
  58. minindex = min(minindex, (index - r_start));
  59. w2 = fftfilt(reshape(FIR(1,2,:), 1, [])', c2);
  60. x2 = fftfilt(reshape(FIR(2,2,:), 1, [])', c2);
  61. y2 = fftfilt(reshape(FIR(3,2,:), 1, [])', c2);
  62. z2 = fftfilt(reshape(FIR(4,2,:), 1, [])', c2);
  63. c3r = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(3)) ".wav"]);
  64. c3 = c3r(range);
  65. [index, peak] = findmaxpeak(c3r);
  66. maxpeak = max(maxpeak, peak);
  67. minindex = min(minindex, (index - r_start));
  68. w3 = fftfilt(reshape(FIR(1,3,:), 1, [])', c3);
  69. x3 = fftfilt(reshape(FIR(2,3,:), 1, [])', c3);
  70. y3 = fftfilt(reshape(FIR(3,3,:), 1, [])', c3);
  71. z3 = fftfilt(reshape(FIR(4,3,:), 1, [])', c3);
  72. c4r = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(4)) ".wav"]);
  73. c4 = c4r(range);
  74. [index, peak] = findmaxpeak(c4r);
  75. maxpeak = max(maxpeak, peak);
  76. minindex = min(minindex, (index - r_start));
  77. w4 = fftfilt(reshape(FIR(1,4,:), 1, [])', c4);
  78. x4 = fftfilt(reshape(FIR(2,4,:), 1, [])', c4);
  79. y4 = fftfilt(reshape(FIR(3,4,:), 1, [])', c4);
  80. z4 = fftfilt(reshape(FIR(4,4,:), 1, [])', c4);
  81. if (raw == 1)
  82. wc = (c1 + c2 + c3 + c4);
  83. xc = (c1 + c2 - c3 - c4);
  84. yc = (c1 - c2 + c3 - c4);
  85. zc = (c1 - c2 - c3 + c4);
  86. rawlabel = "_raw";
  87. else
  88. wc = (w1 + w2 + w3 + w4);
  89. xc = (x1 + x2 + x3 + x4);
  90. yc = (y1 + y2 + y3 + y4);
  91. zc = (z1 + z2 + z3 + z4);
  92. rawlabel = "";
  93. end
  94. BFF(:,1,m) = wc;
  95. BFF(:,2,m) = xc;
  96. BFF(:,3,m) = yc;
  97. BFF(:,4,m) = zc;
  98. if (writefile == 1)
  99. maxw = max(abs(wc));
  100. maxx = max(abs(xc));
  101. maxy = max(abs(yc));
  102. maxz = max(abs(zc));
  103. maxbf = max([maxw, maxx, maxy, maxz]);
  104. printf("m%d: max value is %f\n", m, maxbf);
  105. %% don't know why the written soundfile is clipped...
  106. %% the max value we calculate is less that 1.0...
  107. %% so set up an attenuation factor...
  108. att = 0.05;
  109. wavwrite(wc * att, Fs, 32, ["../cal/2016.04.20/export/b-format-w-" sprintf("%02.0f", m) ".wav"]);
  110. wavwrite(xc * att, Fs, 32, ["../cal/2016.04.20/export/b-format-x-" sprintf("%02.0f", m) ".wav"]);
  111. wavwrite(yc * att, Fs, 32, ["../cal/2016.04.20/export/b-format-y-" sprintf("%02.0f", m) ".wav"]);
  112. wavwrite(zc * att, Fs, 32, ["../cal/2016.04.20/export/b-format-z-" sprintf("%02.0f", m) ".wav"]);
  113. end
  114. if (plt == 1)
  115. f = figure;
  116. hold on;
  117. Nfft = 4096;
  118. wfft = 20*log10(abs(fft(wc, Nfft)));
  119. xfft = 20*log10(abs(fft(xc, Nfft)));
  120. yfft = 20*log10(abs(fft(yc, Nfft)));
  121. zfft = 20*log10(abs(fft(zc, Nfft)));
  122. smooth = 12;
  123. wcr = fastsmooth(wfft, smooth, 2, 1);
  124. xcr = fastsmooth(xfft, smooth, 2, 1);
  125. ycr = fastsmooth(yfft, smooth, 2, 1);
  126. zcr = fastsmooth(zfft, smooth, 2, 1);
  127. fr = (1:Nfft/2) * Fs/Nfft;
  128. grid on;
  129. angle = 360 / 16 * (m - 1);
  130. title(sprintf("azimuth = %05.1f degrees", angle));
  131. axis([300, 20000, -60, 6]);
  132. tics('y', [6, 0, -2.5, -5, -7.5, -10, -12.5, -15, -17.5, -20, -22.5, -25, -30, -40, -50]);
  133. semilogx(fr, wcr(1:end/2), 'k;W;');
  134. semilogx(fr, xcr(1:end/2), 'r;X;');
  135. semilogx(fr, ycr(1:end/2), 'b;Y;');
  136. semilogx(fr, zcr(1:end/2), 'g;Z;');
  137. l = legend('W', 'X', 'Y', 'Z');
  138. legend(l, 'location', 'southwest');
  139. hold off;
  140. print(f, '-dpng','-color',["figures/sphear_b_vs_angle_" sprintf("%03.0f", angle) rawlabel ".png"]);
  141. end
  142. end
  143. [minindex, maxpeak]
  144. end
  145. function [i, v] = findmaxpeak(signal);
  146. [peaks, locs] = findpeaks(signal, "DoubleSided");
  147. [~, index] = max(peaks);
  148. i = locs(index);
  149. v = peaks(index);
  150. end