sphear_capsule_power.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_capsule_power: return sorted array of power for capsule measurements
  5. %%
  6. %% usage:
  7. %% [R Fs] = sphear_af_power(measurements, fmin, fmax, N, Npre)
  8. %%
  9. %% measurements: list of measurements to consider
  10. %% fmin, fmax: range of frequencies to consider
  11. %% Npre: number of samples before the main impulse
  12. %% N: total number of samples to analyze
  13. %%
  14. %% The name of capsule measurement files iscurrently hardwired into this
  15. %% file, this needs to be fixed and turned into 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 [RS Fs] = sphear_capsule_power(measurements, fmin, fmax, N, Npre)
  33. pkg load signal;
  34. onames = cellstr(['00'; '90']);
  35. %% all submitted measurements
  36. i = 1;
  37. for m = measurements
  38. %% orientation
  39. for o = [1, 2]
  40. printf("%s\n", ["../cal/2016.07.26/export/capsule-" sprintf("%02.0f", m) "-" char(onames(o)) "-1.wav"]);
  41. [capsule, Fs] = wavread(["../cal/2016.07.26/export/capsule-" sprintf("%02.0f", m) "-" char(onames(o)) "-1.wav"]);
  42. %% indexes in array for frequency bins
  43. imin = round((N/2) * fmin / (Fs/2));
  44. imax = round((N/2) * fmax / (Fs/2));
  45. %% find peakr for this measurement
  46. [peaks, locs] = findpeaks(abs(capsule));
  47. [~, index] = max(peaks);
  48. start = locs(index) - Npre;
  49. range = start:(start + N - 1);
  50. fdom = fft(capsule(range), N);
  51. %% return total power in dB of signal in frequency range
  52. R(i, o) = 20*log10(sqrt(sum(fdom(imin:imax).*conj(fdom(imin:imax)))/size(fdom(imin:imax),1)));
  53. end
  54. %% add 0 to 90 degree difference in dB (front to side ratio)
  55. R(i, 3) = R(i, 1) - R(i, 2);
  56. if (i > 1)
  57. R(i, 4) = R(i, 3) - R(i - 1, 3);
  58. else
  59. R(i, 4) = 0;
  60. end
  61. R(i, 5) = i;
  62. i = i + 1;
  63. end
  64. avg = mean(R(:,1));
  65. R(:,1) = R(:,1) - avg;
  66. R(:,2) = R(:,2) - avg;
  67. %% sort the results by directivity
  68. [RS, I] = sort(R'(3,:));
  69. RS = R'(:,I)';
  70. %% calculate the derivative of directivity
  71. i = 1;
  72. for m = measurements
  73. if (i > 1)
  74. RS(i, 4) = RS(i, 3) - RS(i - 1, 3);
  75. else
  76. RS(i, 4) = 0;
  77. end
  78. %% add an index for plots
  79. RS(i, 6) = i;
  80. i = i + 1;
  81. end
  82. end