evoked.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function [V,t,Err] = evoked(data,Fs,win,width,plt,err)
  2. % Function to calculate the evoked response given continuous data in the
  3. % form time x channels
  4. % Usage [V,t,Err] = evoked(data,Fs,win,width,plt,err)
  5. %
  6. % Inputs
  7. % Note that all times can be in arbitrary units. But the units have to be
  8. % consistent. So, if win is in secs, width is in secs and Fs has to be Hz.
  9. % If win is in samples, so is width and Fs=1.
  10. %
  11. % data(times, channels/trials or a single vector) (required)
  12. % Fs sampling frequency (required)
  13. % win subsection of data to be used. Default all available data
  14. % width (s) of smoothing kernel. Default 50 samples
  15. % plt plot 'n' for no plot, otherwise plot color. Default blue colored lines.
  16. % err = 0/1. Default 1=calculate bootstrap errorbars.
  17. %
  18. % Outputs
  19. % V = evoked potential
  20. % t = times of evaluation
  21. % Err = bootstrap statdard deviation
  22. if nargin < 2;error('Data, sampling frequency required');end
  23. data=change_row_to_column(data);
  24. N=size(data,1);
  25. data=data';
  26. if nargin <3; win = [0 (N-1)/Fs];end
  27. if nargin <4; width = 50/Fs;end
  28. if nargin <5; plt = 'b';end
  29. if nargin <6;err = 1;end
  30. T=win;
  31. if isempty(T); T = [0 (N-1)/Fs];end
  32. if isempty(width); width = 50/Fs;end
  33. if isempty(plt); plt = 'b';end
  34. if isempty(err);err = 1;end
  35. t = min(T):1/Fs:max(T);
  36. if nargin >= 5
  37. indx = find(t>T(1) & t<T(2));
  38. t = t(indx);
  39. data = data(:,indx);
  40. end
  41. if width > (t(length(t))-t(1))/2
  42. disp('Width is too large for data segment: should be in seconds')
  43. disp('Turn off smoothing')
  44. width = 0;
  45. end
  46. s = t(2)-t(1);
  47. N = fix(width/s);
  48. NT = length(data(:,1));
  49. if NT > 1
  50. mdata = mean(data);
  51. else
  52. mdata = data;
  53. end
  54. if N > 4
  55. smdata = locsmooth(mdata,N,fix(N/2));
  56. else
  57. smdata = mdata;
  58. end
  59. % if errorbars requested then do a bootstrap over trials...
  60. Err = 0;
  61. if NT < 4;
  62. disp('Too few trials: no errorbars calculated')
  63. err = 0;
  64. end
  65. if err ~= 0 && NT > 1
  66. Nboot = 10;
  67. bevk = 0;
  68. sevk = 0;
  69. for b=1:Nboot
  70. indx = floor(NT*rand(1,NT)) + 1;
  71. evktmp = mean(data(indx,:));
  72. if N > 4
  73. evktmp = locsmooth(evktmp,N,fix(N/2));
  74. end
  75. bevk = bevk + evktmp;
  76. sevk = sevk + evktmp.^2;
  77. end
  78. stdevk = sqrt((sevk/Nboot - bevk.^2/Nboot^2));
  79. Err = stdevk;
  80. end
  81. V = smdata;
  82. if plt ~= 'n'
  83. plot(t,smdata,plt)
  84. hold on
  85. mn = mean(smdata);
  86. ax = get(gca,'xlim');
  87. line(ax,mn*[1 1],'color','k')
  88. if err
  89. line(ax,(mn+2*mean(stdevk))*[1 1],'color','r')
  90. line(ax,(mn-2*mean(stdevk))*[1 1],'color','r')
  91. hold off
  92. end
  93. end