LAB 6 F. A. Q.

Updated on 20-Feb-2000

0. Some of these questions don’t seem to make sense with respect to this semester’s lab.

ANSWER: Correct. The FAQs are based on the labs from Fall-99. Refer to that lab description when certain section numbers are cited.


3. Warmup

1. I couldn’t use the inout function to plot the envelope of input and output signals. I got the following error:

inout(x1,y1,0,10,4) ??? Index into matrix is negative or zero.

ANSWER: Use a starting index of 1 instead of 0 for the third argument. MATLAB dislikes zero as an index.


2. For some reason, every time I enter the command “load lab6dat“, I get this error message:

??? Error using ==> load Can’t read file.

I’ve checked the permissions on the file and directory that it’s under. I’ve even tried reading it from different places, but it still won’t let me load that particular file.

ANSWER: If you originally downloaded the lab6dat.mat file directly it may have been corrupted by your browser (see below). Try downloading the ZIPPED version of the file instead. More detailed explanation: When a browser downloads a text file, it changes the file’s line terminators to match the local machine’s setup. This is usually a good thing since the line terminators on the UNIX/MAC/WINDOWS platforms are all different. Most browsers, however, incorrectly treat the binary .mat files as text files and wind up corrupting the file during the download process!


4.1 First Difference Filter

1. In lab6dat, x1 is a 1 x 100 vector, meaning it has elements ranging from x(1) to x(100). So, when you make a “stem plot” of it, the elements go from the first to the hundredth. However, in 4.1.b, we are told to label our axis to run from 0 to 39 and plot the first 40 terms. (i.) Does this mean that it is not acceptable to graph x1 with the axis going from 0 to 39, but with values beginning at element 1? (ii.) If the answer to (i.) is no, and we are supposed to manipulate our plot — do we only manipulate it for 4.1? Or, when we need to plot another stemplot, perhaps to aid the explanation in 4.2(c), must we manipulate it again?


  1. EXCELLENT question, because this causes endless confusion. MATLAB wants to index from 1 to 100. DSP wants to index from 0 to 99. When you make plots, use the DSP indexing. You have to feed the plot( ) or stem( ) command a vector that contains the indices, so make it up as 0:99. Use 0:39. Don’t start the plot at n=1.

  2. In ALL DSP (stem) plots you should feed two arguments to stem( ). One for the indices and one for the signal values. Thus you end up with something like stem( 0:39, xx(1:40)). It’s a pain, but the mathematicians like to count starting at 1, and they defined MATLAB in its early days.

4.2 Time Invariance of the Filter

1. I am not quite sure what is being asked in this section. Do you want us to prove that a first-difference filter is time-invariant by showing that w1 = z1, or do you want us to prove that w1 = z1 given that the first-difference filter is time-invariant?

ANSWER: YES. if you demonstrate that w1=z1, then you can say that you are verifying the time-invariance property.


4.2 Linearity (Superposition) of the Filter

1. How can we get w1 to equal z1? If y1 is x1 modified by the first difference filter, then w1 will be y1 delayed by three, while z1 will be x1 delayed by three. Since y1 does not equal x1, how can you expect to delay each by 3 and expect the results to be equal?

ANSWER: Look at figure 5.16 on page 139 in the book. In one case you delay then filter; in the other case, you filter then delay. In part (b), z1 is delayed and THEN filtered through the first difference filter.


4.3 (b) Linearity (Superposition) of the Filter

1. It is not quite clear to me what is being asked in this portion; perhaps someone can reword the question?

ANSWER: you need to generate the impulse response of an FIR filter. Make an input signal (vector) that is mostly zeros except for a single one (that is an impulse), then run it through firfilt().


2. For xb[n]=delta[n] does this mean xb[n] = delta[n]+delta[n-1]+delta[n-2].. delta[n-9]?

ANSWER: NO. xb[n]=delta[n] means you have a length-10 vector containing 9 zeros and one 1. In your second case, the vector would have 10 ones.


3. (Refer to question #2) How do I find out which entry of the 10 gets the one? Is it arbitrary?

ANSWER: You have to keep track of which one is 1, because that is n=0 when you make a plot.


4.4 (a) Cascading Two Systems

1. The firfilt function takes in the values of bk and the signal xx. I understand how to get the bk values (i.e. alpha raised to the power of the l index) but how do we compute xx?

ANSWER: You want the input to be delta[n]. Make a vector containing a single one and lots of zeros. The location of the 1 is the n=0 index. For example,

xx = [0,0,1,0,0,0,0,0];

In this case, the range of “n” indices for xx will be n=-2 up to n=5. Here’s another way to do the same thing, using the 0/1 property of logical tests:

nn = -2:5; xx = (nn==0);


2. Concerning the bk values and the xx vector, how many coefficients do we need? i.e., [1,zeros(1,?)].

ANSWER: The number of zeros shouldn’t matter, but you do have to keep track of the index location for the 1. MATLAB starts indexing from 1, but all of our mathematical formulas start from zero. Thus, when you make an xx vector, you should also make a corresponding nn vector of indices. That’s why I’d suggest something like the following:

nn = 0:10; xx = (nn==0);

Then you have the indices “nn” for the values “xx.” You should also worry about the indexing range of the output signals.


4.4 Cascading Two Systems

1. The footnote for this section of the lab says that ‘The cascade of FIR Filters #1 and #2 does not perform perfect deconvolution’. But if I use delta[n] as x[n] for the input to the cascade system, I get delta[n] out. Isn’t that perfect deconvolution?

ANSWER: Shouldn’t get delta[n] out for those filters….double check the MATLAB code(?) What does the math predict? The same math was on a recent HW problem, as well as in this lab.


2. (Refer to question #1) Let’s say M=2, and alpha = 0.8. The impulse response of Filter #1 is:

h1[n] = {.8^0*delta[n-0],.8^1*delta[n-1],.8^2*delta[n-2])

h1[n] = { 1, 0.8, 0.8^2 }

So y[n] = w[n] – alpha*w[n-1].. assuming for n<0, w[n] = 0, we get:

y[n] = { 1-0, 0.8-(.8*1), 0.8^2-(0.8*0.8) } y[n] = { 1, 0, 0 }

Am I missing a step, doing something wrong?

ANSWER: You need one more term in y[n]. determine y[3], and y[4] and y[5]. Use conv() or firfilt() to confirm. A length-3 input into a 2-point FIR gives a length-4 output. In general, convolving a length-N input with a length-L impulse response will give an output whose length is N+L-1


3. What is the input vector x[n]that we should use to perform the convolution and deconvolution of the cascade system? Do we use x1 from the lab6dat file or do we just use letters?

ANSWER: In part 4.4(a) the input is delta[n]. Make a MATLAB vector that is delta[n]. Parts (b) and (c) are math.


    1. (a) Cascading Two Systems

1. In the previous posts, it was suggested that we use

nn = 0:10;

xx = (nn==0);

for part 4.4a, but when I try to do a stem plot, MATLAB will say that the index into matrix is zero (since we start nn at 0) and will not plot. Is there any way around this?

ANSWER: MATLAB insists that all INDEXING into a vector START at 1. Thus, xx(nn) would not work. However, stem( nn, xx )should work. Or,

nn = -10:30; stem(nn,(nn==0)).


2. I don’t understand how to do the convolution to produce the impulse response because w[n] is given in terms of x[n] and y[n] is in terms of w[n].

ANSWER: This is a cascade of two systems….just like the example done in lecture; also, it was a homework problem. Put the impulse in….get w[n], then use w[n] as the input to the 2nd system.


4.4 (c) Cascading Two Systems

1. In this portion, we are told to find the condition for h1[n]*h2[n] to achieve perfect deconvolution. The side note suggests that FIR Filter #1 and FIR Filter #2 do not perform perfect deconvolution. According to what I understand, FIR Filter 1 and FIR Filter 2 don’t come anywhere near deconvolution. Are we supposed to create a new filter that will undo the effects of cascading system, and is the cascading system supposed to perform deconvolution?

ANSWER: Be sure to explain these facts in your lab report. You need to have a condition that gives perfect deconvolution in order to say why you don’t have perfect deconvolution. Also, how close you come to “prefect deconvolution” might depend on the value of alpha. Cascading FIRs will NOT perform *perfect* deconvolution, as the lab states. No need to create new filters—use the ones given in the lab. Finally, section 4.4 is a “set up” for section 4.5. Do the work in 4.5 and come back to 4.4 when you write explanations.


2. How is the product of h1*h2 related to deconvolution?

ANSWER: The output is y = h2 * ( h1*x ), so h1 convolves with the input x, and then h2 tries to undo that convolution. Hence the h2-convolution can be called “deconvolution.”


3. Do we need to find the actual values of h2 or do we just find the mathematical identity?

ANSWER: It would be VERY HARD to find the values for h2[n]. Just state the condition that h2[n] and h1[n] have to satisfy.


4. Do I need to get rid of the gray background, or can I ignore this because the point of this is to see if the letters are fine? I can definitely tell that the difference between the different values of alpha.

ANSWER: YES, don’t worry about the gray background. The issue is to evaluate the deconvolution process for different values of alpha.


4.5 Blurring and Deconvolving Images

1. I don’t understand why I am getting a gray background. Can you suggest why?

ANSWER: Filters introduce negative values, especially when you compute differences. therefore, the grayscale range has to be adjusted. If you “autoscale” then the most negative value gets mapped to black, and zero is mapped to some lighter black level. Instead, you could “clip” the values, throwing away all negative values, but that would **distort** the final image. Consult help show_img.

2. The result of deconvolving echart9 always comes with a dark background, do you have any idea when this is occurring?

ANSWER: Make sure that you use colormap(gray(256)); also, check the minimum and maximum values in the image. If they are about the same absolute value, then zero values will be in the middle of the gray scale.


3. Every factor of alpha that I tried still let me down. I am still getting a triple picture. I understand the gray background is relatively normal, how about the triple letters? Are we only supposed to choose between the three values of alpha that are provided in the lab statement, i.e., 0.8, 0.9 and 0.95?

ANSWER: Did you try part (d) where the blurring operator is different because you use alpha = 0.7 ? You have two different blurred images echart7 and echart9 that you will try to de-blur. You should be able to explain the existence of the “triple letters” by look at the effective impulse response of the cascade. In 4.4 you should have figured out that effective impulse response. For the image case, look at one row and try to predict what is happening. Then look at one column.


4. Apparently, the second filter does not always do a great job of restoring the original image. But, in my images, the second filter always works great. I mean, sometimes its a little more blurry than other times, depending on alpha, but you have to really stare and be really picky to notice the difference. Never do I get anything like triple letters…am I doing something wrong?

ANSWER: You should see artifacts (such as triple letters) in some cases. Furthermore, you should be able to use convolution (with the overall impulse response) to predict what you will see, and also to explain the dependence on alpha.


5. After deconvolving, the 3 resulting images varied rather drastically. One was gray (0.9), (0.5) was blurry with a white background, and (0.7) was crystal clear. Should all of them have a gray background like the images in parts c?

ANSWER: Sounds reasonable. NOW…the important issue is explaining what’s going on. Use the impulse response derived in 4.4 to say why the “crystal clear” one works as it does. You’re right that the “crystal clear” one is NOT identical to the original, but can you explain what the subtle differences are? The h[n] of the cascaded “blur-then-decon” system will provide the answer. Bottom line: getting it to work is fine, but understanding why it’s working the way you observe is DSP.


6. Should we use firfilt(),conv2(), or does it matter?

ANSWER: Conv2() can filter all the columns (or rows) at once. Firfilt() would have to be put in a loop to do one column at a time. Conv2() should run a lot faster. Other than that, they do the same FIR filtering operation.


4.5 (c) Blurring and Deconvolving Images

1. Are we supposed to use the given Filter#2 from part 4.4, and just fill in the value for alpha? Everything I’ve tried comes out with multiple copies of each letter in a different shade.

ANSWER: YES, use filter #2 from 4.4. Decon is trying to undo the blurring. Evaluate the output for blurs as well as artifacts. You should find some values of alpha that produce an almost-perfect deconvolution of the filtered image.


2. I am still confused about how to change the filtered image so that the back ground is not so gray. I’ve consulted help show_img, but that doesn’t tell me much. Do we need to change the color range or the scaled variable? One of the posts said to make sure to use colormap((gray(256)), but that didn’t help. My background is about the color gray that is in the border of the display window. How much gray is too much???

ANSWER: Not much you can do about this. Your processed image has both negative and positive values. Your input image had only non-negative values.

INPUT: 0 is black, positive valued pixels are white.

OUTPUT: negative-valued pixels get mapped to black; positive-valued ones to white. zero-valued pixels get mapped to gray.

You might look at one line of the image; input and output to see if you can tell which values are being mapped to which gray levels.


3. What differences am I looking for with the different values for alpha? In my images, the only difference is the contrast with the gray background.

ANSWER: Looking for 2 things:

1. deblurring – is the result SHARPER than the blurred image?

2. artifacts – do you see “annoying” features that should not be present?


4. When I did this part, I got 2 echarts side by side in one figure, why is that?

ANSWER: Look at the impulse response of the row filtering. This is what you should have figured out in 4.4. Think of convolving h[n] with each and every row. The “effective” h[n] should be very simple and you should be able to predict the 2 echarts that you are seeing.

5. Is it alright to use the Z-transform in our explanation for this part of the lab? Because the book provides a pretty clear cut explanation of why the filter we use to deconvolve doesn’t do so adequately, but it explains it in terms of the z-transform.

ANSWER: I suppose so. In this case the z-transform would evaluate the convolution. I think either the h[n] for the cascade or the H(z) for the cascade would lead to the same conclusions. You could probably “translate” the z-transform reasoning back into an explanation in terms of h[n]. Either way—understanding is the key, the method is secondary.


6. We ARE supposed to use Filter#2 from 4.4 and the image should NOT perfectly resemble the original image since there will be double images etc.? Out of all the values of alpha, we are supposed to pick the one that resemble it the most (sharpest)?

ANSWER: Yes, use filter #2 from 4.4. One case should work really well. Others have artifacts: either residual blurring or ghosts.


    1. Filtering a Music Waveform (Extra Credit Portion)

1. I tried creating the filter mentioned in 4.6, but I had a vector of filter coefficients that was almost 2000 elements long, and almost every element was zero. My computer crashes while attempting to convolve my 5-second portion of the song with the filter coefficient vectors. Is there another way to do this, because the amount of computations necessary seem unreasonable?

ANSWER: This is a case where using the built-in filtering function (firfilt) is a BAD idea….crashing is bad. Since most of the filter coeffs are zero, can you implement the delay operation directly ? In other words, figure out how to implement equation (4) with a couple of lines of MATLAB code. If the input signal is in a vector called xx, then you have to delay (or move) the values in xx and then add. Addition of vectors will require that you observe MATLAB’s vector constraints of adding vectors whose lengths are the same. (Said another way, you have to worry about end effects.)