Digital Filters

65
Digital Filters Digital Filters

description

Digital Filters. What have we seen so far?. So far we have seen… Box filter Moving average filter Example of a lowpass passes low frequencies small, gradual changes in the signal are passed higher frequencies are attenuated (reduced/removed/suppressed). - PowerPoint PPT Presentation

Transcript of Digital Filters

Page 1: Digital Filters

Digital FiltersDigital Filters

Page 2: Digital Filters

What have we seen so far?What have we seen so far?

So far we have seen…So far we have seen…• Box filterBox filter

Moving average filterMoving average filter Example of a lowpassExample of a lowpass

• passes low frequenciespasses low frequencies small, gradual changes in the signal are small, gradual changes in the signal are

passedpassed higher frequencies are attenuated higher frequencies are attenuated

(reduced/removed/suppressed)(reduced/removed/suppressed)

Page 3: Digital Filters

““As soon as we started programming, we found As soon as we started programming, we found out to our surprise that it wasn't as easy to get out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging programs right as we had thought. Debugging had to be discovered. I can remember the exact had to be discovered. I can remember the exact instant when I realized that a large part of my life instant when I realized that a large part of my life from then on was going to be spent in finding from then on was going to be spent in finding mistakes in my own programs.”mistakes in my own programs.”

Maurice Wilkes (a pioneering British computer scientist, winner of Maurice Wilkes (a pioneering British computer scientist, winner of the 1967 Turing Award, developed the first stored-program the 1967 Turing Award, developed the first stored-program computer in 1949, invented the concept of microprogramming in computer in 1949, invented the concept of microprogramming in 1951, credited with originating the fundamental software concepts 1951, credited with originating the fundamental software concepts of symbolic labels, macros, and subroutine libraries; from of symbolic labels, macros, and subroutine libraries; from wikipedia)wikipedia)

Page 4: Digital Filters

Applied by cross correlation (sum Applied by cross correlation (sum of products) of image f and mask hof products) of image f and mask h

If mask is centered about origin, (x,y) If mask is centered about origin, (x,y) in image:in image:

If origin, (x,y), in image is aligned with If origin, (x,y), in image is aligned with (0,0) in mask:(0,0) in mask:

2/

2/

2/

2/

, ,,,,w

wi

h

hj

jihjyixfyxhyxfyxg

1

0

1

0

, ,,,,w

i

h

j

jihjyixfyxhyxfyxg

Page 5: Digital Filters

But how do we know what the But how do we know what the application of an arbitrary mask application of an arbitrary mask

actually does to the image?actually does to the image?

Page 6: Digital Filters

For simplicity, let us just consider 1D signals (e.g., For simplicity, let us just consider 1D signals (e.g., mono audio)mono audio)

Page 7: Digital Filters

Our moving average (box) filterOur moving average (box) filter

Example of a lowpass filter (passes low Example of a lowpass filter (passes low frequencies, attenuates high frequencies, attenuates high frequencies)frequencies)

y[n] = 1/3 x[n-1] + 1/3 x[n]y[n] = 1/3 x[n-1] + 1/3 x[n]

+ 1/3 x[n+1]+ 1/3 x[n+1]

More generally,More generally,

y[n] = h[-1] x[n-1] + h[0] x[n]y[n] = h[-1] x[n-1] + h[0] x[n]

+ h[1] x[n+1]+ h[1] x[n+1]

Page 8: Digital Filters
Page 9: Digital Filters
Page 10: Digital Filters
Page 11: Digital Filters
Page 12: Digital Filters

But what exactly do we mean by But what exactly do we mean by “frequency?”“frequency?”

...sinsin

..

sin

222111 ttty

offsetCD

phase

frequency

amplitude

where

tty

Page 13: Digital Filters

0 5 10 15 20 25 30

-10

-5

0

5

10

sin(x) & 2sin(x)

amplitude (loudness)amplitude (loudness)

Page 14: Digital Filters

0 5 10 15 20 25 30

-10

-5

0

5

10

sin(x) & sin(x+pi/2)

phase (time/space) shiftphase (time/space) shift

Page 15: Digital Filters

0 5 10 15 20 25 30

-10

-5

0

5

10

sin(x) & sin(2x)

frequency (speed)frequency (speed)

Page 16: Digital Filters

0 5 10 15 20 25 30

-10

-5

0

5

10

0.5*sin(x)+sin(3*x+pi/3)+sin(5*x+pi/8)

Page 17: Digital Filters

Lowpass filtersLowpass filtersInput: (before)Input: (before) x(t) = 0.5*sin(t) + sin(3*t+pi/3) + sin(5*t+pi/8)x(t) = 0.5*sin(t) + sin(3*t+pi/3) + sin(5*t+pi/8)Output: (after)Output: (after) y(t) = 0.5*sin(t) + sin(3*t+pi/3)y(t) = 0.5*sin(t) + sin(3*t+pi/3)

Page 18: Digital Filters
Page 19: Digital Filters

So how can we determine how our So how can we determine how our moving average filter behaves?moving average filter behaves?

11 point and 51 point moving 11 point and 51 point moving average filters (on the previous slide) average filters (on the previous slide) obviously produce different outputs obviously produce different outputs even when given the same input!even when given the same input!

Answer: By determining how a Answer: By determining how a particular filter responds to an particular filter responds to an impulse (their impulse response impulse (their impulse response function).function).

Page 20: Digital Filters
Page 21: Digital Filters
Page 22: Digital Filters
Page 23: Digital Filters
Page 24: Digital Filters

So given …,h[-1],h[0],h[1],… how So given …,h[-1],h[0],h[1],… how can we plot the impulse response?can we plot the impulse response?

1.1. Perform the z-transform (the discrete Perform the z-transform (the discrete version of the Laplace transform) of h version of the Laplace transform) of h resulting H.resulting H.

2.2. Plot H on the unit circle. The magnitude Plot H on the unit circle. The magnitude of H (abs(H) or |H|) is amplitude and the of H (abs(H) or |H|) is amplitude and the angle of H (arg(H)) is the phase.angle of H (arg(H)) is the phase.

Say we have a 3 point box filter: h[-Say we have a 3 point box filter: h[-1]=h[0]=h[1]=1/3.1]=h[0]=h[1]=1/3.

jjj eeeH 101

3

1

3

1

3

1

Page 25: Digital Filters

So given …,h[-1],h[0],h[1],… how So given …,h[-1],h[0],h[1],… how can we plot the impulse response?can we plot the impulse response?

Say we have an 11 point box filter:Say we have an 11 point box filter:h[-5]=h[-4]=h[-3] =h[-3] =h[-2] =h[-1] =h[0] h[-5]=h[-4]=h[-3] =h[-3] =h[-2] =h[-1] =h[0]

=h[1] =h[2] =h[3] =h[4] =h[5]=1/11.=h[1] =h[2] =h[3] =h[4] =h[5]=1/11.

jj

jjj

jjj

jjj

ee

eee

eee

eeeH

54

321

012

345

11

1

11

111

1

11

1

11

111

1

11

1

11

111

1

11

1

11

1

Page 26: Digital Filters

Frequency response of 3 and 11 Frequency response of 3 and 11 point box filterspoint box filters

0 0.5 1 1.5 2 2.5 3 3.50

0.2

0.4

0.6

0.8

1

1.2

Page 27: Digital Filters

0 0.5 1 1.5 2 2.5 3 3.50

0.2

0.4

0.6

0.8

1

1.2

Page 28: Digital Filters

0 0.5 1 1.5 2 2.5 3 3.5-40

-35

-30

-25

-20

-15

-10

-5

0

5

Frequency response in dB of 3 and Frequency response in dB of 3 and 11 point box filters11 point box filters

Page 29: Digital Filters

dBdB(from http://www.animations.physics.unsw.edu.au/jw/dB.htm)(from http://www.animations.physics.unsw.edu.au/jw/dB.htm)

““The decibel (dB) is used to measure sound The decibel (dB) is used to measure sound level, but it is also widely used in electronics, level, but it is also widely used in electronics, signals and communication. The dB is a signals and communication. The dB is a logarithmic way of describing a ratio. The logarithmic way of describing a ratio. The ratio may be power, sound pressure, voltage ratio may be power, sound pressure, voltage or intensity or several other things.”or intensity or several other things.”

““One decibel is close to the Just Noticeable One decibel is close to the Just Noticeable Difference (JND) for sound level.Difference (JND) for sound level.

Experimentally it was found that a 10 dB Experimentally it was found that a 10 dB increase in sound level corresponds increase in sound level corresponds approximately to a perceived doubling of approximately to a perceived doubling of loudness.”loudness.”

Page 30: Digital Filters

Frequency Frequency response (dB) and response (dB) and phase of 3 and 11 phase of 3 and 11 point box filters.point box filters.

0 0.5 1 1.5 2 2.5 3 3.5-40

-35

-30

-25

-20

-15

-10

-5

0

5

0 0.5 1 1.5 2 2.5 3 3.5-35

-30

-25

-20

-15

-10

-5

0

5

Page 31: Digital Filters

3 point box filter 3 point box filter vs. 3 point vs. 3 point gaussiangaussian

0 0.5 1 1.5 2 2.5 3 3.5-60

-50

-40

-30

-20

-10

0

0 0.5 1 1.5 2 2.5 3 3.50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 32: Digital Filters

Frequency response (dB) and Frequency response (dB) and phase of 3 point gaussianphase of 3 point gaussian

0 0.5 1 1.5 2 2.5 3 3.5-60

-50

-40

-30

-20

-10

0

Page 33: Digital Filters

OTHER FILTERSOTHER FILTERS(OTHER THAN LOWPASS)(OTHER THAN LOWPASS)

Page 34: Digital Filters

How can we make these other How can we make these other filters?filters?

Page 35: Digital Filters

Spectral inversion:Spectral inversion:How to make a highpass filter the How to make a highpass filter the

easy way.easy way.

1.1. Change the sign of each sample in Change the sign of each sample in the filter kernel.the filter kernel.

2.2. Add 1 to the sample at the center of Add 1 to the sample at the center of symmetry.symmetry.

• highpasshighpass lowpass lowpass• lowpasslowpass highpass highpass• bandpassbandpass bandreject (stopband) bandreject (stopband)• bandrejectbandreject bandpass bandpass

Page 36: Digital Filters

Spectral inversion:Spectral inversion:How to make a highpass filter the How to make a highpass filter the

easy way.easy way.

1.1. Change the sign of each sample in Change the sign of each sample in the filter kernel.the filter kernel.

2.2. Add 1 to the sample at the center of Add 1 to the sample at the center of symmetry.symmetry.

Ex. lowpass box Ex. lowpass box highpass highpass

9/19/19/1

9/19/19/1

9/19/19/1

9/19/19/1

9/19/89/1

9/19/19/1

Page 37: Digital Filters

Lowpass Lowpass to to

highpasshighpass

Page 38: Digital Filters

Lowpass followed by highpass = Lowpass followed by highpass = bandpassbandpass

(more efficient implementation)

Page 39: Digital Filters

Bandreject (stopband) = lowpass + Bandreject (stopband) = lowpass + highpass (lowpass or highpass)highpass (lowpass or highpass)

(more efficient implementation)

Page 40: Digital Filters

Application of filtersApplication of filters

Edge detectionEdge detection

Page 41: Digital Filters

Recall the first derivative test from Recall the first derivative test from calculus:calculus:

Let the function f be continuous Let the function f be continuous on some interval (c-on some interval (c-,c+,c+) ) containing the critical point c.containing the critical point c.

1.1. If f’(x)>0 for x in (c-If f’(x)>0 for x in (c-,c) and ,c) and f’(x)<0 for x in (c,c+f’(x)<0 for x in (c,c+), then f ), then f has a has a local maximumlocal maximum at c. at c.

2.2. If f’(x)<0 for x in (c-If f’(x)<0 for x in (c-,c) and ,c) and f’(x)>0 for x in (c,c+f’(x)>0 for x in (c,c+), then f ), then f has a has a local minimumlocal minimum at c. at c.

Page 42: Digital Filters

Can we calculate the first derivative Can we calculate the first derivative of an image?of an image?

Let hLet h11=[-1, 1].=[-1, 1].

Then f’(x) = g(x) = f(x) Then f’(x) = g(x) = f(x) h h11(x)(x)

where where is cross correlation (convolution). is cross correlation (convolution).

So we can then search f’(x) for extrema So we can then search f’(x) for extrema where:where:• 0 occurs0 occurs• -a,+b occurs-a,+b occurs• +a,-b occurs+a,-b occurs

Page 43: Digital Filters

Recall the following from calculus Recall the following from calculus regarding the second derivative:regarding the second derivative:

Let f be continuous on [a,b], and at Let f be continuous on [a,b], and at least twice differentiable on (a,b).least twice differentiable on (a,b).

If f’’(x)>0 on (a,b), then f is If f’’(x)>0 on (a,b), then f is concave concave upup on [a,b]; if f’’(x)<0 on (a,b), then on [a,b]; if f’’(x)<0 on (a,b), then f is f is concave downconcave down on [a,b]. on [a,b].

Page 44: Digital Filters

Recall the second derivative test Recall the second derivative test from calculus:from calculus:

Let the function f be defined on an Let the function f be defined on an open interval containing the critical open interval containing the critical point c where f’(c)=0, and let f’’ be point c where f’(c)=0, and let f’’ be continuous on this interval.continuous on this interval.

1.1. If f’’(c)<0, then c is a local maximum If f’’(c)<0, then c is a local maximum point.point.

2.2. If f’’(c)>0, then c is a local minimum If f’’(c)>0, then c is a local minimum point.point.

3.3. If f’’(c)=0, then no conclusion is If f’’(c)=0, then no conclusion is possible without further investigation.possible without further investigation.

Page 45: Digital Filters

Also recall:Also recall:

A point c is called an inflection point of f A point c is called an inflection point of f if:if:

1.1. f is continuous at c;f is continuous at c;

2.2. the graph of f has a tangent line (possibly the graph of f has a tangent line (possibly vertical) at [c,f(c)];vertical) at [c,f(c)];

3.3. f is concave up on one side of c and f is concave up on one side of c and concave down on the other side.concave down on the other side.

Page 46: Digital Filters

We can consider the inflection point We can consider the inflection point to be the location of an edge!to be the location of an edge!

-3 -2 -1 0 1 2 3 4-60

-40

-20

0

20

40

60

Page 47: Digital Filters

Can we calculate the second Can we calculate the second derivative of an image?derivative of an image?

Let hLet h11=[-1, 1].=[-1, 1].

Step 1: f’(x) = f(x)Step 1: f’(x) = f(x) h h11(x)(x)

Step 2: f’’(x) = f’(x)Step 2: f’’(x) = f’(x) h h11(x)(x) where where is cross correlation (convolution). is cross correlation (convolution).

So we can then search f’’(x) for edges So we can then search f’’(x) for edges where:where:• 0 occurs0 occurs• -a,+b occurs-a,+b occurs• +a,-b occurs+a,-b occurs

Page 48: Digital Filters

Can we calculate the second Can we calculate the second derivative of an image?derivative of an image?

A better way … let hA better way … let h22=[1, -2, 1].=[1, -2, 1].

Then f’’(x) = f(x) Then f’’(x) = f(x) h h22(x)(x)

Much more computationally efficient.Much more computationally efficient.

But where does hBut where does h22 come from? come from?

From [-1, 1] From [-1, 1] [1, -1] which is h [1, -1] which is h11 h h11..

Page 49: Digital Filters

Applying masks to imagesApplying masks to images

Convolution:Convolution:

Discrete form:Discrete form:

ydxdyxfyyxxhyxfyxh ,,,,yx,g

x y

yxfyyxxhyxfyxh ,,,*,yx,g

Page 50: Digital Filters

Frequency response (dB) and Frequency response (dB) and phase of first derivative filter.phase of first derivative filter.

0 0.5 1 1.5 2 2.5 3 3.5-30

-25

-20

-15

-10

-5

0

5

Page 51: Digital Filters

Frequency response (dB) of first Frequency response (dB) of first and second derivative filters.and second derivative filters.

0 0.5 1 1.5 2 2.5 3 3.5-60

-50

-40

-30

-20

-10

0

10

Page 52: Digital Filters

SEPARABLESEPARABLEFILTERSFILTERS

““Danger, Will Robinson!”Danger, Will Robinson!”

Math ahead!Math ahead!

Page 53: Digital Filters

Convolution is associativeConvolution is associative

(f * v) * h = f * (v * h), where(f * v) * h = f * (v * h), where• * is convolution* is convolution• f if the image we wish to convolvef if the image we wish to convolve• v is a filter kernelv is a filter kernel• h is a another filter kernelh is a another filter kernel

Page 54: Digital Filters

SeparableSeparable

(f * v) * h = f * (v * h)(f * v) * h = f * (v * h) (associative)(associative)

If we can express a filter If we can express a filter ss as ass = v * h,s = v * h,

we can expresswe can expressf * s = f * (v * h)f * s = f * (v * h)

asas(f * v) * h(f * v) * h

Page 55: Digital Filters

Example of separable filterExample of separable filter

The classic Sobel filter:The classic Sobel filter:

Convolution of v*h is the Convolution of v*h is the outer productouter product (or tensor (or tensor product) of two vectors.product) of two vectors.

101

1

2

1

*

101

202

101

h

v

hvs

Page 56: Digital Filters

Outer product of two vectorsOuter product of two vectors

Outer (or tensor) product:Outer (or tensor) product:

Inner (or dot or scalar) product:Inner (or dot or scalar) product:

332313

322212

312111

321

3

2

1

vuvuvu

vuvuvu

vuvuvu

vvv

u

u

u

uvvu T

i

iivuvu

Page 57: Digital Filters

Outer product of two vectorsOuter product of two vectors

Outer (or tensor) product:Outer (or tensor) product:

Inner (or dot or scalar) product:Inner (or dot or scalar) product:

Note: The inner product is the trace Note: The inner product is the trace of the outer product.of the outer product.

332313

322212

312111

321

3

2

1

vuvuvu

vuvuvu

vuvuvu

vvv

u

u

u

uvvu T

i

iivuvu

Page 58: Digital Filters

Why do this?Why do this?

Consider an MxN image and a PxQ Consider an MxN image and a PxQ filter kernel. The cost of computation filter kernel. The cost of computation is MNPQ.is MNPQ.

For the separable version, the cost is For the separable version, the cost is MN(P+Q).MN(P+Q).

So the savings is PQ/(P+Q).So the savings is PQ/(P+Q). For a 9x9 kernel, the speed up is For a 9x9 kernel, the speed up is

about 4.5.about 4.5.

Page 59: Digital Filters

Is M separable?Is M separable?

Note: Not all filters are separable!Note: Not all filters are separable!

How can I tell if a matrix M is How can I tell if a matrix M is separable?separable?• rank(M) provides an estimate of the rank(M) provides an estimate of the

number of linearly independent rows or number of linearly independent rows or columns.columns.

• If the rank(M)=1, then it is separable.If the rank(M)=1, then it is separable.

Page 60: Digital Filters

If separable, how?If separable, how?

Use svd (singular value Use svd (singular value decomposition).decomposition).

Page 61: Digital Filters

Octave / MatlabOctave / Matlab

s = [-1 0 1; -2 0 2; -1 0 1]s = [-1 0 1; -2 0 2; -1 0 1] %or try ones(5,5) / 25%or try ones(5,5) / 25

v = [1; 2; 1]v = [1; 2; 1]

h = [-1 0 1]h = [-1 0 1]

v * hv * h

conv2( v, h )conv2( v, h )

rank( s )rank( s ) %should be 1%should be 1

[U,S,V] = svd( s )[U,S,V] = svd( s )

v2 = U(:,1) * sqrt( S(1,1) )v2 = U(:,1) * sqrt( S(1,1) ) %first col of U%first col of U

h2 = V(:,1)' * sqrt( S(1,1) )h2 = V(:,1)' * sqrt( S(1,1) ) %first col of V%first col of VTT

s – v2 * h2s – v2 * h2 %should be 0s%should be 0s

s – v * hs – v * h %ditto%ditto

Page 62: Digital Filters

Octave / MatlabOctave / Matlab

How about s = [ 1 2 1; 2 4 2; 1 2 1 ]?How about s = [ 1 2 1; 2 4 2; 1 2 1 ]?

Page 63: Digital Filters

svd = singular value svd = singular value decompositiondecomposition

M = U S VM = U S VTT

• M is an m x n matrix.M is an m x n matrix.• U is and m x m unitary (UU is and m x m unitary (UTTU = UUU = UUTT = I) matrix. = I) matrix.

The m cols of U are the left-singular vectors of M.The m cols of U are the left-singular vectors of M.

• S is and m x n matrixS is and m x n matrix The values along the diagonal of S are the singular The values along the diagonal of S are the singular

values of M.values of M.

• VVTT is an n x n matrix is an n x n matrix The n cols of V are the right-singular vectors of M.The n cols of V are the right-singular vectors of M.

Page 64: Digital Filters

svd and eigenanalysissvd and eigenanalysis

svd is related to eigenanalysissvd is related to eigenanalysis• The left-singular vectors of M (cols of U) The left-singular vectors of M (cols of U)

are eigenvectors of MMare eigenvectors of MMTT..• The right-singular vectors of M (cols of The right-singular vectors of M (cols of

V) are eigenvectors of MV) are eigenvectors of MTTM.M.• The non-zero singular values of M The non-zero singular values of M

(diagonal of S) are the square roots of (diagonal of S) are the square roots of the non-zero eigenvalues of both Mthe non-zero eigenvalues of both MTTM M and MMand MMTT..

Page 65: Digital Filters

ReferencesReferences

http://blogs.mathworks.com/steve/http://blogs.mathworks.com/steve/2006/10/04/separable-convolution/2006/10/04/separable-convolution/

http://www.gnu.org/software/octave/http://www.gnu.org/software/octave/