DSP Fundamentals Lecture Note Set #4 More Operations Involving One or Two Signals
DSP Fundamentals Lecture Note Set #4 More Operations Involving One or Two Signals
Let x[n] be a discrete-time signal. The following operations may be performed on x[n]:
|x[n]|2 .
7. Signal Power: This is the average energy. For an innite-length sequence it is, Px = 1 K 2K + 1 lim
K
|x[n]|2
n=K
(1)
y[n]
=
=
x[ ] x[n k].
k=0
y[n] y[n]
= x[n] + y[n 1]
(3)
An accumulator is one of the simplest types of Innite Impulse Response (IIR) lters. 9. Moving Average This is the average of the current and past M 1 samples: y[n] = 1 M
M 1
x[n k]
k=0
(4)
A moving average is one of the simplest types of Finite Impulse Response (FIR) lters.
1.1
Now, lets develop an accumulator in Matlab. We will basically be implementing equation (3) as a function. One issue we need to deal with is that, because of the recursive nature of the system, the length of the output could be innite even if the length of the input is nite. Because of this, the system is said to have an innite impulse response and is classied as an IIR system. To handle this problem, we will limit the length of the output to be the same as the input. If we want to make the output longer, we can extend the length of the input by padding it with zeros (i.e. just append a sequence of zeros at the end of the sequence). Here is a function that will perform accumulation: function [y,m] = accumulate(x,n) % implements y(n) = x(n) + y(n-1) % ------------------------% [y,m] = accumulate(x,n) % m = n; % no change in time axis y = zeros( size(x) ); % CORRECTION NEEDED y(1) = x(1); % first output sample for index=2:length(n) y(index) = x(index) + y(index-1); end Exercise: Let x[n] = [n] over 5 n 15. Using the accumulate function, determine y[n] = x[n]+y[n1] over the same range of n. What is the relationship among [n], u[n], and accumulation? Repeat this exercise for x[n] = u[n] and comment about the stability of the accumulation function.
We would also like to perform operations on two signals, such as addition and multiplication of signals. Such operations are called binary because they have two arguments. Here are some very basic binary operations.
2.1
Addition
Addition of two signals implies that samples at the same time instant are added together. As an example: y[n] = x1 [n] + x2 [n] (5)
Adding two signals dened over the same time scale is easy in Matlab. For instance: n = 0:12; x1 = exp(-n/4); x2= cos(pi*n/4); y = x1 + x2; However, things get more complicated when the two signals dont have the same time scale. A new time axis needs to be created that is the union of the two time axes. For instance, if signal x1 [n] is dened for 4 n 8 and x2 [n] is dened for 0 n 12, then the sum y[n] = x1 [n] + x2 [n] must be dened over 4 n 12.
2.2
The following program can be used to add two signals with dierent time axes: function [y,n] = sigadd(x1,n1,x2,n2) % implements y(n) = x1(n)+x2(n) % ----------------------------% [y,n] = sigadd(x1,n1,x2,n2) % y = sum sequence over n, which includes n1 and n2 % x1 = first sequence over n1 % x2 = second sequence over n2 (n2 can be different from n1) % n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n) y1 = zeros(1,length(n)); y2 = y1; % initialization y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y y = y1+y2; % sequence addition This program can be found within the eCampus page. Exercise: Using sigadd, add x1 [n] = exp(n/4), 4 n 8 with x2 [n] = cos(n/4), 0 n 12.
2.3
Suppose we have M signals x0 [n], x1 [n], ..., xM 1 [n]. We could add them all together to form a new signal:
M 1
y[n]
=
k=0
xk [n]
(6)
We could even multiply each signal xk [n] by a scalar value ck to obtain a signal
M 1
y[n]
=
k=0
ck xk [n]
(7)
Such a signal is said to be a linear combination of the signals x0 [n], x1 [n], ..., xM 1 [n]. Example: A moving average is a linear combination of the signals x[n], x[n 1], ..., x[n (M 1)]. What are the values of the coecients ck ?
2.4
Unit-sample Synthesis
Any signal x[n] can be represented as a linear combination of delayed unit-sample sequences [see equation (2.72) in the book):
n2
x[n]
=
k=n1
x[k][n k]
(8)
Exercise: Represent the following sequence as a linear combination of delayed unit-sample sequences x[n] = 2, 4, 1, 2 .
(9)
2.5
Unit-step Synthesis
Similarly, it is possible to represent x[n] as a linear combination of unit-step sequences. Exercise: Represent the previous x[n] as a linear combination of delayed unit-step sequences.
2.6
Multiplication
Multiplication of two signals is analogous to addition, i.e. samples at the same time instant are added together. This is written as: y[n] = x1 [n] x2 [n] To multiply signals in Matlab with the same time scale, use .* n = 0:12; x1 = exp(-n/4); x2= cos(pi*n/4); y = x1.*x2; The period before the asterisk is important because it signals Matlab to perform an element-byelement operation. Without the period, it will try to do a vector-vector multiply (which will fail due to mismatched dimensions). To multiply signals with dierent axes, you may use: function [y,n] = sigmult(x1,n1,x2,n2) % implements y(n) = x1(n)*x2(n) % ----------------------------% [y,n] = sigmult(x1,n1,x2,n2) % y = product sequence over n, which includes n1 and n2 % x1 = first sequence over n1 % x2 = second sequence over n2 (n2 can be different from n1) % n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n) y1 = zeros(1,length(n)); y2 = y1; % y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y y = y1.* y2; Exercise: Using sigmult, multiply x1 [n] = exp(n/4), 4 n 8 with x2 [n] = cos(n/4), 0 n 12. (10)