]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/idct.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / idct.m
1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## y = dct (x, n)
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.
22 ##
23 ## The inverse discrete cosine transform x of X can be defined as follows:
24 ##
25 ##          N-1
26 ##   x[n] = sum w(k) X[k] cos (pi (2n+1) k / 2N ),  n = 0, ..., N-1
27 ##          k=0
28 ##
29 ## with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1
30 ##
31 ## See also: idct, dct2, idct2, dctmtx
32
33 function y = idct (x, n)
34
35   if (nargin < 1 || nargin > 2)
36     print_usage;
37   endif
38
39   realx = isreal(x);
40   transpose = (rows (x) == 1);
41
42   if transpose, x = x (:); endif
43   [nr, nc] = size (x);
44   if nargin == 1
45     n = nr;
46   elseif n > nr
47     x = [ x ; zeros(n-nr,nc) ];
48   elseif n < nr
49     x (n-nr+1 : n, :) = [];
50   endif
51
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);
54     y = ifft (w .* x);
55     y([1:2:n, n:-2:1], :) = 2*real(y);
56   elseif n == 1
57     y = x;
58   else
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);
62     y = x.*w;
63
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 ] );
67
68     ## 3. keep only the original data; toss the reversed copy
69     y = y(1:n, :);
70     if (realx) y = real (y); endif
71   endif
72   if transpose, y = y.'; endif
73
74 endfunction