1
0

sphear_fir_design.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. %%
  2. %% *SpHEAR Project
  3. %%
  4. %% sphear_fir_design: design a matrix of FIR filters that equalizes the microphone
  5. %%
  6. %% usage:
  7. %% [FIR] = sphear_fir_design(F, A2BF, n, Fs, Fl, W, X, Y, Z)
  8. %%
  9. %% F: array of filter section center frequencies
  10. %% A2BF: vector of A2B matrixes, one for each frequency
  11. %% n: order of the FIR filter being created
  12. %% maxboost: max high and low frequency boost in dB of the final filter
  13. %% Fs: sampling rate
  14. %% Fl: frequency limit, below it use A2B matrixes, above it use W, X, Y, Z shapes
  15. %%
  16. %% for the first run do not supply W/X/Y/Z, render the filters and then run
  17. %% sphear_fir_extract to extract the W, X, Y and Z filter shapes, finally run
  18. %% this again with the extra parameters below
  19. %%
  20. %% W: extracted shape of W filter (used above Fl)
  21. %% X: extracted shape of X filter (used above Fl)
  22. %% Y: extracted shape of Y filter (used above Fl)
  23. %% Z: extracted shape of Z filter (used above Fl)
  24. %%
  25. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  26. %% nando@ccrma.stanford.edu
  27. %%
  28. %% This program is free software: you can redistribute it and/or modify
  29. %% it under the terms of the GNU General Public License as published by
  30. %% the Free Software Foundation, either version 3 of the License, or
  31. %% (at your option) any later version.
  32. %%
  33. %% This program is distributed in the hope that it will be useful,
  34. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. %% GNU General Public License for more details.
  37. %%
  38. %% You should have received a copy of the GNU General Public License
  39. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  40. function [FIR] = sphear_fir_design(F, A2BF, n, maxboost, Fs, Fl, W, X, Y, Z)
  41. fig = figure;
  42. %% all b format components
  43. for b = 1:4
  44. %% all capsules
  45. for c = 1:4
  46. for d = 1:size(A2BF(:,1,1))(1)
  47. if (F(d) <= Fl)
  48. %% below the limit frequency we use the A2B coefficients
  49. vlast = A2BF(d, c, b);
  50. dlast = d;
  51. if (exist("W") == 1)
  52. wlast = W(d);
  53. xlast = X(d);
  54. ylast = Y(d);
  55. zlast = Z(d);
  56. end
  57. else
  58. %% above the limit frequency we interpolate the W, X, Y and Z
  59. %% filters if they exist, otherwise keep the last valid value
  60. if (b == 1)
  61. if (exist("W") == 1)
  62. A2BF(d, c, b) = vlast * (W(d)/wlast);
  63. else
  64. A2BF(d, c, b) = vlast;
  65. end
  66. end
  67. if (b == 2)
  68. if (exist("X") == 1)
  69. A2BF(d, c, b) = vlast * (X(d)/xlast);
  70. else
  71. A2BF(d, c, b) = vlast;
  72. end
  73. end
  74. if (b == 3)
  75. if (exist("Y") == 1)
  76. A2BF(d, c, b) = vlast * (Y(d)/ylast);
  77. else
  78. A2BF(d, c, b) = vlast;
  79. end
  80. end
  81. if (b == 4)
  82. if (exist("Z") == 1)
  83. A2BF(d, c, b) = vlast * (Z(d)/zlast);
  84. else
  85. A2BF(d, c, b) = vlast;
  86. end
  87. end
  88. end
  89. end
  90. %% fit a spline to the W, X, Y and Z segments and extrapolate
  91. %% highest frequency if it does not exist in the measurements
  92. %%
  93. %% the 200Hz spacing of the points is arbitrary, we should
  94. %% select something that makes more sense
  95. fh = (F(dlast-1)/(Fs/2):200/(Fs/2):1)';
  96. %% make the end really 1.0
  97. fh(end) = 1;
  98. rh = interp1(F(dlast-1:end)/(Fs/2), A2BF(dlast-1:end,c,b)', fh, "cubic", "extrap");
  99. %% we design the spline taking into account one additional point
  100. %% on the bottom edge so that it will fit better with the existing
  101. %% low frequency points, now we need to find the exact transition
  102. %% frequency in the interpolated array
  103. for idx = 1:size(fh(:))
  104. if (fh(idx) <= F(dlast)/(Fs/2))
  105. istart = idx;
  106. end
  107. end
  108. %% extract the low frequency A2B coefficients
  109. fl = [0 F(1:dlast-1)']' ./ (Fs/2);
  110. rl = [A2BF(1,c,b) A2BF(1:dlast-1,c,b)']';
  111. %% concatenate low and high frequency arrays
  112. fi = cat(1, fl, fh(istart:end));
  113. ri = cat(1, rl, rh(istart:end));
  114. printf("low frequency boost %f, high frequency boost %f\n", 20*log10(ri(1)), 20*log10(ri(end)));
  115. %% force a max boost at low and high frequencies
  116. if (maxboost != 0)
  117. if (abs(ri(1)) > 10^(maxboost/20))
  118. printf("limit low freq boost from %f to %f dB (%f)\n", 20*log10(ri(1)), maxboost, ri(1));
  119. ri(1) = sign(ri(1)) * 10^(maxboost/20);
  120. printf("new limit %f\n", ri(1));
  121. end
  122. if (abs(ri(end)) > 10^(maxboost/20))
  123. printf("limit high freq boost from %f dB to %f dB (%f)\n", 20*log10(ri(end)), maxboost, ri(end));
  124. ri(end) = sign(ri(end)) * 10^(maxboost/20);
  125. printf("new limit %f\n", ri(end));
  126. end
  127. end
  128. %% plot real measured points
  129. axis([300, 24000]);
  130. grid on;
  131. if (b == 1)
  132. semilogx(F, 20*log10(abs(A2BF(:,c,b)))','*k');
  133. else
  134. semilogx(F, 20*log10(abs(A2BF(:,c,b)))','*k');
  135. end
  136. hold on;
  137. %% plot extrapolated spline
  138. if (b == 1)
  139. semilogx(fi*(Fs/2), 20*log10(abs(ri)), 'b');
  140. else
  141. semilogx(fi*(Fs/2), 20*log10(abs(ri)), 'r');
  142. end
  143. %% design the filter
  144. FIR(b, c, :) = fir2(n, fi, ri);
  145. end
  146. end
  147. if (exist("W") == 1)
  148. print(fig, '-dpng','-color',["figures/w_xyz_filter_full.png"]);
  149. else
  150. print(fig, '-dpng','-color',["figures/w_xyz_filter_low.png"]);
  151. end
  152. end