Professional Documents
Culture Documents
OK Es Ayuda Tarea MAfilter
OK Es Ayuda Tarea MAfilter
4 Examples of Systems
0.8
0.6
y(t)
0.4
0.2
0
0 5 10 15
Time
FIGURE 1.26
Response of the mass–spring–damper system to a unit-step input
with M = 1, K = 2, and D = 0.5.
values for M, D, and K, the response y(t) to a step input, x1t2 = u1t2, is a decaying
oscillation that settles to a constant (or steady-state) value, as seen in Figure 1.26.
The oscillation is due to the transfer of energy between kinetic energy (proportional to
the velocity squared of the mass) and the potential energy (energy stored in the spring
as it compresses or stretches). The decay of the oscillation is due to the dissipation of
energy that occurs in the damper.
Mass–
Spring– A detailed discussion of vibrations is not the objective of this example or the online
Damper demo. However, the mass–spring–damper is a system whose response can be visualized
System
readily via animation.A series RLC circuit is governed by the same general equation and
responds in the same manner as this system, but the response cannot be visualized
easily via animation. Therefore, the mass–spring–damper system and the accompany-
ing online demo will be used throughout this text to demonstrate basic system
input/output concepts.
1
y[n] = [x[n] + x[n - 1] + x[n - 2] + Á + x[n - N + 1]] (1.11)
N
where x[n] is the input applied to the filter and y[n] is the resulting output response.
For example, if N = 3, the 3-point MA filter is given by the input/output relationship
1
y[n] = [x[n] + x[n - 1] + x[n - 2]]
3
From (1.11), it is seen that the output y[n] at time n of the N-point MA filter is the
average of the N input values x[n], x[n - 1], x[n - 2], Á , x[n - N + 1]. Hence, the
term “N-point” refers to the number of input values used in the computation of the filter’s
output. The filter is referred to as a “moving average filter,” since we compute the
next value y[n + 1] of the output by moving the range of time points over which the
27
www.EngineeringEBooksPdf.com
Chapter 1 Fundamental Concepts
filter output is computed. In particular, y[n + 1] is the average of x[n + 1], x[n],
x[n - 1], Á , x[n - N + 2], so that
1
y[n + 1] = [x[n + 1] + x[n] + x[n - 1] + Á + x[n - N + 2]] (1.12)
N
Note that (1.12) follows from (1.11) by the setting of n = n + 1 in (1.11). Some au-
thors refer to the MA filter as the running average filter.
MA filters are often used to reduce the magnitude of the noise that may be pre-
sent in a signal. To see how this is possible, suppose that the input x[n] is given in the
form x[n] = s[n] + e[n], where s[n] is the smooth part of x[n] and e[n] is the erratic or
noisy part of x[n]. Then the output y[n] of the N-point MA filter is given by
1
y[n] = [s[n] + s[n - 1] + Á + s[n - N + 1]]
N
1
+ [e[n] + e[n - 1] + Á + e[n - N + 1]] (1.13)
N
The noisy part of the MA filter output y[n] given by (1.13) is the average of the noise
values e[n], e[n - 1], Á , e[n - N + 1], which is equal to
1
[e[n] + e[n - 1] + Á + e[n - N + 1]] (1.14)
N
If e[n] varies randomly about zero, the noisy term given by the average (1.14) can be
made as small as desired (in theory) by taking the value of N to be sufficiently large.
This explains why MA filters can work well in reducing the magnitude of the erratic or
noisy part of a signal.
If the value of N is sufficiently large, the output of the MA filter is approximately
equal to a time delay of the smooth part s[n] of x[n] with the amount of the delay equal
to 1N - 12/2 time units. The occurrence of the time delay will be verified mathemati-
cally in Chapter 5 with the discrete Fourier transform. An illustration of the N-point
MA filter and the time delay that can occur is given in the next example, where the fil-
ter is applied to price data for the stock fund QQQQ. First, it should be pointed out
that the output y[n] of the filter given by (1.11) is easily computed by the MATLAB
command sum. In particular, if the input signal x[n] is written as the column vector
x[n - N + 1]
x[n - N + 2]
x = D T
o
x[n]
then the output y[n] at time n is equal to the MATLAB command (1/N)*sum(x).
28
www.EngineeringEBooksPdf.com
Section 1.4 Examples of Systems
Note that in this case the input x[n] to the filter is the closing price c[n] of QQQQ. Also
note that the first value of the filter output y[n] is the output y[11] at day 11, since the 11-day MA
filter requires the input values c[1], c[2], c[3], Á , c[11] in order to compute y[11]. The resulting
MATLAB plot for the filter input c[n] and the filter output y[n] is given in Figure 1.27. In the
plot, the values of c[n] are plotted with o’s, and the values of y[n] are plotted with *’s. Note that
even though the closing prices are quite erratic, the filter does a good job of smoothing out the
data. However, it’s clear from the plot that the filter delays the input signal by several days. As
noted previously, the delay is approximately equal to 1N - 12/2, which is equal to five days in
this example. If y[n] is shifted to the left by five days (which corresponds to the left-shifted signal
37.5
37
36.5
c[n] and y[n]
36
35.5
35
34.5
34
10 15 20 25 30 35 40 45 50
Day (n)
FIGURE 1.27
MATLAB plot of filter input and output.
29
www.EngineeringEBooksPdf.com
Chapter 1 Fundamental Concepts
37.5
37
36.5
35.5
35
34.5
34
5 10 15 20 25 30 35 40 45
Day (n)
FIGURE 1.28
MATLAB plot of filter input and left-shifted output.
y[n + 5]) and then plotted with c[n], the result is as shown in Figure 1.28. Note that the left-shifted
output y[n + 5] fits the input data very nicely. Unfortunately, the shifted output cannot be
generated in real time, since its computation requires future values of c[n]. To see this, let
r[n] = y[n + 5]. Then, replacing n by n + 5 and setting N = 11 in (1.11) give
1
r[n] = [c[n + 5] + c[n + 4] + c[n + 3] + c[n + 2] + c[n + 1]
11
+ c[n] + c[n - 1] + Á + c[n - 5]] (1.15)
Hence, the computation of r[n] at time n requires the future values c[n + 5], c[n + 4],
c[n + 3], c[n + 2], and c[n + 1] of the input c[n].
Application to trading QQQQ. Individuals who trade stocks sometimes use MA fil-
ters to determine when to buy and when to sell a particular stock (such as QQQQ).
In the application to trading, MA filters are often referred to as SMA filters, where
SMA stands for “simple moving average.” An even more common type of filter used
in trading is the EWMA filter, also called the EMA filter, where EWMA stands for
“exponentially-weighted moving average.” The N-point EWMA filter is defined in
Section 2.1; in Section 7.5, a “recursive version” of the EWMA filter is defined. In
Section 7.5, an approach to buying and selling QQQQ is given in terms of the differ-
ence in the responses to two EWMA filters having different parameter values. For
details on the use of moving average filters in trading, type “moving average crossover”
into a search engine.
30
www.EngineeringEBooksPdf.com
Chapter 1 Fundamental Concepts
Except for the brief introduction to nonlinear systems given previously, this book
deals only with linear systems. A general class of linear systems is defined in the next
chapter.
But the response to input x1t - t12 is tx1t - t12, which does not equal 1t - t12x1t - t12, in
general. Hence y1t - t12 is not equal to the t1-second shift of the response to x(t), and thus the
system is time varying. Note that this system can be viewed as an ideal amplifier with time-varying
gain t.
1
r[n] = [x[n - q] + x[n - q - 1] + x[n - q - 2] + Á + x[n - q + N - 1]] (1.20)
N
36
www.EngineeringEBooksPdf.com
Problems
PROBLEMS
1.1. Consider the continuous-time signals displayed in Figure P1.1.
(i) Show that each of these signals is equal to a sum of rectangular pulses pt1t2 and/or
triangular pulses 11 - 2 ƒ t ƒ /t2pt1t2.
(ii) Use MATLAB to plot the signals in Figure P1.1.
37
www.EngineeringEBooksPdf.com
Section 2.1 Input/Output Representation of Discrete-Time Systems
It turns out that we can generalize (2.2) to be a large class of causal linear time-
invariant discrete-time systems by taking the input/output representation to be
N-1
y[n] = a wix[n - i] (2.3)
i=0
where the wi (that is, w0 , w1 , w2 , Á , wN - 1) are real numbers, and are called the
weights of the linear combination given by (2.3). Note that if all the weights are equal
to 1/N, that is, wi = 1>N for i = 0, 1, 2, Á , N - 1, then (2.3) reduces to (2.2). Hence,
the N-point MA filter is an example of a system whose input/output representation can
be written in the form (2.3).
Here the term “exponentially weighted” refers to the exponent of b in the weights given
by (2.6). Since 0 6 b 6 1, the weights given by (2.6) decrease in value as i increases in
value. For example, if N = 3 and b = 0.5, then from (2.5) it follows that
1 - 0.5 0.5
a = 3
= = 0.571
1 - 0.5 0.875
and using (2.6) gives w0 = a = 0.571, w1 = ab = 0.286, and w2 = ab2 = 0.143. Hence,
the 3-point EWMA filter with b = 0.5 is given by the input/output relationship
From (2.7) it is seen that in the computation of the filter output y[n], a larger “weight”
is given to the more recent values of the input x[n], whereas in the 3-point MA filter,
the same weight is given to all the input values; that is, in the 3-point MA filter the
output is
46
www.EngineeringEBooksPdf.com
Chapter 2 Time-Domain Models of Systems
Due to the exponential weighting, the N-point EWMA filter given by (2.4) has
a quicker response to time variations in the filter input x[n] in comparison with
the N-point MA filter. In other words, for a fixed value of N, the time delay through
the EWMA filter is less than that of the MA filter; however, in general, the MA filter
does a better job of removing noise than the EWMA filter does. For a given value of
N, the time delay through the EWMA filter depends on the choice of b: the smaller
b is, the smaller the time delay will be. In particular, if b = 0, then as previously
noted, the input/output relationship is y[n] = x[n], and thus there is no delay through
the system in this case. In the following example, the EWMA is applied to filtering of
price data for the index fund QQQQ.
w = [wN - 1 wN - 2 wN - 3 Á w0]
Then y[n] is given by the MATLAB multiplication w*c(n-10:n). The MATLAB commands for
computing the filter output y[n] for 11 … n … 50 are as follows:
c=csvread('QQQQdata2.csv',1,4,[1 4 50 4]);
b=0.7;
a=(1-b)/(1-b^11);
i=1:11;
w=a*(b.^(11-i));
for n=11:50;
y(n)=w*c(n-10:n);
end;
n=11:50;
plot(n,c(n),n,c(n),’o’,n,y(n),n,y(n),’*’)
grid
xlabel('Day(n)')
ylabel('c[n] and y[n]')
The resulting MATLAB plot for the filter input c[n] and the filter output y[n] is given in
Figure 2.1, with the values of c[n] plotted using o’s and the values of y[n] plotted using *’s.
Comparing Figure 1.27 in Chapter 1 with Figure 2.1 shows that there is less time delay
through the 11-day EWMA filter with b = 0.7 than there is through the 11-day MA filter. To
see this more clearly, in Figure 2.2 the output of the 11-day MA filter is combined with the
plot of c[n] and the output of the 11-day EWMA filter. The values of the MA filter response
are plotted with +’s.
47
www.EngineeringEBooksPdf.com
Section 2.1 Input/Output Representation of Discrete-Time Systems
37.5
37
36.5
35.5
35
34.5
34
10 15 20 25 30 35 40 45 50
Day (n)
FIGURE 2.1
MATLAB plot of EWMA filter input c[n] and output y[n].
37.5
37
36.5
c[n] and filter outputs
36
35.5
35
34.5
34
10 15 20 25 30 35 40 45 50
Day (n)
FIGURE 2.2
MATLAB plot of c[n] and filter outputs.
48
www.EngineeringEBooksPdf.com
Chapter 2 Time-Domain Models of Systems
Now, since d[n - i] = 0 for all i Z n, and d[n - i] = 1 when i = n, (2.9) reduces to
h[n] = wn , n Ú 0
Hence, the value h[n] of the unit-pulse response at time n is equal to the weight wn .
a h[i]x[n - i]
i=0
49
www.EngineeringEBooksPdf.com
Section 2.2 Convolution of Discrete-Time Signals
0.4
0.3
h1[n]
0.2
0.1
0
5 0 5 10 15 20
n
0.4
0.3
h2[n]
0.2
0.1
0
5 0 5 10 15 20
n
FIGURE 2.3
Unit-pulse responses of 11-day MA filter, h1[n], and EWMA filter, h2[n].
is called the convolution of h[n] and x[n], and is denoted by the symbol ‘‘*”; that is,
n
h[n] * x[n] = a h[i]x[n - i]
i=0
By (2.11), the output response y[n] resulting from input x[n] with x[n] = 0 for all
n 6 0 is equal to the convolution of the unit-pulse response h[n] with the input x[n].
Equation (2.11) [or (2.10)] is called the convolution representation of the system. This is
a time domain model, since the components of (2.11) are functions of the discrete-time
index n.
An interesting consequence of the convolution representation (2.10) is the result
that the system is determined completely by the unit-pulse response h[n]. In particu-
lar, if h[n] is known, the output response resulting from any input x[n] can be comput-
ed by evaluating (2.10). The evaluation of the convolution operation is studied in the
next section.
50
www.EngineeringEBooksPdf.com