sphear_bf_plot_directions.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_bf_plot_directions: render amplitude response plots of B format signals
  6. %% version for current prototype
  7. %%
  8. %% usage:
  9. %% [] = sphear_bf_plot_directions(fmin, fmax, ymin, ymax, Nfft, plot_on, smooth)
  10. %%
  11. %% fmin, fmax: range of frequencies to plot (Hz)
  12. %% ymin, ymax: vertical scale for plot (dB)
  13. %% Nfft: size of FFT
  14. %%
  15. %% optional parameters:
  16. %%
  17. %% plot_on: plot frequency response of components, default is "epsc"
  18. %% can be one of 0, "eps", "epsc" (color), "png", "pngc" (color)
  19. %% smooth: number of samples to average, default is 0
  20. %%
  21. %% implied parameter:
  22. %% files: "../data/current/bf-tetraproc/b-format-xxx.wav"
  23. %% four channel B format, FUMA ordering and weights
  24. %%
  25. %% example:
  26. %% sphear_bf_plot_directions(lf_limit, hf_limit, -15, 3, render_fft_size, 1, 0);
  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 [] = sphear_bf_plot_directions(fmin, fmax, ymin, ymax, Nfft, plot_on, smooth)
  44. %% no smoothing or plotting by default
  45. if (!exist("smooth"))
  46. smooth = 0;
  47. end
  48. if (!exist("plot_on"))
  49. plot_on = "epsc";
  50. else
  51. if (plot_on == 1)
  52. plot_on = "epsc";
  53. end
  54. end
  55. %% plot if requested
  56. if (plot_on != 0)
  57. if (strcmp(plot_on, "epsc") || strcmp(plot_on, "pngc"))
  58. %% color plot
  59. wopts = 'k;W;';
  60. xopts = 'r;X;';
  61. yopts = 'b;Y;';
  62. copt = '-color';
  63. cext = "_color";
  64. else
  65. %% mono plot
  66. wopts = '-';
  67. xopts = '--';
  68. yopts = ':';
  69. copt = '-mono';
  70. cext = "_mono";
  71. end
  72. f = figure;
  73. hold on;
  74. grid on;
  75. axis([fmin, fmax, ymin, ymax]);
  76. tics('y', [6, 3, 0, -3, -6, -9, -12, -15, -20, -25, -30]);
  77. tics('x', [300, 500, 1000, 1500, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  78. ca=get (gcf, "currentaxes");
  79. set(ca,"fontweight","bold","linewidth",1.5);
  80. %% measurements, first four in cardinal directions
  81. measurements = [1, 5, 9, 13, 3, 7, 11, 15];
  82. %% which components to plot, we ommit the ones that are nulled
  83. %% 0: do not plot, 1: cardinal, 2: diagonal plot
  84. m_W = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0];
  85. m_X = [1, 0, 2, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0];
  86. m_Y = [0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0, 1, 0, 2, 0];
  87. m_Z = [1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0];
  88. idx = 1;
  89. for m = measurements
  90. %% read rendered four channel B format measurement
  91. %% trim to just the needed part
  92. range = [512:4096];
  93. [bsig, Fs] = wavread(["../data/current/bf-tetraproc/b-format-" sprintf("%03d", m(1)) ".wav"]);
  94. %% convert tetraproc FUMA output to SN3D
  95. bsig(:,1) = bsig(:,1) / 0.707;
  96. %% calculate B format frequency responses
  97. for b = 1:4
  98. bsig_fft = fft(bsig(range,b), Nfft);
  99. if (smooth > 0)
  100. bfft(idx,b,:) = fastsmooth(10*log10(abs(bsig_fft)), smooth, 2, 1)(1:end/2);
  101. else
  102. bfft(idx,b,:) = 10*log10(abs(bsig_fft))(1:end/2);
  103. end
  104. idx = idx + 1;
  105. end
  106. end
  107. %% scale everything to average of W (1st measurement)
  108. fft_avg = mean(bfft(1,1,round(end/4):round(end/2)));
  109. %% now do the plots
  110. idx = 1;
  111. W_offset = +0;
  112. X_offset = -6;
  113. Y_offset = -6;
  114. fr = (1:Nfft/2) * Fs/Nfft;
  115. for m = measurements
  116. for b = 1:4
  117. if (m_W(m) > 0 && b == 1)
  118. if (m_W(m) > 1)
  119. pW = semilogx(fr, bfft(idx,b,:) - fft_avg + W_offset, wopts, "linewidth", 1.5, "color", 'k', "linestyle", "--");
  120. else
  121. pW = semilogx(fr, bfft(idx,b,:) - fft_avg + W_offset, wopts, "linewidth", 1.5, "color", 'k');
  122. end
  123. end
  124. if (m_X(m) > 0 && b == 2)
  125. if (m_X(m) > 1)
  126. pXd = semilogx(fr, bfft(idx,b,:) - fft_avg + X_offset, xopts, "linewidth", 1.5, "color", "r", "linestyle", "--");
  127. else
  128. pXp = semilogx(fr, bfft(idx,b,:) - fft_avg + X_offset, xopts, "linewidth", 1.5, "color", 'r');
  129. end
  130. end
  131. if (m_Y(m) > 0 && b == 3)
  132. if (m_Y(m) > 1)
  133. pYd = semilogx(fr, bfft(idx,b,:) - fft_avg + Y_offset, yopts, "linewidth", 1.5, "color", "b", "linestyle", "--");
  134. else
  135. pYp = semilogx(fr, bfft(idx,b,:) - fft_avg + Y_offset, yopts, "linewidth", 1.5, "color", 'b');
  136. end
  137. end
  138. idx = idx + 1;
  139. end
  140. end
  141. l = legend([pW, pXp, pXd, pYp, pYd],
  142. {sprintf("W principal directions (%.1fdB offset)", W_offset),
  143. sprintf("X principal directions (%.1fdB offset)", X_offset),
  144. sprintf("X diagonal directions (%.1fdB offset)", Y_offset),
  145. sprintf("Y principal directions (%.1fdB offset)", X_offset),
  146. sprintf("Y diagonal directions (%.1fdB offset)", Y_offset),
  147. });
  148. legend(l, 'location', 'southwest');
  149. if (strcmp(plot_on, 'eps') || strcmp(plot_on, 'epsc') || plot_on == 1)
  150. print(f, '-deps', '-tight', '-S1920,1080', copt, ["../figures/b_format_sphear3.eps"]);
  151. else
  152. print(f, '-deps', copt, ["../figures/b_format_sphear3.eps"]);
  153. end
  154. end
  155. end