Lab 1
Lab 1
Lab 1
11PFIEV2
Introduction to Matlab
1.1.
Matlab programming
1.2.
Matlap examples
1.2.1. Plotting a signal
1.2.2. Writing an m-file
1.2.3. Functions
2.
Lab Procedure
2.1.
2.2.
2.3.
Pure Tones
Write an m-file called tone.m to generate a pure tone, play the signal through the sound
system using the sound command, and plot the signal. Start your program by clearing the
workspace and then assign values to the following variables:
Fs - sampling frequency in Hz
f - tone frequency Hz
T - duration of signal
t - vector of sample times Amp - amplitude of tone
ph - phase of tone x - the vector of signal values
N - no. of samples to be plotted
Make sure your plot displays a grid, has the axes correctly labeled, and is given a title.
Use tone.m to generate a pure tone of 3 seconds duration at middle C. Use a sampling
frequency 8 KHz.
X L TN HIU TNG T V S
11PFIEV2
Fs=8000;
f=262;
A=1;
N=300;
t = 0:1/Fs:N/Fs;
ph=0;
x = A*sin(2*pi*f*t+ph);
plot(t,x,'r')
grid
xlabel('time-secs')
ylabel('signal value - volts')
title('A Plot of a Simple Signal')
sound(x,Fs)
What do you get if you try to plot to whole signal vs time. Explain.
Do tn s ly mu Fs = 8000Hz l rt ln nn th thu c c dng gn nh l sin iu
ha.
A Plot of a Simple Signal
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0.005
0.01
0.015
0.02
time-secs
0.025
0.03
0.035
0.04
X L TN HIU TNG T V S
11PFIEV2
How does changing the value of Amp affect the sound of the signal? Try values greater
than and less one.
Khi ta thay i gi tr Amp th m lng ca tn hiu cng s thay i theo. Amp
tng th m lng tng. Thay i r rt trong khong Amp t 1 n 5, khi Amp t 10 tr
ln th m lng ta nghe c gn nh l nh nhau.
How does changing the value of ph affect the sound of the signal?
Thay i ph khng lm nh hng n m thanh ca tn hiu.
What happens if you decrease the sampling frequency to 1 KHz? Does it drastically alter
the quality of the results? How low can you bring the sampling frequency before the
sound quality changes dramatically?
Khi Fs = 1Khz th cht lng m gim, m thu c nh hn v trm hn.
2.4.
Chords
Create the C Major chord by adding three sinusoids together of the notes C, E, and G, with
the same amplitudes. Listen to the result using soundsc (to avoid clipping). Also, plot this
signal.
Fs=8000;T=2;A=2;ph=0;N=300;
fC=262;
fE=330;
fG=392;
t=[0:1/Fs:T];
xC=A*sin(2*pi*fC*t+ph);
xE=A*sin(2*pi*fE*t+ph);
xG=A*sin(2*pi*fG*t+ph);
figure(1)
plot(t,xC,'b')
grid
xlabel('time-secs')
ylabel('signal value-volts')
title('A Plot of a Simple Signal')
sound(xC,Fs)
figure(2)
plot(t,xE,'r')
grid
xlabel('time-secs')
ylabel('signal value-volts')
title('A Plot of a Simple Signal')
sound(xE,Fs)
figure(3)
plot(t,xG,'g')
X L TN HIU TNG T V S
11PFIEV2
grid
xlabel('time-secs')
ylabel('signal value-volts')
title('A Plot of a Simple Signal')
sound(xG,Fs)
x=xC+xE+xG;
sound(x,Fs);
2.5.
Familiar sounds
Create the following signals, each defined as a mathematical function. Create the signals
using a sampling frequency of 10,000Hz, and play them as sounds. You should use
amplitude 1/2 and phase shift 0 for the sinusoids.
function x=myFunction(Fs,f,a);
A=1/2;
ph=0;
t=[0:1/Fs:a]
x=A*sin(2*pi*f*t+ph);
plot(t,x,'r')
grid
xlabel('time-secs')
ylabel('signal value-volts')
title('A Plot of a Simple Signal')
sound(v,Fs);
Task:
Create a variable d that is the sum of two sinusoids with frequencies 350 Hz and 440 Hz,
evaluated over the interval [0; 4].
m1=myFunction(10000,350,4);
n1= myFunction (10000,440,4);
figure(1)
plot(m1,'b');
figure(2)
plot(n1,'g');
d=m1+n1;
sound(d,10000);
figure(3)
plot(d,'y');
Create a variable b1 that is the sum of two sinusoids with frequencies 480 Hz and 620
Hz, evaluated over the interval [0; :5]. Next, create a vector z1 of 5,000 zeros. Finally,
create a variable b which is composed of alternating copies of b1 and z1, four of each.
Create z1 have 50001 sampling:
m2= myFunction(10000,480,5);
n2= myFunction (10000,620,5);
figure(4)
plot(m2,'b');
figure(5)
plot(n2,'g');
b1=m2+n2;
figure(6)
plot(b1,'y');
X L TN HIU TNG T V S
11PFIEV2
sound(b1,10000);
z1=zeros(1,50001);
b=[];k=0;
for i=1:12500
a=[b1(i+k+0),b1(i+k+1),b1(i+k+2),b1(i+k+3),
z1(i+k+0),z1(i+k+1),z1(i+k+2),z1(i+k+3)];
b=[b a];
k=k+3;
end
b=[b b(50001)];
Create a variable r1 that is the sum of two sinusoids with frequencies 440 Hz and 480 Hz,
evaluated over the interval [0; 2].
Next, create a vector z2 of 40,000 zeros. Finally, create a variable r which is composed of
alternating copies of r1 and z2, three of each:
Z2 have 20001 sampling
m3=myFunction(10000,440,2);
n3=myFunction(10000,480,2);
figure(7)
plot(m3,'b');
figure(8)
plot(n3,'g');
r1=m3+n3;
figure(9)
plot(r1,'y');
sound(r1,10000);
z2=zeros(1,20001);
r=[];k=0;
for i=1:6667
a=[r1(i+k+0),r1(i+k+1),r1(i+k+2),z2(i+k+0),
z2(i+k+1),z2(i+k+2)];
r=[r a];
k=k+2;
end
If you created these vectors correctly, d, b, and r should sound familiar. What are these
sounds?
sound(d,10000);sound(b,10000);sound(r,10000);
2.6.
Create the following variables as matrices of size 64x64, and look at them using the imagesc
command. Use no for loops! You may find the vector outer product to be useful.
X L TN HIU TNG T V S
11PFIEV2
surf(twosm);
rampm1, a matrix which is constant along the rows and a ramp along each column
m2=5*ones(1,64);
n2=rand(64,1);
figure(2)
rampm1=n2*m2
surf(rampm1);
rampm2, a matrix which looks like a ramp in both rows and columns
m3=rand(1,64);
n3=rand(64,1);
figure(3)
rampm2=n3*m3;
surf(rampm2);
sinm1, a matrix which looks like a sinusoid with the same frequency in both directions
N=180;n=0:N-1;A1=10;f1=2;Fs=20;
x=A1*sin(2*pi*f1*n/Fs);
figure(4)
plot(n,x);
m41=[x(2:65)];
m42=[x(70:133)];
n4=m42';
sinm1=n4*m42;
figure(5)
surf(sinm1);
sinm2, a matrix which looks like a sinusoid with different frequencies in each direction
N=180;n=0:N-1;Fs=20;A2=5;f2=3;
y=A2*sin(2*pi*f2*n/Fs);
m51=[x(2:65)];
m52=[y(2:65)];
n5=m52';
sinm2=n5*m51;
figure(6)
surf(sinm2);
X L TN HIU TNG T V S
11PFIEV2