sphear_signal_plot_frequency_response.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_signal_plot_frequency_response: plot frequency response of a signal
  6. %%
  7. %% usage:
  8. %% [] = sphear_signal_plot_frequency_response(SIG, fmin, fmax, ymin, ymax, FS, fft_size, title, filename, smooth)
  9. %%
  10. %% SIG: signals for each measurement
  11. %% fmin, fmax: range of frequencies to use
  12. %% ymin, ymax: y range to plot
  13. %% FS: sampling rate
  14. %% fft_size: size of fft used to calculate frequency response
  15. %%
  16. %% optional parameters:
  17. %%
  18. %% title: title to add to figure
  19. %% filename: prefix to filename of figure (figure is not written unless this is specified)
  20. %% smooth: number of samples to average, default is 0
  21. %%
  22. %% Copyright 2016-2017, Fernando Lopez-Lezcano, All Rights Reserved
  23. %% nando@ccrma.stanford.edu
  24. %%
  25. %% This program is free software: you can redistribute it and/or modify
  26. %% it under the terms of the GNU General Public License as published by
  27. %% the Free Software Foundation, either version 3 of the License, or
  28. %% (at your option) any later version.
  29. %%
  30. %% This program is distributed in the hope that it will be useful,
  31. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. %% GNU General Public License for more details.
  34. %%
  35. %% You should have received a copy of the GNU General Public License
  36. %% along with this program. If not, see <htp://www.gnu.org/licenses/>.
  37. function [] = sphear_signal_plot_frequency_response(SIG, measurements, fmin, fmax, ymin, ymax, FS, fft_size, figure_title, filename, smooth)
  38. %% no smoothing by default
  39. if (!exist("smooth"))
  40. smooth = 0;
  41. end
  42. for s = 1:size(SIG,2)
  43. %% calculate frequency responses
  44. for m = measurements
  45. if (smooth > 0)
  46. sfft(m,s,:) = fastsmooth(20*log10(abs(fft(SIG(m,s,:), fft_size))), smooth, 2, 1);
  47. else
  48. sfft(m,s,:) = 20*log10(abs(fft(SIG(m,s,:), fft_size)));
  49. end
  50. end
  51. fig = figure;
  52. hold on;
  53. fr = (1:fft_size/2) * FS/fft_size;
  54. grid on;
  55. if (exist("figure_title"))
  56. title(figure_title);
  57. end
  58. axis([fmin, fmax, ymin, ymax]);
  59. tics('y', [9, 6, 3, 0, -3, -6, -9, -12, -15, -20, -25, -30, -40, -50]);
  60. tics('x', [300, 500, 1000, 2000, 3000, 5000, 7000, 10000, 15000, 20000]);
  61. ca=get (gcf, "currentaxes");
  62. set(ca, "fontweight", "bold", "linewidth", 1.5)
  63. pbaspect([16, 9, 1])
  64. for m = measurements
  65. if (m == 1)
  66. col = "k";
  67. else
  68. if (m < 8)
  69. col = "r";
  70. else
  71. if (m == 9)
  72. col = "g";
  73. else
  74. col = "b";
  75. end
  76. end
  77. end
  78. semilogx(fr, sfft(m,s,1:end/2), col, "linewidth", 1.5);
  79. end
  80. hold off;
  81. if (exist("filename"))
  82. %% write file with eps plot
  83. print(fig, '-deps', '-tight', '-color', '-depsc', '-S1920,1080', ["../data/current/figures/" filename ".eps"]);
  84. %% write png file
  85. print(fig, '-deps', '-tight', '-color', '-dpng', '-S1920,1080', ["../data/current/figures/" filename ".png"]);
  86. end
  87. end
  88. end