1## Copyright (C) 2006 Peter V. Lanspeary <pvl@mecheng.adelaide.edu.au>
2##
3## This program is free software: you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation, either version 3 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; see the file COPYING. If not, see
15## <https://www.gnu.org/licenses/>.
16
17## Usage:
18##   [Pxx,freq] = cohere(x,y,Nfft,Fs,window,overlap,range,plot_type,detrend)
19##
20##     Estimate (mean square) coherence of signals "x" and "y".
21##     Use the Welch (1967) periodogram/FFT method.
22##     Compatible with Matlab R11 cohere and earlier.
23##     See "help pwelch" for description of arguments, hints and references
24##     --- especially hint (7) for Matlab R11 defaults.
25##
26
27function varargout = cohere(varargin)
28
29##
30  if ( nargin<2 )
31    error( 'cohere: Need at least 2 args. Use help cohere.' );
32  endif
33  nvarargin = length(varargin);
34  ## remove any pwelch RESULT args and add 'trans'
35  for iarg=1:nvarargin
36    arg = varargin{iarg};
37    if ( ~isempty(arg) && ischar(arg) && ( strcmp(arg,'power') || ...
38           strcmp(arg,'cross') || strcmp(arg,'trans') || ...
39           strcmp(arg,'coher') || strcmp(arg,'ypower') ))
40      varargin{iarg} = [];
41    endif
42  endfor
43  varargin{nvarargin+1} = 'coher';
44  ##
45  saved_compatib = pwelch('R11-');
46  if ( nargout==0 )
47    pwelch(varargin{:});
48  elseif ( nargout==1 )
49    Pxx = pwelch(varargin{:});
50    varargout{1} = Pxx;
51  elseif ( nargout>=2 )
52    [Pxx,f] = pwelch(varargin{:});
53    varargout{1} = Pxx;
54    varargout{2} = f;
55  endif
56  pwelch(saved_compatib);
57  saved_compatib = 0;
58
59endfunction
60