Review of Transforms: ECGR 6118 Computer Project: Transforms Student Name
Review of Transforms: ECGR 6118 Computer Project: Transforms Student Name
Review of Transforms: ECGR 6118 Computer Project: Transforms Student Name
Weldon, 2008)
Computer Project: Transforms
Student Name:___________
∞
⌠
X ( Ω) := ⎮
⌡
x( t) ⋅ e( − i⋅ Ω⋅ t) dt
−∞
∞
x( t) :=
1 ⌠
⋅⎮ X ( Ω) ⋅ e( i⋅ Ω⋅ t) dΩ
2π ⌡ −∞
The DTFT (Discrete Time Fourier transform) is defined for the discrete time sequence
x[n] as X(ω) given below
∑ (x(n)⋅ e )
∞
− i⋅ ω ⋅ n
X( ω) :=
Recall that the DTFT is periodic, with period = 2π .
n=−∞
π
1 ⌠ i⋅ ω ⋅ n
x ( n ) := ⋅⎮ X( ω) ⋅ e dω
2π ⌡ −π
Discrete-time Convolution
(Linear Convolution, not Circular Convolution) dummy fnction h(t)
h ( t) := 1
T( t) := 1
Convolution for a discrete time sequence y[n] = x[n] * h[n] = h[n] * x[n]is given below
∞
y ( n ) :=
∑ ( x( p) ⋅ h( n − p) )
p=−∞
or ∞
y ( n ) :=
∑ ( h( p) ⋅ x( n − p) )
p=−∞
If linear, y1(n) = T(x1(n)), y2(n)=T(x2(n)) => T(a x1(n) + b x2(n)) = a y1(n) + b y2(n)
so, for T() linear:
∞ ∞
y ( n ) :=
∑ T( x ( p ) δ( n − p ) ) or we could write as y ( n ) :=
∑ ( x ( p ) ⋅ T( δ( n − p ) ) )
p=−∞ p=−∞
The DTFT X(ω) is computed from a discrete-time sampled signal x[n], but has a continuous
frequency variable ω. The DFT is sampled in time and frequency, and is equal to the DFT at
each frequncy sample, where X[k] = X(ω) at ω = 2πk/N where N is the number of samples
in the time and frequency domains. In addition, the time and frequency domains in the
case of the DFT both are periodic (hence, circular convolution), with period N.
N−1
⎡ ( − i⋅ 2⋅ π) ⋅ n⋅ k⎤
⎢ ⎥
∑
N
X( k ) := ⎣x( n) e ⎦
n=0
N− 1
⎡ ( i⋅ 2⋅ π) ⋅ n⋅ k⎤
⎢ ⎥
∑
1 N
x ( n ) := ⋅ ⎣X( k ) e ⎦
N
k= 0
Circular convolution
N−1
y ( n ) :=
∑ ( x ( p ) ⋅ h ( mod( n − p , N) ) )
p=0
N−1 N−1
⎡ ( − i⋅ 2⋅ π) ⋅ ( u⋅ x+ v⋅ y) ⎤
⎢ ⎥
∑ ∑
1 N
F( u , v ) := ⋅ ⎣f ( x , y) e ⎦
N
x=0 y=0
or in separable form:
⎡⎢ ( − i⋅ 2⋅ π) ⋅ v⋅ y ⎡⎢ N−1 ⎡ ( − i⋅ 2⋅ π) ⋅ u⋅ x⎤⎤⎤
N−1 ⎢ ⎥⎥⎥
∑ ∑
1
F( u , v ) := ⋅ ⎢e
N
⋅ ⎢ ⎣f ( x , y ) ⋅ e
N
⎦⎥⎥
N ⎢ ⎢ ⎥⎥
y=0 ⎣ ⎣x = 0 ⎦⎦
1-D DFT implemented as a Mathcad program/function
N := 8 n := 0 .. N − 1 x := n k := n
n Define the vector x of length N
⎛0⎞ ⎛ 28 ⎞
⎜1⎟ ⎜ −4 + 9.657i ⎟
⎜ ⎟ ⎜ ⎟
⎜2⎟ ⎜ −4 + 4i ⎟
⎜3⎟ ⎜ −4 + 1.657i ⎟
x=⎜ ⎟ X= ⎜ ⎟
−4
⎜4⎟ ⎜ ⎟
⎜5⎟ ⎜ −4 − 1.657i ⎟
⎜6⎟ ⎜ −4 − 4i ⎟
⎜ ⎟ ⎜ ⎟
⎝7⎠ ⎝ −4 − 9.657i ⎠
z := n + k ⋅ 10
n, k
⎛0 10 20 30 40 50 60 70 ⎞
⎜1 11 21 31 41 51 61 71
⎟
⎜ ⎟
⎜2 12 22 32 42 52 62 72 ⎟
⎜3 13 23 33 43 53 63 73 ⎟
z=⎜ ⎟
⎜4 14 24 34 44 54 64 74
⎟
⎜5 15 25 35 45 55 65 75 ⎟
⎜6 16 26 36 46 56 66 76 ⎟
⎜ ⎟
⎝7 17 27 37 47 57 67 77 ⎠
− ( i⋅ 2⋅ π⋅ n⋅ k)
1 N
Wdft := ⋅e
k, n N
⎛0 0 0 0 0 0 0 0 ⎞
⎜0 −45 −90 −135 −180 135 90 45
⎟
⎜ ⎟
⎜0 −90 −180 90 0 −90 −180 90 ⎟
⎜0 135 ⎟
(
360 ⎯⎯⎯⎯ →
)
arg ( Wdft) = ⎜
−135 90 −45 −180 45 −90
⎟
2⋅ π −180 −180 −180 −180
⎜0 0 0 0
⎟
⎜0 135 −90 45 −180 −45 90 −135 ⎟
⎜0 90 −180 −90 0 90 −180 −90 ⎟
⎜ ⎟
⎝0 45 90 135 −180 −135 −90 −45 ⎠
⎛1 0 0 0 0 0 0 0⎞
⎜0 1 0 0 0 0 0 0
⎟
⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟
⎯ ⎜0 0⎟
Wdft⋅ ( Wdft ) = ⎜
T 0 0 1 0 0 0
⎟
⎜0 0 0 0 1 0 0 0
⎟
⎜0 0 0 0 0 1 0 0⎟
⎜0 0 0 0 0 0 1 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠
So:
⎯
(
invWdtft := Wdft
T )
From the 1D DFT we developed earlier, we had
⎛0⎞ ⎛ 28 ⎞
⎜1⎟ ⎜ −4 + 9.657i ⎟
⎜ ⎟ ⎜ ⎟
⎜2⎟ ⎜ −4 + 4i ⎟
⎜3⎟ ⎜ −4 + 1.657i ⎟
x=⎜ ⎟ X= ⎜ ⎟
−4
⎜4⎟ ⎜ ⎟
⎜5⎟ ⎜ −4 − 1.657i ⎟
⎜6⎟ ⎜ −4 − 4i ⎟
⎜ ⎟ ⎜ ⎟
⎝7⎠ ⎝ −4 − 9.657i ⎠
⎛0⎞ ⎛ 9.899 ⎞
⎜1⎟ ⎜ −1.414 + 3.414i ⎟
⎜ ⎟ ⎜ ⎟
⎜2⎟ ⎜ −1.414 + 1.414i ⎟
⎜3⎟ ⎜ −1.414 + 0.586i ⎟
x=⎜ ⎟ Wdft⋅ x = ⎜ ⎟
−1.414
⎜4⎟ ⎜ ⎟
⎜5⎟ ⎜ −1.414 − 0.586i ⎟
⎜6⎟ ⎜ −1.414 − 1.414i ⎟
⎜ ⎟ ⎜ ⎟
⎝7⎠ ⎝ −1.414 − 3.414i ⎠
⎛0⎞
⎛1 0 0 0 0 0 0 0⎞
⎜1⎟
⎜0 ⎟
1 0 0 0 0 0 0 ⎜ ⎟
⎜ ⎟ ⎜2⎟
⎜0 0 1 0 0 0 0 0⎟
⎜3⎟
⎡( ⎯ T⎤ ⎜0 0⎟ −1
⎣ Wdft) ⎦ ⋅ Wdft = ⎜
0 0 1 0 0 0 Wdft ⋅ Wdft⋅ x = ⎜ ⎟
⎟ ⎜4⎟
⎜0 0 0 0 1 0 0 0
⎟ ⎜5⎟
⎜0 0 0 0 0 1 0 0⎟
⎜6⎟
⎜0 0 0 0 0 0 1 0⎟ ⎜ ⎟
⎜ ⎟ ⎝7⎠
⎝0 0 0 0 0 0 0 1⎠
1-D DCT (Pratt Eq. 8.3-1 )
redefine delta
δ( x ) := δ( x , 0 )
N−1
⎡ ( n ) ⋅ 2 − δ( k) cos⎡ π⋅ k ⋅ ( n + 0.5)⎤⎤
F( k ) :=
∑ ⎢f
⎣ N
⎢
⎣ N
⎥⎥
⎦⎦
n=0
N− 1
⎡ ( k ) ⋅ 2 − δ( k) cos⎡ π⋅ k ⋅ ( n + 0.5)⎤⎤
f ( n ) :=
∑ ⎢F
⎣ N
⎢
⎣ N
⎥⎥
⎦⎦
k= 0
N− 1 ⎡ N−1 ⎤
cos⎡⎢
⎢ 2 − δ( u ) 2 − δ( v ) π⋅ u ⋅ ( x + 0.5) ⎤ ⎡ π⋅ v⋅ ( y + 0.5) ⎤⎥
F( u , v ) :=
∑ ∑ ⎢
f ( x , y)⋅
N
⋅
N ⎣ N
⋅
⎥ ⎢
⎦ ⎣
cos
N
⎥
⎦⎥
x = 0 ⎣y = 0 ⎦
N−1 N−1
⎡F( u , v ) ⋅ 2 − δ( u) ⋅ 2 − δ( v ) ⋅ cos⎡ π⋅ u⋅ ( x + 0.5) ⎤ ⋅ cos⎡ π⋅ v⋅ ( y + 0.5) ⎤⎤
f ( x , y ) :=
∑ ∑ ⎢
⎣ N N
⎢
⎣ N
⎥ ⎢
⎦ ⎣ N
⎥⎥
⎦⎦
u=0 v=0
Matrix form of 1-D DCT
one way to create a DCT matrix is using the if function to load the matrix
⎡ π⋅ k ⋅ ( n + 0.5) ⎤⎤
cos⎡⎢
π⋅ k ⋅ ( n + 0.5) ⎤
cos⎡⎢
1 2
Wdct := if ⎢k = 0 , ⎥, ⎥⎥
k, n ⎣ N ⎣ N ⎦ N ⎣ N ⎦⎦
Wdct := wdctmatrix( N)
⎛1 0 0 0 0 0 0 0⎞
⎜0 1 0 0 0 0 0 0
⎟
⎜ ⎟ Note, orthogonal vectors!
⎜0 0 1 0 0 0 0 0⎟ Is unitary and orthogonal (since real).
⎯ T ⎜0 0⎟
Wdct⋅ ( Wdct) = ⎜
0 0 1 0 0 0
⎟ Unitary: A-1 = A *T = A H
⎜0 0 0 0 1 0 0 0
⎟ Orthogonal: A -1 = A T
⎜0 0 0 0 0 1 0 0⎟
⎜0 0 0 0 0 0 1 0⎟ Is orthogonal transform, so inverse DCT =
⎜ ⎟ transpose
⎝0 0 0 0 0 0 0 1⎠
Note: Wdct is full rank, invertible, full/complete basis for vector space
Comparison of DTFT and DCT basis functions:
⎡ − ( i⋅ 2⋅ π⋅ u⋅ t) ⎤
dctbasis( u , t) := cos⎡⎢
⎢ ⎥ π⋅ u ⋅ ( t + 0.5) ⎤
dtftbasis( u , t) := Re⎣e
N
⎦ ⎥
⎣ N ⎦
18 18
16.2 16.2
14.4 14.4
dtftbasis( 0 , t) 12.6 dctbasis( 0 , t) 12.6
dtftbasis( 1 , t) + 4 dctbasis( 1 , t) + 4
10.8 10.8
dtftbasis( 2 , t) + 8 dctbasis( 2 , t) + 8
9 9
dtftbasis( 3 , t) + 12 dctbasis( 3 , t) + 12
7.2 7.2
dtftbasis( 4 , t) + 16 dctbasis( 4 , t) + 16
5.4 5.4
3.6 3.6
1.8 1.8
0 0
0 1.6 3.2 4.8 6.4 8 0 1.6 3.2 4.8 6.4 8
t t
Q1 : Change the above 2 plots to compare the first 8 basis functions, instead
of the first 5
Matrix form of 1-D Hadamard Transform
⎛ 1 1 ⎞⋅ 1 Hadamard matrix size will be
Whad( exp) := H1 ← ⎜ ⎟
⎝ 1 −1 ⎠ 2 2exp
for n ∈ 1 .. exp − 1
Ht ← augment( H1 , H1)
Hb ← augment( H1 , −H1)
H ← stack( Ht , Hb)
1
H1 ← H⋅
2
H ← H1
H
⎛1 0 0 0 0 0 0 0⎞
⎜0 1 0 0 0 0 0 0
⎟ Note, orthogonal vectors!
⎜ ⎟ Is unitary and orthogonal (since real).
⎜0 0 1 0 0 0 0 0⎟
T
⎜0 0 0 1 0 0 0 0⎟ Unitary: A-1 = A *T = A H
Whad⋅ Whad = ⎜ ⎟ Orthogonal: A -1 = A T
⎜0 0 0 0 1 0 0 0
⎟
⎜0 0 0 0 0 1 0 0⎟ Is orthogonal transform, so inverse DCT =
⎜0 0 0 0 0 0 1 0⎟ transpose
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠
Sequency is the number of zero-crossings in each row,
we can write a function to compute the sequency of each row
⎛0⎞
⎜7⎟
⎜ ⎟
⎜3⎟
⎜4⎟
seqW := sequency ( Whad) seqW = ⎜ ⎟
⎜1⎟
⎜6⎟
⎜2⎟
⎜ ⎟
⎝5⎠
Matrix form 1-D Sequency-ordered Hadamard Transform
N= 8
⎛ 1 1 ⎞⋅ 1
seqHad( exp) := H1 ← ⎜ ⎟
⎝ 1 −1 ⎠ 2
for n ∈ 1 .. exp − 1
Ht ← augment( H1 , H1)
Hb ← augment( H1 , −H1)
H ← stack( Ht , Hb)
1
H1 ← H⋅
2
H ← H1
ww ← H
seqW ← sequency ( ww)
out ← ww
for n1 ∈ 0 .. rows( seqW ) − 2
for n2 ∈ n1 + 1 .. rows( seqW ) − 1
se1 ← seqW
n1
se2 ← seqW
n2
〈 〉
( T) n1
rn1 ← out if se2 < se1
〈n2〉
rn2 ← ( out )
T
if se2 < se1
seqW ← se1 if se2 < se1
n2
seqW ← se2 if se2 < se1
n1
for n3 ∈ 0 .. rows( seqW ) − 1 if se2 < se1
out ← rn2
n1 , n3 n3
out ← rn1
n2 , n3 n3
se1 ← se2
out
Wseqhad := seqHad( log( N , 2 ) )
⎛1 0 0 0 0 0 0 0⎞
⎜0 ⎟ ⎛0⎞
1 0 0 0 0 0 0 ⎜1⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟
⎜2⎟
⎜0 0⎟
T
Wseqhad ⋅ Wseqhad = ⎜
0 0 1 0 0 0 ⎜3⎟
⎟ sequency ( Wseqhad ) = ⎜ ⎟
⎜0 0 0 0 1 0 0 0
⎟ ⎜4⎟
⎜0 0 0 0 0 1 0 0⎟
⎜5⎟
⎜0 0⎟
0 0 0 0 0 1 ⎜6⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠ ⎜ ⎟
⎝7⎠
Note: Hadamard is full rank, invertible, full/complete basis for vector space
⎡ − ( i⋅ 2⋅ π⋅ u⋅ t) ⎤ N−1 〈u〉
⎢
dtftbasis( u , t) := Re⎣e
N ⎥
⎦
seqHbasis( u , t) := N⋅
∑
⎡⎡
( T) ⎤ 1 ⎤
⎢⎣ Wseqhad ⎦ n⋅ rect⎛⎜ t − n − ⎟⎞⎥
⎣ ⎝ 2 ⎠⎦
n=0
18 20
16.2 18
14.4 16
dtftbasis( 0 , t) 12.6 seqHbasis( 0 , t) 14
dtftbasis( 1 , t) + 4 seqHbasis( 1 , t) + 4
10.8 12
dtftbasis( 2 , t) + 8 seqHbasis( 2 , t) + 8
9 10
dtftbasis( 3 , t) + 12 seqHbasis( 3 , t) + 12
7.2 8
dtftbasis( 4 , t) + 16 seqHbasis( 4 , t) + 16
5.4 6
3.6 4
1.8 2
0 0
0 1.6 3.2 4.8 6.4 8 0 1.6 3.2 4.8 6.4 8
t t
Q3 : Change the above 2 plots to compare the first 8 basis functions, instead
of the first 5.
Comparison of DTFT, DCT, and Sequency-ordered Hadamard transforms of a ramp signal
Q4,Q5,Q6 : For the N=16 point ramp signal x shown below, compute the 1-D
DFT,DCT, and Hadamard transforms. Use above functions to compute the
transfofm matrices M, then write Mx= to show the result.
Sequency-ordered
DTFT result: DCT result: Hadamard result:
0
0 0
1 1
2 2
3 3
4 4
5 5
6 6
x= 7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
2-D Transforms
The 2-D DFT, DCT, and Hadamard can all be generated from the 1-D matrix forms
of each transform as follows. All three transforms are unitary and separable, and
so the same procedures will apply.
A simple initial test image for transforms
x
r, c
(
:= 2 ⋅ 10⋅ r + c )
2
⎛ 0 2 8 18 32 50 72 98 ⎞
⎜ 20 22 28 38 52 70 92 118
⎟
⎜ ⎟
⎜ 40 42 48 58 72 90 112 138 ⎟
⎜ 60 62 68 78 92 110 132 158 ⎟
x=⎜ ⎟
⎜ 80 82 88 98 112 130 152 178
⎟
⎜ 100 102 108 118 132 150 172 198 ⎟
⎜ 120 122 128 138 152 170 192 218 ⎟
⎜ ⎟
x ⎝ 140 142 148 158 172 190 212 238 ⎠
⎯⎯⎯ →
magXdtft := Xdtft
magXdtft
x 250 ⋅
max( magXdtft)
⎯ ⎯
( T
)
xx := Wdft ⋅ Xdft⋅ Wdft Check that we can recover
the original image using the
inverse DFT
⎛ 0 2 8 18 32 50 72 98 ⎞
⎜ 20 22 28 38 52 70 92 118
⎟
⎜ ⎟
⎜ 40 42 48 58 72 90 112 138 ⎟
⎜ 60 62 68 78 92 110 132 158 ⎟
xx = ⎜ ⎟
⎜ 80 82 88 98 112 130 152 178
⎟
⎜ 100 102 108 118 132 150 172 198 ⎟
⎜ 120 122 128 138 152 170 192 218 ⎟
⎜ ⎟
⎝ 140 142 148 158 172 190 212 238 ⎠
x xx
magXdct :=
magXdct =
DFT DCT
magXdct
250 ⋅
max( magXdct)
magXdtft
250 ⋅
max( magXdtft)
2-D inverse DCT using matrices (Pratt Eq. 8.3-1 )
Q8 : For the same image x in the DFT above, compute the inverse 2-D DCT
transform below, and show that the original image is recovered.
xx :=
⎛0 0 0 0 0 0 0 0⎞
⎜0 0 0 0 0 0 0 0
⎟
⎜ ⎟
⎜0 0 0 0 0 0 0 0⎟
⎜0 0 0 0 0 0 0 0⎟
xx = ⎜ ⎟
⎜0 0 0 0 0 0 0 0
⎟
⎜0 0 0 0 0 0 0 0⎟
⎜0 0 0 0 0 0 0 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 0⎠
x IDCT
xx
x
Xseqhad :=
magXseqhad :=
magXseqhad =
magXseqhad
250 ⋅
max( magXseqhad)
magXdtft
250 ⋅
max( magXdtft)
DFT Hadamard
2-D inverse Hadamard using matrices
Q10 : For the same image x in the Hadamard above, compute the inverse 2-D
Hadamard transform below, and show that the original image is recovered.
xx :=
⎛0 0 0 0 0 0 0 0⎞
⎜0 0 0 0 0 0 0 0
⎟
⎜ ⎟
⎜0 0 0 0 0 0 0 0⎟
⎜0 0 0 0 0 0 0 0⎟
xx = ⎜ ⎟
⎜0 0 0 0 0 0 0 0
⎟
⎜0 0 0 0 0 0 0 0⎟
⎜0 0 0 0 0 0 0 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 0⎠
xx
x
original image x
inverse Hadamard
Q11,Q12,Q13 : For the 256x256 pixel wheel.gif image, compute the 2-D
DFT,DCT, and sequency-ordered Hadamard transforms and display the results
as spectral images. Use the following logarithmic plotting
function for your output images. You mustremove the DC component from
the image x before taking transforms (see last line below).
⎯⎯→
magXdct := Xdct Logarithmic plotting
function for output
maxdct := max( magXdct) spectra
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ →
⎛ ⎛
dctplot := 3 ⋅ ⎜ 20⋅ log⎜ 1 +
10000 ⋅ magXdct ⎞ ⎞
⎟⎟
⎝ ⎝ maxdct ⎠⎠
max( dctplot) =
x := infile
x =
rows( x ) = cols( x ) = 1, 1
infile
x := infile − mean( infile) Note: you cannot plot x anymore, because it has
negative values