]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/dct.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / dct.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 discrete cosine transform of x.  If n is given, then
18 ##    x is padded or trimmed to length n before computing the transform.
19 ##    If x is a matrix, compute the transform along the columns of the
20 ##    the matrix. The transform is faster if x is real-valued and even
21 ##    length.
22 ##
23 ## The discrete cosine transform X of x can be defined as follows:
24 ##
25 ##               N-1
26 ##   X[k] = w(k) sum x[n] cos (pi (2n+1) k / 2N ),  k = 0, ..., N-1
27 ##               n=0
28 ##
29 ## with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1.  There
30 ## are other definitions with different scaling of X[k], but this form
31 ## is common in image processing.
32 ##
33 ## See also: idct, dct2, idct2, dctmtx
34
35 ## From Discrete Cosine Transform notes by Brian Evans at UT Austin,
36 ## http://www.ece.utexas.edu/~bevans/courses/ee381k/lectures/09_DCT/lecture9/
37 ## the discrete cosine transform of x at k is as follows:
38 ##
39 ##          N-1
40 ##   X[k] = sum 2 x[n] cos (pi (2n+1) k / 2N )
41 ##          n=0
42 ##
43 ## which can be computed using:
44 ##
45 ##   y = [ x ; flipud (x) ]
46 ##   Y = fft(y)
47 ##   X = exp( -j pi [0:N-1] / 2N ) .* Y
48 ##
49 ## or for real, even length x
50 ##
51 ##   y = [ even(x) ; flipud(odd(x)) ]
52 ##   Y = fft(y)
53 ##   X = 2 real { exp( -j pi [0:N-1] / 2N ) .* Y }
54 ##
55 ## Scaling the result by w(k)/2 will give us the desired output.
56
57 function y = dct (x, n)
58
59   if (nargin < 1 || nargin > 2)
60     print_usage;
61   endif
62
63   realx = isreal(x);
64   transpose = (rows (x) == 1);
65
66   if transpose, x = x (:); endif
67   [nr, nc] = size (x);
68   if nargin == 1
69     n = nr;
70   elseif n > nr
71     x = [ x ; zeros(n-nr,nc) ];
72   elseif n < nr
73     x (nr-n+1 : n, :) = [];
74   endif
75
76   if n == 1
77     w = 1/2;
78   else
79     w = [ sqrt(1/4/n); sqrt(1/2/n)*exp((-1i*pi/2/n)*[1:n-1]') ] * ones (1, nc);
80   endif
81   if ( realx && rem (n, 2) == 0 )
82     y = fft ([ x(1:2:n,:) ; x(n:-2:1,:) ]);
83     y = 2 * real( w .* y );
84   else
85     y = fft ([ x ; flipud(x) ]);
86     y = w .* y (1:n, :);
87     if (realx) y = real (y); endif
88   endif
89   if transpose, y = y.'; endif
90
91 endfunction