Nonlinear Digital Filtering with Python An Introduction 1st Edition scribd download
Nonlinear Digital Filtering with Python An Introduction 1st Edition scribd download
Edition
Visit the link below to download the full version of this book:
https://medidownload.com/product/nonlinear-digital-filtering-with-python-an-intr
oduction-1st-edition/
Contents
Preface ix
Authors xiii
1 Introduction 1
1.1 Linear versus nonlinear filters: an example . . . . . . . . . . . . . 2
1.2 Why nonlinearity? Data cleaning filters . . . . . . . . . . . . . . 10
1.3 The many forms of nonlinearity . . . . . . . . . . . . . . . . . . . 14
1.3.1 A simple nonlinear filter taxonomy . . . . . . . . . . . . . 15
1.3.2 Recursive filters: instability and chaos . . . . . . . . . . . 17
1.4 Python and reproducible research . . . . . . . . . . . . . . . . . . 19
1.4.1 A very brief introduction to Python . . . . . . . . . . . . 20
1.4.2 Reproducible research . . . . . . . . . . . . . . . . . . . . 21
1.4.3 How this book was developed . . . . . . . . . . . . . . . . 24
1.5 Organization of this book . . . . . . . . . . . . . . . . . . . . . . 25
2 Python 27
2.1 A high-level overview of the language . . . . . . . . . . . . . . . . 27
2.1.1 How Python programs work . . . . . . . . . . . . . . . . . 28
2.1.2 Using Python interactively . . . . . . . . . . . . . . . . . 28
2.1.3 Using Python from the command prompt . . . . . . . . . 32
2.2 Key language elements . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.1 The general structure of Python . . . . . . . . . . . . . . 34
2.2.2 Object types . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.2.3 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.2.4 Lists: a key data type . . . . . . . . . . . . . . . . . . . . 39
2.2.5 Tuples: immutable lists . . . . . . . . . . . . . . . . . . . 41
2.2.6 Character strings . . . . . . . . . . . . . . . . . . . . . . . 42
2.2.7 Dictionaries: another key data type . . . . . . . . . . . . 45
2.2.8 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.2.9 Control structures . . . . . . . . . . . . . . . . . . . . . . 49
2.2.10 Input and output . . . . . . . . . . . . . . . . . . . . . . . 53
2.3 Caveat emptor: a few Python quirks . . . . . . . . . . . . . . . . 58
2.3.1 In-place operations, side-effects, and None returns . . . . . 58
v
© 2016 by Taylor & Francis Group, LLC
i i
i i
i i
vi CONTENTS
i i
i i
CONTENTS vii
i i
i i
viii CONTENTS
Bibliography 273
Index 283
i i
i i
Preface
Linear filters that separate signals into components based on their frequency
content play an essential role in the theory and practice of electrical engineer-
ing. Historically, the linearity of these filters has been exploited heavily in the
development of theoretical characterizations and practical design rules. Unfor-
tunately, not all practical signal separation problems are amenable to linear
solutions: the data cleaning problem that provides the central motivation for
this book is a case in point. There, localized “spikes,” “glitches,” or “impul-
sive noise” cannot be effectively removed from other signals using linear filters.
In fact, this inability is a direct consequence of linearity, as demonstrated in
Chapter 1.
Fortunately, a number of nonlinear digital filters have been developed to
address these problems, and research in this area remains active. A classic
example is the standard median filter, proposed by John Tukey in a confer-
ence paper in 1974. This filter turns out to be extremely effective in removing
impulsive “spikes” from signals, but in many applications it also introduces un-
acceptable distortion in the underlying signal components we wish to preserve.
These two characteristics of the median filter have led to the development of
many extensions that attempt to retain the median filter’s effectiveness against
impulsive noise, while reducing its tendency to distort signal components of
interest. Because of the central role the class of median-based filters plays in
practial applications, this book devotes an entire chapter to this class, including
such extensions of the standard median filter as weighted median filters, recur-
sive median filters, recursive weighted median filters, and further generalizations
like the class of stack filters.
A key practical challenge in the analysis of these filters and the design of new
ones is their nonlinearity. Specifically, linearity represents an extremely pow-
erful mathematical condition that has led to remarkably complete and useful
characterizations of linear filters. Removing this mathematical constraint gives
us tremendous additional flexibility in how the resulting filters can behave, but
it introduces equally tremendous obstacles to their analysis and design. In par-
ticular, while the class of linear, time-invariant digital filters exhibits essentially
complete and essentially equivalent behavioral and structural characterizations,
this is no longer true for the class of nonlinear digital filters. That is, the prin-
ciple of superposition describes how linear filters behave, and this behavioral
description leads directly to the convolution representation of these filters in
ix
© 2016 by Taylor & Francis Group, LLC
i i
i i
i i
x PREFACE
i i
i i
xi
both distinct and much harder to characterize in the nonlinear case. In addi-
tion, many nonlinear recursive filters can exhibit highly undesirable behavior like
chaotic responses to simple input changes (e.g., steps or impulses), something
nonrecursive nonlinear digital filters are immune to. Beyond explaining why
most nonlinear digital filters now in use are nonrecursive—the recursive median
filter is a notable exception, discussed in both Chapters 4 and 7—a key motiva-
tion for including a chapter on recursive nonlinear filters here is to highlight an
extremely challenging but potentially equally promising research frontier. That
is, recursive linear filters are known to exhibit a number of practial advantages
over their nonrecursive counterparts. If a class of recursive nonlinear filters
could be identified that was immune to the worst forms of pathological behavior
described in Chapter 7, it might form the basis for novel new nonlinear filter
designs with characteristics superior to anything known today.
Without question, the most popular software environment for digital filter
research and design is MATLAB. Why, then, does this book advocate Python?
First, while excellent, MATLAB is a commercial product with a non-negligible
cost, while Python is a free, open-source computing environment. Although
the level of specialized support for digital filter design is much more extensive
in MATLAB, there is some support in Python (e.g., the scipy package sup-
ports linear digital filter design), and this support is growing (indeed, this book
represents a small contribution). Second, MATLAB was developed to support
mathematical computations based on linear algebra and related notions, while
Python is a general-purpose programming language like Java or C, with an
enormous variety of support packages available to extend the language’s ba-
sic capabilities: as of 29-March-2015, the Python package index (PyPI) listed
57,266 add-on packages, a number that grows daily. These packages support
everything from machine learning tools (e.g., in the scikit-learn package) and
natural language processing (e.g., in the nltk package), to web scraping (e.g.,
with the scrapy package) and HTML parsing (e.g., with the BeautifulSoup
package). In addition, Python has extensive capabilities as an operating system-
independent scripting language (e.g., file management, scheduling tasks, etc.),
with extensive support for website development. Finally, Python also provides
substantial and growing support for reproducible research or dynamic document
development, creating documents that contain text, computational results, and
the code used to obtain those results. This last capability was used in the prepa-
ration of this book and it offers enormous time savings when minor revisions
are required, a point discussed further in Chapter 1. Python implementations
of the most important digital filters discussed in this book are included, and
Chapter 2 presents an essential introduction to Python programming for those
with little or no prior Python experience.
This book is intended as either a textbook or a reference for self-study. As
a textbook, the material presented here would be appropriate for an introduc-
tory course on nonlinear digital filters, at either the advanced undergraduate or
the beginning graduate level. As a reference for self-study, this book should be
useful to those interested in implementing, developing, and using data cleaning
filters for dynamic data analysis or time-series modeling. Specific application
i i
i i
xii PREFACE
i i
i i
Authors
xiii
© 2016 by Taylor & Francis Group, LLC
i i
i i
i i
xiv AUTHORS
Chairman of the DSP Technical Committee of the IEEE Circuits and Systems
Society. He was Honorary Guest Professor of Jilin University, China (2005–
2010). He served as Distinguished Lecturer for the IEEE Circuits and Systems
Society in 2004–2005, and is a Past-Chairman of the IEEE-EURASIP Nonlinear
Signal and Image Processing Board. He was Chairman of the Algorithm Group
of the Eurpean Commisson COST 211quat, served as Associate Editor of the
IEEE Transactions on Image Processing, and was Guest Editor of Multimedia
Tools and Applications and the European Journal Applied Signal Processing.
He is the past Chairman of the IEEE Finland Section and the IEEE SP/CAS
Finland Chapter. He was also Chairman and Technical Program Chair of many
national and international conferences and workshops. He is a member of the
IEEE Signal Processing (SP) and Circuits and Systems (CAS) societies.
Dr. Gabbouj was recipient of the 2012 Nokia Foundation Visiting Professor
Award, the 2005 Nokia Foundation Recognition Award, and many best-paper
awards. He has co-authored over 600 publications and supervised 40 PhD theses.
i i
i i
Chapter 1
Introduction
The title of this book immediately raises the following three questions:
1. Why digital filters?
Signal processing is one of the core areas of electrical engineer-
ing, emerging from advances central to the development of radio,
television, and other forms of electronic communication. One of
the key subject areas within this field is the design and appli-
cation of filters, devices that separate signal components (e.g.,
extracting the signal transmitted by the radio station you want
to listen to from others around it). With the advent and sub-
sequent miniaturization of computers, digital filter implementa-
tions became increasingly practical and widely applied.
2. Why nonlinear digital filters?
The theory of linear filters is well-developed and provides an
extremely powerful foundation for the analysis and design of
practical filters, both digital and analog. Not all filtering prob-
lems have adequate linear solutions, however: a case in point is
the data cleaning problem introduced in this chapter that mo-
tivates much of the work presented in this book. Because the
class of “nonlinear filters” is vastly more heterogeneous than the
class of “linear filters,” there is no general theory analogous to
that for linear filters. The primary objective of this book is to
present a collection of ideas and techniques that are useful in
the design and application of nonlinear digital filters.
3. Why Python?
Without question, the primary software environment used in
signal processing research is MATLAB, a commercial product
1
© 2016 by Taylor & Francis Group, LLC
i i
i i
i i
2 CHAPTER 1. INTRODUCTION
for some nonnegative integer K and some function Φ(·) that maps 2K + 1
i i
i i
1.5
1.0
0.5
0.0
ecg
−0.5
−1.0
−1.5
Time
real numbers into another real number. Given this general structure, the art
of designing digital filters reduces to specifying K and Φ(·). As we will see,
however, the range of practical choices for the function Φ(·) is so vast that its
specification is both challenging and critical.
The filters defined by Eq. (1.1) constitute the class of symmetric moving win-
dow filters. The simplest member of this filter class is the symmetric unweighted
moving average filter, obtained by taking the function Φ(·) as the unweighted
average of its arguments:
xk−K + · · · + xk + · · · + xk+K
Φ(xk−K , . . . , xk , . . . , xk+K ) =
2K + 1
K
1 X
= xk−j . (1.2)
2K + 1
j=−K
i i
i i
4 CHAPTER 1. INTRODUCTION
1.5
0.5
0.5
x(k)
y(k)
−0.5
−0.5
−1.5
−1.5
300 400 500 600 700 300 400 500 600 700
k k
MA Filter, K = 10 MA Filter, K = 25
1.5
1.5
0.5
0.5
y(k)
y(k)
−0.5
−0.5
−1.5
−1.5
300 400 500 600 700 300 400 500 600 700
k k
Figure 1.2: A 512-point portion of the original ECG signal (upper left), and the
results of moving average filters with different window half-width parameters:
K = 5 (upper right), K = 10 (lower left), and K = 25 (lower right).
Note that the only flexibility inherent in this filter is the choice of the window
half-width parameter K. Also, note that this filter remains well-defined for
K = 0, reducing to the identity mapping: yk = xk for all k. This observation is
useful because it defines one extreme limit of the moving average filter’s possible
behavior: when K = 0, the filter does nothing, returning the original sequence
{xk } unmodified. As the following example illustrates, increasing K causes the
filter’s output to become smoother, more so the larger we make K.
This point is illustrated in Fig. 1.2, which shows the results of applying the
linear moving average filter defined in Eqs. (1.1) and (1.2) for four different
values of the tuning parameter K. In all cases, the filter is applied to the
512-point subsequence of the original data sequence {xk }, corresponding to the
i i
i i
1.5
1.0
0.5
x(k) and y(k)
0.0
−0.5
−1.0
−1.5
Figure 1.3: Comparison of the moving average filter response with K = 5 (heavy
line) with the original ECG data sequence (open circles).
portion from k = 256 to k = 767. The upper left plot shows the results for
K = 0, corresponding to the raw signal; the presence of the large spikes in the
original signal is clear in this plot. The upper right plot shows the results of
applying the moving average filter with K = 5, corresponding to an 11-point
unweighted moving average (i.e., 2K + 1 = 11). Comparing these plots clearly
illustrates the point noted above that the effect of increasing K is to cause the
filter output to be smoother than the filter input. This effect is seen even more
clearly as we increase K to 10 (lower left plot) or 25 (lower right plot).
To give a clearer picture of the effects of the unweighted moving average
filter, Fig. 1.3 shows both the original data sequence (represented as open
circles) and the filter response with K = 5 (the solid line). While the spikes
in the original data sequence (i.e., the open circles well above and well below
i i