sphear_capsule_power.m 3.1 KB

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