Vous êtes sur la page 1sur 3

59 2.

9 FIR spatial filters

% Script file: firstream.m


% FIR filter implementation using stream processing
% Generate an input signal sequence
N=20; ni=(0:N-1); x=(3/4).ˆni+0.1*rnd(size(ni));
% Store impulse response
M=5; h=ones(1,M)/M; % M-point Moving Average filter
% Initialize signal memory
s=zeros(1,M);
% Compute filter output sequence
for n=1:N % Sampling-time index
xin=x(n); % Get input sample from ADC or storage
s(1)=xin;
yout=h(1)*s(1);
for m=M:-1:2
yout=yout+h(m)*s(m); % Multiply, Accumulate
s(m)=s(m-1); % and Shift Operation
end
y(n)=yout; % Put output sample to DAC or storage
end
Figure 2.22 M ATLAB script illustrating the real-time implementation of an FIR filter.

The M ATLAB function y=filter(h,1,x) computes the convolution y[n] of the


sequences h[n], 0 ≤ n ≤ M − 1 and x[n], 0 ≤ n ≤ N − 1, in the same range 0 ≤ n ≤ N − 1
with the input sequence. In contrast, y=conv(h,x) computes the convolution in the full
range 0 ≤ n ≤ N + M − 2.

2.9 FIR spatial filters


••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

As explained in Chapter 1, a black-and-white picture is a signal that describes intensity


variation over a spatial region. The sampled version of this picture over a rectangular grid
is represented by a 2D discrete-space signal x[m, n], [m, n] ∈ {(0, M − 1) × (0, N − 1)},
which is also known as a digital image. Each sample of the digital image is a picture
element and hence is called a pixel.
Spatial FIR filters are very popular and useful in the processing of digital images
to implement visual effects like noise filtering, edge detection, etc. Although digital
image processing is not the topic of this book, we will use FIR spatial filters as a visual
demonstration of the convolution operation, albeit in two dimensions.
Let us consider the task of smoothing sharp image features, like edges. Images
have sharp edges when the local intensity rises or drops sharply and have blurred or
60 Discrete-time signals and systems

x[m, n] n y[m, n] n

m m

Figure 2.23 The FIR spatial filtering operation.

fuzzy perception when local intensity is smooth. A simple smoothing operation involves
replacing each pixel by its average over a local region as shown in Figure 2.23.
Consider a 3 × 3 region around the pixel x[m, n]. Then the smoothed pixel value y[m, n]
can be computed as an arithmetic mean of the nine pixels in the local region

y[m, n] = 19 (x[m − 1, n − 1] +x[m − 1, n] +x[m − 1, n + 1]


+ x[m, n − 1] +x[m, n] +x[m, n + 1]
+ x[m + 1, n − 1] +x[m + 1, n] +x[m + 1, n + 1]), (2.73)

which can be written in a compact form as


1 1  
 1
y[m, n] = x[m − k, n −
]. (2.74)
9
k=−1
=−1

We next define a 2D sequence h[m, n]


1
9, −1 ≤ m, n ≤ 1
h[m, n] = (2.75)
0, otherwise

which can be seen as an FIR spatial filter impulse response. Then we can write (2.74) as


1 
1
y[m, n] = h[k,
]x[m − k, n −
], (2.76)
k=−1
=−1

which is a 2D convolution of image x[m, n] with an FIR spatial filter h[m, n]. A general
expression for 2D convolution, when the FIR spatial filter has finite symmetric support
(2K + 1) × (2L + 1), is given by


K 
L
y[m, n] = h[k,
]x[m − k, n −
]. (2.77)
k=−K
=−L

Figure 2.23 shows the result of a 5 × 5 smoothing filter operation on the image Lena.
61 2.10 Systems described by linear constant-coefficient difference equations

+
+
+

I
u

u I¢

Figure 2.24 FIR spatial filter implementation.

Filter implementation Note that (2.77) can also be written as


m+K 
n+L
y[m, n] = x[k,
]h[m − k, n −
]. (2.78)
k=m−K
=n−L

This suggests the following steps for the computation of convolution at each pixel [m, n]:
1. The filter array h[k,
] is rotated by 180◦ to obtain h[−k, −
] array.
2. The rotated array is moved over the image so that the origin h[0, 0] coincides with the
current image pixel x[m, n].
3. All filter coefficients are multiplied with the corresponding image pixels and the results
are added.
4. The resulting sum is stored at the current pixel [m, n] in the filtered image y[m, n].
These steps are shown in Figure 2.24 which pictorially illustrates the convolution opera-
tion. The M ATLAB function y=conv2(h,x) implements the 2D convolution operation in
(2.78). However, the more suitable function for FIR spatial filtering is y=filter2(h,x)
which uses the conv2 function but provides the output sequence y with the same size as
that of the input sequence x. Using different shapes and values for the FIR filter support,
various visual effects like motion-blur, edge detection, edge enhancement, etc. can be
obtained. These and other issues are examined in Problems 15, 16, and 46.

2.10 Systems described by linear constant-coefficient


difference equations
••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

We have shown in Section 2.4 that every linear time-invariant system (1) is uniquely
characterized by its impulse response sequence, and (2) its output can be determined

Vous aimerez peut-être aussi