Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

37
Discrete Fourier Transform Nuno Vasconcelos UCSD

Transcript of Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Page 1: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform

Nuno VasconcelosUCSD

Page 2: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Fourier Transforms• we started by considering the Discrete-Space Fourier

Transform (DSFT)• the DSFT is the 2D extension of the Discrete-Time

Fourier Transform

• note that this is a continuous function of frequency– inconvenient to evaluate numerically in DSP hardware– we need a discrete version– this is the 2D Discrete Fourier Transform (2D-DFT)

2121221

2121

2211

2211

1 2

),()2(

1],[

],[),(

ωωωωπ

ωω

ωω

ωω

ddeeXnnx

eennxX

njnj

njnj

n n

∫∫

∑∑

=

= −−

2

Page 3: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

2D-DFT• the 2D-DFT is obtained by sampling the DSFT at regular

frequency intervals

• this turns out to make the 2D-DFT harder to work with than the DSFT– because we are sampling in frequency we have aliasing in space– this means that, even though the sequence x[n1,n2] is finite, we

are effectively working with a periodic sequence– the DFT therefore inherits all the properties of the frequency

representations of periodic sequences

• it is better understood by first considering the 2D Discrete Fourier Series (2D-DFS)

22

211

12,22121 ),(],[

kN

kN

XkkX πωπωωω

===

3

Page 4: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

2D-DFS• it is the natural representation for a periodic sequence• a sequence x[n1,n2] is periodic of period N1xN2 if

• note that

• makes no sense for a periodic signal– the sum will be infinite for any pair r1,r2

– neither the 2D DSFT or the Z-transform will work here

[ ] [ ][ ] 21221

21121

, ,, ,,

nnNnnxnNnxnnx

∀+=+=

∑ ∑∞

−∞=

−∞=

−−=1 2

21212121 ],[),(

n n

jnjn rrnnxrrX

4

Page 5: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

2D-DFS• the 2D-DFS solves this problem

• note that X[k1,k2] is also periodic outside

• like the DSFT, – properties of the 2D-DFS are identical to those of the 1D-DFS– with the straightforward extension of separability

( ) [ ]

[ ] [ ] 222

111

1

1

2

2

222

111

1

1

2

2

221

0

1

021

2121

221

0

1

02121

,1,

,,

nkN

jnkN

jN

k

N

k

nkN

jnkN

jN

n

N

n

eekkXNN

nnx

eennxkkX

ππ

ππ

∑∑

∑∑−

=

=

−−−

=

=

=

=

10 ,10 2211 −≤≤−≤≤ NkNk

5

Page 6: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Periodic convolution• like the Fourier transform,

– the inverse transform of multiplication is convolution

– however, we have to be careful about how we define convolution– since the sequences have no end, the standard definition

makes no sense– e.g. if x and h are both positive sequences, this will always be

infinite

[ ] [ ] ( ) ( )2121

2121 ,,,, kkYkkXnnxnnxDFS

×↔∗

],[],[],[ 221121211 2

knknhkkxnnyk k

−−= ∑ ∑∞

−∞=

−∞=

6

Page 7: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Periodic convolution• to deal with this, we introduce the idea of periodic

convolution• instead of the regular definition

• which, from now on, we refer to as linear convolution• periodic convolution only considers one period of our

sequences

• the only difference is in the summation limits

],[ ],[* 2211211 2

knknhkkxyxk k

−−= ∑ ∑∞

−∞=

−∞=

],[ ],[ 2211

1

0

1

021

1

1

2

2

knknhkkxhxN

k

N

k−−= ∑ ∑

=

=

o

7

Page 8: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Periodic convolution

8

• note that the sequence which results from the convolution is also periodic

• it is important to remember the following– we work with a single period (the

fundamental period) to make things manageable– but remember that we have periodic sequences– it is like if we were peeking through a window– if we shift, or flip the sequence we need to remember that – the sequence does not simply move out of the window, but the

next period walks in!!!– note, that this

can make thefundamentalperiod changeconsiderably

k1

k2

k1

k2

shiftby

(1,1)

Page 9: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• the DFT is defined as

(here X(ω1,ω2) is the DSFT) which can be written as

22

211

12,22121 ),(],[

kN

kN

XkkX πωπωωω

===

[ ] [ ]

[ ] [ ]⎪⎩

⎪⎨

<≤<≤

=

⎪⎩

⎪⎨

<≤<≤

=

∑∑

∑∑

=

=

−−−

=

=

otherwiseNnNn

eekkXNNnnx

otherwiseNkNk

eennxkkX

nkN

jnkN

jN

k

N

k

nkN

jnkN

jN

n

N

n

000

,1,

000

,,,

22

11221

0

1

021

2121

22

11221

0

1

021

21

222

111

1

1

2

2

222

111

1

1

2

2

ππ

ππ

9

Page 10: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• comparing this

with the DFS

[ ] [ ]

[ ] [ ]⎪⎩

⎪⎨

<≤<≤

=

⎪⎩

⎪⎨

<≤<≤

=

∑∑

∑∑

=

=

−−−

=

=

otherwiseNnNn

eekkXNNnnx

otherwiseNkNk

eennxkkX

nkN

jnkN

jN

k

N

k

nkN

jnkN

jN

n

N

n

000

,1,

000

,,,

22

11221

0

1

021

2121

22

11221

0

1

021

21

222

111

1

1

2

2

222

111

1

1

2

2

ππ

ππ

[ ] [ ]

[ ] [ ] 222

111

1

1

2

2

222

111

1

1

2

2

221

0

1

021

2121

221

0

1

02121

,1,

,,

nkN

jnkN

jN

k

N

k

nkN

jnkN

jN

n

N

n

eekkXNN

nnx

eennxkkX

ππ

ππ

∑∑

∑∑−

=

=

−−−

=

=

=

=

10

Page 11: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• we see that inside the boxes

the two transforms are exactly the same• if we define the indicator function of the box

• we can write

22

11

00

NkNk

<≤<≤

22

11

00

NnNn

<≤<≤

[ ]⎪⎩

⎪⎨⎧

<≤<≤

otherwiseNnNn

nnR NN

000

,1,22

11

2121

[ ] [ ] [ ]212121 ,,,21

nnRnnxnnx NN ×= [ ] [ ] [ ]212121 ,,,21

kkRkkXkkX NN ×=

11

Page 12: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• note from

that working in the DFT domain is equivalent to– working in the DFS domain– extracting the fundamental period at the end

• we can summarize this as

• in this way, I can work with the DFT without having to worry about aliasing

[ ] [ ] [ ]212121 ,,,21

nnRnnxnnx NN ×= [ ] [ ] [ ]212121 ,,,21

kkRkkXkkX NN ×=

[ ] [ ] [ ] [ ]2121

2121 ,

,,

, kkXkkXnnxnnx

truncate

eperiodiciz

DFS

eperiodiciz

truncate←→

↔←→

12

Page 13: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform

• this trick can be used to derive all the DFT properties• e.g. what is the inverse transform of a phase shift?

– let’s follow the steps

– 1) periodicize: this causes the same phase shift in the DFS

[ ] [ ] [ ] [ ]2121

2121 ,

,,

, kkXkkXnnxnnx

truncate

eperiodiciz

DFS

eperiodiciz

truncate←→

↔←→

[ ] [ ] 222

111

22

2121 ,,mk

Njmk

Nj

eekkXkkYππ

−−

=

[ ] [ ] 222

111

22

2121 ,,mk

Njmk

Nj

eekkXkkYππ

−−

=

13

Page 14: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform

[ ] [ ] [ ] [ ]2121

2121 ,

,,

, kkXkkXnnxnnx

truncate

eperiodiciz

DFS

eperiodiciz

truncate←→

↔←→

– 2) compute the inverse DFS: it follows from the properties of the DFS (page 142 on Lim) that we get a shift in space

– 3) truncate: the inverse DFT is equal to one period of the shifted periodic extension of the sequence

– in summary, the new sequence is obtained by making the original periodic, shifting, and taking the fundamental period

[ ] [ ]221121 ,, mnmnxnny −−=

[ ] [ ] [ ]21221121 ,,,21

nnRmnmnxnny NN ×−−=

14

Page 15: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Example

• note that what leaves on one end, enters on the other

n1

n2

shift by (1,1)

n1

n2

periodicize

n1

n2

n1

n2

truncate

15

Page 16: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Example• for this reason it is called a circular shift

• note that this is way more complicated than in 1D• to get it right we really have to think in terms of the

periodic extension of the sequence• it shows up in most properties of the DFT, • e.g. what is the inverse DFT of the product of two DFTs?

n1

n2

n1

n2

circular shift by (1,1)

16

Page 17: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform

• we use our trick again

– 1) periodicize:

– 2) compute the inverse DFS: this is just the periodic convolution

[ ] [ ] [ ] [ ]2121

2121 ,

,,

, kkXkkXnnxnnx

truncate

eperiodiciz

DFS

eperiodiciz

truncate←→

↔←→

[ ] [ ] [ ]212121 ,,, kkHkkXkkY =

[ ] [ ] [ ]212121 ,,, kkHkkXkkY =

[ ] [ ] [ ]212121 ,,, nnynnxnny o=

17

Page 18: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform

[ ] [ ] [ ] [ ]2121

2121 ,

,,

, kkXkkXnnxnnx

truncate

eperiodiciz

DFS

eperiodiciz

truncate←→

↔←→

– 3) truncate: the inverse DFT is equal to one period of the periodic convolution of the sequences

– in summary, the new sequence is obtained by making the original sequences periodic, computing the periodic convolution,and taking the fundamental period

– this is the circular convolution of x[n1,n2] and h[n1,n2]

[ ] [ ] [ ]( ) [ ]21212121 ,,,,21

nnRnnhnnxnny NN ×= o

[ ] [ ] [ ] [ ]( ) [ ]2121212121 ,,,,,21

nnRnnhnnxnnhnnx NN ×=⊗ o

18

Page 19: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• we therefore have the property that

– the product of two DFTs is the– DFT of the circular convolution of the two sequences

• note that circular convolution = one period of periodic convolution

• hence, there is really not much that is new– periodicize the sequences, and apply what we learned for the

convolution of DFSs– e.g. n2

n1

n2

(1) (2)

],[ 21 nnx(3) (4)

],[ 21 nnh

n1 ⊗

19

Page 20: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• step 1): express sequences in terms of (k1, k2),

we next proceed exactly as for periodic convolution

k1

k2

k1

k2

(1) (2)

(3) (4)

],[ 21 kkx ],[ 21 kkh

[ ] [ ] [ ] [ ]( ) [ ]2121212121 ,,,,,21

nnRnnhnnxnnhnnx NN ×=⊗ o

20

Page 21: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• step 2): invert h(k1, k2)

k1

k2

(1)(2)

(3)(4)

],[],[ 2121 kkhkkg −−=

k1

k2

(1) (2)

(3) (4)

],[ 21 kkh

k1

k2

(1) (2)

(3) (4)

],[ 21 kkh −

1

2

],[ ],[ 2211

1

0

1

021

1

1

2

2

knknhkkxhxN

k

N

k

−−= ∑ ∑−

=

=

o

21

Page 22: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• step 3): shift g(k1, k2) by (n1, n2)

],[ ],[ 2211

1

0

1

021

1

1

2

2

knknhkkxhxN

k

N

k

−−= ∑ ∑−

=

=

o

k1

k2

(1)(2)

(3)(4)

k2

(1)(2)

(3)(4)

n

n2

this sendswhatever is at (0,0) to (n1,n2)

k11

],[],[

2211

2211

knknhnknkg−−

=−−

],[],[ 2121 kkhkkg −−=

22

Page 23: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• e.g. for (n1,n2) = (1,0)

but here we – recall that we are working with periodic sequences– use periodicity to fill values missing in the flipped sequence

k1

k2

(1)(2)

(3)(4)

n1

n2

k1

k2

],[ 21 kkx ],[ 2211 knknh −−

],[ ],[ 2211

1

0

1

021

1

1

2

2

knknhkkxhxN

k

N

k

−−= ∑ ∑−

=

=

o23

Page 24: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• step 4): we can finally point-wise multiply the two signals

and sum

– e.g. for (n1,n2) = (1,0)

k1

k2

],[ 21 kkx

x =k1

k2

],[ 21 nny

k1

k2

(1) (2)

(4)

],[ 2211 knknh −−

(3)3x1+1x1=4

],[ ],[ 2211

1

0

1

021

1

1

2

2

knknhkkxhxN

k

N

k

−−= ∑ ∑−

=

=

o

24

Page 25: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• finally, we extract the fundamental period

• note that the sequence never grows beyond our original window

• this is fundamentally different from linear convolution– it is the reason why we need to do circular shifts

• note that, because of this, it can be very different to– 1) convolve two signals– 2) take the DFTs, multiply, and take inverse DFT

• let’s see what happens on MATLAB

[ ] [ ] [ ] [ ]( ) [ ]2121212121 ,,,,,21

nnRnnhnnxnnhnnx NN ×=⊗ o

25

Page 26: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Circular convolution• >> x = [1 2 3; 3 3 1; 1 5 5]; h = [0 3 4; 5 2 1; 1 3 2]; z = conv2(x,h)• z =• 0 3 10 17 12• 5 21 41 23 7• 16 29 44 53 27• 8 39 52 24 7• 1 8 22 25 10

• >> H = fft2(h); X = fft2(x); Y = X.*H; y = ifft2(Y)• y =• 49 61 62• 54 46 63• 69 56 44

26

Page 27: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Fourier Transform• why do we care about the DFT?

– 1) we need a discrete representation of the frequency spectrumif we are to implement algorithms on computers

– the DSFT cannot be used for this because it is continuous– 2) there are very fast algorithms to compute the DFT

• in 1D DSP you may have mentioned the Fast Fourier Transform (FFT)– it is a fast algorithm to compute the DFT– if the sequence has N points, instead of O(N2) complexity, it has

O(N logN)– this has made a tremendous historical difference– FFT speedup = one or two generations of DSP hardware

• Q: is there a two dimensional FFT?

27

Page 28: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Fast Fourier Transform• to answer this we look at the expression of the DFT

• note that this can be computed with

• given n2, f[k1,n2] is the 1D DFT of x[n1,n2]– i.e. the 1D-DFT of row n2 of the sequence x

• we have seen something like this when we studied separability

[ ] [ ] 222

111

1

1

2

2

221

0

1

02121 ,,

nkN

jnkN

jN

n

N

neennxkkX

ππ−−−

=

=∑∑=

[ ] [ ]∑ ∑−

=

−−

=

⎪⎭

⎪⎬⎫

⎪⎩

⎪⎨⎧

=1

0

),(

21

021

2

21

2

2

21

111

1

1

222 ,,

N

n

nkf

nkN

jN

n

nkN

jennxekkX

444 3444 21

ππ

28

Page 29: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Fast Fourier Transform• the idea is to create an intermediate sequence f[k1,n2]

– whose rows are the DFTs of the rows of x

• next we realize that

k1

n2

n1

n2

],[ 21 nkf],[ 21 nnx

1D-DFT

[ ] [ ]∑−

=

=1

021

2

21

2

2

222 ,,

N

n

nkN

jnkfekkX

π

29

Page 30: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Fast Fourier Transform• is just the 1D DFT of column k1 of f[k1,n2]

• this means that the 2D-DFT can be computed with a sequence of 1D-DFTs

• note that THIS DOES NOT REQUIRE SEPARABILITY• this property is valid for any sequence• it has obvious implications on the computational

complexity of the DFT

k1

n2

k1

k2

],[ 21 nkf ],[ 21 kkX

1D-DFT

30

Page 31: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Fast Fourier Transform• note that the 2D-DFT requires

– N2 1D-DFTs on size N1

– followed by N1 1D-DFTs of size N2

– when these are implemented with the FFT, total complexity is

– i.e. we have the same type of expression as in 1D

• in summary, the 2D-FFT simply consists of– 1) applying the 1D-FFT to the rows of the sequence– 2) applying the 1D-FFT to the columns of this intermediate

sequence

)log()log()log()log()log(

2121

221121

221112

NNNNONNNONNNONNONNNON

=+=+

31

Page 32: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Properties of the DFT

32

Page 33: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Properties of the DFT

33

Page 34: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Properties of the DFT

34

Page 35: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Cosine Transform• due to its computational efficiency the DFT is very

popular• however, it has strong disadvantages for some

applications– it is complex– it has poor energy compaction

• energy compaction– is the ability to pack the energy of the spatial sequence into as

few frequency coefficients as possible– this is very important for image compression– we represent the signal in the frequency domain– if compaction is high, we only have to transmit a few coefficients– instead of the whole set of pixels

35

Page 36: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

Discrete Cosine Transform• a much better transform, from this point of view, is the

DCT• it is defined by

with

• we will talk more about it in the next class

[ ] [ ]

[ ] [ ]⎪⎩

⎪⎨⎧

<≤<≤

⎟⎟⎠

⎞⎜⎜⎝

⎛+⎟⎟

⎞⎜⎜⎝

⎛+=

⎪⎩

⎪⎨⎧

<≤<≤

⎟⎟⎠

⎞⎜⎜⎝

⎛+⎟⎟

⎞⎜⎜⎝

⎛+=

∑∑

∑∑

=

=

=

=

otherwiseNnNn

nkN

nkN

kkCkwkwNNnnx

otherwiseNkNk

nkN

nkN

nnxkkC

N

k

N

kx

N

n

N

nx

000

)12(2

cos)12(2

cos,][][1,

000

,)12(2

cos)12(2

cos,,

22

1122

211

1

1

0

1

0212211

2121

22

1122

211

1

1

0

1

021

21

1

1

2

2

1

1

2

2

ππ

ππ

[ ] [ ]⎪⎩

⎪⎨⎧

<≤=

=⎪⎩

⎪⎨⎧

<≤=

=22

222

11

111 1,1

0,21

,1,1

0,21

Nkkkw

Nkkkw

36

Page 37: Discrete Fourier Transform - SVCL - Statistical Visual Computing Lab

37