sphear_af_calibrate.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_af_calibrate: read reference microphone signal, filter with convolution
  6. %% kernel created by DRC and filter all recorded signals,
  7. %% calibrated A format files are written to disk
  8. %%
  9. %% usage:
  10. %% [] = sphear_af_calibrate(measurements, keep, fade)
  11. %%
  12. %% keep: time to keep after main impulse in seconds
  13. %% fade: fade out time after keep in seconds
  14. %%
  15. %% Copyright 2016-2017, 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 [] = sphear_af_calibrate(measurements, keep, fade);
  31. %% the measurement from the reference microphone is processed through
  32. %% DRC (Digital Room Correction) to create a convolution kernel that is
  33. %% used to equalize the measurements
  34. %%
  35. %% read the convolution kernel
  36. [ref, Fs] = wavread('../data/current/cal/drc-filter.wav');
  37. %% window the DRC generated filter, eliminate the stuff before the main
  38. %% peak and limit the length to the approximate useful length of the
  39. %% original recorded impulse response
  40. [peaks, locs] = findpeaks(abs(ref));
  41. [value, index] = max(peaks);
  42. loc = locs(index);
  43. %% truncate before the main peak
  44. b_len = 4;
  45. before = tukeywin(2 * b_len, 0.9)(1:end/2) .* ref(loc-2*b_len:loc-b_len-1);
  46. %% useful samples
  47. samples = keep * Fs/2;
  48. during = ref(loc-b_len:loc+samples-1);
  49. %% fade out after useful samples
  50. a_len = fade * Fs/2;
  51. after = tukeywin(2 * a_len, 0.9)(end/2:end) .* ref(loc+samples:loc+samples+a_len);
  52. %% and the whole thing is:
  53. trunc = cat(1, before, during, after);
  54. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  55. %% all requested measurements
  56. for m = measurements
  57. %% read in capsule signal and normalize it
  58. for c = 1:4
  59. printf("processing capsule %d\n", c);
  60. sig = wavread(["../data/current/af/a-format-" sprintf("%03d", m) "-" char(capnames(c)) ".wav"]);
  61. cal_sig(:,c) = conv(trunc, sig);
  62. wavwrite(cal_sig(:,c), Fs, ["../data/current/af/a-format-cal-" sprintf("%03d", m) "-" char(capnames(c)) ".wav"]);
  63. printf("end capsule %d\n", c);
  64. end
  65. wavwrite(cal_sig, Fs, ["../data/current/af/a-format-cal-" sprintf("%03d", m) ".wav"]);
  66. end
  67. end