diff --git a/eeg_rpsd.m b/eeg_rpsd.m index 7d21d00..96a467e 100644 --- a/eeg_rpsd.m +++ b/eeg_rpsd.m @@ -1,14 +1,26 @@ function psdmed = eeg_rpsd(EEG, nfreqs, pct_data) + +% Validate EEG input +if ~isstruct(EEG) || ~isfield(EEG, 'icaweights') || ~isfield(EEG, 'icaact') || ... + ~isfield(EEG, 'srate') || ~isfield(EEG, 'pnts') || ~isfield(EEG, 'trials') + error('EEG must be a struct with fields: icaweights, icaact, srate, pnts, trials'); +end + % clean input cutoff freq nyquist = floor(EEG.srate / 2); if ~exist('nfreqs', 'var') || isempty(nfreqs) nfreqs = nyquist; +elseif ~isnumeric(nfreqs) || nfreqs <= 0 || floor(nfreqs) ~= nfreqs + error('nfreqs must be a positive integer'); + elseif nfreqs > nyquist nfreqs = nyquist; end if ~exist('pct_data', 'var') || isempty(pct_data) pct_data = 100; +elseif ~isnumeric(pct_data) || pct_data <= 0 || pct_data > 100 + error('pct_data must be a number between 0 and 100'); end % setup constants @@ -23,15 +35,13 @@ cutoff = floor(EEG.pnts / n_points) * n_points; index = bsxfun(@plus, ceil(0:n_points / 2:cutoff - n_points), (1:n_points)'); if ~exist('OCTAVE_VERSION', 'builtin') - rng('default') + rng('shuffle') else - rand('state', 0); + rand('seed', sum(100 * clock)); end n_seg = size(index, 2) * EEG.trials; subset = randperm(n_seg, ceil(n_seg * pct_data / 100)); % need to improve this -if exist('OCTAVE_VERSION', 'builtin') == 0 - rng('shuffle') -end + % calculate windowed spectrums psdmed = zeros(ncomp, nfreqs);