sphear_common.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_common: library of shared functions
  6. %%
  7. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  8. %% nando@ccrma.stanford.edu
  9. %%
  10. %% This program is free software: you can redistribute it and/or modify
  11. %% it under the terms of the GNU General Public License as published by
  12. %% the Free Software Foundation, either version 3 of the License, or
  13. %% (at your option) any later version.
  14. %%
  15. %% This program is distributed in the hope that it will be useful,
  16. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. %% GNU General Public License for more details.
  19. %%
  20. %% You should have received a copy of the GNU General Public License
  21. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. %% make sure octave treats this as a script file
  23. 1;
  24. pkg load signal;
  25. %% Render a calibrated capsule impulse response through the FIR filter A2B matrix
  26. %%
  27. %% m: number of measurement to render
  28. %% FIR: matrix of A2B FIR filters
  29. %% Nfft: size of FFT to use
  30. function [bf] = sphear_a_to_b(m, FIR, Nfft);
  31. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  32. %% read in calibrated capsule signals and apply FIR filter
  33. for c = 1:4
  34. cs(c,:) = wavread(["../data/current/af/a-format-cal-" sprintf("%03d", m) "-" char(capnames(c)) ".wav"]);
  35. for b = 1:4
  36. %% we add trailing zeros to let the filter ring
  37. bs(c,b,:) = fftfilt(reshape(FIR(b,c,:), 1, [])', cat(1, cs(c,:)', zeros(2*size(FIR(1,1,:))(3),1))', Nfft);
  38. end
  39. end
  40. %% add filtered capsule output to b format components
  41. for b = 1:4
  42. bf(b,:) = bs(1,b,:) + bs(2,b,:) + bs(3,b,:) + bs(4,b,:);
  43. end
  44. end
  45. %% Find maximum and location of maximum in a signal
  46. %%
  47. %% signal: vector with time domain signal
  48. function [i, v] = findmaxpeak(signal);
  49. [peaks, locs] = findpeaks(signal, "DoubleSided", "MinPeakDistance", 8);
  50. [~, index] = max(peaks);
  51. i = locs(index);
  52. v = peaks(index);
  53. end
  54. function [i, v] = findfirstpeak(signal);
  55. [peaks, locs] = findpeaks(signal, "DoubleSided", "MinPeakHeight", 0.01, "MinPeakDistance", 8);
  56. [~, index] = max(peaks);
  57. i = locs(1);
  58. v = peaks(1);
  59. end