sphear_fir_design.m 4.7 KB

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