function [ graph, faxis, taxis ] = showSpect( S, fs, shift )
%
% Corded by D. Kitamura (d-kitamura@nii.ac.jp) on 2 Jan. 2016 (ver1.0).
% Modified by D. Kitamura (d-kitamura@ieee.org) on 18 Nov, 2018 (ver1.1).
%
% This function shows spectrogram from time-frequency matrix.
% This function supports both complex and nonnegative input and both
% monaural and multichannel spectrograms.
% For a multichannel spectrogram, the order of its indexes must be
% [nbin x nframe x nch].
% Note that color map range is moderately defined. Tune by yourself.
%
% See also
% http://d-kitamura.net
%
% [syntax]
%   [graph, faxis, taxis] = showSpect( S )
%   [graph, faxis, taxis] = showSpect( S, fs, shift )
%
% [inputs]
%       S: STFT matrix ([bins x frames] for a monaural spectrogram, and
%                       [bins x frames x channels] for a multichannel
%                       spectrogram, where bins is fs/2+1, and both complex
%                       and nonnegative spectrograms are supported)
%      fs: sampling frequency [Hz]
%   shift: length of window shift
%
% [outputs]
%   graph: graphics handle
%   faxis: frequency axis (1 x nbin)
%   taxis: time axis (1 x nframe)

% Check errors and set default values
[nbin, nframe, nch] = size(S);
if isreal(S) == false % for complex spectrogram S
    S = real(abs(S).^2); % calculate power spectrogram
end
if (nargin < 2)
    faxis = 1 : nbin;
    taxis = 1 : nframe;
elseif (nargin < 3)
    error('Too few input arguments.\nIf you input fs, the argument "shift" is also required.\n');
else
    faxis = 0 : fs/(2*nbin) : fs/2 - (fs/(2*nbin));
    taxis = 0 : shift/fs : (shift/fs)*(nframe-1);
end

% Draw spectrogram surface
logS = 10*log10( S );
minS = min( min( min( logS ) ) );
maxS = max( max( max( logS ) ) );
for ch = 1:nch
    graph(ch) = figure;
    surf( taxis, faxis, 10*log10(S(:,:,ch)), 'edgecolor', 'none' );
    axis tight;
    box on;
    caxis( [(minS - maxS)/6, maxS] ); % moderately define color map range
    view( 0, 90 );
    set( gca, 'FontName', 'Times', 'FontSize', 16 );
    if nch ~= 1
        title( sprintf('%dch spectrogram',ch), 'FontName', 'Arial', 'FontSize', 16 );
    end
    xlabel( 'Time [s]', 'FontName', 'Arial', 'FontSize', 16 );
    ylabel( 'Frequency [Hz]', 'FontName', 'Arial', 'FontSize', 16 );
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EOF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%