Professional Documents
Culture Documents
DSP Using MATLAB and Wavelets: Mymax (x1) Ans
DSP Using MATLAB and Wavelets: Mymax (x1) Ans
>> mymin(x1)
ans =
-2
>> mymin(x2)
ans =
-2.6992
>> mymax(x1)
ans =
2
MATLAB 75
>> mymax(x2)
ans =
4.5000
Answer:
Array t does not need to be adjusted, but we have to be careful about n.
That is, x1[n 1] does not exist when n = 1 (MATLAB will not let us index an
array at location 0). Therefore, we have to start n at 2, so that n 1 is a valid
array value. Also, notice that we use parentheses for MATLAB code, in
place of the square brackets.
for n=2:length(x1)
y1(n) = x1(n) - .9 * x1(n-1);
end
plot(y1)
5. Create y2[n] to be the average of the current and previous two values of
x2[n]. Plot y2.
Answer:
We have to be careful with our starting value for n, as in the problem above.
for n=3:length(x2)
y2(n) = (x2(n) + x2(n-1) + x2(n-2))/3;
end
plot(y2)
Answer:
You have to give y3 an initial value to use.
y3(1) = 0;
for n=2:length(x2)
y3(n) = x2(n) - y3(n-1);
end
plot(y3)
for n=length(x2)+1:200
y3(n) = 0 - y3(n-1);
end
plot(y3)
Answer:
From the help rand function, we can see how to use the rand function. For
example, rand(1,8) returns 8 random values between 0 and 1 as an array.
The numbers should range from low to high, so we multiply the values by
(high low). Next, we add low to the results.
ans =
10
>> max(x)
ans =
90
%
% Return a vector of m random values
% between low and high.
%
% Usage: result = myrand(low, high, m)
%
function result = myrand(low, hi, m)
Answer:
%
% mysort.m
%
% Given an array of numbers, sort them.
% (Use a bubble-sort since it is simple).
%
function sorted = mysort(unsorted)
sorted = unsorted;
done = false;
78 DSP Using MATLAB and Wavelets
while (~done)
done = true;
% Go through the whole array,
% until no switches are made. for
k=2:length(sorted)
% check the current value
% to its left neighbor
if (sorted(k-1) > sorted(k))
% Switch the two temp =
sorted(k); sorted(k) =
sorted(k-1); sorted(k-1) =
temp; done = false;
end
end
end
unsorted =
93 91 41 88 6 35 81 1
>> mysort(unsorted)
ans =
1 6 35 41 81 88 91 93
9. Calculate the average from an array, and call your function \average."
Answer:
Yes, this could be done easily with the following one line command:
>> sum(unsorted)/length(unsorted)
ans =
MATLAB 79
54.5000
But in the spirit of doing things for ourselves, here is an alternate solution.
%
% average.m
%
% Find the average of an array of numbers.
%
function avg = average(x)
mysum = 0;
for k=1:length(x)
mysum = mysum + x(k);
end
avg = mysum/k;
Answer:
The ceil and floor functions both return an integer value. If a value divided
by 2 is the same as the ceil or floor of that number divided by 2, then it
must be even.
%
% evenodd.m
% Print whether a number is even or odd.
%
% Usage: evenodd(unsorted_array)
%
function evenodd(value)
if ((value/2) == floor(value/2))
% The number is even.
disp( even );
else
80 DSP Using MATLAB and Wavelets
>> evenodd(7)
odd
>> evenodd(8)
even
11. Find the median value from an array, and call this function \mymedian." (A
median is the value in the middle of a sorted array. If there are two values
in the middle, which happens when the number of elements is even, the
median should then be the average of those two numbers.)
Answer:
%
% mymedian.m
% Return the median value of an array
%
% Usage: m = mymedian(unsorted_array)
%
function m = mymedian(unsorted)
m = (sorted(k) + sorted(k+1))/2;
else
% There are an odd number.
% Just get the middle number m
= sorted(k);
end
12. Use the disp and sprintf functions as needed to print the time in a nice
format.
Answer:
time = clock;
disp(sprintf( Current time %2d:%2d:%2d , ...
time(1,4), time(1,5), round(time(1,6))))
(Notice that there is no semicolon after sprintf since we want it to appear on the
screen.)
plot(1:length(x), x, r)
break
return
close all
clear all
who
whos
save
load
zplane
fir1
vpa
82 DSP Using MATLAB and Wavelets
surf
fopen
fread
fwrite
fprintf
fprintf(1, text for a file );
fscanf
fclose
Time-related functions:
tic / toc
clock
cputime
Wavelet-related commands:
MATLAB 83
wfilters
wavedec
dwt / idwt
dwt2 / idwt2
2.14 Summary
This chapter presents an overview of MATLAB, and demonstrates some useful
com-mands. Matrix operations are important to DSP. In fact, we can implement
trans-forms with matrix multiplications.
2. Write a MATLAB function that removes duplicate numbers. That is, given
an array [5, 2, 2, 1, 4, 4, 4], it should return [5, 2, 1, 4]. Assume that the rst
occurrence of a number is kept, such as in the case of the array [7, 3, 8, 3,
7], where your function should return [7, 3, 8].
3. If x = f121, 145, 167, 192, 206g, what is y = 3x 4? Is sum(y) the same value
as 3sum(x) 4?
4. Use the plot command to graph x and y from above. Both x and y should
appear on the same graph.
5. Using MATLAB, plot the sinusoid 2 cos(2 1500t + =2), starting at t = 0 sec-
onds. Be sure to use enough points to make a smooth graph, and only
show a few repetitions.
for any value N . What will be the result if N is an array? If your function
does not work when N is an array, modify it so that it will. It should return
an array of the same length.
8. The following MATLAB code should return the magnitudes and phases for
an array of complex numbers. The line marked \problem" actually has two
problems with it.
yr = real(y);
yi = imag(y);
for i=1:length(yr)
phi(i) = atan(yi(i)/yr(i)); % problem
mag(i) = sqrt(yr(i)*yr(i)+yi(i)*yi(i));
end
Filters
We are often interested in manipulating a signal. For example, you might turn up
the volume on a stereo system, boost the bass (low-frequency sounds), or adjust
sounds in other frequency ranges with the equalizer. These examples are not neces-
sarily digital signal processing, since they can also be done with analog parts, but
they do serve as examples of ways we might want to change a signal. Filters allow
us to change signals in these ways. How does a lter function? Architecturally, how
do we make a lter? We will examine these questions and more in this chapter.
Figure 3.1 (top) shows an example signal, the low temperatures from Chapter 1,
\Introduction." In the middle, we see the output from a lowpass lter, i.e., a lter that
keeps the low-frequency information. Notice how much it looks like the original. Also,
you may see that the ltered version has one more value than the original, owing to
the e ect of the lter (the number of lter outputs equals the number of inputs plus the
number of lter coe cients minus one). The bottom graph on this gure shows the
output from a highpass lter, which allows the rapidly changing part of the signal
through. We see that most of the points are around zero, indicating little change.
However, a visible dip around output 5 shows how the big change between input
samples 4 and 5 becomes encoded here.
Shown in Figure 3.2 is the original frequency content of the example input
signal (top). The distinctive pattern seen here owes its shape to the sharp drop
between the end of the input signal and zeros used to pad the signal. The sinc
function can be used to model this phenomenon, and often windows are used to
minimize this e ect by allowing the signal to gradually rise at the beginning, then
slowly taper o at the end. A triangle function is one example of such a window.
In the middle of Figure 3.2, the lowpass impulse response appears, which gives
us an idea of how the lter will a ect our signal. The low frequencies will get through it
well, but the higher frequencies will be attenuated (zeroed out). For example, an
85
86 DSP Using MATLAB and Wavelets
Original
75
70
65
60
550 5 10 15
Lowpass filtered
75
70
65
600 2 4 6 8 10 12 14 16
Highpass filtered
5
100 2 4 6 8 10 12 14 16
input frequency of 60 Hz will still be present in the output, but only at about 10%
of its original strength. We can see this by looking at the spot marked 60 along
the x-axis, and looking up to see the lowpass impulse response is right around
the 10% mark on the y-axis.
The bottom plot on Figure 3.2 has the highpass impulse response, showing the
opposite e ect of the plot above it. This one will allow the high-frequency content
through, but attenuate the low-frequency content. Note that both lters here are not
ideal, since they have a long, slow slope. We would expect that a good lter would
have a sharper cuto , and appear more like a square wave. These lters were made
using only 2 lter coe cients, [0.5, 0.5] for the lowpass lter, and [0.5, -0.5] for the
highpass lter. These particular values will be used again, especially in Chapter 9,
\The Wavelet Transform." For the moment, su ce it to say that having few lter coe
cients leads to impulse responses like those seen in Figure 3.2.
0.5
0
10 20 30 40 50 60
Lowpass impulse response
1
0.5
00 10 20 30 40 50 60
Highpass impulse response
1
0.5
00 10 20 30 40 50 60
Figure 3.2: The frequency content of the example signal, and low/highpass lters.
Finite Impulse Response (FIR) lters are a simple class of lters that perform
much of the work of digital signal processing. They are composed of multipliers,
88 DSP Using MATLAB and Wavelets
adders, and delay elements. In diagrams showing lters, we show a plus sign
within a circle. Strictly speaking, this is a summation whenever the number of
inputs is greater than two. Architecturally, the summation can be implemented
with an adder tree.
Multipliers are rather complex parts, so sometimes they may be replaced by
simpler parts. For example, to multiply by 8, it is easier to shift the number three
places to the left (since 23 = 8). Of course, the drawback here is that a shift
register is not as exible as a multiplier, and this would only work if the value-to-
multiply-by is always a power of 2.
The delay elements can be thought of as registers, in fact, since registers are
used to store a value for a short period of time, a register actually implements a
delay element. Having two registers in a row has the e ect of delaying the signal by
two time units. In Figure 3.3, we show this for the general case. The output from the
delay unit(s) appears as a time-shifted version of the input. When the second input
value enters a single delay unit, the rst input value would exit, leading to x[n] entering
on the left and x[n 1] exiting on the right. If we consider a group of
k delay units, x[n k] would exit as x[n] enters.
x[n] x[n1]
Delay
The name \Finite Impulse Response" comes from the way the lter a ects a
signal. An impulse function is an interesting input, where the signal is 0
everywhere, except for one place where it has the value of 1 (a single unit). An
FIR lter's response to this input is nite in duration, and thus it bears the name
Finite Impulse Response lter.
Example:
If a = [1, 2, 3, 4] and b = [2, 1, 2, 1], what is c? See Figure 3.4.
b[n]
a[n] c[n]
Answer:
c[0] = a[0] + b[0], c[1] = a[1] + b[1], c[2] = a[2] + b[2], c[3] = a[3] + b[3]
c[0] = 1 + 2, c[1] = 2 + 1, c[2] = 3 + 2, c[3] = 4 + 1
c = [3, 3, 5, 5]
Thus, we conclude that c[n] = a[n] + b[n].
Example:
If a = [1, 2, 3, 4] and b = [2, 1, 2, 1], what is c? See Figure 3.5.
b[n]
a[n] c[n]
Answer:
c[0] = a[0] b[0], c[1] = a[1] b[1], c[2] = a[2] b[2], c[3] = a[3] b[3]
90 DSP Using MATLAB and Wavelets
Example:
If a = [1,2,3,4] and b = [2,1,2,1], what is c? See Figure 3.6.
0.5
a[n] c[n]
0.5
b[n]
Figure 3.6: An example FIR lter with coe cients 0.5 and -0.5.
Answer:
c = [1 0:5 2 0:5; 2 0:5 1 0:5; 3 0:5 2 0:5; 4 0:5 1 0:5]
c = [-0.5, 0.5, 0.5, 1.5]
We see that c[n] = 0:5 a[n] 0:5 b[n].
The delay unit shifts a signal. We assume that any signal value outside of our
index's range is 0, so if a signal is delayed by one unit, we can insert a 0 at the
beginning of it. For example, if we delay signal x by one unit to form signal y,
Figure 3.7, and signal x = [1, 3, 5], then signal y = [0, 1, 3, 5]. How do these two
signals relate mathematically?
x[-1] = 0, y[0] = 0,
x[0] = 1, y[1] = 1,
x[1] = 3, y[2] = 3,
x[2] =5 y[3] = 5
Clearly, y[n] = x[n 1].
Delay units are sometimes shown with \z 1" in place of the \D." The reason
for this will have to wait until Chapter 8, \The z-Transform," but for now, just treat
the two as equivalent.
Filters 91
x[n] D y[n]
x[n] y[n]
D 0.5
Example:
For Figure 3.9, if x = f1g, what is y? Express y as an equation.
0.6
x[n] y[n]
D 0.2
Figure 3.9: An example FIR lter with coe cients 0.6 and 0.2.
Answer:
It is easy to see that for an input of 1, the output is 0:6. But we must not stop
there, since this 1 will be the delay unit's output on the next time step. We can
(and should) assume that all future inputs outside of the ones given are zero. On
the next time step, we compute y[1] = 0:2. For the following time step, notice that
the last nonzero input (i.e., 1) no longer appears, and we can stop here.
y = [0:6; 0:2]
Figure 3.10 shows the general form of the FIR lter, for K + 1 lter coe cients.
(There are K + 1 of them because we start at 0 and count to K.) The number of
lter coe cients is also called the number of taps. By convention, the number of
taps equals the number of lter coe cients. So a lter with coe cients fb0, b1, ...
, bK g has K + 1 taps, since there are K + 1 total lter coe cients. However, it is
said to be of order K. In other words, the order of the lter and the taps express
the same idea, but with a di erence of 1.
With the structure of Figure 3.10 [11], it is possible to determine the output. It
is also possible to determine an equation for the output, which is
Notice that whatever index is used for b[ ] is also used in x[n ]. This means we
can represent everything on the righthand side of the equation as a summation.
K
X
y[n] = b[k]x[n k]
k=0
Example:
Given b = [0:1; 0:2; 0:3] and x = [1; 0; 0; 2; 0; 1; 4; 3], what is y?
Answer:
y[n] = x[n 2]b[2] + x[n 1]b[1] + x[n]b[0]
This can be found by noting the above equation, then making a chart.