1
0

sphear_fir_render_directions.m 3.3 KB

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