33
1 ITCT Lecture 10.2: Discrete Cosine Transform (DCT) Cited from Internet! Good for self-learner!

Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

1

ITCT Lecture 10.2:Discrete Cosine Transform (DCT)

Cited from Internet!

Good for self-learner!

Page 2: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

2

DCT is a Fourier-related transform similar to DFT, but using only real numbers. It is equivalent to a DFT roughly twice the transform length, operating on real data with “Even symmetry” (since Fourier transform of

a real and even function is real and even). The most common variant of DCT is the type-II DCT and its inverse is the type-III DCT.

Page 3: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

3

Two related transform are the discrete sine transform (DST), which is equivalent to a DFT of “Real and odd” functions, and the modified DCT (MDCT), which is based on a DCT of overlapping data.

The other interesting transform is the discrete Hartley transform (DHT) in which

Even part of DHT = Real part of DFT

Odd part of DHT = Imaginary part of DFT

Page 4: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

4

DCT is often used in signal and image processing, especially for lossy data compression, because it has a strong “Energy Compaction” property: most of the signal information tends to be concentrated in a few low-frequency components of the DCT, approaching the KLT for signals based on certain limits of Markov processes.

DCT is used in JPEG image compression, MJPEG , MPEG, H.264, and HEVC video compression.

MDCT is used in MP3, AAC, etc. audio compression.

Page 5: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

5

Formulation:

DCT is a linear and invertible function

F: Rn →Rn (where R denotes the set of real

numbers.), or equivalently on nxn matrix.

DCT-I:

2

1

10 ]1

cos[))1((2

1 n

k

kn

j

j jkn

xxxf

Page 6: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

6

A DCT-I of n=5 real numbers abcde is exactly equivalent to a DFT of 8 real numbers abcdedcb (even symmetry), here divided by 2. (In contrast, DCT-II ~ IV involve a half-sample shift in the equivalent DFT.)

Note that DCT-I is not defined for n less than 2. (All other DCT types are defined for any positive n.)

Page 7: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

7

DCT-II:

Some authors further multiply the fo term by

(see below for the corresponding change in DCT-III) . This makes the DCT-II matrix orthogonal (up to a scale factor), but breaks the direct correspondence with a real-even DFT of half-shifted input.

1

0

)]2

1(cos[

n

k

kj kjn

xf

2/1

Page 8: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

8

DCT-III:

Some authors further multiply the x0 term by , this makes the DCT-III matrix orthogonal (up to a scale factor), but breaks the direct correspondence with a real even DFT of half-shifted output.

1

1

0 ])2

1(cos[

2

1 n

k

kj kjn

xxf

2/1

Page 9: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

9

DCT-IV:

DCT-IV matrix is orthogonal (up to a scale factor).

MDCT is based on DCT-IV with overlapped data.

1

0

)]2

1)(

2

1(cos[

n

k

kj kjn

xf

Page 10: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

10

DCT V-VIII:

DCT types I-IV are equivalent to real-even DFTs of even order; therefore, there are 4 additional types of DCT corresponding to real-even DFTs of logically odd order, which have factors of (n + 1/2) in the denominators of the cosine arguments. These variants seem to be rarely used in practice.

Page 11: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

11

Inverse Transforms:

IDCT-I is DCT-I multiplied by 2/(n-1).

IDCT-IV is DCT-IV multiplied by 2/n.

IDCT-II is DCT-III multiplied by 2/n (and versa).

Page 12: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

12

Computation

Direct application of the above formulas would require O(n2) operations, as in the FFT it is possible to compute the same thing with only O(nlogn) complexity by factorizing the computation. (One can also compute DCTs via FFTs combined with O(n)) pre- and post-processing steps.)

Page 13: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

13

References: 1. Rao and Yip, Discrete Cosine Transform:

Algorithms, Advantages, Applications; Academic Press, Boston, 1990.

2. Arai, Agui, Nakajima, A Fast DCT-SQ scheme for Images, Trans. On IEICE-E, 71(11), 1095, Nov. 1998.

3. Tseng and Millen, On Computing the DCT, IEEE Trans. On Computers, pp. 966-968, Oct. 1978.

4. Frigo and Johnson, The Design and Implementation of FFTW3, IEEE Proceedings, vol. 93, no. 2, pp. 216-231, 2005.

Page 14: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

14

The implementation of the 2D-IDCT

Let the 8-point 1-D DCT of input data f(x) be:

First the 1-D DCT is applied to all the rows of the 2-D input f(y,x):

7

0

816

)12(cos)(

2)(

x

u uxxf

CuS

7

0

816

)12(cos),(

2),(

x

ur

uxxyf

CuyS

(1)

(2)

Page 15: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

15

Then the 1-D DCT is applied to the columns of the results of (2):

By substitution we get the formulation of the 2-D DCT:

7

0

816

)12(cos),(

2),(

y

ru vy

uySC

uvS

7

0

7

0 16

)12(cos

16

)12(cos),(

22),(

y x

uv vyuxxyf

CCuvS

(3)

(4)

Page 16: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

16

Fast 1-D DCT Algorithms

Define

Eqn. (1) can be written as:

Since

. and,16

,16

2

H

uux

7

0

7

0

8 )cos()(216

)12(cos)(

2)(

x

u

x

u xfCux

xfC

uS

16

)15(2cos

16

2coscos)cos(2coscos2

)(

uxuxH

HW

Page 17: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

17

If we constitute a sequence of elements f(k), k = 0, 1, …, 15 with

7

0

8 ]16

)15(2cos

16

2)[cos()(

16cos

4

xu

uxuxxfuS

u

C

8),15(

8 ),()(

kkf

kkfkf

(5)

Page 18: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

18

We can re-write (5) as

When

Because the 16-point DFT is defined by

15

0

15

0

16

2

8 })(Re{16

2cos)()(

16cos

4

k k

ukj

u

ekfuk

kfuSu

C

1j

15

0

16

2

16 )()(k

ukj

ekfuF

Page 19: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

19

We have the following DCT v.s. DFT relationship

Instead of performing an IDCT an IDFT of twice the length is performed. IDFT can be implemented by IFFT with complexity O(NlogN)!

)}(Re{)(16

cos4

168 uFuSu

Cu

: only the first 8 values

are needed.

Page 20: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

20

The multiplication by seems not so efficient. However, bear in mind that the last operation before the IDCT is the “Quantization”. That means every value is to be multiplied with a certain constant (1/Quantization factor) depending on its position in DCT matrix. So we can merge the multiplications by and the multiplication by Quantizer dependent constant together. As a result, the Quantization and DCT-DFT transform can be performed in one step.

16cos

4 u

Cu

16cos

4 u

Cu

Page 21: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

21

Let XM = (f(0), f(1), f(2), f(3), f(4), f(5), f(6),

f(7)): original data

FM = ( F(0), F(1), F(2), F(3), F(4),

F(5), F(6), F(7)): scaled transformed data

Define P(a,b) =

We can establish a matrix TM:

16

1

8

18

1

8

1

8

1

8

1

8

1

8

1

16

2cos

16

2cos

ba

Page 22: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

22

)56,49()63,42()70,35()77,28()84,21()91,14()98,7()105,0(

)48,42()54,36()60,30()66,24()72,18()78,12()84,6()90,0(

)40,35()45,30()50,25()55,20()60,15()65,10()70,5()75,0(

)32,28()36,24()40,20()44,16()48,12()52,8()56,4()60,0(

)24,21()27,18()30,15()33,12()36,9()39,6()42,3()045(

)16,14()18,12()20,10()22,8()24,6()26,4()28,2()30,0(

)8,7()9,6()10,5()11,4()12,3()13,2()14,1()15,0(2

)0,0(

2

)0,0(

2

)0,0(

2

)0,0(

2

)0,0(

2

)0,0(

2

)0,0(

2

)0,0(

PPPPPPPP

PPPPPPPP

PPPPPPPP

PPPPPPPP

PPPPPPPP

PPPPPPPP

PPPPPPPP

PPPPPPPP

TM

Page 23: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

23

FM = XM x (1/8)TM

Because

Define

cosa = cos(-a); -cosa = cos(p -a) = cos(p +a)

cos(2pn+a) = cosa;

8sin

8

3cos;

2

2

4cos

8

2cos;

8cos 642

kkk

Page 24: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

24

11

1111

11

11111111

11

1111

11

11111111

224466664422

44444444

664422224466

664422224466

44444444

224466664422

kkkkkkkkkkkk

kkkkkkkk

kkkkkkkkkkkk

kkkkkkkkkkkk

kkkkkkkk

kkkkkkkkkkkk

TM

Page 25: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

25

XM = FM x (8TM-1) = FM x L; (L = 8TM

-1)

11111111

11111111

11111111

11111111

11111111

11111111

11111111

11111111

224246246422

4444

664642642466

664642642466

4444

224246246422

CCCCCCCCCCCC

CCCC

CCCCCCCCCCCC

CCCCCCCCCCCC

CCCC

CCCCCCCCCCCC

L

Page 26: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

26

Where

The matrix L can be factored as:

L = B1 x M x A1 x A2 x A3

with:

8sin2

8

3cos2

28

2cos2;

8cos2

6

42

C

CC

Page 27: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

27

11100000

00001100

10110000

00000010

10110000

00001100

11100000

00000001

1B

10000000

000000

0000000

000000

00001000

0000000

00000010

00000001

26

4

62

4

CC

C

CC

C

M

Page 28: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

28

1

0

0

0

0

0

0

0

00100001

00100001

00000001

01000000

10000100

00000100

00011010

00011010

1A

00000001

01010000

10000000

00000100

01010000

00101000

00101000

10000010

2A

Page 29: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

29

So the IDFT can be performed by 5 steps:

00011000

00011000

00100100

10000001

01000010

01000010

00100100

10000001

3A

Page 30: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

30

21

)7(8

1)1(

8

1

21

)5(8

1)3(

8

12

)7(8

1)1(

8

11

)3(8

1)5(

8

1

)6(8

1)2(

8

1

)6(8

1)2(

8

1

)4(8

1

)0(16

1

:1

7

6

5

4

3

2

1

0

temptempa

FFa

temptempa

FFtemp

FFtemp

FFa

FFa

FFa

Fa

Fa

B

77

26646

455

66244

33

422

11

00

)(

)(:

ab

CaCab

Cab

CaCab

ab

cab

ab

ab

M

Page 31: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

31

77

36

45

4

103

322

101

50

76

3

3

3

:1

bn

bn

bn

tempn

bbn

bbn

bbn

btempn

bbtemp

A

057

636

215

634

213

42

01

70

:2

nnm

nnm

nnm

nnm

nnm

nm

nm

nm

A

04

23

15

76

76

15

23

04

)7(

)6(

)5(

)4(

)3(

)2(

)1(

)0(

:3

mmf

mmf

mmf

mmf

mmf

mmf

mmf

mmf

A

Page 32: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

32

In part M, a simplification can be made:

So define

)()(

)()(

)(

626646

6666266426642

624646

6464662466244

CCaaaC

CaCaCaCaCaCab

CCaaaC

CaCaCaCaCaCab

4

4

)(4

and

66

44

646

6262

tempRab

tempQab

aaCtemp

CCRCCQ

Page 33: Discrete Cosine Transform (DCT) - 國立臺灣大學itct/slide/2017/ITCT Lecture... · 2014-05-07 · 3 Two related transform are the discrete sine transform (DST), which is equivalent

33

If you count the operations you will get 29 additions and 5 multiplications.

Because an 8x8 2-D DCT can be computed by applying 8-point 1-D DCT to each row and each column of the 2D data. Therefore, 2x8x29 = 464 additions and 2x8x5 = 80 multiplications are required in total.

A more efficient DCT algorithm has been proposed by Feig and Winograd in IEEE Trans. on ASSP, pp. 2174-2193, 1992.