1
0

sphear_wxyz_power.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_a_power: return array of power for WXYZ B format signals in a range of frequencies
  5. %%
  6. %% usage:
  7. %% [B Fs] = sphear_wxyz_power(measurements, fmin, fmax, N, Npre, Nfft, plotz)
  8. %%
  9. %% measurements: measurements to consider
  10. %% fmin, fmax: range of frequencies to consider
  11. %% N: total number of samples to analyze
  12. %% Npre: number of samples before the main impulse
  13. %% Nfft: size of fft
  14. %% plotz: 0 -> plot cardioid, 1 -> plot Z
  15. %%
  16. %% The names of reference microphone impulse response and capsule measurements are
  17. %% currently hardwired into this file, this needs to be fixed and turned into
  18. %% parameters
  19. %%
  20. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  21. %% nando@ccrma.stanford.edu
  22. %%
  23. %% This program is free software: you can redistribute it and/or modify
  24. %% it under the terms of the GNU General Public License as published by
  25. %% the Free Software Foundation, either version 3 of the License, or
  26. %% (at your option) any later version.
  27. %%
  28. %% This program is distributed in the hope that it will be useful,
  29. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. %% GNU General Public License for more details.
  32. %%
  33. %% You should have received a copy of the GNU General Public License
  34. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  35. function [B Fs] = sphear_wxyz_power(measurements, fmin, fmax, N, Npre, Nfft, plotz)
  36. %% read in reference microphone, select samples to process
  37. [ref, Fs] = wavread('../data/2016.08.03/st250/reference-001-1.wav');
  38. [peaks, locs] = findpeaks(abs(ref));
  39. [~, index] = max(peaks);
  40. start = locs(index) - Npre;
  41. range = start:(start + N - 1);
  42. ref_fft = fft(ref(range), Nfft);
  43. %% indexes in array for frequency bins
  44. imin = round((N/2) * fmin / (Fs/2));
  45. imax = round((N/2) * fmax / (Fs/2));
  46. %% name of B format components
  47. bnames = cellstr(['W'; 'X'; 'Y'; 'Z']);
  48. %% all measurements
  49. for m = measurements
  50. %% all components
  51. for c = 1:4
  52. %% all b format components, 4 channel soundfile
  53. bsignal = wavread(["../data/2016.08.03/st250/b-format-0" sprintf("%02.0f", m) "-" char(bnames(c)) ".wav"]);
  54. if (c == 1)
  55. %% FUMA to SN3D for W
  56. bsignal = bsignal / 0.707;
  57. end
  58. bfft = fft(bsignal(range), Nfft) ./ ref_fft;
  59. if (plotz == 0)
  60. %% calculate a forward facing cardiod
  61. cfft = (fft(bsignal(range) + 1.0 * bsignal(range), Nfft)) ./ ref_fft;
  62. end
  63. %% return total power of signal in frequency range
  64. B(m, c) = sqrt(sum(bfft(imin:imax,:).*conj(bfft(imin:imax,:)))/size(bfft(imin:imax,:),1));
  65. %% make a cardiod if we don't want Z
  66. if (c == 4)
  67. if (plotz == 0)
  68. B(m, 4) = sqrt(sum(cfft(imin:imax).*conj(cfft(imin:imax)))/size(cfft(imin:imax),1)) / 2.0;
  69. end
  70. end
  71. end
  72. end
  73. end