Download - Digital Filters
Digital FiltersDigital 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)
Applied by cross correlation (sum Applied by cross correlation (sum of products) of image f and mask hof products) of image f and mask hIf 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
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?
For simplicity, let us just consider 1D signals (e.g., For simplicity, let us just consider 1D signals (e.g., mono audio)mono audio)
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] + 1/3 y[n] = 1/3 x[n-1] + 1/3 x[n] + 1/3 x[n+1]x[n+1]
More generallyMore generallyy[n] = h[-1] x[n-1] + h[0] x[n] + h[1] y[n] = h[-1] x[n-1] + h[0] x[n] + h[1]
x[n+1]x[n+1]
But what exactly do we mean by But what exactly do we mean by “frequency?”“frequency?”
...sinsin ..
sin
222111 tttyoffsetCD
phasefrequencyamplitude
wheretty
0 5 10 15 20 25 30
-10
-5
0
5
10
sin(x) & 2sin(x)
amplitude (loudness)amplitude (loudness)
0 5 10 15 20 25 30
-10
-5
0
5
10
sin(x) & sin(x+pi/2)phase (time/space shift)phase (time/space shift)
0 5 10 15 20 25 30
-10
-5
0
5
10
sin(x) & sin(2x)frequency (speed)frequency (speed)
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)
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)
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).
So given …,h[-1],h[0],h[1],… how So given …,h[-1],h[0],h[1],… how can we plot the impulse reponse.can we plot the impulse reponse.
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
31
31
31
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
111
111
111
111
111
111
111
111
111
111
111
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
0 0.5 1 1.5 2 2.5 3 3.50
0.2
0.4
0.6
0.8
1
1.2
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
Frequency response Frequency response (dB) and phase of 3 (dB) and phase of 3
and 11 point box and 11 point box filters.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
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
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
How can we make these other How can we make these other filters?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.
• highpass highpass lowpass lowpass• lowpass lowpass highpass highpass• bandpass bandpass bandreject (stopband) bandreject (stopband)• bandreject bandreject bandpass bandpass
Lowpass Lowpass to to
highpasshighpass
Lowpass followed by highpass = Lowpass followed by highpass = bandpassbandpass
Bandreject (stopband) = lowpass + Bandreject (stopband) = lowpass + highpass (lowpass or highpass)highpass (lowpass or highpass)
Application of filtersApplication of filters
Edge detectionEdge detection
Recall the first derivative test from Recall the first derivative test from calculus:calculus:
Let the function f be continuous on Let the function f be continuous on some interval (c-some interval (c-,c+,c+) containing ) containing the critical point c.the critical point c.
1.1. If f’(x)>0 for x in (c-If f’(x)>0 for x in (c-,c) and f’(x)<0 for ,c) and f’(x)<0 for x in (c,c+x in (c,c+), then f has a local ), then f has a local maximum at c.maximum at c.
2.2. If f’(x)<0 for x in (c-If f’(x)<0 for x in (c-,c) and f’(x)>0 for ,c) and f’(x)>0 for x in (c,c+x in (c,c+), then f has a local ), then f has a local minimum at c.minimum at c.
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)hh11(x)(x)
where where is cross correlation. is cross correlation.So we can then search f’(x) for So we can then search f’(x) for
extrema where:extrema where:• 0 occurs0 occurs• -a,+b occurs-a,+b occurs• +a,-b occurs+a,-b occurs
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). If least twice differentiable on (a,b). If f’’(x)>0 on (a,b), then f is concave f’’(x)>0 on (a,b), then f is concave up on [a,b], while if f’’(x)<0 on up on [a,b], while if f’’(x)<0 on (a,b), then f is concave down on (a,b), then f is concave down on [a,b].[a,b].
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.
Also recall:Also recall:
A point c is called an inflection point of A point c is called an inflection point of f if:f if:
1.1. f is continuous at c;f is continuous at c;2.2. the graph of f has a tangent line the graph of f has a tangent line
(possibly vertical) at [c,f(c)];(possibly 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.
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
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].Then f’(x) = f(x)Then f’(x) = f(x)hh11(x)(x)Then f’’(x) = f’(x)Then f’’(x) = f’(x)hh11(x)(x)
where where is cross correlation. is cross correlation.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
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)hh22(x)(x)Much more computationally efficient.Much more computationally efficient.
Where does hWhere does h22 come from? come from?From [-1, 1] From [-1, 1] [1, -1] which is h [1, -1] which is h11 * h * h11 where * is convolution.where * is convolution.
Applying masks to imagesApplying masks to images
Convolution:Convolution:
Discrete form:Discrete form:
ydxdyxfyyxxhyxfyxh ,,,,yx,g
x y
yxfyyxxhyxfyxh ,,,*,yx,g
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
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