0001 function [regions] = eeg_region_peaks(p,times,regions,missing)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
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
0048 if ~exist('times','var'),
0049
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
0058 if ~exist('regions','var'),
0059 regions = elec_regions;
0060 end
0061 if isempty(regions),
0062 regions = elec_regions;
0063 end
0064
0065 if ~exist('missing','var'),
0066 missing = 0;
0067 end
0068 if isempty(missing),
0069 missing = 0;
0070 end
0071
0072
0073
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
0082
0083
0084
0085
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
0097 region.data = p.volt.data(:,regions(r).elec);
0098 region.time = p.volt.timeArray(:,regions(r).elec);
0099
0100
0101 peaks = p.volt.peaks.data(:,regions(r).elec);
0102 peakdata = peaks .* and(region.time >= times(1), region.time <= times(3));
0103
0104
0105
0106 if isempty(find(peakdata>0)),
0107 if missing,
0108
0109 regions(r).pospeaks = NaN;
0110 regions(r).postimes = NaN;
0111 regions(r).poselecs = NaN;
0112 else
0113
0114 pospeaks.data = region.data .* (region.time==times(2));
0115 pospeaks.time = region.time .* (region.time==times(2));
0116
0117
0118 [i,j,v] = find(pospeaks.data);
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
0132 [i,j,v] = find(pospeaks.data);
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
0144 if isempty(find(peakdata<0)),
0145 if missing,
0146
0147 regions(r).negpeaks = NaN;
0148 regions(r).negtimes = NaN;
0149 regions(r).negelecs = NaN;
0150 else
0151
0152 negpeaks.data = region.data .* (region.time==times(2));
0153 negpeaks.time = region.time .* (region.time==times(2));
0154
0155
0156 [i,j,v] = find(negpeaks.data);
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
0170 [i,j,v] = find(negpeaks.data);
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