<P>function [msg,nfft,Fs,window,noverlap,p,dflag] = psdchk(P,x,y)<BR>%PSDCHK Helper function for PSD, CSD, COHERE, and TFE.<BR>% [msg,nfft,Fs,window,noverlap,p,dflag]=PSDCHK(P,x,y) takes the cell <BR>% array P and uses each element as an input argument. Assumes P has <BR>% between 0 and 7 elements which are the arguments to psd, csd, cohere<BR>% or tfe after the x (psd) or x and y (csd, cohere, tfe) arguments.<BR>% y is optional; if given, it is checked to match the size of x.<BR>% x must be a numeric vector.<BR>% Outputs:<BR>% msg - error message, [] if no error<BR>% nfft - fft length<BR>% Fs - sampling frequency<BR>% window - window vector<BR>% noverlap - overlap of sections, in samples<BR>% p - confidence interval, [] if none desired<BR>% dflag - detrending flag, 'linear' 'mean' or 'none'</P>
<P>% Author(s): T. Krauss, 10-28-93</P>
<P>msg = [];</P>
<P>if length(P) == 0 <BR>% psd(x)<BR> nfft = min(length(x),256);<BR> window = hanning(nfft);<BR> noverlap = 0;<BR> Fs = 2;<BR> p = [];<BR> dflag = 'none';<BR>elseif length(P) == 1<BR>% psd(x,nfft)<BR>% psd(x,dflag)<BR> if isempty(P{1}), dflag = 'none'; nfft = min(length(x),256); <BR> elseif isstr(P{1}), dflag = P{1}; nfft = min(length(x),256); <BR> else dflag = 'none'; nfft = P{1}; end<BR> Fs = 2;<BR> window = hanning(nfft);<BR> noverlap = 0;<BR> p = [];<BR>elseif length(P) == 2<BR>% psd(x,nfft,Fs)<BR>% psd(x,nfft,dflag)<BR> if isempty(P{1}), nfft = min(length(x),256); else nfft=P{1}; end<BR> if isempty(P{2}), dflag = 'none'; Fs = 2;<BR> elseif isstr(P{2}), dflag = P{2}; Fs = 2;<BR> else dflag = 'none'; Fs = P{2}; end<BR> window = hanning(nfft);<BR> noverlap = 0;<BR> p = [];<BR>elseif length(P) == 3<BR>% psd(x,nfft,Fs,window)<BR>% psd(x,nfft,Fs,dflag)<BR> if isempty(P{1}), nfft = min(length(x),256); else nfft=P{1}; end<BR> if isempty(P{2}), Fs = 2; else Fs = P{2}; end<BR> if isstr(P{3})<BR> dflag = P{3};<BR> window = hanning(nfft);<BR> else<BR> dflag = 'none';<BR> window = P{3};<BR> if length(window) == 1, window = hanning(window); end<BR> if isempty(window), window = hanning(nfft); end<BR> end<BR> noverlap = 0;<BR> p = [];<BR>elseif length(P) == 4<BR>% psd(x,nfft,Fs,window,noverlap)<BR>% psd(x,nfft,Fs,window,dflag)<BR> if isempty(P{1}), nfft = min(length(x),256); else nfft=P{1}; end<BR> if isempty(P{2}), Fs = 2; else Fs = P{2}; end<BR> window = P{3};<BR> if length(window) == 1, window = hanning(window); end<BR> if isempty(window), window = hanning(nfft); end<BR> if isstr(P{4})<BR> dflag = P{4};<BR> noverlap = 0;<BR> else<BR> dflag = 'none';<BR> if isempty(P{4}), noverlap = 0; else noverlap = P{4}; end<BR> end<BR> p = [];<BR>elseif length(P) == 5<BR>% psd(x,nfft,Fs,window,noverlap,p)<BR>% psd(x,nfft,Fs,window,noverlap,dflag)<BR> if isempty(P{1}), nfft = min(length(x),256); else nfft=P{1}; end<BR> if isempty(P{2}), Fs = 2; else Fs = P{2}; end<BR> window = P{3};<BR> if length(window) == 1, window = hanning(window); end<BR> if isempty(window), window = hanning(nfft); end<BR> if isempty(P{4}), noverlap = 0; else noverlap = P{4}; end<BR> if isstr(P{5})<BR> dflag = P{5};<BR> p = [];<BR> else<BR> dflag = 'none';<BR> if isempty(P{5}), p = .95; else p = P{5}; end<BR> end<BR>elseif length(P) == 6<BR>% psd(x,nfft,Fs,window,noverlap,p,dflag)<BR> if isempty(P{1}), nfft = min(length(x),256); else nfft=P{1}; end<BR> if isempty(P{2}), Fs = 2; else Fs = P{2}; end<BR> window = P{3};<BR> if length(window) == 1, window = hanning(window); end<BR> if isempty(window), window = hanning(nfft); end<BR> if isempty(P{4}), noverlap = 0; else noverlap = P{4}; end<BR> if isempty(P{5}), p = .95; else p = P{5}; end<BR> if isstr(P{6})<BR> dflag = P{6};<BR> else<BR> msg = 'DFLAG parameter must be a string.'; return<BR> end<BR>end</P>
<P>% NOW do error checking<BR>if (nfft<length(window)), <BR> msg = 'Requires window''s length to be no greater than the FFT length.';<BR>end<BR>if (noverlap >= length(window)),<BR> msg = 'Requires NOVERLAP to be strictly less than the window length.';<BR>end<BR>if (nfft ~= abs(round(nfft)))|(noverlap ~= abs(round(noverlap))),<BR> msg = 'Requires positive integer values for NFFT and NOVERLAP.';<BR>end<BR>if ~isempty(p),<BR> if (prod(size(p))>1)|(p(1,1)>1)|(p(1,1)<0),<BR> msg = 'Requires confidence parameter to be a scalar between 0 and 1.';<BR> end<BR>end<BR>if min(size(x))~=1 | ~isnumeric(x) | length(size(x))>2<BR> msg = 'Requires vector (either row or column) input.';<BR>end<BR>if (nargin>2) & ( (min(size(y))~=1) | ~isnumeric(y) | length(size(y))>2 )<BR> msg = 'Requires vector (either row or column) input.';<BR>end<BR>if (nargin>2) & (length(x)~=length(y))<BR> msg = 'Requires X and Y be the same length.';<BR>end</P>
<P>dflag = lower(dflag);<BR>if strncmp(dflag,'none',1)<BR> dflag = 'none';<BR>elseif strncmp(dflag,'linear',1)<BR> dflag = 'linear';<BR>elseif strncmp(dflag,'mean',1)<BR> dflag = 'mean';<BR>else<BR> msg = 'DFLAG must be ''linear'', ''mean'', or ''none''.';<BR>end<BR><BR>是不是这个?</P> |