LAB 3 F. A. Q.
Updated on 12-Jan-2000
(warmup 2.6a) Given a = [-1,0,1] what does the following lines of code do?
a= a.*(a>0)
a=a+pi.*(a= =0)
ANSWER: These are explained in Appendix B of the book. Also, you may run tests/examples in MATLAB. When a is a matrix, (a>0) tests each element of the matrix "a"
versus zero and returns a new matrix filled with 0s and 1s. (1 means true; 0 means false)
Example below: notice how the negative elements become zero in a>0. Then the pointwise multiply ZAPS all the negative elements in the original “a” matrix.
ªa = randn(3,4)
a =
-0.4326 0.2877 1.1892 0.1746
-1.6656 -1.1465 -0.0376 -0.1867
0.1253 1.1909 0.3273 0.7258
ªa>0
ans =
0 1 1 1
0 0 0 0
1 1 1 1
ªa .* (a>0)
ans =
0 0.2877 1.1892 0.1746
0 0 0 0
0.1253 1.1909 0.3273 0.7258
(3.2) I am trying to write the code for the vsinus() function and I keep getting an error that the inner dimensions do not agree, but I think the code is right. Here are the two main parts of the code:
tt = tstart: 1/fs : dur; xx = real(Xk * transpose(exp(j*2*pi*fk*tt)));
Any suggestions?
ANSWER: Check the dimensions of all the vectors. Are Xk, tt, and fk all row vectors or all column vectors or some of both? Use size() or whos to confirm. Use the **DEBUGGER** to stop inside your function and examine what’s going on. Then you don’t have to guess. Additionally, in lieu of debugging, you could change your code to be the following:
tt = tstart: 1/fs : dur;
size(tt)
size(fk)
size(Xk)
xx = real(Xk * transpose(exp(j*2*pi*fk*tt)));
Then, before the program crashes on the xx line, you would have info about the dimensions of the vectors. This is the "Print statement approach" to debugging, and it has the disadvantage that you have to add code to your function.
Also, read the “vectorization help” link from lab #3….under the “Lab Assignments.” It is a short PDF file that says a lot about the matrix product that you are trying to create.
You do want matrix multiply, so .* is out. The exp() will produce a matrix—what do you want its dimensions to be? (HINT: consider the fact that you are going to multiply it by the vector Xk.)
(3.2) The lab says that we need to use form (4) for the waveform, but this form takes the real part of the summation; the imaginary part will be deleted from the x(t). Should I fix that?
ANSWER: You WANT to plot the real part. You are throwing away the imag part—it is not used at all. Remember in Euler’s formula that the real part gives the cosine part.
(3.2) I am having a really hard time understanding matrix-vector multiplication for the writing of the vsinus() function. What is the command for matrix-vector multiplication??? I use the .* (point-star) and * (star) operators, and it says either that the matrix dimensions don’t agree, or the inner dimensions don’t agree?
ANSWER: Read pages 402 and 403 in Appendix B. For matrix-vector multiply, use * (star). That is normal matrix multiplication. In A*B, the number of columnss of A must equal the number of rows in B. Also, did you read the PDF file that was “linked” in with the lab? If you want element-by-element multiplication use .* (point-star).
(3.3b) With regard to the hint that is given, it says that if I multiply two matrices together, I can get a sum of products in a one-dimensional matrix. If I multiply a 3 by 4 matrix with another 3 by 4 matrix, I’m still getting a 3 by 4, not a 3 by 1. So how do you do this?
ANSWER: First of all, multiplying a 3 by 4 matrix with another 3 by 4 matrix will FAIL.
The matrix product C = A*B would have B as a column vector, so you might have the following: A is 3 by 4 and B is 4 by 1. What is the dimension of C? ( ans: 3 by 1!)
(3.4) When I tested the data with my vsinus() function, the plots I got were straight lines rather than sinusoids. I think it is because the result I got is in rectangular form (ex: 1+2j). How can I fix this problem?
ANSWER: Examine the numbers coming back from vsinus(). Usually the “straight lining” is caused by having the wrong time vector, tt.
(4) Parts 4 and 5 did not specify the complex amplitude of the sinusoid we are to construct. Does that mean we can choose any complex amplitude at random?
ANSWER: sections 4(a) and (b) are the ONLY places where the complex amp is NOT specified. So they are the only places where you can choose the amplitude to be anything EXCEPT zero.
(4a) For generating a sinusoid I assume we have to use the vsinus() function. What do I put in for the complex amplitude vector? Is there a way to determine the amplitude & phase from the given information?
ANSWER: NO, but the amp and phase don’t matter. Soundsc() scales out the amplitude.
In addition, the human ear can’t detect the phase of a sine wave.
(4b) We are asked to use concatenation to play the sounds, and also use the onenote() function. The onenote() function plays a single note given the key number. So how would I store the values of every single note (without having them played) so I can concatenate it?
ANSWER: Read the lab instructions carefully. This is discussed on the top of page 9 (Fall-99 version of the Lab). Use xx = [ xx1, xx2, xx3 ]; where xx1 holds the first note, xx2 the second one, etc.
(4d) The two plots we are supposed to make should be sinusoids from which we can determine the frequency rather the frequency response plots, right?
ANSWER: CORRECT!
(4.1) In this part of the lab, we are asked to make all amplitudes equal and pick random phases. Does that mean we do not utilize the onenote() function, since in the onenote() function all the complex amplitudes are the same? In our case, all amplitudes are the same, but the
complex amplitudes don’t have to be the same.
ANSWER: onenote() produces a single note, so there is only ONE complex amp. You have to call it several times, so each call should use the same amplitude, but a random phase.
(4.1b) For the concatenation, how do you use zeros for the 0.5 second pauses?
ANSWER: Make a signal that is all zeros for the correct duration. Or, think of making a sinusoid that has zero amplitude and the 0.5 sec duration.
(5a) I wrote a function that will automatically calculate the sum of the signals for different N. The problem that I am having is that my amplitude is not big enough to get a square wave as N gets large. I am getting a straight line instead. Any suggestions?
ANSWER: Two possiblities:
1. The sampling rate is wrong
2. The amplitudes are too small (like you say).
In either case, you need to look at the values while your function is running. Try to use the debugger to stop within the function and then inspect the values, OR just display some of the variables inside the function. You can do that by removing some semi-colons.
(5a) This specifies k = 0 and Xk = -1/8; should we try to generate three cases, one for each of N = 3,9, and 15? i.e., the frequency vector filled with zeros to N capacity and the Xk vector filled with repeated -1/8’s?
ANSWER: NO. You have a different formula for k not equal to zero. You only use the k=0 formula once in each Xk vector. So, the first three elements, for example of the Xk vector should be calculated as [-1/8, j/2*pi*k, 0, …].
(5a) Periodic Waveform Synthesis: Could you please re-word this question, because I still don’t understand what is being asked?
ANSWER: Have you read the section(s) in Chapter 3 that discusses this? There are a couple of excellent examples there. Basically, I am giving you a list of frequencies and complex amps, and asking you to synthesize the signal. The frequencies are harmonics, so you can figure out their values. The complex amps are specified by a formula that you can evaluate. So, you need to call vsinus() to add them all together and take the real part. The result is a signal, x(t). Like in the “testing vsinus” section of the lab. Also, recall the vowel waveform from class, or do part (c) of lab 3-5 before doing part (a). It is the same thing.
More explicitly, N is the summation limit, so k gets values from 0 to N. Calculate the vector of freq. (fk) by doing fk = k*f0, where f0 is fundamental freq. Xk is calculated by (7) when
k = 0, Xk = -1/8 when k is odd number (1,3,5,7,..) Xk = j/(2*pi*k) when k is even (2,4,6,..) Xk = 0.
(5a) First, can I make a separate function to produce the fk and Xk vectors? Also, since k goes from 0 to N, shouldn’t the first index be fk= 0 and Xk= -1/8 for all values of N?
ANSWER: Sure. Actually you should be making a script file to call vsinus(). That script m-file will define the fk and Xk vectors. In answer to your second question, yes for k=0, f_0 = 0 and X_0 = -1/8, and yes, it must be done this way for all values of N. Also, you can “vectorize” almost any "for" loop, so you should try to vectorize this assignment. Consider using the “point divide” operator, e.g., 1 ./ [2,3,4];
(5a) I wanted to know how you could reference all the odd or even integers in a vector? for example if I had x = [1, 2, 3, 4] and I wanted to replace all the even integers with another value, how would I reference them?
ANSWER: This exact problem was done in the warm-up of lab #1. I’ll let you look that up. We knew you’d need it later on. (Hint: use the colon notation.)
(5a) Does N represent a summation from 0 to N (and not 1 to N)?
ANSWER: YES!
(5a) For n=3,9,15, are all the plots supposed to look the same?
ANSWER: NO. You are adding in additional sinusoids as you go from N=3 to N=9 to N=15 , so it has to change. Same thing for the vowel case later on in 3.5, part (c).
(5a) We are given three N values, are these the values we have to substitute for k? For example:
To = 150msec; fo = 1/(150) = 7Hz; fk = 7×3 = 21Hz; Xk = j/2*pi*(3) = j/6*pi;
so, [xx,tt] =vsinus([21],[j/6*pi],11025,-0.3,0.3)is this right?
ANSWER: NO ! N is the upper limit on the summation, so when N=7 you have to generate 7 freqs and 7 complex amplitudes and pass them to vsinus(). In other words, fk will be a 7-element vector and Xk will be a 7-element vector. Your example is only passing one freq and one Xk.
(5a) I’m having problems plotting the sinusoid since N = 3, hence k = 0,1,2 (right??) Hence, [xx7,tt7]= vsinus([0,7,14],[-1/8,j/(2*pi),0],11025,-0.3,0.3)? I get an empty matrix when I do this.
ANSWER: You have a fairly simple error, but it would be best if you find it….so…..Time to learn how to use the DEBUGGER. You can put a “breakpoint” in your function and examine variables to see what’s going on. Consult help debug for info. It is very powerful! OR, add one line at the end of your function, which is simply the word keyboard. That will kick you into keyboard mode, and you can examine variables. Use the whos command; then type some variable names to see what they contain.
(5a) I used the following code for N = 3: ª [xx3,tt3] = vsinus([0,7,14],[-1/8,j/(2*pi),0],11025,0.5,-0.3); This results in a smooth sinusoidal curve; is this what we should get?
ANSWER: NO, but with your code it will be a sinusoid. Why is the third element of the vector [-1/8,j/(2*pi),0] equal to zero?
(5a) Since N = 3, I took values of k from 0 to 2. hence fk = [0xfo,1xfo,2xfo] = [0,7,14] ; Xk = [-1/8 (as k=0), j/2*pi (as k=1), 0 (as k=2)] Is this approach right?
ANSWER: You want N=3, where the lab states: “N is the largest subscript for Xk”. Although the summation in equation (4) runs from k=1 to k=N, in this case you are also including k=0.
(5b) In this section, we are asked to repeat the synthesis from part (a), so do we just synthesize, listen, and answer the questions for this part only? Or, should we have a 3-panel plot for this section also?
ANSWER: A plot is not needed because three periods will look the same as before. Concentrate on a description of how the sound changes and why.
(5c) In this section, we have been asked to synthesize the vowel ‘ah’ using vsinus(). Do we also have to produce the sound, or do we just make the waveform corresponding to this signal?
ANSWER: Make the waveform for the report. Listen for your own interest.