1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## Computes the inverse discrete cosine transform of x. If n is
18 ## given, then x is padded or trimmed to length n before computing
19 ## the transform. If x is a matrix, compute the transform along the
20 ## columns of the the matrix. The transform is faster if x is
21 ## real-valued and even length.
23 ## The inverse discrete cosine transform x of X can be defined as follows:
26 ## x[n] = sum w(k) X[k] cos (pi (2n+1) k / 2N ), n = 0, ..., N-1
29 ## with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1
31 ## See also: idct, dct2, idct2, dctmtx
33 function y = idct (x, n)
35 if (nargin < 1 || nargin > 2)
40 transpose = (rows (x) == 1);
42 if transpose, x = x (:); endif
47 x = [ x ; zeros(n-nr,nc) ];
49 x (n-nr+1 : n, :) = [];
52 if ( realx && rem (n, 2) == 0 )
53 w = [ sqrt(n/4); sqrt(n/2)*exp((1i*pi/2/n)*[1:n-1]') ] * ones (1, nc);
55 y([1:2:n, n:-2:1], :) = 2*real(y);
59 ## reverse the steps of dct using inverse operations
60 ## 1. undo post-fft scaling
61 w = [ sqrt(4*n); sqrt(2*n)*exp((1i*pi/2/n)*[1:n-1]') ] * ones (1, nc);
64 ## 2. reconstruct fft result and invert it
65 w = exp(-1i*pi*[n-1:-1:1]'/n) * ones(1,nc);
66 y = ifft ( [ y ; zeros(1,nc); y(n:-1:2,:).*w ] );
68 ## 3. keep only the original data; toss the reversed copy
70 if (realx) y = real (y); endif
72 if transpose, y = y.'; endif