You are on page 1of 12

LAB 3 REPORT

EXERCISE 1: Convolution and Echo


1.Create a new script for this problem. Download the trumpet jazz lick fall
here1, and then load it into MATLAB using load(fall) and plot it.
>> load('fall')
>> plot(fall)
>>grid on


Use whos to see that the variables fall and Fs are created for you. (The sampling
rate (Fs) for this signal should be 8000 Hz)
>> whos
Name Size Bytes Class Attributes
Fs 1x1 8 double
fall 24000x1 192000 double
2. Use the following commands to convolve the following impulse response h ,
with the trumpet sound.
Fs = 8000 % for this example
h = [1 zeros(1,10000) .25 zeros(1,1000)];
y = conv(fall, h);
plot(y)
soundsc(y, Fs)



3. What if the second echo (in h) is a negative coefficient? When you play it, it
should not sound different since your ear is not sensitive to that sort of
modification (simple phase change).




4. Now let's build a system that delays the echo for a quarter second by inserting
Fs/4 zeros before the second impulse:
h = [1 zeros(1, round(Fs/4)) 0.25 zeros(1,1000)];
load('fall');
Fs = 8000 % for this example
h = [1 zeros(1,10000) .25 zeros(1,1000)];
y = conv(fall, h);
soundsc(y, Fs)
pause
h = [1 zeros(1, round(Fs/4)) 0.25 zeros(1,1000)];
y = conv(fall, h);
plot(y)
soundsc(y, Fs)
>> grid on








EXERCISE 2:
1. Build a box impulse response:
h2=[ones(1,50)/50 zeros(1,20)];
Create a new signal y2 by convolving "fall" with h2
>> Fs = 8000;
>> h2=[ones(1,50)/50 zeros(1,20)];
>> y2=conv(fall, h2);
>> subplot(2,1,1);
>> plot(fall)
>>grid on
>> subplot(2,1,2);
>> plot(y2)
>> grid on

2. The output sound is softer and smoother than the input sound fall.
3. The average value of the signal fall is 0.0396. Therefore, it is not centered on
value 0.
>> sum(fall)/length(fall)

ans =

0.0396
4. Next, to see what this system does to the input signal, zoom in on part of the
signal:
>> load('fall');
>> h2=[ones(1,50)/50 zeros(1,20)];
>> Fs = 8000;
>> y2=conv(fall,h2);
>> subplot(2,1,1), plot(6400:6500, fall(6400:6500))
>>grid on
>>subplot(2,1,2), plot(6400:6500, y2(6400:6500))
>>grid on

Exercise 3: Box Function.
1 . Create a new function called unitstep.m in MATLAB. The function should take
two parameters, a time vector that specifies the finite range of the signal and a
time shift value.
function [usdelay]=unitstep(timevector,ts,del)

ts=ts/del;
us = ones(size(timevector)).*(timevector >= 0);
if ts==0;
usdelay = us;
elseif ts<0 % right shift
ustemp = [us ones(1,abs(ts))];
usdelay = ustemp(abs(ts)+1:end);
else % left shift
ustemp = [zeros(1,ts) us];
usdelay = ustemp(1:end-ts);
end




2. Use the unitstep function to create a box-shaped time signal.
function [boxsig]=boxt(timevector,t1,t2,del)

[usdelayt1]=unitstep(timevector,t1,del);
[usdelayt2]=unitstep(timevector,t2,del);
boxsig= usdelayt1 - usdelayt2;


3. Create a script file called boxtscript.m
function
[boxsig,timevector]=boxtscript(s1,s2,t1,t2,del)

timevector=[s1:del:s2];
boxsig = boxt(timevector,t1,t2,del);

>> subplot(3,1,1);
>> x=boxtscript(-3,3,-1,1,0.5);
>> plot(x);
>> grid on
>> subplot(3,1,2);
>> y=boxtscript(-3,3,-1,1,0.05);
>> plot(y);
>> grid on
>> subplot(3,1,3);
>> z=boxtscript(-3,3,-1,1,0.01);
>> plot(z);
>> grid on

4. Plot all three versions in one figure using subplot and save it as boxtscript.tif.


5. Time: If u is a vector of length n with time span tu = t1:del:t2, and v is a vector
of length m with time span tv = t3:del:t4, and both have the same time step del,
then the result of conv(u,v) will be a vector of length n + m - 1 with a time span
tc = (t1+t3):del:(t2+t4).

6.
>> del=.01;
>> x=boxtscript(-5,10,0,4,del);
>> y=boxtscript(-5,10,-1,1,del);
>> subplot(3,1,1);
>> plot(x)
>> grid on
>> subplot(3,1,2);
>> plot(y)
>> grid on
>> subplot(3,1,3);
>> plot(conv(x,y));
>> grid on


By calculating theoretically, I have:

>
s s
< s
< s +
<
= =
5 0
5 3 5
3 1 2
1 1 1
1 0
*
t
t t
t
t t
t
y x z
7.
>> del=.01;
>>x=boxtscript(-5,10,0,4,del);
>>y=boxtscript(-5,10,-1,1,del);
>>subplot(3,1,1);
>>plot(x)
>>grid on
>>subplot(3,1,2);
>>plot(y)
>>grid on
>> suplot(3,1,3);
>> subplot(3,1,3);
>> plot(conv(x,y).*del)
>> grid on


8. Triangle: Design the impulse response for a system h and a system input x such
that you get a perfectly symmetric triangle of length 100 as the system output y.
Use subplot to plot x,h, and y, and save the plot as tri.tif.
>> del=.01;
>>h=boxtscript(-1,3,0,0.5,del);
>>x=2.*boxtscript(-1,3,-0,0.5,del);
>>subplot(3,1,1);
>>plot(h)
>> grid on
>> subplot(3,1,2);
>>plot(x)
>> grid on
>> subplot(3,1,3);
>>plot(conv(h,x).*del);
>> grid on
>> soundsc(conv(h,x).*del,8000)

You might also like