Home > vbmeg > demo > tutorial_for_vbmeg2 > advanced > 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, 'crri_all.info.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 % Show trial-averaged EEG data
0031 for ta = 1:length(p.task_list)
0032     
0033     % Set input file
0034     data_file = fullfile(p.proj_root, p.eeg_dirname, p.trial_dirname, ...
0035         ['crri_' p.task_list{ta} '.info.mat']);
0036     
0037     % Load EEG data
0038     data = vb_load_meg_data(data_file);
0039     
0040     % Average EEG data across trials
0041     y = mean(data(:, from_show:to_show, :), 3);
0042     
0043     % Detect peaks of power
0044     [~, from_peak] = min(abs(time-time_of_interest(1)));
0045     [~, to_peak] = min(abs(time-time_of_interest(2)));
0046     Nsample = to_peak-from_peak+1;
0047     py = mean(y(:,from_peak:to_peak).^2, 1);
0048     dpy = diff(py);
0049     ix = find(dpy(1:Nsample-2) > 0 & dpy(2:Nsample-1) < 0)+1;
0050     ix2 = sortrows([ix(:) py(ix)'], 2);
0051     ix_peak = sort(ix2(end-Npeak+1:end, 1))+from_peak-1;% index of peak time
0052     
0053     % Plot time series of averaged EEG data
0054     h = figure;
0055     max_y = max(y(:));
0056     min_y = min(y(:));
0057     subplot(2, 1, 1)
0058     plot(time, y)
0059     hold on
0060     for peak = 1:Npeak
0061         ix = ix_peak(peak);
0062         plot([time(ix) time(ix)], [min_y max_y])
0063     end
0064     xlabel('Time [s]')
0065     ylabel('Potential [V]')
0066     axis([time(1) time(end) min_y max_y])
0067     
0068     % Plot spatial patterns at peaks
0069     for peak = 1:Npeak
0070         ix = ix_peak(peak);
0071         subplot(2, Npeak, Npeak+peak)
0072         vb_plot_sensor_2d(pos, y(:,ix), [min_y max_y]);
0073         vb_plot_sensor_2d_head_plot_add(gca);
0074         axis square off
0075         colorbar
0076         title([num2str(time(ix)) ' s'])
0077     end
0078     
0079     % Save figure
0080     vb_savefig_as_shown(h, fullfile(save_dir, p.task_list{ta}))
0081 end
0082

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