sphear_af_power.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_af_power: return array of power for all capsules in a range of frequencies
  5. %%
  6. %% usage:
  7. %% [R Fs] = sphear_af_power(fmin, fmax, N, Npre)
  8. %%
  9. %% fmin, fmax: range of frequencies to consider
  10. %% Npre: number of samples before the main impulse
  11. %% N: total number of samples to analyze
  12. %%
  13. %% The names of reference microphone impulse response and capsule measurements are
  14. %% currently hardwired into this file, this needs to be fixed and turned into
  15. %% input parameters
  16. %%
  17. %% Copyright Fernando Lopez-Lezcano 2016
  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 [R Fs] = sphear_af_power(measurements, fmin, fmax, N, Npre)
  33. pkg load signal;
  34. %% read in reference microphone, select samples to process
  35. [ref, Fs] = wavread('../cal/2016.04.20/export/reference-001-1.wav');
  36. [peaks, locs] = findpeaks(abs(ref));
  37. [~, index] = max(peaks);
  38. start = locs(index) - Npre;
  39. range = start:(start + N - 1);
  40. ref_fft = fft(ref(range), N);
  41. %% indexes in array for frequency bins
  42. imin = round((N/2) * fmin / (Fs/2));
  43. imax = round((N/2) * fmax / (Fs/2));
  44. capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
  45. %% all submitted measurements
  46. i = 1;
  47. for m = measurements
  48. %% all capsules
  49. for c = 1:4
  50. %% read in capsule measurement and equalize to reference microphone
  51. %% this is only valid for the frequency range covered by the test speaker,
  52. %% anything outside is undefined
  53. %%
  54. capsule = wavread(["../cal/2016.04.20/export/a-format-0" sprintf("%02.0f", m) "-" char(capnames(c)) ".wav"]);
  55. equalized = fft(capsule(range), N) ./ ref_fft;
  56. %% return total power of signal in frequency range
  57. R(i, c) = sqrt(sum(equalized(imin:imax).*conj(equalized(imin:imax)))/size(equalized(imin:imax),1));
  58. end
  59. i = i + 1;
  60. end
  61. end