hem_powerBand_pwelchHamming1sEpochs.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. %% Compute average power in band for HEM ICs during the 2x2 TaskXCond using pwelch() on 1" epochs tapered with Hanning window
  2. % Task labels
  3. task = {'MOM-INS', 'MOM-SEP', 'MTT-INS', 'MTT-SEP'};
  4. % bands
  5. alpha = [8 12];
  6. beta = [12 25];
  7. gamma = [35 45];
  8. delta = [1 4];
  9. theta = [4 8];
  10. % band_limits = [8 12; 12 25; 35 45; 1 4; 4 8];
  11. band_limits = [alpha; beta; gamma; delta; theta];
  12. band_names = {'alpha', 'beta', 'gamma', 'delta', 'theta'};
  13. % sampling frequency
  14. Fs= 250;
  15. % Load signals for HEM ICs (n.tasks, n.subjects, n.points)
  16. load hem_data.mat;
  17. % permute (n.points, n.subjects, n.tasks)
  18. hem_data = permute( hem_data, [3, 2, 1]);
  19. tot_sub= size( hem_data, 2);
  20. tot_epochs= floor( size( hem_data, 1) / Fs);
  21. % for each task
  22. for n_task= 1: 4
  23. % for each subject
  24. for n_sub= 1: tot_sub
  25. % data for current subject, task
  26. y= hem_data(:, n_sub, n_task);
  27. % for each 1s-epoch
  28. for t= 1: tot_epochs
  29. % 1s-epoch
  30. x = y(1+(t-1)*250 : t*250);
  31. N = length(x);
  32. freq = 0:Fs/length(x):Fs/2;
  33. % power spectral density (PSD) estimate using welch estimator
  34. [pxx, f] = pwelch(x, 250, 0, freq, Fs);
  35. psd(t,:) = pxx;
  36. end
  37. % compute average power in each band for each 1s-epoch
  38. for n_band = 1:length(band_limits)
  39. % lower bound for selected band
  40. b = band_limits(n_band,1);
  41. % higher bound for selected band
  42. B = band_limits(n_band,2);
  43. % container for frequencies of specdata in selected band
  44. freq_b = zeros(size(freq));
  45. % for all frequencies of specdata
  46. for j = 1:length(freq)
  47. % extract frequencies
  48. if and( freq(j)>=b, freq(j)<=B )
  49. freq_b(j) = 1;
  50. end
  51. end
  52. % band frequencies matrix for all 1s-epoch
  53. freq_bR = repmat(freq_b, [size(psd, 1), 1] );
  54. % data power in selected band
  55. dp = freq_bR .* psd;
  56. % extract non-zero columns
  57. % (https://it.mathworks.com/matlabcentral/answers/40018-delete-zeros-rows-and-columns)
  58. dp( :, ~any(dp,1) ) = [];
  59. % power in band for each 1s-epoch
  60. t= sprintf('Average power in band %s for subject %d in task %s...', band_names{n_band}, n_sub, task{n_task}); disp(t);
  61. pAvg_inTime_band = mean( dp, 2);
  62. % power in band of whole VEM IC for current subject, task
  63. pAvg_band( n_task, n_sub, n_band) = mean( pAvg_inTime_band);
  64. end
  65. end
  66. end
  67. % Save data
  68. % Rearrange matix (n.band, n.subject, n.taskXcond)
  69. data = permute( pAvg_band, [3,2,1] );
  70. save hem_powerBand_pwelch.mat data;
  71. % figure;plot(pAvg_inTime_band');legend('alpha','beta','gamma','delta','theta')
  72. %
  73. % plot(freq,10*log10(psdx))
  74. % grid on
  75. % title('Periodogram Using FFT')
  76. % xlabel('Frequency (Hz)')
  77. % ylabel('Power/Frequency (dB/Hz)')