Home > vbmeg > demo > tutorial_for_vbmeg2 > easy_VBMEG > show_trial_average_eeg.m

show_trial_average_eeg

PURPOSE ^

Show trial-averaged EEG data

SYNOPSIS ^

function show_trial_average_eeg(p)

DESCRIPTION ^

 Show trial-averaged EEG data

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function show_trial_average_eeg(p)
0002 % Show trial-averaged EEG data
0003 %
0004 % Copyright (C) 2011, ATR All Rights Reserved.
0005 % License : New BSD License(see VBMEG_LICENSE.txt)
0006 
0007 disp(mfilename);
0008 
0009 % Set parameters
0010 time_to_show = [-0.1 0.4];% sec
0011 time_of_interest = [0 0.24];% sec
0012 Npeak = 2;% Number of peaks at which spatial patterns is shown
0013 
0014 % Make directory to save figures
0015 save_dir = fullfile(p.fig_root, mfilename, p.sub);
0016 if exist(save_dir, 'dir') ~= 7
0017     vb_mkdir(save_dir);
0018 end
0019 
0020 % Load time information
0021 data_file = fullfile(p.proj_root, p.eeg_dirname, p.trial_dirname, 'cribr_s1.eeg.mat');
0022 [~, ~, time_info] = vb_load_meg_data(data_file);
0023 [~, from_show] = min(abs(time_info.time-time_to_show(1)));
0024 [~, to_show] = min(abs(time_info.time-time_to_show(2)));
0025 time = time_info.time(from_show: to_show);
0026 
0027 % Load positions of channels
0028 pos = vb_load_channel(data_file);
0029 
0030 
0031 % Load EEG data
0032 data = vb_load_meg_data(data_file);
0033 
0034 % Average EEG data across trials
0035 y = mean(data(:, from_show:to_show, :), 3);
0036 
0037 % Detect peaks of power
0038 [~, from_peak] = min(abs(time-time_of_interest(1)));
0039 [~, to_peak] = min(abs(time-time_of_interest(2)));
0040 Nsample = to_peak-from_peak+1;
0041 py = mean(y(:,from_peak:to_peak).^2, 1);
0042 dpy = diff(py);
0043 ix = find(dpy(1:Nsample-2) > 0 & dpy(2:Nsample-1) < 0)+1;
0044 ix2 = sortrows([ix(:) py(ix)'], 2);
0045 ix_peak = sort(ix2(end-Npeak+1:end, 1))+from_peak-1;% index of peak time
0046     
0047 % Plot time series of averaged EEG data
0048 h = figure;
0049 max_y = max(y(:));
0050 min_y = min(y(:));
0051 subplot(2, 1, 1)
0052 plot(time, y)
0053 hold on
0054 for peak = 1:Npeak
0055     ix = ix_peak(peak);
0056     plot([time(ix) time(ix)], [min_y max_y])
0057 end
0058 xlabel('Time [s]')
0059 ylabel('Potential [V]')
0060 axis([time(1) time(end) min_y max_y])
0061 
0062 % Plot spatial patterns at peaks
0063 for peak = 1:Npeak
0064     ix = ix_peak(peak);
0065     subplot(2, Npeak, Npeak+peak)
0066     vb_plot_sensor_2d(pos, y(:,ix), [min_y max_y]);
0067     vb_plot_sensor_2d_head_plot_add(gca);
0068     axis square off
0069     colorbar
0070     title([num2str(time(ix)) ' s'])
0071 end
0072 
0073 % Save figure
0074 vb_savefig_as_shown(h, fullfile(save_dir, p.task_list{ta}))
0075 end
0076

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005