sphear_fir_design.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if (plot_on != 0)
  45. fig = figure;
  46. end
  47. if (exist("W") == 1 && ! exist("Z"))
  48. %% Z was not supplied, just average X and Y data
  49. Z = (X + Y) / 2;
  50. end
  51. %% all b format components
  52. for b = 1:4
  53. %% all capsules
  54. for c = 1:4
  55. %% all frequency bands
  56. for d = 1:size(A2BF(:,1,1))(1)
  57. %% create an array of points to design the filter
  58. if (F(d) <= Fl)
  59. %% below the limit frequency we use the A2B coefficients
  60. vlast = A2BF(d, c, b);
  61. dlast = d;
  62. if (exist("W") == 1)
  63. wlast = W(d);
  64. xlast = X(d);
  65. ylast = Y(d);
  66. zlast = Z(d);
  67. end
  68. else
  69. %% above the limit frequency we insert the W, X, Y and Z
  70. %% data if they exist, otherwise keep the last valid value
  71. %% from the A2B coefficients (ie: flat above the transition
  72. %% frequency
  73. if (b == 1)
  74. if (exist("W") == 1)
  75. A2BF(d, c, b) = vlast * (W(d)/wlast);
  76. else
  77. A2BF(d, c, b) = vlast;
  78. end
  79. end
  80. if (b == 2)
  81. if (exist("X") == 1)
  82. A2BF(d, c, b) = vlast * (X(d)/xlast);
  83. else
  84. A2BF(d, c, b) = vlast;
  85. end
  86. end
  87. if (b == 3)
  88. if (exist("Y") == 1)
  89. A2BF(d, c, b) = vlast * (Y(d)/ylast);
  90. else
  91. A2BF(d, c, b) = vlast;
  92. end
  93. end
  94. if (b == 4)
  95. if (exist("Z") == 1)
  96. A2BF(d, c, b) = vlast * (Z(d)/zlast);
  97. else
  98. A2BF(d, c, b) = vlast;
  99. end
  100. end
  101. end
  102. end
  103. end
  104. end
  105. %% normalize all components to the average of W levels for all
  106. %% capsules around the transition frequency
  107. if ((dlast+1) > size(A2BF(:,1,1))(1))
  108. %% special case for testing with a very high transition frequency
  109. avg = [dlast-3, dlast-2, dlast-1];
  110. else
  111. avg = [dlast-1, dlast, dlast+1];
  112. end
  113. nrm = mean([mean(A2BF(avg,1,1))
  114. mean(A2BF(avg,2,1))
  115. mean(A2BF(avg,3,1))
  116. mean(A2BF(avg,4,1))]);
  117. for b = 1:4
  118. for c = 1:4
  119. %% normalize coefficients
  120. for dn = 1:size(A2BF(:,1,1))(1)
  121. A2BF(dn,c,b) = A2BF(dn,c,b) ./ nrm;
  122. end
  123. %% fit a spline to the W, X, Y and Z data and extrapolate
  124. %% highest frequency if it does not exist in the measurements
  125. %%
  126. %% the 200Hz spacing of the points is arbitrary, we should
  127. %% select something that makes more sense
  128. %%
  129. fh = (F(dlast-1)/(Fs/2):200/(Fs/2):1)';
  130. %% make the end really 1.0
  131. fh(end) = 1;
  132. rh = interp1(F(dlast-1:end)/(Fs/2), A2BF(dlast-1:end,c,b)', fh, "spline", "extrap");
  133. %% we fit the spline taking into account one additional point
  134. %% on the bottom edge so that it will fit better with the existing
  135. %% low frequency points, now we need to find the exact transition
  136. %% frequency in the interpolated array
  137. for idx = 1:size(fh(:))
  138. if (fh(idx) <= F(dlast)/(Fs/2))
  139. istart = idx;
  140. end
  141. end
  142. %% extract the low frequency A2B coefficients
  143. fl = [0 F(1:dlast-1)']' ./ (Fs/2);
  144. rl = [A2BF(1,c,b) A2BF(1:dlast-1,c,b)']';
  145. %% concatenate low and high frequency arrays
  146. fi = cat(1, fl, fh(istart:end));
  147. ri = cat(1, rl, rh(istart:end));
  148. printf("low frequency boost %f, high frequency boost %f\n", 20*log10(ri(1)), 20*log10(ri(end)));
  149. %% force a max boost at low and high frequencies
  150. if (maxboost != 0)
  151. if (abs(ri(1)) > 10^(maxboost/20))
  152. printf("limit low freq boost from %f to %f dB (%f)\n", 20*log10(ri(1)), maxboost, ri(1));
  153. ri(1) = sign(ri(1)) * 10^(maxboost/20);
  154. printf("new limit %f\n", ri(1));
  155. end
  156. if (abs(ri(end)) > 10^(maxboost/20))
  157. printf("limit high freq boost from %f dB to %f dB (%f)\n", 20*log10(ri(end)), maxboost, ri(end));
  158. ri(end) = sign(ri(end)) * 10^(maxboost/20);
  159. printf("new limit %f\n", ri(end));
  160. end
  161. end
  162. if (plot_on != 0)
  163. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  164. %% color plot
  165. popts = '.k';
  166. wopts = 'b';
  167. xyopts = 'r';
  168. zopts = 'g';
  169. copt = '-color';
  170. cext = "_color";
  171. else
  172. %% mono plot
  173. popts = '.k';
  174. wopts = '-';
  175. xyopts = '--';
  176. zopts = ':';
  177. copt = '-mono';
  178. cext = "_mono";
  179. end
  180. grid on;
  181. axis([400, 20000]);
  182. xticks = [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 20000];
  183. set(gca, 'XTick', xticks);
  184. set(gca, 'XTickLabel', sprintf('%d|', xticks));
  185. ca=get (gcf, "currentaxes");
  186. set(ca,"fontweight","bold","linewidth",1.5)
  187. pbaspect([16, 9, 1])
  188. %% title(sprintf("B format FIR filters (transition frequency: %d Hz)", Fl));
  189. hold on;
  190. %% plot real measured points
  191. %% ph = semilogx(F, 20*log10(abs(A2BF(:,c,b)))', popts, "linewidth", 1.5);
  192. %% plot extrapolated filter
  193. if (b == 1)
  194. %% W
  195. wh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), wopts, "linewidth", 1.5);
  196. else
  197. if (b == 4)
  198. %% Z
  199. zh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), zopts, "linewidth", 1.5);
  200. else
  201. %% XY
  202. xyh = semilogx(fi*(Fs/2), 20*log10(abs(ri)), xyopts, "linewidth", 1.5);
  203. end
  204. end
  205. end
  206. %% design the filter
  207. FIR(b, c, :) = fir2(n, fi, ri);
  208. end
  209. end
  210. if (plot_on != 0)
  211. l = legend([wh, zh, xyh], {'W', 'XY', 'Z'});
  212. legend(l, 'location', 'west');
  213. if (exist("W") == 1)
  214. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  215. print(fig, '-deps','-tight',copt,["figures/wxyz_filter_ff" cext ".eps"]);
  216. else
  217. print(fig, '-dpng',copt,["figures/wxyz_filter_ff" cext ".png"]);
  218. end
  219. else
  220. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  221. print(fig, '-deps','-tight',copt,["figures/wxyz_filter_lf" cext ".eps"]);
  222. else
  223. print(fig, '-dpng',copt,["figures/wxyz_filter_lf" cext ".png"]);
  224. end
  225. end
  226. end
  227. end