2-D Fourier Transforms - NYU Tandon School ofeeweb.poly.edu/~yao/EL5123/lecture4_2DFT.pdf · 2-D...

Preview:

Citation preview

2-D Fourier Transforms

Yao WangPolytechnic University Brooklyn NY 11201Polytechnic University, Brooklyn, NY 11201

With contribution from Zhu Liu, Onur Guleryuz, andGonzalez/Woods, Digital Image Processing, 2ed

Lecture Outline• Continuous Fourier Transform (FT)

– 1D FT (review)– 2D FT

• Fourier Transform for Discrete Time Sequence (DTFT)(DTFT)– 1D DTFT (review)– 2D DTFT

Li C l ti• Linear Convolution– 1D, Continuous vs. discrete signals (review)– 2D

• Filter Design• Computer Implementation

Yao Wang, NYU-Poly EL5123: Fourier Transform 2

What is a transform?• Transforms are decompositions of a function f(x)

into some basis functions Ø(x, u). u is typicallyinto some basis functions Ø(x, u). u is typically the freq. index.

Yao Wang, NYU-Poly EL5123: Fourier Transform 3

Illustration of Decomposition Φ3

f

α3

f = α1Φ1+α2Φ2+α3Φ3

Φ2o

α1

α2

Yao Wang, NYU-Poly EL5123: Fourier Transform 4

Φ1

Decomposition

• Ortho-normal basis function 1

21

2121 ,0

,1),(*),(

uuuu

dxuxux

• Forward

dff )()()()()( *Projection of

Inverse

dxuxxfuxxfuF ),()(),(),()( *Projection of f(x) onto (x,u)

• Inverse

duuxuFxf ),()()( Representing f(x) as sum of (x,u) for all u, with weight

Yao Wang, NYU-Poly EL5123: Fourier Transform 5

F(u)

Fourier Transform

• Basis function

.,,),( 2 ueux uxj

• Forward Transform

dffFF uxj 2)()}({)(

Inverse Transform

dxexfxfFuF uxj 2)()}({)(

• Inverse Transform

dueuFuFFxf uxj 21 )()}({)(

Yao Wang, NYU-Poly EL5123: Fourier Transform 6

Important Transform Pairs

fFf

uuFxfxfj

)()()(

)()(1)(2 0

fufuuFxfxf

fuuFexf xfj

)()(21)()2cos()(

)()()(

000

02 0

fufuj

uFxfxf )()(21)()2sin()( 000

t

uxxu

uxuFotherwise

xxxf

)sin(

)2sinc(2)2sin()(,0,1

)( 0000

tttwhere

)sin()sinc(,

Yao Wang, NYU-Poly EL5123: Fourier Transform 7

Derive the last transform pair in class

FT of the Rectangle Function

tttwhereuxx

uuxuF

)sin()sinc(,)2sinc(2)2sin()( 00

0

f(x) x0=1 f(x) x0=2

x1-1 x2-2

Yao Wang, NYU-Poly EL5123: Fourier Transform 8

Note first zero occurs at u0=1/(2 x0)=1/pulse-width, other zeros are multiples of this.

IFT of Ideal Low Pass Signal • What is f(x)?

F(u)

uu0-u0

Yao Wang, NYU-Poly EL5123: Fourier Transform 9

Representation of FT• Generally, both f(x) and F(u) are complex• Two representations

– Real and Imaginary– Magnitude and Phase

)(j

)()()( ujIuRuF

)()(tan)(,)()()(

,)()(

122

)(

uRuIuuIuRuA

whereeuAuF uj

F(u)II(u)

• Relationship

)(uR

)(i)()()()()( AIAR

RR(u)Φ(u)

• Power spectrum)(sin)()(),(cos)()( uuAuIuuAuR

2*2 )()()()()( uFuFuFuAuP

Yao Wang, NYU-Poly EL5123: Fourier Transform 10

)()()()()( uFuFuFuAuP

What if f(x) is real?• Real world signals f(x) are usually real• F(u) is still complex but has special properties• F(u) is still complex, but has special properties

)()(* uFuF

functionodd:)()()()(functioneven :)()(),()(),()(

)()(

uuuIuIuPuPuAuAuRuR

uFuF

function odd:)()(),()( uuuIuI

Yao Wang, NYU-Poly EL5123: Fourier Transform 11

Property of Fourier Transform• Duality

)()()()(uftF

uFtf

• Linearity

S li )}({)}({)()( 22112211 xfFaxfFaxfaxfaF

• Scaling

• Translation )}({)( xfaFxafF

• Translation

• Convolution dff )()()()(

)()(,)()( 022

000 uuFexfeuFxxf xujuxj

dgxfxgxf )()()()(

)()()()( uGuFxgxf

Yao Wang, NYU-Poly EL5123: Fourier Transform 12

We will review convolution later!

Two Dimension Fourier Transform• Basis functions

);( 22)22( vueeevuyx vyjuxjvyuxj

• Forward – Transform

j )(2

.,,,),;,( )( vueeevuyx yjjyj

• Inverse – Transform

dxdyeyxfyxfFvuF vyuxj )(2),()},({),(

P t

dudvevuFvuFFyxf vyuxj )(21 ),()},({),(

• Property– All the properties of 1D FT apply to 2D FT

Yao Wang, NYU-Poly EL5123: Fourier Transform 13

Example 1

4sin}4{sin

6cos4sin),()(2

dxdyxexF

yxyxfvyuxj

f(x,y)

)(4sin

4sin2

22

vdxxe

dyedxxeuxj

vyjuxj

)())2()2((21

vuu

j

0

)),2(),2((21

yx

vuvuj

uF(u,v)

))3()3((1}6{cos

,00,

)()(),(

vuvuyFLikewise

otherwiseyx

yxyxwhere

v

Yao Wang, NYU-Poly EL5123: Fourier Transform 14

))3,()3,((2

}6{cos, vuvuyFLikewise

Example 2

21)32sin(),(

)(2)32()32(

)32()32(

dxdyeeeF

eej

yxyxf

yvxujyxjyxj

yxjyxj

)31()3()1(

2322

vuvu

dyeedxee

dxdyeeeFyvjyjuxjxj

[X Y]=meshgrid( 2:1/16:2 2:1/16:2);

)23,1(,

)2

,1()2

()1(

)32(

h f

vueFLikewise

vuvu

yxj

[X,Y]=meshgrid(-2:1/16:2,-2:1/16:2);f=sin(2*pi*X+3*pi*Y);imagesc(f); colormap(gray)Truesize, axis off;

)

23,1()

23,1(

21)32sin(

,

vuvuj

yxF

Therefore

u

F(u,v)

v

Yao Wang, NYU-Poly EL5123: Fourier Transform 15

Important Transform Pairs

),(),(21)22sin( yxyxyx fvfufvfuj

yfxf

),(),(21)22cos(

2

yxyxyx fvfufvfuyfxf

j

2D rectangular function 2D sinc functiong

Yao Wang, NYU-Poly EL5123: Fourier Transform 16

Properties of 2D FT (1)

• Linearity )}({)}({)()( yxfFayxfFayxfayxfaF

• Translation )},({)},({),(),( 22112211 yxfFayxfFayxfayxfaF

),(),(

,),(),(

00)(2

)(200

00

00

vvuuFeyxf

evuFyyxxfyvxuj

vyuxj

• Conjugation),(),( 00 vvuuFeyxf

** ),(),( ** vuFyxf

Yao Wang, NYU-Poly EL5123: Fourier Transform 17

Properties of 2D FT (2)

• Symmetry)()()( vuFvuFrealisyxf

• Convolution

),(),(),( vuFvuFrealisyxf

– Definition of convolution

ddgyxfyxgyxf ),(),(),(),(

– Convolution theory

ddgyxfyxgyxf ),(),(),(),(

),(),(),(),( vuGvuFyxgyxf

Yao Wang, NYU-Poly EL5123: Fourier Transform 18

We will describe 2D convolution later!

Separability of 2D FT and Separable SignalSignal

• Separability of 2D FT)}}({{)}}({{)}({ yxfFFyxfFFyxfF

– where Fx, Fy are 1D FT along x and y.

)}},({{)}},({{)},({2 yxfFFyxfFFyxfF yxxy

y– one can do 1DFT for each row of original image, then

1D FT along each column of resulting image• Separable Signal• Separable Signal

– f(x,y) = fx(x)fy(y)– F(u,v) = Fx(u)Fy(v),( ) x( ) y( )

• where Fx(u) = Fx{fx(x)}, Fy(u) = Fy{fy(y)}– For separable signal, one can simply compute two 1D

transforms!

Yao Wang, NYU-Poly EL5123: Fourier Transform 19

transforms!

Example 1

)2/3()2/3(1)()3sin()(

)5cos()3sin(),(

uuuFxxf

yxyxf

xx

)()()(

)2/5()2/5(21)()5cos()(

)()(2

)()()(

vFyuFxvuF

vvvFyyf

jf

yy

xx

)25,

23()

25,

23()

25,

23()

25,

23(

41

)()(),(

vuvuvuvuj

vFyuFxvuF

u

v

Yao Wang, NYU-Poly EL5123: Fourier Transform 20

Example 2

,0||,||,1

),( 00

otherwiseyyxx

yxf

)2sinc()2sinc(4),(,

0000 vyuxyxvuF

u

x

2

2

vy1 1

x0 = 2y0 = 1

y

-2

-1 1

Yao Wang, NYU-Poly EL5123: Fourier Transform 21

2

w/ logrithmic mapping

Rotation

• Let2D FT in polar coordinate (r θ) and (ρ Ø)

.sin,cos,sin,cos vuryrx

• 2D FT in polar coordinate (r, θ) and (ρ, Ø)

rdrderfF rrj2 )sinsincoscos(2),(),(

rdrderf

frj )cos(2

0 0

),(

),(),(

• Property),(),( 00 Frf

Yao Wang, NYU-Poly EL5123: Fourier Transform 22

Example of Rotation

Yao Wang, NYU-Poly EL5123: Fourier Transform 23

Fourier Transform For Discrete Time Sequence (DTFT)Sequence (DTFT)

• One Dimensional DTFTf(n) is a 1D discrete time sequence– f(n) is a 1D discrete time sequence

– Forward TransformF( ) i i di i ith i d

I T f

n

unjenfuF 2)()(F(u) is periodic in u, with period of 1

– Inverse Transform

2/1 2)()( dueuFnf unj 2/1

)()( dueuFnf

Yao Wang, NYU-Poly EL5123: Fourier Transform 24

Properties unique for DTFT

• PeriodicityF(u) = F(u+1)– F(u) = F(u+1)

– The FT of a discrete time sequence is only considered for u є ( ½ ½) and u = +½ is theconsidered for u є (-½ , ½), and u = +½ is the highest discrete frequency

• Symmetry for real sequences• Symmetry for real sequences

FFuFuFnfnf

)()()()()()( **

symmetricisuF

uFuF

)(

)()(

Yao Wang, NYU-Poly EL5123: Fourier Transform 25

Example

0;1,...,1,0,1

)(others

Nnnf

)2/(2sin1)(

,0

)1(2

212 NueeeuF

others

uNjj

uNjNnuj

)2/1(2sin1)( 2

0 ue ujn

f(n)

n0 N-1 N=10

Yao Wang, NYU-Poly EL5123: Fourier Transform 26

There are N/2 zeros in (0, ½], 1/N apart

Two Dimensional DTFT• Let f(m,n) represent a 2D sequence• Forward Transform• Forward Transform

m n

nvmujenmfvuF )(2),(),(

• Inverse Transform m n

2/1 2/1

• Properties

2/1

2/1

2/1

2/1

)(2),(),( dudvevuFnmf nvmuj

Properties– Periodicity, Shifting and Modulation, Energy

Conservation

Yao Wang, NYU-Poly EL5123: Fourier Transform 27

Periodicity

nvmujenmfvuF )(2),(),(

• F(u,v) is periodic in u, v with period 1, i.e.,

m n

for all integers k, l:– F(u+k, v+l) = F(u, v)

• To see this consider))()((2 lvnkumje

)(2

)(2)(2

nvmuj

nlmkjnvmuj

eee

Yao Wang, NYU-Poly EL5123: Fourier Transform 28

)(jeIn MATLAB, frequency scaling is such that 1 represents maximum freq u,v=1/2.

Illustration of Periodicity u

1.0

Low frequencies

0.5

High frequencies

v-0.5 0.5 1.0-1.0 0

Yao Wang, NYU-Poly EL5123: Fourier Transform 29

Fourier Transform Types

Low Pass High Pass Band Pass

u

0.5

u

0.5

u

0.5

0.5-0.5 v0.5-0.5 v

0.5-0.5 v

-0.5 -0.5 -0.5

Non-zero frequency components

Yao Wang, NYU-Poly EL5123: Fourier Transform 30

Shifting and Modulation• Shifting

),()},({

))()((2

)(20000 ennmmfnnmmfF

lkj

m n

nvmuj

)(

),(,

)(2)(2

))()((200

00

lkf

elkfnnlmmk

lvkujvnumj

k l

vnlumkj

),(),(

),(

)(200

)(2)(2

00

00

vuFennmmf

elkfe

vnumjk l

lvkujvnumj

• Modulation

),(),( 00f

)()()(2 fnvmuj

Yao Wang, NYU-Poly EL5123: Fourier Transform 31

),(),( 00)(2 00 vvuuFnmfe nvmuj

Energy Conservation

• Inner Product

ff )()( *

dudvevuGnmf

nmgnmfgf

nvmuj

m n

),(),(

),(),(,

5.0 5.0 )(2*

*

dudvvuGenmf

f

nvmuj

m n

),(),(

),(),(

5.0

5.0

5.0

5.0

*)(2

5.0 5.0

Energy Conservation

GFdudvvuGvuF

m n

,),(),(5.0

5.0

5.0

5.0

*

• Energy Conservation

5.0

5.0

5.0

5.0

22 ),(),( dudvvuFnmf

Yao Wang, NYU-Poly EL5123: Fourier Transform 32

m n

Delta Function

• Fourier transform of a delta function

1)(

1),(),( )(2

enmvuFm n

nvmuj

• Inverse Fourier transform of a delta

1),( nm

Inverse Fourier transform of a delta function

50 50

),(1

1),(),(5.0

5.0

5.0

5.0

)(2

vu

dudvevunmf nvmuj

Yao Wang, NYU-Poly EL5123: Fourier Transform 33

),(

Example

000121

)( nmf

mf(m,n)

121

000),( nmfn

121121),(

)*1*1(2)*0*1(2)*1*1(2

)*1*1(2)*0*1(2)*1*1(2

eeevuFvujvujvuj

vujvujvuj

2sin22sin42sin2121

22

)11(2)01(2)11(2

uejujuejeee

vjvj

vujvujvuj

)12(cos2sin4 vuj

Note: This signal is low pass in the horizontal direction,

Yao Wang, NYU-Poly EL5123: Fourier Transform 34

g p ,and band pass in the vertical direction.

Graph of F(u,v)

du = [-0.5:0.01:0.5];d [ 0 5 0 01 0 5]20 dv = [-0.5:0.01:0.5];Fu = abs(sin(2 * pi * du));Fv = cos(2 * pi * dv);F = 4 * Fu' * (Fv + 1);mesh(du, dv, F);

204060 Using MATLAB freqz2:

Yao Wang, NYU-Poly EL5123: Fourier Transform 35

( , , );colorbar;Imagesc(F);colormap(gray); truesize;

20 40 60 80 100

8000

f=[1,2,1;0,0,0;-1,-2,-1];

freqz2(f)

Linear Convolution

• Convolution of Continuous Signals1D convolution– 1D convolution

dxhfdhxfxhxf )()()()()()(

• Equalities

)()()()()()( xxfxxxfxfxxf

– 2D convolution)()()(),()()( 00 xxfxxxfxfxxf

ddyxhf

ddhyxfyxhyxf

)()(

),(),(),(),(

Yao Wang, NYU-Poly EL5123: Fourier Transform 36

ddyxhf ),(),(

Examples of 1D Convolutionf(α)h(x-α)f(x)

1h(x-α)

αx0x10

h(x)

αxx-1(1) 0 ≤ x < 1

f(α)h(x-α)

α0x10

1 h(x-α)

α1 αx-10 110 αxx-1(2) 1 ≤ x < 2

h(-α)

x

f(x)*h(x)1

x10

1

α

Yao Wang, NYU-Poly EL5123: Fourier Transform 37

x1 2

Example of 2D Convolution

1

y

f(x,y)1

y

βh(x-α,y-β) 1y

βh(x-α,y-β)

11 x

y

1x

(1) 0<x≤1 0<y≤1

1x α

(2) 0<x≤1 1<y≤2

y-1

1h(x,y)

y (1) 0<x≤1, 0<y≤1g(x,y)=x*y

(2) 0<x≤1, 1<y≤2g(x,y)=x*(2-y)

β β1 x

1y α

βh(x-α,y-β)

1y

α

β h(x-α,y-β)

y-1f(x,y)*h(x,y)2y

1x-1

α

(3) 1<x≤2, 0<y≤1( ) (2 )*

x1x-1

α

(4) 1<x≤2, 1<y≤2g(x y)=(2 x)*(2 y)

x2

Yao Wang, NYU-Poly EL5123: Fourier Transform 38

g(x,y)=(2-x)*y g(x,y)=(2-x) (2-y)2 x

Convolution of 1D Discrete Signals• Definition of convolution

hfhfhf )()()()()(*)(

m m

mnhmfmhmnfnhnf )()()()()(*)(

• The convolution can be considered as the weighted average:– sample n-m is multiplied by h(m)

• Filtering: f(n) is the signal, and h(n) is the filter– Finite impulse response (FIR) filterp p ( )– Infinite impulse response (IIR) filter

• If the range of f(n) is (n0, n1), and the range of h(n) is (m0 m1) then the range of f(n)*h(n) is (n0+m0 n1+m1)

Yao Wang, NYU-Poly EL5123: Fourier Transform 39

(m0, m1), then the range of f(n) h(n) is (n0+m0, n1+m1)

Example of 1D Discrete Convolution

f(n)h(n-m)

0 3 n

h(n)

nn-5 m0(a) n < 0, g(n) = 0

h(n)

0 5 n h(n-m)

0n-5 mnh(-m) m

(b) 0 ≤ n ≤ 8, g(n) > 0

h(n-m)0-5 m

n n-5 m0(c) n > 8 g(n) = 0

f(n)*h(n)

Yao Wang, NYU-Poly EL5123: Fourier Transform 40

(c) n > 8, g(n) 00 8 n

Convolution of 2D Discrete Signals• Definition of convolution

lkhlkfhf )()()(*)(

k l

lnkmhlkf

lkhlnkmfnmhnmf

)()(

),(),(),(*),(

• Weighted average:

k l

lnkmhlkf ),(),(

Weighted average: – Pixel (m-k,n-l) is weighted by h(k,l)

• Range– If the range of f(m n) is m0 ≤ m ≤ m1 n0 ≤ n ≤ n1If the range of f(m, n) is m0 ≤ m ≤ m1, n0 ≤ n ≤ n1– If the range of h(m, n) is k0 ≤ m ≤ k1, l0 ≤ n ≤ l1,– Then the range of f(m, n)*h(m, n) is

m0 + k0 ≤ m ≤ m1 + k1, n0 + l0 ≤ n ≤ n1 + l1

Yao Wang, NYU-Poly EL5123: Fourier Transform 41

Example of 2D Discrete Convolutionm

1f(m,n)

k

1f(k,l)h(-1-k, -2-l)

n l

f(k l)h(2 k 1 l)k

1h(m,n)

m

l

1f(k,l)h(2-k,1-l)

n

k

l

f(m,n)*h(m,n)1

k

l

1h(-k,-l)

l

1

3

2

Yao Wang, NYU-Poly EL5123: Fourier Transform 42

l l

• Go through an example in class for an arbitrary 3x3 (non-symmetric) filterarbitrary 3x3 (non-symmetric) filter

Yao Wang, NYU-Poly EL5123: Fourier Transform 43

Separable Filtering

• A filter is separable if h(x, y)=hx(x)hy(y) or h(m n)=h (m)h (n)h(m, n)=hx(m)hy(n).

• Matrix representationThhH

– Where hx and hy are column vectors

TyxhhH

• Example

1101

TyxhhH

101

12

101202

Yao Wang, NYU-Poly EL5123: Fourier Transform 44

DTFT of Separable Filter• Previous example

TyxhhH

101121

101202101

• Recognizing that the filter is separable

vjeevFyujuj

vjvj

22221)(121

2sin2)1(01)(10122

22

vujvFyuFxvuF

ueeuFx ujuj

2sin)2cos1(4)()(),(2cos2221)(121 22

Yao Wang, NYU-Poly EL5123: Fourier Transform 45

Separable Filtering• If H(m,n) is separable, the 2D convolution can

be accomplished by first applying 1D filteringbe accomplished by first applying 1D filtering along each row using hy(n), and then applying 1D filtering to the intermediate result along each column using the filter hx(n)

• Proof )()(),(),(*),( lhkhlnkmfnmhnmf yx

)()(),( khlhlnkmf xk l

y

k ly

)(*))(*)((

)(),(

hhf

khnkmg xk

y

k l

Yao Wang, NYU-Poly EL5123: Fourier Transform 46

)(*))(*),(( mhnhnmf xy

• Go through an example of filtering using separable filter in classseparable filter in class

Yao Wang, NYU-Poly EL5123: Fourier Transform 47

Computation Cost

• SupposeThe size of the image is M*N– The size of the image is M N.

– The size of the filter is K*L.N bl filt i• Non-separable filtering– Each pixel: K*L mul; K*L – 1 add.– Total: M*N*K*L mul; M*N*(K*L-1) add.– When M=N, K=L

• M2K2 mul + M2(K2-1) add.

Yao Wang, NYU-Poly EL5123: Fourier Transform 48

Computation Cost• Separable:

– Each pixel in a row: L mul; L-1 add.Each pixel in a row: L mul; L 1 add.– Each row: N*L mul; N*(L-1) add.– M rows: M*N*L mul; M*N*(L-1) add.– Each pixel in a column: K mul; K-1 add.– Each column: M*K mul; M*(K-1) add.– N columns: N*M*K mul; N*M*(K-1) add.– Total: M*N*(K+L) mul; M*N*(K+L-2) add.

When M N K L– When M=N, K=L:• 2M2K mul; 2M2(K-1) add.• Significant savings if K (and L) is large!

Yao Wang, NYU-Poly EL5123: Fourier Transform 49

Boundary Treatment• When assuming the

image values outside a given image are zero, the filtered values in the boundary are affected byboundary are affected by assumed zero values adverselyF b tt lt

200 200 205 203 203

200 200 205 203 203• For better results, use

symmetric extension (mirror image) to fill the

195 195 200 200 200

200 200 205 195 195( g )outside image values

200 200 205 195 195

200 200 205 195 195

Yao Wang, NYU-Poly EL5123: Fourier Transform 50

Actual image pixels Extended pixels

Computer implementation of convolution• Boundary treatment

– An image of size MxN convolving with a filter of size KxL will yield an image of size (M+K-1,N+L-1)

– If the filter is symmetric, the convolved image should have extra boundary of thickness K/2 on each side

– Filtered values in the outer boundary of K/2 pixels depend on the extended pixel values

– For simplicity, we can ignore the boundary problem, and process only the inner rows and columns of the image, leaving the outer K/2 rows and L/2 columns unchanged (if filter is low-pass) or as zero (if filter is high-pass)

R li ti• Renormalization:– The filtered value may not be in the range of (0,255) and may not be

integers– Use two-pass operationp p

• First pass: save directly filtered value in an intermediate floating-point array• Second pass: find minimum and maximum values of the intermediate image,

renormalize to (0,255) and rounding to integers– F= round((F1-fmin)*255/(fmax-fmin))

T di l th li d i di tl “i ( )” f ti

Yao Wang, NYU-Poly EL5123: Fourier Transform 51

• To display the unnormalized image directly, use “imagesc( )” function

Sample Matlab Program% readin bmp filex = imread('lena.bmp');[xh xw] = size(x);

% define 2D filterh = ones(5,5)/25;[hh hw] = size(h);

y = double(x);

% linear convolution, assuming the filter is non-separable (although this example filter is separable)z = y; %or z=zeros(xh xw) if not low pass filter

hhh = (hh - 1) / 2;hhw = (hw- 1) / 2;

z = y; %or z=zeros(xh,xw) if not low-pass filterfor m = hhh + 1:xh - hhh,

%skip first and last hhh rows to avoid boundary problemsfor n = hhw + 1:xw - hhw,

%skip first and last hhw columns to avoid boundary problemstmpy = 0;for k = -hhh:hhh,

for l = -hhw:hhw,tmpv = tmpv + y(m - k,n – l)* h(k + hhh + 1, l + hhw + 1);

%h(0 0) is stored in h(hhh+1 hhw+1)%h(0,0) is stored in h(hhh+1,hhw+1)end

endz(m, n) = tmpv;

%for more efficient matlab coding, you can replace the above loop with

Yao Wang, NYU-Poly EL5123: Fourier Transform 52

z(m,n)=sum(sum(y(m-hhh:m+hhh,n-hhw:n+hhw).*h))end

end

Results

111111111111111

251h

1111111111

25

f(m,n) g(m,n)

Yao Wang, NYU-Poly EL5123: Fourier Transform 53

• What if the filter is symmetric? Can you write a different MATLAB code that haswrite a different MATLAB code that has reduced computation time?MATLAB built in functions• MATLAB built-in functions– 1D filtering: conv– 2D filtering: conv2– Computing DTFT: freqz, freqz2

Yao Wang, NYU-Poly EL5123: Fourier Transform 54

Convolution Theorem

• Convolution TheoremHFhfHFhf **

• ProofHFhfHFhf *,*

),(),(),(*),(),( lkhlnkmfnmhnmfnmgk l

),(),(),( )(2elkhlnkmfvuGsidesbothonFT

nm

nvmuj

lk

k l

)()(

),(),(

)(2))()((2

,

)(2

,

))()((2

, ,

elkhelnkmf

elkhelnkmf

lvkujvlnukmj

nm

lvkuj

lk

vlnukmj

nm lk

),()','(

),(),(

',' ,

)(2)''(2

, ,

elkhenmf

elkhelnkmf

nm lk

lvkujvnumj

nm lk

Yao Wang, NYU-Poly EL5123: Fourier Transform 55

),(),( vuHvuF

Explanation of Convolution in the Frequency Domainin the Frequency Domain

f(x)h(x)

g(x)=f(x)*h(x)

x∆/2-∆/2∆/4-∆/4 x x

F(u) H(u)

∆/4/

G(u)=F(u)H(u)( ) H(u)

u1/∆-1/∆u2/∆-2/∆ u1/∆-1/∆

Yao Wang, NYU-Poly EL5123: Fourier Transform 56

Example• Given a 2D filter, show the frequency response. Apply to a given

image, show original image and filtered image in pixel and freq. domaindomain

1111111111

1

111111111111111

251h

Yao Wang, NYU-Poly EL5123: Fourier Transform 57

111111111111111

1h

111111111111111

25h

Yao Wang, NYU-Poly EL5123: Fourier Transform 58

Matlab Program Used

x = imread('lena256.bmp');figure(1); imshow(x);f = double(x);ff=abs(fft2(f));figure(2); imagesc(fftshift(log(ff+1))); colormap(gray);truesize;axis off;figure(2); imagesc(fftshift(log(ff+1))); colormap(gray);truesize;axis off;h = ones(5,5)/9;hf=abs(freqz2(h));figure(3);imagesc((log(hf+1)));colormap(gray);truesize;axis off;y = conv2(f, h);figure(4);imagesc(y);colormap(gray);truesize;axis off;yf=abs(fft2(y));figure(5);imagesc(fftshift(log(yf+1)));colormap(gray);truesize;axis off;figure(5);imagesc(fftshift(log(yf+1)));colormap(gray);truesize;axis off;

Yao Wang, NYU-Poly EL5123: Fourier Transform 59

000111

1H

111000

31H

Yao Wang, NYU-Poly EL5123: Fourier Transform 60

Filter Design• Given desired frequency response of the filter

Hd(u,v)d• Perform an inverse transform to obtain the

desired impulse response hd(m,n).• When Hd(u,v) is band limited, hd(m,n) is infinitely

long• Truncate h (m n) to yield a realizable h(m n)• Truncate hd(m,n) to yield a realizable h(m,n)• Will distort the original frequency response• Better approach is to apply a well designed• Better approach is to apply a well designed

window function over the specified frequency response.

Yao Wang, NYU-Poly EL5123: Fourier Transform 61

Use of Window Function in Filter Design

Hd(u) H (u) Hw(u)

u u uu u u

hd(x) h (x)windowfunction

w(x)

hw (x)

x

… …

x x

Yao Wang, NYU-Poly EL5123: Fourier Transform 62

XxXxxw 0),2cos(46.054.0)( Hamming window

Homework 1. Let

Fi d h l d i l ( ) f( ) * h( ) f h f ll i

xyyfxfyxhyxfyxf cc

20)2sin()2sin(),(),(2sin),(

Find the convolved signal g(x, y) = f(x, y) * h(x, y) for the following two cases:

a) f0/2 < fc < f0; and b) f0 < fc < 2f0.

Hint: do the filtering in the frequency domain. Explain what happened by sketching the original signal, the filter, the convolution process and the convolved signal in the frequency domain.

2. Repeat the previous problem for

otherwise

fyx

ffyxh cc

c

,021},{

21,),(

2

Yao Wang, NYU-Poly EL5123: Fourier Transform 63

Homework (cntd)3. For the three filters given below (assuming the origin is at the center):

a) find their Fourier transforms (2D DTFT);b) sketch the magnitudes of the Fourier transforms You should sketch byb) sketch the magnitudes of the Fourier transforms. You should sketch by hand the DTFT as a function of u, when v=0 and when v=1/2; also as a function of v, when u=0 or ½. Also please plot the DTFT as a function of both u and v, using Matlab plotting function.g p gc) Compare the functions of the three filters.

In your calculation, you should make use of the separable property of the filter whenever appropriate. If necessary, split the filter into several additive terms such that each term can be calculated more efficiently.

121

1121

1111

1

1212122

241

1212122

241

111111

91

321 HHH

Yao Wang, NYU-Poly EL5123: Fourier Transform 64

Programming Assignment1. Write a Matlab or C-program for implementing filtering of a gray scale

image. Your program should allow you to specify the filter with an arbitrary size (in the range of k0->k1, l0->l1), read in a gray scale image, and save the filtered image into another file For simplicity ignore theand save the filtered image into another file. For simplicity, ignore the boundary effect. That is, do not compute the filtered values for pixels residing on the image boundary. If the filter has a size of M x N, the boundary pixels are those in the top and bottom M/2 rows, and left and right most N/2 columns. You should properly normalize the filtered image g p p y gso that the resulting image values can be saved as 8-bit unsigned characters. Apply the filters given in the previous problem to a test image. Observe on the effect of these filters on your image. Note: you cannot use the MATLAB conv2( ) function. In your report, include your MATLAB code the original test image and the images obtained with the threecode, the original test image and the images obtained with the three filters. Write down your observation of the effect of the filters.

2. Optional: Now write a convolution program assuming the filter is separable. Your program should allow you to specify the horizontal and vertical filters and call a 1D convolution sub-program to accomplish thevertical filters, and call a 1D convolution sub program to accomplish the 2D convolution. Note: you cannot use the MATLAB conv( ) function.

3. Learn the various options of conv2( ) function in MATLAB using online help manual.

Yao Wang, NYU-Poly EL5123: Fourier Transform 65

Reading

• Gonzalez and Woods, Sec. 4.1 and 4.2, 4 3 4 3 5 3 54., 3.4, 3.5, 3.5

Yao Wang, NYU-Poly EL5123: Fourier Transform 66

Recommended