annotate main/signal/inst/dct.m @ 2795:7a614782378d octave-forge

Spelling corrections
author pkienzle
date Tue, 05 Dec 2006 20:45:13 +0000
parents 38e4c6572b8d
children 73fa4496fb07
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2372
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
1 ## Copyright (C) 2001 Paul Kienzle
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
2 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
6 ## (at your option) any later version.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
7 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
11 ## GNU General Public License for more details.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
12 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
16
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
17 ## y = dct (x, n)
2795
7a614782378d Spelling corrections
pkienzle
parents: 2372
diff changeset
18 ## Computes the discrete cosine transform of x. If n is given, then
2372
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
19 ## x is padded or trimmed to length n before computing the transform.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
20 ## If x is a matrix, compute the transform along the columns of the
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
21 ## the matrix. The transform is faster if x is real-valued and even
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
22 ## length.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
23 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
24 ## The discrete cosine transform X of x can be defined as follows:
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
25 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
26 ## N-1
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
27 ## X[k] = w(k) sum x[n] cos (pi (2n-1) k / 2N ), k = 0, ..., N-1
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
28 ## n=0
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
29 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
30 ## with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1. There
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
31 ## are other definitions with different scaling of X[k], but this form
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
32 ## is common in image processing.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
33 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
34 ## See also: idct, dct2, idct2, dctmtx
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
35
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
36 ## From Discrete Cosine Transform notes by Brian Evans at UT Austin,
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
37 ## http://www.ece.utexas.edu/~bevans/courses/ee381k/lectures/09_DCT/lecture9/
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
38 ## the discrete cosine transform of x at k is as follows:
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
39 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
40 ## N-1
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
41 ## X[k] = sum 2 x[n] cos (pi (2n-1) k / 2N )
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
42 ## n=0
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
43 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
44 ## which can be computed using:
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
45 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
46 ## y = [ x ; flipud (x) ]
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
47 ## Y = fft(y)
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
48 ## X = exp( -j pi [0:N-1] / 2N ) .* Y
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
49 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
50 ## or for real, even length x
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
51 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
52 ## y = [ even(x) ; flipud(odd(x)) ]
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
53 ## Y = fft(y)
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
54 ## X = 2 real { exp( -j pi [0:N-1] / 2N ) .* Y }
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
55 ##
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
56 ## Scaling the result by w(k)/2 will give us the desired output.
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
57
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
58 ## Author: Paul Kienzle
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
59 ## 2001-02-08
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
60 ## * initial release
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
61 function y = dct (x, n)
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
62
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
63 if (nargin < 1 || nargin > 2)
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
64 usage ("y = dct(x [, n])");
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
65 endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
66
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
67 realx = isreal(x);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
68 transpose = (rows (x) == 1);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
69
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
70 if transpose, x = x (:); endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
71 [nr, nc] = size (x);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
72 if nargin == 1
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
73 n = nr;
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
74 elseif n > nr
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
75 x = [ x ; zeros(n-nr,nc) ];
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
76 elseif n < nr
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
77 x (nr-n+1 : n, :) = [];
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
78 endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
79
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
80 if n == 1
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
81 w = 1/2;
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
82 else
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
83 w = [ sqrt(1/4/n); sqrt(1/2/n)*exp((-1i*pi/2/n)*[1:n-1]') ] * ones (1, nc);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
84 endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
85 if ( realx && rem (n, 2) == 0 )
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
86 y = fft ([ x(1:2:n,:) ; x(n:-2:1,:) ]);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
87 y = 2 * real( w .* y );
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
88 else
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
89 y = fft ([ x ; flipud(x) ]);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
90 y = w .* y (1:n, :);
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
91 if (realx) y = real (y); endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
92 endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
93 if transpose, y = y.'; endif
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
94
38e4c6572b8d Changed the directory structure to match the package system
hauberg
parents:
diff changeset
95 endfunction