📄 erpimage.m
字号:
if length(signifs) ~= 3 fprintf('\nerpimage(): signif arg [%g] must have 3 values\n',Arg); return end Signifflag = NO; elseif Allcohersflag == YES data2=Arg; if size(data2) ~= size(data) fprintf('erpimage(): allcohers data matrix must be the same size as data.\n'); return end Allcohersflag = NO; elseif Phaseflag == YES n = length(Arg); if n > 5 error('erpimage(): Too many arguments for keyword ''phasesort'''); end phargs = Arg; if phargs(3) < 0 error('erpimage(): Invalid negative argument for keyword ''phasesort'''); end if n>=4 if phargs(4) < 0 error('erpimage(): Invalid negative argument for keyword ''phasesort'''); end end if min(phargs(1)) < times(1) | max(phargs(1)) > times(end) error('erpimage(): time for phase sorting filter out of bound.'); end if phargs(2) >= 100 | phargs(2) < -100 error('%-argument for keyword ''phasesort'' must be (-100;100)'); end if length(phargs) >= 4 & phargs(3) > phargs(4) error('erpimage(): Phase sorting frequency range must be increasing.'); end if length(phargs) == 5 topphase = phargs(5); end Phaseflag = NO; elseif Ampflag == YES % 'ampsort',[center_time,prcnt_reject,minfreq,maxfreq] n = length(Arg); if n > 4 error('erpimage(): Too many arguments for keyword ''ampsort'''); end ampargs = Arg; if ampargs(3) < 0 error('erpimage(): Invalid negative argument for keyword ''ampsort'''); end if n>=4 if ampargs(4) < 0 error('erpimage(): Invalid negative argument for keyword ''ampsort'''); end end if min(ampargs(1)) < times(1) | max(ampargs(1)) > times(end) error('erpimage(): time for amplitude sorting filter out of bounds.'); end if ampargs(2) >= 100 | ampargs(2) < -100 error('%-argument for keyword ''ampsort'' must be (-100;100)'); end if length(ampargs) == 4 & ampargs(3) > ampargs(4) error('erpimage(): Amplitude sorting frequency range must be increasing.'); end Ampflag = NO; elseif Valflag == YES % sort by potential value in a given window % Usage: 'valsort',[mintime,maxtime,direction] n = length(Arg); if n > 3 error('erpimage(): Too many arguments for keyword ''valsort'''); end valargs = Arg; if min(valargs(1)) < times(1) | max(valargs(1)) > times(end) error('erpimage(): start time for value sorting out of bounds.'); end if n > 1 if min(valargs(2)) < times(1) | max(valargs(2)) > times(end) error('erpimage(): end time for value sorting out of bounds.'); end end if n > 1 & valargs(1) > valargs(2) error('erpimage(): Value sorting time range must be increasing.'); end if n==3 & (~isnumeric(valargs(3)) | valargs(3)==0) error('erpimage(): Value sorting direction must be +1 or -1.'); end Valflag = NO; elseif Erpalphaflag == YES erpalpha = Arg(1); if erpalpha < MIN_ERPALPHA | erpalpha > MAX_ERPALPHA fprintf('erpimage(): erpalpha value is out of bounds [%g, %g]\n',... MIN_ERPALPHA,MAX_ERPALPHA); return end Erpalphaflag = NO; elseif strcmp(Arg,'nosort') Nosort = YES; elseif strcmp(Arg,'showwin') Showwin = YES; elseif strcmp(Arg,'renorm') Renormflag = YES; elseif strcmp(Arg,'noshow') Noshowflag = YES; elseif strcmp(Arg,'noplot')|strcmp(Arg,'noshow') Noshow = YES; elseif strcmp(Arg,'caxis') Caxflag = YES; elseif strcmp(Arg,'coher') Coherflag = YES; elseif (strcmp(Arg,'allamps') | strcmp(Arg,'plotamps')) Allampsflag = YES; elseif strcmp(Arg,'allcohers') Allcohersflag = YES; elseif strcmp(Arg,'topo') | strcmp(Arg,'topoplot') Topoflag = YES; elseif strcmp(Arg,'spec') | strcmp(Arg,'spectrum') Specflag = YES; elseif strcmp(Arg,'erp')| strcmp(Arg,'ERP') Erpflag = YES; elseif strcmpi(Arg,'erpstd') Erpstdflag = YES; elseif strcmpi(Arg,'erpalpha') Erpalphaflag = YES; elseif strcmpi(Arg,'rmerp') Rmerp = 'yes'; elseif strcmp(Arg,'align') Alignflag = YES; elseif strcmp(Arg,'cbar') | strcmp(Arg,'colorbar') Colorbar = YES; elseif strcmp(Arg,'limits') Limitflag = YES; elseif (strcmp(Arg,'phase') | strcmp(Arg,'phasesort')) Phaseflag = YES; elseif strcmp(Arg,'ampsort') Ampflag = YES; elseif strcmp(Arg,'valsort') Valflag = YES; elseif strcmp(Arg,'auxvar') Auxvarflag = YES; elseif strcmp(Arg,'cycles') Cycleflag = YES; elseif strcmp(Arg,'yerplabel') yerplabelflag = YES; elseif strcmp(Arg,'srate') Srateflag = YES; elseif strcmp(Arg,'vert') | strcmp(Arg,'verttimes') Vertflag = YES; elseif strcmp(Arg,'horz') | strcmp(Arg,'horiz') | strcmp(Arg,'horizontal') Horzflag = YES; elseif strcmp(Arg,'signif')|strcmp(Arg,'signifs')|strcmp(Arg,'sig')|strcmp(Arg,'sigs') Signifflag = YES; elseif strcmp(Arg,'noxlabel') | strcmp(Arg,'noxlabels') | strcmp(Arg,'nox') NoTimeflag = YES; else help erpimage if isstr(Arg) fprintf('\nerpimage(): unknown arg %s\n',Arg); else fprintf('\nerpimage(): unknown arg %d, size(%d,%d)\n',a,size(Arg,1),size(Arg,2)); end return end end % Argendif Caxflag == YES ... |Coherflag == YES ... |Alignflag == YES ... |Limitflag == YES help erpimage fprintf('\nerpimage(): missing option arg.\n') returnendif (Allampsflag | exist('data2')) & ( any(isnan(coherfreq)) | ~Cohsigflag ) fprintf('\nerpimage(): allamps and allcohers flags require coher freq, srate, and cohsig.\n'); returnendif Allampsflag & exist('data2') fprintf('\nerpimage(): cannot image both allamps and allcohers.\n'); returnendif ~exist('srate') | srate <= 0 fprintf('\nerpimage(): Data srate must be specified and > 0.\n'); returnendif ~isempty(auxvar) % whos auxvar if size(auxvar,1) ~= ntrials & size(auxvar,2) ~= ntrials fprintf('erpimage(): auxvar size should be (N,ntrials), e.g., (N,%d)\n',... ntrials); return end if size(auxvar,1) == ntrials & size(auxvar,2) ~= ntrials % make (N,frames) auxvar = auxvar'; end if size(auxvar,2) ~= ntrials fprintf('erpimage(): auxvar size should be (N,ntrials), e.g., (N,%d)\n',... ntrials); return end if exist('auxcolors')==YES % if specified if isa(auxcolors,'cell')==NO % if auxcolors is not a cell array fprintf(... 'erpimage(): auxcolors argument to auxvar flag must be a cell array.\n'); return end endendif exist('phargs') if phargs(3) > srate/2 fprintf(... 'erpimage(): Phase-sorting frequency (%g Hz) must be less than Nyquist rate (%g Hz).',... phargs(3),srate/2); end % DEFAULT_CYCLES = 9*phargs(3)/(phargs(3)+10); % 3 cycles at 5 Hz if frames < DEFAULT_CYCLES*srate/phargs(3) fprintf('\nerpimage(): phase-sorting freq. (%g) too low: epoch length < %d cycles.\n',... phargs(3),DEFAULT_CYCLES); return end if length(phargs)==4 & phargs(4) > srate/2 phargs(4) = srate/2; end if length(phargs)==5 & (phargs(5)>180 | phargs(5) < -180) fprintf('\nerpimage(): coher topphase (%g) out of range.\n',topphase); return endendif exist('ampargs') if ampargs(3) > srate/2 fprintf(... 'erpimage(): amplitude-sorting frequency (%g Hz) must be less than Nyquist rate (%g Hz).',... ampargs(3),srate/2); end %DEFAULT_CYCLES = 9*ampargs(3)/(ampargs(3)+10); % 3 cycles at 5 Hz if frames < DEFAULT_CYCLES*srate/ampargs(3) fprintf('\nerpimage(): amplitude-sorting freq. (%g) too low: epoch length < %d cycles.\n',... ampargs(3),DEFAULT_CYCLES); return end if length(ampargs)==4 & ampargs(4) > srate/2 ampargs(4) = srate/2; fprintf('> Reducing max ''ampsort'' frequency to Nyquist rate (%g Hz)\n',srate/2) endendif ~any(isnan(coherfreq)) if coherfreq(1) <= 0 | srate <= 0 fprintf('\nerpimage(): coher frequency (%g) out of range.\n',coherfreq(1)); return end if coherfreq(end) > srate/2 | srate <= 0 fprintf('\nerpimage(): coher frequency (%g) out of range.\n',coherfreq(end)); return end %DEFAULT_CYCLES = 9*coherfreq(1)/(coherfreq(1)+10); % 3 cycles at 5 Hz if frames < DEFAULT_CYCLES*srate/coherfreq(1) fprintf('\nerpimage(): coher freq. (%g) too low: epoch length < %d cycles.\n',... coherfreq(1),DEFAULT_CYCLES); return endend if isnan(timelimits) timelimits = [min(times) max(times)];endif ~isstr(aligntime) & ~isnan(aligntime) if ~isinf(aligntime) ... & (aligntime < timelimits(1) | aligntime > timelimits(2)) help erpimage fprintf('\nerpimage(): requested align time outside of time limits.\n'); return endend% %%%%%%%%%%%%%%%% Replace nan's with 0s %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%nans= find(isnan(data));if length(nans) fprintf('Replaced %d nan in data with 0s.\n'); data(nans) = 0;end%%%%%%%%%%%%%%% Reshape data to (frames,ntrials) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if size(data,2) ~= ntrials if size(data,1)>1 % fprintf('frames %d, ntrials %d length(data) %d\n',frames,ntrials,length(data)); data=reshape(data,1,frames*ntrials); end data=reshape(data,frames,ntrials);endfprintf('\nPlotting input data as %d epochs of %d frames sampled at %3.1f Hz.\n',... ntrials,frames,srate);%%%%%%%%%%%%%%% Reshape data2 to (frames,ntrials) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if exist('data2') == 1 if size(data2,2) ~= ntrials if size(data2,1)>1 data2=reshape(data2,1,frames*ntrials); end data2=reshape(data2,frames,ntrials); endend%%%%%%%%%%%%%%%% if sortvar=NaN, remove lines %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -ad%if any(isnan(sortvar)) nanlocs = find(isnan(sortvar)); fprintf('Removing %d trials with NaN sortvar values.\n', length(nanlocs)); data(:,nanlocs) = []; sortvar(nanlocs) = []; if length(sortvar) < 4 & avewidth > 1 error('Not enough trials'); end; if exist('data2') == 1 data2(:,nanlocs) = []; end; if ~isempty(auxvar) auxvar(:,nanlocs) = []; end if ~isempty(verttimes) if size(verttimes,1) == ntrials verttimes(nanlocs,:) = []; end; end; ntrials = size(data,2); if ntrials <= 1, close(gcf); error('Too few trials'); end;end;%%%%%%%%%%%%%%%%%%%% Renormalize sortvar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%switch lower(renorm) case 'yes', disp('erpimage warning: *** sorting variable renormalized ***'); sortvar = (sortvar-min(sortvar)) / (max(sortvar) - min(sortvar)) * ... 0.5 * (max(times) - min(times)) + min(times) + 0.4*(max(times) - min(times)); case 'no',; otherwise, if ~isempty(renorm) locx = findstr('x', lower(renorm)); if length(locx) ~= 1, error('erpimage: unrecognized renormalizing formula'); end; eval( [ 'sortvar =' renorm(1:locx-1) 'sortvar' renorm(locx+1:end) ';'] ); end;end;%%%%%%%%%%%%%%%%%%%% Align data to sortvar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if isstr(aligntime) | ~isnan(aligntime) if ~isstr(aligntime) & isinf(aligntime) aligntime= median(sortvar); fprintf('Aligning data to median sortvar.\n'); % Alternative below: trimmed median - ignore top/bottom 5% % ssv = sort(sortvar); % ssv = 'sorted sortvar' % aligntime= median(ssv(ceil(ntrials/20)):floor(19*ntrials/20)); end if ~isstr(aligntime) fprintf('Realigned sortvar plotted at %g ms.\n',aligntime); aligndata=zeros(frames,ntrials); % begin with matrix of zeros() shifts = zeros(1,ntrials); for t=1:ntrials, %%%%%%%%% foreach trial %%%%%%%%% shft = round((aligntime-sortvar(t))*srate/1000); shifts(t) = shft; if shft>0, % shift right if frames-shft > 0 aligndata(shft+1:frames,t)=data(1:frames-shft,t); else fprintf('No aligned data for epoch %d - shift (%d frames) too large.\n',t,shft); end elseif shft < 0 % shift left if frames+shft > 0 aligndata(1:frames+shft,t)=data(1-shft:frames,t); else fprintf('No aligned data for epoch %d - shift (%d frames) too large.\n',t,shft); end else % shft == 0 aligndata(:,t) = data(:,t); end end % end trial fprintf('Shifted epochs by %d to %d frames.\n',min(shifts),max(shifts)); data = aligndata; % now data is aligned to sortvar else aligntime = str2num(aligntime); if isinf(aligntime), aligntime= median(sortvar); end; end;end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -