1
0

sphear_fir_design.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, plot_on, 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. %% plot_on: plot frequency response of components, default is "epsc"
  16. %% can be one of 0, "eps", "epsc" (color), "png", "pngc" (color)
  17. %%
  18. %% for the first run do not supply W/X/Y/Z, render the filters and then run
  19. %% sphear_fir_extract to extract the W, X, Y and Z filter shapes, finally run
  20. %% this again with the extra parameters below
  21. %%
  22. %% W: extracted shape of W filter (used above Fl)
  23. %% X: extracted shape of X filter (used above Fl)
  24. %% Y: extracted shape of Y filter (used above Fl)
  25. %% Z: extracted shape of Z filter (used above Fl)
  26. %% Z is optional, if not supplied X and Y are averaged to obtain Z
  27. %%
  28. %% Copyright 2016, Fernando Lopez-Lezcano, All Rights Reserved
  29. %% nando@ccrma.stanford.edu
  30. %%
  31. %% This program is free software: you can redistribute it and/or modify
  32. %% it under the terms of the GNU General Public License as published by
  33. %% the Free Software Foundation, either version 3 of the License, or
  34. %% (at your option) any later version.
  35. %%
  36. %% This program is distributed in the hope that it will be useful,
  37. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. %% GNU General Public License for more details.
  40. %%
  41. %% You should have received a copy of the GNU General Public License
  42. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  43. function [FIR] = sphear_fir_design(F, A2BF, n, maxboost, Fs, Fl, plot_on, W, X, Y, Z)
  44. fig = figure;
  45. if (exist("W") == 1 && ! exist("Z"))
  46. %% Z was not supplied, just average X and Y data
  47. Z = (X + Y) / 2;
  48. end
  49. %% all b format components
  50. for b = 1:4
  51. %% all capsules
  52. for c = 1:4
  53. %% all frequency bands
  54. for d = 1:size(A2BF(:,1,1))(1)
  55. %% create an array of points to design the filter
  56. if (F(d) <= Fl)
  57. %% below the limit frequency we use the A2B coefficients
  58. vlast = A2BF(d, c, b);
  59. dlast = d;
  60. if (exist("W") == 1)
  61. wlast = W(d);
  62. xlast = X(d);
  63. ylast = Y(d);
  64. zlast = Z(d);
  65. end
  66. else
  67. %% above the limit frequency we insert the W, X, Y and Z
  68. %% data if they exist, otherwise keep the last valid value
  69. %% from the A2B coefficients (ie: flat above the transition
  70. %% frequency
  71. if (b == 1)
  72. if (exist("W") == 1)
  73. A2BF(d, c, b) = vlast * (W(d)/wlast);
  74. else
  75. A2BF(d, c, b) = vlast;
  76. end
  77. end
  78. if (b == 2)
  79. if (exist("X") == 1)
  80. A2BF(d, c, b) = vlast * (X(d)/xlast);
  81. else
  82. A2BF(d, c, b) = vlast;
  83. end
  84. end
  85. if (b == 3)
  86. if (exist("Y") == 1)
  87. A2BF(d, c, b) = vlast * (Y(d)/ylast);
  88. else
  89. A2BF(d, c, b) = vlast;
  90. end
  91. end
  92. if (b == 4)
  93. if (exist("Z") == 1)
  94. A2BF(d, c, b) = vlast * (Z(d)/zlast);
  95. else
  96. A2BF(d, c, b) = vlast;
  97. end
  98. end
  99. end
  100. end
  101. end
  102. end
  103. %% normalize all components to the average of W levels for all
  104. %% capsules around the transition frequency
  105. if ((dlast+1) > size(A2BF(:,1,1))(1))
  106. %% special case for testing with a very high transition frequency
  107. avg = [dlast-3, dlast-2, dlast-1];
  108. else
  109. avg = [dlast-1, dlast, dlast+1];
  110. end
  111. nrm = mean([mean(A2BF(avg,1,1))
  112. mean(A2BF(avg,2,1))
  113. mean(A2BF(avg,3,1))
  114. mean(A2BF(avg,4,1))]);
  115. for b = 1:4
  116. for c = 1:4
  117. %% normalize coefficients
  118. for dn = 1:size(A2BF(:,1,1))(1)
  119. A2BF(dn,c,b) = A2BF(dn,c,b) ./ nrm;
  120. end
  121. %% fit a spline to the W, X, Y and Z data and extrapolate
  122. %% highest frequency if it does not exist in the measurements
  123. %%
  124. %% the 200Hz spacing of the points is arbitrary, we should
  125. %% select something that makes more sense
  126. %%
  127. fh = (F(dlast-1)/(Fs/2):200/(Fs/2):1)';
  128. %% make the end really 1.0
  129. fh(end) = 1;
  130. rh = interp1(F(dlast-1:end)/(Fs/2), A2BF(dlast-1:end,c,b)', fh, "cubic", "extrap");
  131. %% we fit the spline taking into account one additional point
  132. %% on the bottom edge so that it will fit better with the existing
  133. %% low frequency points, now we need to find the exact transition
  134. %% frequency in the interpolated array
  135. for idx = 1:size(fh(:))
  136. if (fh(idx) <= F(dlast)/(Fs/2))
  137. istart = idx;
  138. end
  139. end
  140. %% extract the low frequency A2B coefficients
  141. fl = [0 F(1:dlast-1)']' ./ (Fs/2);
  142. rl = [A2BF(1,c,b) A2BF(1:dlast-1,c,b)']';
  143. %% concatenate low and high frequency arrays
  144. fi = cat(1, fl, fh(istart:end));
  145. ri = cat(1, rl, rh(istart:end));
  146. printf("low frequency boost %f, high frequency boost %f\n", 20*log10(ri(1)), 20*log10(ri(end)));
  147. %% force a max boost at low and high frequencies
  148. if (maxboost != 0)
  149. if (abs(ri(1)) > 10^(maxboost/20))
  150. printf("limit low freq boost from %f to %f dB (%f)\n", 20*log10(ri(1)), maxboost, ri(1));
  151. ri(1) = sign(ri(1)) * 10^(maxboost/20);
  152. printf("new limit %f\n", ri(1));
  153. end
  154. if (abs(ri(end)) > 10^(maxboost/20))
  155. printf("limit high freq boost from %f dB to %f dB (%f)\n", 20*log10(ri(end)), maxboost, ri(end));
  156. ri(end) = sign(ri(end)) * 10^(maxboost/20);
  157. printf("new limit %f\n", ri(end));
  158. end
  159. end
  160. if (plot_on != 0)
  161. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  162. %% color plot
  163. popts = '.k';
  164. wopts = 'b';
  165. xyopts = 'r';
  166. zopts = 'g';
  167. copt = '-color';
  168. cext = "_color";
  169. else
  170. %% mono plot
  171. popts = '.k';
  172. wopts = '-';
  173. xyopts = '--';
  174. zopts = ':';
  175. copt = '-mono';
  176. cext = "_mono";
  177. end
  178. axis([300, (Fs/2)]);
  179. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  180. grid on;
  181. pbaspect([16, 9, 1])
  182. title(sprintf("B format FIR filters (transition frequency: %d Hz)", Fl));
  183. hold on;
  184. %% plot real measured points
  185. ph = semilogx(F, 20*log10(abs(A2BF(:,c,b)))', popts);
  186. %% plot extrapolated filter
  187. if (b == 1)
  188. %% W
  189. wh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), wopts);
  190. else
  191. if (b == 4)
  192. %% Z
  193. zh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), zopts);
  194. else
  195. %% XY
  196. xyh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), xyopts);
  197. end
  198. end
  199. end
  200. %% design the filter
  201. FIR(b, c, :) = fir2(n, fi, ri);
  202. end
  203. end
  204. if (plot_on != 0)
  205. l = legend([wh, zh, xyh], {'W', 'XY', 'Z'});
  206. legend(l, 'location', 'southwest');
  207. if (exist("W") == 1)
  208. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  209. print(fig, '-deps','-tight',copt,["figures/wxyz_filter_ff" cext ".eps"]);
  210. else
  211. print(fig, '-dpng',copt,["figures/wxyz_filter_ff" cext ".png"]);
  212. end
  213. else
  214. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  215. print(fig, '-deps','-tight',copt,["figures/wxyz_filter_lf" cext ".eps"]);
  216. else
  217. print(fig, '-dpng',copt,["figures/wxyz_filter_lf" cext ".png"]);
  218. end
  219. end
  220. end
  221. end