Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Review of Transforms: ECGR 6118 Computer Project: Transforms Student Name

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ECGR 6118 (copyright T.

Weldon, 2008)
Computer Project: Transforms
Student Name:___________

Review of transforms dummy fnction x(t)


x( t) := 1
Continuous-time, 1D Fourier transform:
The 1-D continuous-time Fourier transform is:



X ( Ω) := ⎮

x( t) ⋅ e( − i⋅ Ω⋅ t) dt
−∞


x( t) :=
1 ⌠
⋅⎮ X ( Ω) ⋅ e( i⋅ Ω⋅ t) dΩ
2π ⌡ −∞

Discrete Time Fourier Transforms (DTFT)

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=−∞

LTI (Linear Time Invariant) and Convolution

more generally, let T be a general transformation



y ( n ) := T( x ( n ) ) and x ( n ) :=
∑ ( x ( p ) ⋅ δ( 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=−∞

and letting h(n,p) = T( δ( n − p ) )


∞ ∞
y ( n ) :=
∑ ( x( p) ⋅ h( n , p) ) or we could write as y ( n ) :=
∑ ⎡⎣x ( p ) ⋅ h n( p )⎤⎦
p=−∞ p=−∞

if time invariant, h(n,p) = h(n-p) simply shifts with time



y ( n ) :=
∑ ( x( p) ⋅ h( n − p) )
p=−∞
1D DFT

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.

The DFT and inverse DFT are defined as

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

dummy fnction h(t)


2D DFT
f ( x , y ) := 1
assume an NxN pixel image

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

dft( x ) := out ← x ⋅ 0 Here we define an algorithm


to compute DFT of data stored
for k ∈ 0 .. rows( out ) − 1
in a vector x
rows( x) − 1
⎡⎢ − ( i⋅ 2⋅ π⋅ n⋅ k) ⎤⎥
out ←
k ∑ ⎢x n⋅ e rows( x) ⎥
⎣ ⎦
n=0
out

Example: take the 8-point DFT of a ramp signal x.

N := 8 n := 0 .. N − 1 x := n k := n
n Define the vector x of length N

X := dft( x ) Take the DFT of x

⎛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 ⎠

⎛ 28 ⎞ ⎛ 0 ⎞ Find the magnitude and angle


⎜ 10.453 ⎟ ⎜ 1.963 ⎟ of the DFT
⎜ ⎟ ⎜ ⎟
⎜ 5.657 ⎟ ⎜ 2.356 ⎟
⎯→ ⎜ 4.33 ⎟ ⎯⎯→ ⎜ 2.749 ⎟
X =⎜ ⎟ arg( X) = ⎜ ⎟
⎜ 4 ⎟ ⎜ −3.142 ⎟
⎜ 4.33 ⎟ ⎜ −2.749 ⎟
⎜ 5.657 ⎟ ⎜ −2.356 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 10.453 ⎠ ⎝ −1.963 ⎠
Matrix form (Unitary form) of 1-D DFT
Below, simple example to demonstrate
mathcad matrix indexing:

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 ⎠

N := 8 n := 0 .. N − 1 x := n k := n Define the vector x of length N


n

Define the elements of DFT matrix Wdft

− ( i⋅ 2⋅ π⋅ n⋅ k)
1 N
Wdft := ⋅e
k, n N

⎛ 0.354 0.354 0.354 0.354 0.354 0.354 0.354 0.354 ⎞


⎜ 0.354 0.25 − 0.25i −0.354i −0.25 − 0.25i −0.354 −0.25 + 0.25i 0.354i 0.25 + 0.25i

⎜ ⎟
⎜ 0.354 −0.354i −0.354 0.354i 0.354 −0.354i −0.354 0.354i ⎟
⎜ 0.354 −0.25 − 0.25i 0.354i 0.25 − 0.25i −0.354 0.25 + 0.25i −0.354i −0.25 + 0.25i ⎟
Wdft = ⎜ ⎟
−0.354 −0.354 −0.354 −0.354
⎜ 0.354 0.354 0.354 0.354

⎜ 0.354 −0.25 + 0.25i −0.354i 0.25 + 0.25i −0.354 0.25 − 0.25i 0.354i −0.25 − 0.25i ⎟
⎜ 0.354 0.354i −0.354 −0.354i 0.354 0.354i −0.354 −0.354i ⎟
⎜ ⎟
⎝ 0.354 0.25 + 0.25i 0.354i −0.25 + 0.25i −0.354 −0.25 − 0.25i −0.354i 0.25 − 0.25i ⎠
observe the behavior of the angle of the DFT matrix:

⎛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 ⎠

Note: you may wish to use Format>Result>ComplexThreshold and zeroThreshold


and set to 12 to eliminate small residual roundoff/numerical error in your results display

⎛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⎠

Note, the above result implies orthogonal vectors,


i.e., inner product (x,x*)=0
Also, the above shows that DTFT matrix form is unitary.

Unitary matrix means: A-1 = A *T = A H

Wdtft is unitary, so inverse DCT = conjugate transpose.


Since DTFT is also a symmetric matrix, it so happens that
the conjugate of Wdtft is also the inverse.

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 ⎠

Using the matrix form we get the same result:

⎛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 ⎠

we can check the inverse is we can check the inverse DFT:


conjugate transpose:

⎛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 )

The 1-D DCT and inverse are

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

2-D DCT (Pratt Eq. 8.3-1 )

The 2-D DCT and inverse are defined as

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 ⎦⎦

another way to create a DCT matrix is using a short program as below

wdctmatrix( nn) := for k ∈ 0 .. nn − 1


for n ∈ 0 .. nn − 1
π⋅ k ⋅ ( n + 0.5) ⎤
cos⎡⎢
2
w
k, n
← ⎥
nn ⎣ nn ⎦
π⋅ k ⋅ ( n + 0.5) ⎤
cos⎡⎢
1
w
k, n
← ⎥ if k = 0
nn ⎣ nn ⎦
w

Wdct := wdctmatrix( N)

⎛ 0.354 0.354 0.354 0.354 0.354 0.354 0.354 ⎞


0.354
⎜ 0.49 0.416 0.278 0.098 −0.098 −0.278 −0.416 −0.49

⎜ ⎟
⎜ 0.462 0.191 −0.191 −0.462 −0.462 −0.191 0.191 0.462 ⎟
⎜ 0.416 −0.098 −0.49 −0.278 0.278 0.49 0.098 −0.416 ⎟
Wdct = ⎜ ⎟
−0.354 −0.354 0.354 −0.354 −0.354
⎜ 0.354 0.354 0.354

⎜ 0.278 −0.49 0.098 0.416 −0.416 −0.098 0.49 −0.278 ⎟
⎜ 0.191 −0.462 0.462 −0.191 −0.191 0.462 −0.462 0.191 ⎟
⎜ ⎟
⎝ 0.098 −0.278 0.416 −0.49 0.49 −0.416 0.278 −0.098 ⎠

⎛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:

Dct basis functions


Dtft basis functions
N := 8

⎡ − ( i⋅ 2⋅ π⋅ u⋅ t) ⎤
dctbasis( u , t) := cos⎡⎢
⎢ ⎥ π⋅ u ⋅ ( t + 0.5) ⎤
dtftbasis( u , t) := Re⎣e
N
⎦ ⎥
⎣ N ⎦

DFT basis functions N := 8 DCT basis functions N := 8

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

Whad := Whad( log( N , 2 ) )

⎛ 0.354 0.354 0.354 0.354 0.354 0.354 0.354 0.354 ⎞


⎜ 0.354 −0.354 0.354 −0.354 0.354 −0.354 0.354 −0.354

⎜ ⎟
⎜ 0.354 0.354 −0.354 −0.354 0.354 0.354 −0.354 −0.354 ⎟
⎜ 0.354 −0.354 −0.354 0.354 0.354 −0.354 −0.354 0.354 ⎟
Whad = ⎜ ⎟
−0.354 −0.354 −0.354 −0.354
⎜ 0.354 0.354 0.354 0.354

⎜ 0.354 −0.354 0.354 −0.354 −0.354 0.354 −0.354 0.354 ⎟
⎜ 0.354 0.354 −0.354 −0.354 −0.354 −0.354 0.354 0.354 ⎟
⎜ ⎟
⎝ 0.354 −0.354 −0.354 0.354 −0.354 0.354 0.354 −0.354 ⎠

⎛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

sequency ( x ) := for r ∈ 0 .. rows( x ) − 1


ss ← 0
r
for r ∈ 0 .. rows( x ) − 1
for c ∈ 0 .. cols( x ) − 2
x
r, c
ss ← ss + 1 if ≤0
r r x
r , c+ 1
ss

Note that the sequency is out of order:

⎛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 ) )

⎛ 0.354 0.354 0.354 0.354 0.354 0.354 0.354 0.354 ⎞


⎜ 0.354 0.354 0.354 0.354 −0.354 −0.354 −0.354 −0.354

⎜ ⎟
⎜ 0.354 0.354 −0.354 −0.354 −0.354 −0.354 0.354 0.354 ⎟
⎜ 0.354 0.354 −0.354 −0.354 0.354 0.354 −0.354 −0.354 ⎟
Wseqhad = ⎜ ⎟
−0.354 −0.354 0.354 −0.354 −0.354
⎜ 0.354 0.354 0.354

⎜ 0.354 −0.354 −0.354 0.354 −0.354 0.354 0.354 −0.354 ⎟
⎜ 0.354 −0.354 0.354 −0.354 −0.354 0.354 −0.354 0.354 ⎟
⎜ ⎟
⎝ 0.354 −0.354 0.354 −0.354 0.354 −0.354 0.354 −0.354 ⎠

The new method produces a Hadamard with ordered sequency

⎛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

Q2 : For N=8 as above, show that inverse Hadamard = itself = Wseqhad:


Comparison of DTFT and Sequency-ordered Hadamard basis functions:

Hadamard basis functions


Dtft basis functions
rect( t) := Φ ( t + 0.5) − Φ ( t − 0.5)
N= 8

⎡ − ( 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

DFT basis functions N := 8 N := 8


Hadamard basis functions

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

N := 16 n := 0 .. N − 1 x := n k := n Define the vector x of length N


n

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 := 0 Sometimes it is best to clear a variable before re-use

N := 8 n := 0 .. N − 1 x := n k := n Define the vector x of length N


n

First, create a 2-D image matrix for test purposes


r := n c := n

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 ⎠

2-D DFT using the cfft fast Fourier transform


This Mathcad built-in function
Xdtft := cfft( x ) cfft uses a 2-D FFT algorithm

⎯⎯⎯ →
magXdtft := Xdtft

⎛ 840 154.794 80 60.72 56 60.72 80 154.794 ⎞


⎜ 209.05 0 0 0 0 0 0 0

⎜ ⎟
⎜ 113.137 0 0 0 0 0 0 0 ⎟
⎜ 86.591 0 0 0 0 0 0 0 ⎟
magXdtft = ⎜ ⎟
⎜ 80 0 0 0 0 0 0 0

⎜ 86.591 0 0 0 0 0 0 0 ⎟
⎜ 113.137 0 0 0 0 0 0 0 ⎟
⎜ ⎟
⎝ 209.05 0 0 0 0 0 0 0 ⎠
2-D DTFT using matrices (Pratt Eq. 8.1-13a, 8.2-15a )

T Note: this would NOT be a fast


Xdft := Wdft⋅ x ⋅ ( Wdft)
algorithm, since it is implemented
as a regular matrix multiply
⎯⎯→
magXdft := Xdft

⎛ 840 154.794 80 60.72 56 60.72 80 154.794 ⎞


⎜ 209.05 0 0 0 0 0 0 0

⎜ ⎟
⎜ 113.137 0 0 0 0 0 0 0 ⎟
⎜ 86.591 0 0 0 0 0 0 0 ⎟
magXdft = ⎜ ⎟
⎜ 80 0 0 0 0 0 0 0

⎜ 86.591 0 0 0 0 0 0 0 ⎟
⎜ 113.137 0 0 0 0 0 0 0 ⎟
⎜ ⎟
⎝ 209.05 0 0 0 0 0 0 0 ⎠

magXdtft
x 250 ⋅
max( magXdtft)

input image x Magnitude spectrum image


2-D inverse DFT using matrices ((Pratt Eq. 8.3-1 )

⎯ ⎯
( 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

original image x IDFT

xx := xx − xx Sometimes it is best to clear a variable before re-use


2-D DCT using matrices (Pratt Eq. 8.3-1 )
Q7 : : For the same image x in the DFT above, compute the 2-D DCT transform
below using a matrix method similar to the DFT form Xdft=Wdft x Wdft T.

Xdct := fix this!

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

original image x IDCT

xx := xx − xx Sometimes it is best to clear a variable before re-use


2-D Sequency-ordered Hadamard using matrices
Q9 : For the same image x in the DFT above, compute the 2-D Hadamard
transform below.

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) =

infile := READBMP( "wheel.gif" )

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

You might also like