
function X = dctall(x,type)
% DCTALL Discrete cosine transform
%   X = dctall(x,type)
%
%   All the DCT transformation types I-IV.
%   Type II is the most widely used.
%
%   x:    input signal to be transformed
%   type: dct type, (1:4)
%   X:    the transformed signal
%
%   Notes:  The implementation uses unitary DCT matrices
%   and no fancy speed increases through FFT()s
%   Here are some properties to help you invert stuff ...
%   The inverse of types I and IV is itself (involutary)
%   The inverse of type II is type III and vice versa
%   The inverse of type II is its transpose
%   The inverse of type III is its transpose
%
%   Example:
%
%   Reference:  "Discrete Cosine Transform" K.R. rao, P. Yip
%               ISBN 0-12-580203-X,  pp 10-16

% ------- dctall.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
if nargin < 2; type = 2; end

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

% Frame length x number of frames
[flen,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(C) ~= flen || type ~= ptype
    C     = dctallmtx(flen,type);
    ptype = type;
end

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