sphear_fir_render_directions.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_fir_render_directions: plot frequency response of FIR filter A2B matrix for principal
  6. %% and diagonal directions
  7. %%
  8. %% usage:
  9. %% [] = sphear_fir_render_directions(FIR, fmin, fmax, ymin, ymax, Fs, Nfft, plot_on, smooth)
  10. %%
  11. %% FIR: A2B matrix of FIR filters
  12. %% fmin, fmax: range of frequencies to use
  13. %% ymin, ymax: vertical scale for plot
  14. %% Fs: sampling rate
  15. %% Nfft: size of FFT
  16. %%
  17. %% optional parameters:
  18. %%
  19. %% plot_on: plot frequency response of components, default is "epsc"
  20. %% can be one of 0, "eps", "epsc" (color), "png", "pngc" (color)
  21. %% smooth: number of samples to average, default is 0
  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 [BFF] = sphear_fir_render_directions(FIR, fmin, fmax, ymin, ymax, Fs, Nfft, plot_on, smooth)
  39. %% no smoothing or plotting by default
  40. if (!exist("smooth"))
  41. smooth = 0;
  42. end
  43. if (!exist("plot_on"))
  44. plot_on = "epsc";
  45. else
  46. if (plot_on == 1)
  47. plot_on = "epsc"
  48. end
  49. end
  50. %% plot if requested
  51. if (plot_on != 0)
  52. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  53. %% color plot
  54. wopts = 'k;W;';
  55. xopts = 'r;X;';
  56. yopts = 'b;Y;';
  57. copt = '-color';
  58. cext = "_color";
  59. else
  60. %% mono plot
  61. wopts = '-';
  62. xopts = '--';
  63. yopts = ':';
  64. copt = '-mono';
  65. cext = "_mono";
  66. end
  67. f = figure;
  68. hold on;
  69. grid on;
  70. axis([fmin, fmax, ymin, ymax]);
  71. tics('y', [6, 3, 0, -3, -6, -9, -12, -15, -20, -25, -30]);
  72. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  73. ca=get (gcf, "currentaxes");
  74. set(ca,"fontweight","bold","linewidth",1.5)
  75. %% measurements, first four in cardinal directions
  76. measurements = [1, 5, 9, 13, 3, 7, 11, 15];
  77. %% which components to plot, we ommit the ones that are nulled
  78. %% 0: do not plot, 1: cardinal, 2: diagonal plot
  79. m_W = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0];
  80. m_X = [1, 0, 2, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0];
  81. m_Y = [0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0, 1, 0, 2, 0];
  82. m_Z = [1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0];
  83. W_offset = +0;
  84. X_offset = -3;
  85. Y_offset = -3;
  86. idx = 1;
  87. for m = measurements
  88. %% render b format components
  89. bf(idx,:,:) = sphear_a_to_b(m, FIR, Nfft);
  90. %% calculate B format frequency responses
  91. for b = 1:4
  92. if (smooth > 0)
  93. bfft(idx,b,:) = fastsmooth(10*log10(abs(fft(bf(idx,b,:), Nfft))), smooth, 2, 1)(1:end/2);
  94. else
  95. bfft(idx,b,:) = 10*log10(abs(fft(bf(idx,b,:), Nfft)))(1:end/2);
  96. end
  97. end
  98. fr = (1:Nfft/2) * Fs/Nfft;
  99. fft_avg = mean(bfft(1,1,round(end/4):round(end/2)));
  100. %%fft_avg = 0.0;
  101. for b = 1:4
  102. if (m_W(m) > 0 && b == 1)
  103. if (m_W(m) > 1)
  104. pW = semilogx(fr, bfft(idx,b,:) - fft_avg + W_offset, wopts, "linewidth", 1.5, "linestyle", "--");
  105. else
  106. pW = semilogx(fr, bfft(idx,b,:) - fft_avg + W_offset, wopts, "linewidth", 1.5);
  107. end
  108. end
  109. if (m_X(m) > 0 && b == 2)
  110. if (m_X(m) > 1)
  111. pXd = semilogx(fr, bfft(idx,b,:) - fft_avg + X_offset, xopts, "linewidth", 1.5, "linestyle", "--");
  112. else
  113. pXp = semilogx(fr, bfft(idx,b,:) - fft_avg + X_offset, xopts, "linewidth", 1.5);
  114. end
  115. end
  116. if (m_Y(m) > 0 && b == 3)
  117. if (m_Y(m) > 1)
  118. pYd = semilogx(fr, bfft(idx,b,:) - fft_avg + Y_offset, yopts, "linewidth", 1.5, "linestyle", "--");
  119. else
  120. pYp = semilogx(fr, bfft(idx,b,:) - fft_avg + Y_offset, yopts, "linewidth", 1.5);
  121. end
  122. end
  123. end
  124. idx = idx + 1;
  125. end
  126. l = legend([pW, pXp, pXd, pYp, pYd],
  127. {sprintf("W principal directions (%.1fdB offset)", W_offset),
  128. sprintf("X principal directions (%.1fdB offset)", X_offset),
  129. sprintf("X diagonal directions (%.1fdB offset)", Y_offset),
  130. sprintf("Y principal directions (%.1fdB offset)", X_offset),
  131. sprintf("Y diagonal directions (%.1fdB offset)", Y_offset),
  132. });
  133. legend(l, 'location', 'southwest');
  134. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  135. print(f, '-deps', '-tight', '-S1920,1080', copt,["../figures/sphear_directions.eps"]);
  136. else
  137. print(f, '-deps', copt, ["../figures/sphear_directions.eps"]);
  138. end
  139. end
  140. end