sphear_fir_design.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. %%
  15. %% for the first run do not supply Fl/W/XYZ, render the filters and from inspection
  16. %% determine the limit frequency, then run sphear_fir_extract to extract the
  17. %% W and XYZ filter shapes and run this again with the extra parameters below
  18. %%
  19. %% Fl: frequency limit, below it use A2B matrixes, above it use WXYZ shapes
  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. FIR = zeros(4, 4, (n + 1));
  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 over the existing points and extrapolate
  74. %% for low and high ends of spectrum
  75. fi = (0:200/(Fs/2):1)';
  76. ri = spline(F/(Fs/2), A2BF(:,c,b)', fi);
  77. printf("low frequency boost %f, high frequency boost %f\n", 20*log10(ri(1)), 20*log10(ri(end)));
  78. %% force a max boost at low and high frequencies
  79. if (maxboost != 0)
  80. if (abs(ri(1)) > 10^(maxboost/20))
  81. printf("limit low freq boost from %f to %f dB (%f)\n", 20*log10(ri(1)), maxboost, ri(1));
  82. ri(1) = sign(ri(1)) * 10^(maxboost/20);
  83. printf("new limit %f\n", ri(1));
  84. end
  85. if (abs(ri(end)) > 10^(maxboost/20))
  86. printf("limit high freq boost from %f dB to %f dB (%f)\n", 20*log10(ri(end)), maxboost, ri(end));
  87. ri(end) = sign(ri(end)) * 10^(maxboost/20);
  88. printf("new limit %f\n", ri(end));
  89. end
  90. end
  91. %% real measured points
  92. if (b == 1)
  93. semilogx(F/(Fs/2), A2BF(:,c,b)','*g');
  94. else
  95. semilogx(F/(Fs/2), A2BF(:,c,b)','*b');
  96. end
  97. hold on;
  98. %% extrapolated spline
  99. if (b == 1)
  100. semilogx(fi,ri,'g');
  101. else
  102. semilogx(fi,ri,'r');
  103. end
  104. %% design the filter
  105. FIR(b, c, :) = fir2(n, fi, ri);
  106. end
  107. end
  108. end