plot_multichan.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. function plot_multichan( x, y, interval, normalize )
  2. % Simple MATLAB function for plot multi-channel time-series data
  3. %
  4. % Usage:
  5. % plot_multichan( y ) % <- y: signal
  6. % plot_multichan( x, y ) % <- x: time
  7. %
  8. % Example:
  9. % y = randn([20, 2000]);
  10. % plot_multichan(y);
  11. %
  12. % Written by Hio-Been han, hiobeen.han@kaist.ac.kr, 2020-03-07
  13. %
  14. % parse arguments
  15. if nargin==1, y=x; x = 1:length(y); end
  16. nChan = size(y,1);
  17. if nChan > size(y,2), y = y'; nChan = size(y,1); end
  18. if nargin < 4, normalize = 0; end
  19. if normalize
  20. stds = nanstd( y, 0, 2 );
  21. for chIdx = 1:size(y,1), y(chIdx,:) = nanmean(stds) * (y(chIdx,:) / stds(chIdx)); end
  22. end
  23. if nargin < 3
  24. interval = nanmean(range(y, 2)) * nChan / 5;
  25. end
  26. y_center = linspace( -interval, interval, nChan );
  27. % set colormap
  28. color_template =...
  29. [843 088 153;
  30. 992 750 280;
  31. 400 200 030;
  32. 573 716 350;
  33. 055 538 083]*.001;
  34. c_space = repmat( color_template, [ ceil( nChan/size(color_template,1)), 1]);
  35. % c_space=imresize(colormap('lines'),[nChan,3],'nearest');
  36. % main plot
  37. chanlab = {}; chanlab_pos = [];
  38. lw = 1;
  39. for chanIdx = 1:nChan
  40. shift = y_center(chanIdx) + nanmean( y( chanIdx, : ), 2);
  41. plot( x, y( chanIdx, : ) - shift, 'Color', c_space( chanIdx,: ) , 'LineWidth', lw);
  42. chanIdx_reverse = nChan-chanIdx+1;
  43. chanlab{chanIdx} = sprintf( 'Ch %02d', chanIdx_reverse);
  44. chanlab_pos(chanIdx) = y_center(chanIdx) ;
  45. if chanIdx ==1, hold on; end
  46. end
  47. hold off;
  48. % enhance visibility
  49. set(gca, 'YTick', chanlab_pos, 'YTickLabel', chanlab,...
  50. 'Clipping', 'on', 'Box', 'off', 'LineWidth', 2);
  51. ylim([-1 1]*interval*2);