sphear_bf_power_fons.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_bf_power_fons: return array of power for all capsules in a range of frequencies
  5. %%
  6. %% usage:
  7. %% [R] = sphear_a_power_fons(fmin, fmax, N, Fs, plotz)
  8. %%
  9. %% fmin, fmax: range of frequencies to consider
  10. %% N: fft size
  11. %% Fs: sampling rate
  12. %% plotz: 0 -> calculate cardioid, 1 -> calculate Z
  13. %%
  14. %% The names of reference microphone impulse response and capsule measurements are
  15. %% currently hardwired into this file, this needs to be fixed and turned into
  16. %% input parameters
  17. %%
  18. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  19. %% nando@ccrma.stanford.edu
  20. %%
  21. %% This program is free software: you can redistribute it and/or modify
  22. %% it under the terms of the GNU General Public License as published by
  23. %% the Free Software Foundation, either version 3 of the License, or
  24. %% (at your option) any later version.
  25. %%
  26. %% This program is distributed in the hope that it will be useful,
  27. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. %% GNU General Public License for more details.
  30. %%
  31. %% You should have received a copy of the GNU General Public License
  32. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. function [B] = sphear_bf_power_fons(fmin, fmax, N, Fs, plotz)
  34. pkg load signal;
  35. %% indexes in array for frequency bins
  36. imin = round((N/2) * fmin / (Fs/2));
  37. imax = round((N/2) * fmax / (Fs/2));
  38. r_start = 0;
  39. r_end = 0;
  40. %% all measurements
  41. for m = 1:16
  42. %% all b format components, 4 channel soundfile
  43. bsignal = wavread(["../cal/2016.08.03/fons/b-format-rendered-" sprintf("%03.0f", m) "-16.wav"]);
  44. %% transform from FUMA to SN3D (the encoder is outputing FUMA)
  45. %% bsignal(:,1) = bsignal(:,1) * (sqrt(2)/2);
  46. %% find first peak, read samples around it
  47. Npre = 64;
  48. Nbuf = 256;
  49. [index, value] = findmaxpeak(bsignal(:,1));
  50. r_start = index - Npre;
  51. r_end = r_start + Nbuf -1;
  52. range = r_start:r_end;
  53. printf("%d: samples = %d -> %d (%d), imin:imax %d:%d\n", m, r_start, r_end, r_end-r_start, imin, imax);
  54. bfft = fft(bsignal(range,:), N);
  55. if (plotz == 0)
  56. %% calculate a forward facing cardiod
  57. cfft = fft(bsignal(range,1) + 1.0 * bsignal(range,2), N);
  58. end
  59. %% return total power of signal in frequency range
  60. scaler = 8;
  61. B(m, :) = sqrt(sum(bfft(imin:imax,:).*conj(bfft(imin:imax,:)))/size(bfft(imin:imax,:),1)) / scaler;
  62. %% make a cardiod if we don't want Z
  63. if (plotz == 0)
  64. B(m, 4) = sqrt(sum(cfft(imin:imax).*conj(cfft(imin:imax)))/size(cfft(imin:imax),1)) / (2 * scaler);
  65. end
  66. end
  67. end