function theResult = speclist(theSpec, theFilename)

% speclist -- Listing of "specav" output.
%  speclist(theSpec, 'theFilename') writes a listing of theSpec,
%   output from the "specav" spectral averaging function, to
%   'theFilename' (dialog invoked if not given).
 
% Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO.
%  All Rights Reserved.
%   Disclosure without explicit written consent from the
%    copyright owner does not constitute publication.
 
% Version of 03-Feb-1999 09:09:33.

STDOUT = 1;

if nargin < 1, help(mfilename), return, end

if ischar(theSpec)
	theSpec = evalin('caller', theSpec);
end

if nargin < 2, theFilename = '*'; end

if any(theFilename == '*')
	if isequal(theFilename, '*')
		theFilename = 'speclist.out';
	end
	[theFile, thePath] = uiputfile(theFilename, 'Output File Name?');
	if any(theFile)
		if thePath(end) ~= filesep
			thePath = [thePath filesep];
		end
		theFilename = [thePath theFile];
	else
		theFilename = '';
	end
end

if isequal(lower(theFilename), 'stdout')
	fp = STDOUT;
elseif any(theFilename)
	fp = fopen(theFilename, 'w');
	if fp < 0, return, end
else
	fp = STDOUT;
end

s = theSpec;

a = [s.Pxx(:, 1) s.Pyy(:, 1) s.Pxx(:, 2:3) ...
		s.Txy(:, 1:2) ...
		abs(s.Cxy(:, 1)) s.Cxy(:, 2) ...
		angle(s.Cxy(:, 1))*180/pi s.Cxy(:, 3) ...
		s.Freq(:, 2) s.Freq(:, 1).^-1];

theFormat = ['  %12.4e  %12.4e  %5.3f  %5.2f  ' ...
				'%12.4e  %12.4e  %5.2f  %5.2f  %5.0f  %5.0f  %5g  %12.5e\n'];

theHeading = ['Pxx Pyy conf_lo conf_hi TF_amp conf Cxy_amp sig_level phase_deg phase_err bandwidth period\n'];

fprintf(fp, '\n');
fprintf(fp, theHeading);
fprintf(fp, '\n');
fprintf(fp, theFormat, a.');
fprintf(fp, '\n');

if fp ~= STDOUT, fclose(fp); end
