CHAPTER 8 Discrete-Time Signals and Systems
8.3.4 Linear and Nonlinear Filtering with MATLAB
One is not always able to get rid of undesirable components of a signal by means of linear filtering.
In this section we will illustrate the possible advantages of using nonlinear filters.
Linear Filtering
To illustrate the way a linear filter works, consider getting rid of a random disturbanceη[n], which we model as Gaussian noise (this is one of the possible noise signals MATLAB provides) that has been added to a sinusoidx[n]=cos(πn/16). Lety[n]=x[n]+η[n]. We will use an averaging filter having an input–output equation
z[n]= 1 M
M−1
X
k=0
y[n−k]
8.3 Discrete-Time Systems 495
ThisM-order filter averagesMpast input values{y[n−k],k=0,. . .,M−1}and assigns this average to the outputz[n]. The effect is to smooth out the input signal by attenuating the high-frequency components of the signal due to the noise. The larger the value ofMthe better the results, but at the expense of more complexity and a larger delay in the output signal (this is due to the linear-phase frequency response of the filter, as we will see later).
We use a third-order and a fifteenth-order filter, implemented by our functionaveragergiven below.
The denoising is done by means of the following script.
%%%%%%%%%%%%%%%%%
% Linear filtering
%%%%%%%%%%%%%%%%%
N = 200; n = 0:N−1;
x = cos(pi∗n/16); % input signal noise = 0.2∗randn(1, N); % noise y = x + noise; % noisy signal
z = averager(3, y); % averaging linear filter with M = 3 z1 = averager(15, y); % averaging linear filter with M = 15
Our functionaveragerdefines the coefficients of the averaging filter and then uses the MATLAB func- tion filterto compute the filter response. The inputs of filter are the vector b=(1/M)[1ã ã ã1], the coefficients connected with the input, the unit coefficient connected with the output, andxis a vector with the entries the signal samples we wish to filter. The results of filtering using these two filters are shown in Figure 8.12. As expected, the performance of the filter withM=15 is a lot better, but a delay of 8 samples (or the integer larger thanM/2) is shown in the filter output.
function y = averager(M,x)
% Moving average of signal x
% M: order of averager
% x: input signal
%
b = (1/M)∗ones(1, M);
y = filter(b, 1, x);
Nonlinear Filtering
Is linear filtering always capable of getting rid of noise? The answer is: It depends on the type of noise.
In the previous example we showed that a high-order averaging filter, which is linear, performs well for Gaussian noise. Let us now consider an impulsivenoise that is either zero or a certain value at random. This is the type of noise occurring in communications whenever cracking sounds are heard in the transmission, or the “salt-and-pepper” noise that appears in images.
It will be shown that even the 15th-order averager—that did well before—is not capable of denoising the signal with impulsive noise. Amedian filterconsiders a certain number of samples (the example shows the case of a 5th-order median filter), orders them according to their amplitudes, and chooses the one in the middle value (i.e., the median) as the output of the filter. Such a filter is nonlinear as it does not satisfy superposition. The following script is used to filter the noisy signal using a linear and
0 20 40 60 80 100 (a)
(b)
120 140 160 180
0 20 40 60 80 100 120 140 160 180
−1
−0.5 0 0.5 1
y[n], z[n]y[n], z1[n]
−1
−0.5 0 0.5 1
n
FIGURE 8.12
Averaging filtering with filters of order (a)M=3and of order (b)M=15result used to get rid of Gaussian noise added to a sinusoidx[n]=cos(πn/16). Solid line corresponds to the noisy signal, while the dashed line is for the filtered signal. The filtered signal is very much like the noisy signal (a) whenM=3is the order of the filter, while the filtered signal looks like the sinusoid, but shifted, (b) whenM=15. The plotting in this figure is done using plotinstead ofstemto allow a better visualization of the filtering results.
a nonlinear filter, and a comparison of the results is shown in Figure 8.13. In this case the nonlinear filter is able to denoise the signal much better than the linear filter.
%%%%%%%%%%%%%%%%%%%
% Nonlinear filtering
%%%%%%%%%%%%%%%%%%%
clear all; clf
N = 200;n = 0:N−1;
% impulsive noise for m = 1:N,
d = rand(1, 1);
if d>= 0.95, noise(m) =−1.5;
else
noise(m) = 0;
end end
8.3 Discrete-Time Systems 497
0 20 40 60 80 100 (a)
120 140 160 180 200
−2
−1 0 1 2
n y1[n]
(b) (c)
0 20 40 60 80 100 120 140 160 180 200 n
0 20 40 60 80 100 120 140 160 180 200 n
−2
−1 0 1 2
z1[n] z2[n]
−2
−1 0 1 2
FIGURE 8.13
Top figure (a): noisy signal (dashed blue line) and clean signal (solid line). The clean signal (dashed line) is superposed on the denoised signal (solid blue line) in the bottom plots. The solid line in plot (b) is the result of median filtering, and the solid line in plot (c) is the result of the averager.
x = [2∗cos(pi∗n(1:100)/256) zeros(1, 100)];
y1 = x + noise;
% linear filtering z2 = averager(15, y1);
% nonlinear filtering -- median filtering z1(1) = median([0 0 y1(1) y1(2) y1(3)]);
z1(2) = median([0 y1(1) y1(2) y1(3) y1(4)]);
z1(N−1) = median([y1(N−3) y1(N−2) y1(N−1) y1(N) 0]);
z1(N) = median([y1(N−2) y1(N−1) y1(N) 0 0]);
for k = 3:N−2,
z1(k) = median([y1(k−2) y1(k−1) y1(k) y1(k + 1) y1(k + 2)]);
end
Although the theory of nonlinear filtering is beyond the scope of this book, it is good to remember that in cases like this when linear filters do not seem to do well, there are other methods to use.