Home > bioelectromagnetism > eeg_region_peaks.m

eeg_region_peaks

PURPOSE ^

eeg_region_peaks - Find peaks in regions of electrodes

SYNOPSIS ^

function [regions] = eeg_region_peaks(p,times,regions,missing)

DESCRIPTION ^

 eeg_region_peaks - Find peaks in regions of electrodes

 Usage: [regions] = eeg_region_peaks(p,times,regions,missing)

 requires p.volt.data and p.volt.timeArray

 'times' is 1 row x 3 columns, with values
 for start, peak and end time windows (in that order)
 in the scale of p.volt.timeArray

 'regions' is optional.  When omitted or an empty matrix, 
 it is created by elec_regions.m, see that function for 
 more information.

 'missing' is a boolean switch.  If missing = 0, then
 the maximal region value at the peak time (ie, times(r,2))
 is returned. This avoids missing values in the return 
 data (default).

 returns 'regions' - an array of structures with
 fields: name, elec, pospeaks, postimes, negpeaks &
 negtimes.  The latter are arrays of the max/min peak
 values at the time windows of 'times'.

 Depends on eeg_peaks & sometimes elec_regions.  Called
 in, for example, eeg_region_peaks_script.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [regions] = eeg_region_peaks(p,times,regions,missing)
0002 
0003 % eeg_region_peaks - Find peaks in regions of electrodes
0004 %
0005 % Usage: [regions] = eeg_region_peaks(p,times,regions,missing)
0006 %
0007 % requires p.volt.data and p.volt.timeArray
0008 %
0009 % 'times' is 1 row x 3 columns, with values
0010 % for start, peak and end time windows (in that order)
0011 % in the scale of p.volt.timeArray
0012 %
0013 % 'regions' is optional.  When omitted or an empty matrix,
0014 % it is created by elec_regions.m, see that function for
0015 % more information.
0016 %
0017 % 'missing' is a boolean switch.  If missing = 0, then
0018 % the maximal region value at the peak time (ie, times(r,2))
0019 % is returned. This avoids missing values in the return
0020 % data (default).
0021 %
0022 % returns 'regions' - an array of structures with
0023 % fields: name, elec, pospeaks, postimes, negpeaks &
0024 % negtimes.  The latter are arrays of the max/min peak
0025 % values at the time windows of 'times'.
0026 %
0027 % Depends on eeg_peaks & sometimes elec_regions.  Called
0028 % in, for example, eeg_region_peaks_script.m
0029 %
0030 
0031 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $
0032 
0033 % Licence: GNU GPL, no express or implied warranties
0034 % History: 03/2002, Darren.Weber_at_radiology.ucsf.edu
0035 %
0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0037 
0038 % Check p structure fields
0039 if ~isfield(p.volt,'timeArray'),
0040     msg = sprintf('EEG_REGION_PEAKS: p.volt.timeArray does not exist.\n');
0041     error(msg);
0042 end
0043 if isempty(p.volt.timeArray),
0044     msg = sprintf('EEG_REGION_PEAKS: p.volt.timeArray is empty.\n');
0045     error(msg);
0046 end
0047 % check for time windows of analysis
0048 if ~exist('times','var'),
0049     % Set times for beginning and end of volt.timeArray
0050     times = [p.volt.timeArray(1,1) 1 p.volt.timeArray(end,1)];
0051     missing = 1;
0052 end
0053 if isempty(times),
0054     times = [p.volt.timeArray(1,1) 1 p.volt.timeArray(end,1)];
0055     missing = 1;
0056 end
0057 % check for regions of analysis
0058 if ~exist('regions','var'),
0059     regions = elec_regions;
0060 end
0061 if isempty(regions),
0062     regions = elec_regions;
0063 end
0064 % check for missing value instruction
0065 if ~exist('missing','var'),
0066     missing = 0;
0067 end
0068 if isempty(missing),
0069     missing = 0;
0070 end
0071 
0072 
0073 % first get all peaks for input p.volt.data
0074 p = eeg_peaks(p);
0075 
0076 tic
0077 fprintf('EEG_REGION_PEAKS: Finding regional peaks...');
0078 
0079 for r=1:length(regions),
0080     
0081     % the regions.elec field can be empty, especially
0082     % when processing a control condition of a 2 condition
0083     % experiment where no positive or negative peak is
0084     % found in the experimental condition.  See
0085     % eeg_region_peaks_script.m
0086     if isnan(regions(r).elec),
0087         regions(r).pospeaks = NaN;
0088         regions(r).postimes = NaN;
0089         regions(r).poselecs = NaN;
0090         regions(r).negpeaks = NaN;
0091         regions(r).negtimes = NaN;
0092         regions(r).negelecs = NaN;
0093         continue;
0094     end
0095     
0096     % Select region voltage/time arrays
0097     region.data = p.volt.data(:,regions(r).elec);
0098     region.time = p.volt.timeArray(:,regions(r).elec);
0099     
0100     % select peak indices between start/end times
0101     peaks = p.volt.peaks.data(:,regions(r).elec);
0102     peakdata = peaks .* and(region.time >= times(1), region.time <= times(3));
0103     
0104     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0105     % select all positive region peaks between start/end times
0106     if isempty(find(peakdata>0)),
0107         if missing,
0108             % Create missing data
0109             regions(r).pospeaks = NaN;
0110             regions(r).postimes = NaN;
0111             regions(r).poselecs = NaN;
0112         else
0113             % Select region data at times(2)
0114             pospeaks.data = region.data .* (region.time==times(2));
0115             pospeaks.time = region.time .* (region.time==times(2));
0116             
0117             % find max positive peak value, timing & electrode
0118             [i,j,v] = find(pospeaks.data); % non-zero elements
0119             regions(r).pospeaks = max(v);
0120             index = find(v == max(v));
0121             if ~isequal(size(index),[1 1]),
0122                 fprintf('\nEEG_REGION_PEAKS: Warning: Selecting first of two equal max values.\n');
0123             end
0124             regions(r).postimes = pospeaks.time(i(index(1,1)),j(index(1,1)));
0125             regions(r).poselecs = regions(r).elec(j(index(1,1)));
0126         end
0127     else
0128         pospeaks.data = region.data .* (peakdata > 0);
0129         pospeaks.time = region.time .* (peakdata > 0);
0130         
0131         % find max positive peak value, timing & electrode
0132         [i,j,v] = find(pospeaks.data); % non-zero elements
0133         regions(r).pospeaks = max(v);
0134         index = find(v == max(v));
0135         if ~isequal(size(index),[1 1]),
0136             fprintf('\nEEG_REGION_PEAKS: Warning: Selecting first of two equal max values.\n');
0137         end
0138         regions(r).postimes = pospeaks.time(i(index(1,1)),j(index(1,1)));
0139         regions(r).poselecs = regions(r).elec(j(index(1,1)));
0140     end
0141     
0142     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0143     % select all negative region peaks between start/end times
0144     if isempty(find(peakdata<0)),
0145         if missing,
0146             % Create missing data
0147             regions(r).negpeaks = NaN;
0148             regions(r).negtimes = NaN;
0149             regions(r).negelecs = NaN;
0150         else
0151             % Select region data at times(2)
0152             negpeaks.data = region.data .* (region.time==times(2));
0153             negpeaks.time = region.time .* (region.time==times(2));
0154             
0155             % find min negative peak value, timing & electrode
0156             [i,j,v] = find(negpeaks.data); % non-zero elements
0157             regions(r).negpeaks = min(v);
0158             index = find(v == min(v));
0159             if ~isequal(size(index),[1 1]),
0160                 fprintf('\nEEG_REGION_PEAKS: Warning: Selecting first of two equal min values.\n');
0161             end
0162             regions(r).negtimes = negpeaks.time(i(index(1,1)),j(index(1,1)));
0163             regions(r).negelecs = regions(r).elec(j(index(1,1)));
0164         end
0165     else
0166         negpeaks.data = region.data .* (peakdata < 0);
0167         negpeaks.time = region.time .* (peakdata < 0);
0168         
0169         % find min negative peak value, timing & electrode
0170         [i,j,v] = find(negpeaks.data); % non-zero elements
0171         regions(r).negpeaks = min(v);
0172         index = find(v == min(v));
0173         if ~isequal(size(index),[1 1]),
0174             fprintf('\nEEG_REGION_PEAKS: Warning: Selecting first of two equal min values.\n');
0175         end
0176         regions(r).negtimes = negpeaks.time(i(index(1,1)),j(index(1,1)));
0177         regions(r).negelecs = regions(r).elec(j(index(1,1)));
0178     end
0179 end
0180 
0181 t = toc;
0182 fprintf('done (%5.2f sec)\n',t);
0183 
0184 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003