
function X = dtt(x,type,orth)
% DTT Discrete trigonometric transform
%   X = dtt(x,type,orth)
%
%   This calculates all 16 DTT transforms of input x.
%
%   x:    input signal to be transformed
%   type: dtt type. Here are all the possible types:
%         'dct1e','dct2e','dct3e','dct4e'
%         'dct1o','dct2o','dct3o','dct4o'
%         'dst1e','dst2e','dst3e','dst4e'
%         'dst1o','dst2o','dst3o','dst4o'
%   orth: if this flag is set we calculate the orthogonal
%         version (Wang) otherwise the non orthogonal (Martucci)
%   X:    the transformed signal
%
%   Notes: The implementation uses either orthogonal or
%          non orthogonal DTT matrices and no fancy speed
%          increases through FFT()s
%
%   Example:
%   Transforming an framed input signal x (frame per column)
%   can be done as:
%   X = dtt(x,'dct2e'); % Type II even orthogonal
%
%   Reference: Z. Wang and B. Hunt, The discrete W-transform
%              Appl. Math. Comput., 16 (1985), pp. 19-48.
%
%              S.A. Martucci, "Symmetric convolution and the discrete
%              sine and cosine transforms", IEEE Trans. Sig. Processing
%              SP-42, pp1038-1051, 1994

% ------- dtt.m --------------------------------------------
% Marios Athineos, marios@ee.columbia.edu
% http://www.ee.columbia.edu/~marios/
% Copyright (c) 2005 by Columbia University.
% All rights reserved.
% ----------------------------------------------------------

% The default type is II even orthogonal
if nargin < 2; type = 'dct2e'; end
if nargin < 3; orth = 1;       end

% Cache the trasformation matrix so that we don't remake
% it all the time (assuming the same size and type)
persistent T ptype porth;

% Frame length x number of frames
[N,fnum] = size(x);

% If the size of the transform is not the same as the frame
% length we need to make it cause it's not initialized
if length(T) ~= N || ~strcmpi(type,ptype) || orth ~= porth
    if orth;
        T = dttmtx(N,type);
    else
        T = dttmtxno(N,type,1);
    end
    % Save the type and orth flag    
    ptype = type;
    porth = orth;
end

% And the transformation is simply
X = T*x;