You are on page 1of 28

Q#1.

Unit Impulse Signal


First we will generate a function of impsignal(). As
given:
Program:
function [y,t]=imp(t1,t2,t0)
% n1 &n2 is length of n in x-axis
% n0 is delay
t=t1:t2;
y=[(t-t0)==0];
end

Now plot this


[y,t]=impsignal(-1,7,0);
%it is necessary to save impulseseq function as
"impulseseq.m" in your
%directory where you installed a MATLAB. Otherwise it
will give you
%error......
stem(t,y,'^')
title('impulse signal infinity at 0[13-EE-25');

Figure:

t=0

Figure 1 Impulse response is infinity at

Conclusion:
Figure 1 show the output of the impulse response and at t= 0 (t)
=
And if is proved by definition.

Q#2.
Unit step Sequence
First we will generate a function of stepseq(). As
given:

Program:

function [y,n]=stepseq(n1,n2,n0)
% n1 &n2 is length of n in x-axis
% n0 is delay
n=n1:n2;
y=[(n-n0)>=0];
end
Now to plot sequence
[y,n]=stepseq(0,4,0);
stem(n,y)
xlabel('n');
ylabel('amplitude');
title('unit step sequence[13-EE-25]');
Output:

Figure 2 unit step sequence

Conclusion:
Figure 2 show the output of the unit step sequence and at t>= 0 u
(t) = 1
So we conclude that we can generate it practically for our
required application.
Q#3.
Sine wave:
First we will generate a function of singeneration().
As given:
Program:
function [y,t]=singeneration(A,f)
% A is amplitude ...
%f is frequency of sin wave...
fs=8000;
%frequency per sample
ts=1/fs;
%time per sample
st=0.025 ;
%stop time
t=0:ts:st-ts;
F=f;
y=A*sin(2*pi*F*t);
end
And in new script we plot a sine as shown:
Program:
[x,t]=singeneration(5,600);
%f=600Hz & amplitude=5V
%it is necessary to save singeneration function as
"singeneration.m" in your
%directory where you installed a MATLAB. Otherwise it
will give you
%error......

plot(t,x)
xlabel('time(in second)');
ylabel('amplitude(V)');
title('sine wave[13-EE-25');
zoom xon;

Figure:

sine wave[13-EE-25

5
4
3

amplitude(V)

2
1
0
-1
-2
-3
-4
-5

0.005

0.01
0.015
time(in second)

Figure 3 sine wave

Conclusion:

0.02

0.025

Figure 1 show the output of the sine signal. And it is periodic over
fundamental time period. We can also generate any signal of
varying frequency.
And if is proved by definition.

Q#4. Cosine wave:


Again first we will generate a function of
cosgeneration(). As given:

Program:

function [y,time]=cosgeneration(A,f)
% A is amplitude ...
%f is frequency of sin wave...
fs=8000;
%frequency per sample
dt=1/fs;
%time per sample
st=0.025 ;
%stop time
time=0:dt:st-dt;
F=f;
y=A*cos(2*pi*F*time);
end

And in new script we plot a Cosine as shown:


Program:

[x,t]=cosgeneration(3,600);
%f=600Hz & amplitude=3V
%it is necessary to save cosgeneration function as
"singeneration.m" in your
%directory where you installed a MATLAB. Otherwise it
will give you
%error......
plot(t,x)
xlabel('time(second(s))');
ylabel('amplitude(Volt)');
title('cos wave[13-EE-25]');
zoom xon;

Figure

cos wave[13-EE-25]

amplitude(Volt)

-1

-2

-3

0.005

0.01

time(second(s))

0.015

Figure 4 cosine wave

Conclusion:

0.02

0.025

Just same like a sine signal Figure 1 show the output of the Cosine
signal. And it is periodic over fundamental time period. We can
also generate any signal of varying frequency.

Q#5. Square Wave


Again first making a function of squrewave() as
given:
Program:
function [y,time]=squrewave(A,D)
%D represent Duty cycle in percentage 1% to 100%
%A represent amplitude
time=linspace(0,10*pi,500);
y= A*square(time,D);
end
And using this function plotting it:
Program:
[x,t]=squrewave(1,40);
%dutycycle=40% & Amplitude
of wave is =1
%it is necessary to save cosgeneration function as
"squrewave.m" in your
%directory where you installed a MATLAB. Otherwise it
will give you
%error......
plot(t,x)
axis([-3 40 0 1.5]);
xlabel('time(second(s))');
ylabel('unit amplitude');
title('Squre wave[13-EE-25]');
zoom xon;

grid off;
Figure

Figure 5 square wave

Conclusion:
Figure 5 show the output of the square wave. And it is periodic
over fundamental time period. Its duty cycle can also be changed.
Because a MATLAB signal generated as above also give ability to
change duty cycle.

Q#6. Ramp Sequence

First making a function of rampseq() as given:

function [y,n]=rampseq(n1,n2,n0)
% n1 &n2 is length of n in x-axis
% n0 is delay

%it is necessary to save rampseq function as


%"rampseq.m" in your
%directory where you installed a MATLAB. Otherwise it
%will give you an
%error......

n=n1:n2;
k=n2-n1+1;
m=n1;
if(n0>1)
for i=n0+1:k
if(m<=0)
y(i)=0;
else
y(i)=m;
end
m=m+1;
end
end
if(n0<=1)
for j=1:k
if(m<=0)
y(j)=0;
else
y(j)=m;
end
m=m+1;
end
end
end
Now we will plot ramp sequence AS given:

[y,n]=rampseq(-1,15,4);
% n1=-1 & n2= 6 is length of n in x-axis
% n0= is delay
%it is necessary to save rampseq function as
"rampseq.m" in your
%directory where you installed a MATLAB.
Otherwise it will give you
%error......
stem(n,y,'filled')
xlabel('n');
ylabel('amplitude');
title('\delta(n-4)( delayed )ramp sequence[13EE-25]');
Figure:

Figure 6 delayed ramp sequence

Conclusion:
Just like a ramp signal a discrete time ramp sequences is
generated in MATLAB just by using stem command. Figure 6 shoe
a delayed ramp sequence at r(n-4)
LAB No.7: Draw a sine functions and determines sampling
at given interval
of time.
A)

0.25 sec

B)

0.5 sec

C)

1.0 sec

t=0:0.25:10;
t1=0:0.5:10;
t2=0:1:10;
y=sin(t);y1=sin(t1);y2=sin(t2);
subplot(2,2,1);
plot(t,y,'m');
xlabel('time');
ylabel('sine function Amplitude');
title('original signal[13-EE-25]');
subplot(2,2,2);
stem(t,y,'b:*');
xlabel('time');
ylabel('sine function');
title('sampling at t=0.25[13-EE-25]');
subplot(2,2,3);

stem(t1,y1,'m:*');
xlabel('time');
ylabel('sine function Amplitude');
title('sampling at t=0.5[13-EE-25]');
subplot(2,2,4);
stem(t2,y2,'filled');
title('sampling at t=1[13-EE-25]');
xlabel('time');
ylabel('sine function amplitude');

Figure:

Figure 7 samplings of signal


Conclusion:

Signals are sampled at different instant of time by using a discrete time interval and
by using a stem function in matlab. And this sampling leads to a discrete time signal
and after using sampling we can convert analog signal to digital signal.

LAB No.8: Create the signal x (n) = cos (2f0n) using a


time vector of length 2000. Plot the rst 20-100
samples for the frequencies 0, 1/16, 1/8, 1/4, and
1/5. Did you expect the results? Are they periodic?
Listen to the signals using the command sound. Which
frequency do you hear? Repeat the assignment for a
sinusoid with frequency 1/2 and explain the resulting
plot.

Program:

n=20:100;
char('OK');
f = input('select frequency:
(f=0,1/6,1/8,1/4,1/2,1/sqrt(5):');
%after running program pleas open commond window
to select frequency:
%after this window will ask you to here sound
press 'OK' insted of just OK.
if(f==0 || f==1/6 || f==1/8 || f==1/4 || f==1/2
|| f==1/sqrt(5))
if(f==0)
x2= cos(2*pi*0*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');

title('sampling signal');
C=input('To plot OR to here sound
of original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)
close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');
ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)
xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity');
axis([-21 21 -0.4 1.1])
sound(x1);
end
end
if(f==1/6)
x2= cos(2*pi*1/6*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');
title('samlipling signal at f=1/6');
C=input('To plot OR to here sound of
original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)

close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');
ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)
xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity[13-EE25');
axis([-21 21 -0.4 1.1])
sound(x1);
end
end
if(f==1/8)
x2= cos(2*pi*1/8*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');
title('samlipling signal f=1/8');
C=input('To plot OR to here sound of
original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)
close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');

ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)
xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity');
axis([-21 21 -0.4 1.1])
sound(x1);
end
end
if(f==1/4)
x2= cos(2*pi*1/4*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');
title('samlipling signal f=1/4');
C=input('To plot OR to here sound of
original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)
close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');
ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)

xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity');
axis([-21 21 -0.4 1.1])
sound(x1);
end
end
if(f==1/2)
x2= cos(2*pi*1/2*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');
title('samlipling signal f=1/2');
C=input('To plot OR to here sound of
original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)
close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');
ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)
xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity');
axis([-21 21 -0.4 1.1])
sound(x1);
end

end
if(f==1/sqrt(5))
x2= cos(2*pi*1/sqrt(5)*n);
stem(n,x2);
xlabel('n');
ylabel('cose function');
title('samlipling signal f=1/sqrt(5)');
C=input('To plot OR to here sound of
original signal enter OK:');
s1 = strcmp(C,tf);
if(s1==1)
close ;
x1=cos(2*pi*f*t);
subplot(1,2,1);
plot(t,x1,'g');
xlabel('t');
ylabel('cos function');
title('original signal');
[autocor,lags] =
xcorr((cos(2*pi*f*t)),'coeff');
subplot(1,2,2);
plot(lags,autocor)
xlabel('Lag ')
ylabel('Autocorrelation')
title('signal to check predocity');
axis([-21 21 -0.4 1.1])
sound(x1);
end
end
else
disp('you enter a invalid frequency')
end

conclusion:

By entering f=1/6 we have output of sampled


signal and continuous time signal. And we
also determine the periodicity of signal
using correlation method.

By entering OK

Clearly signal is periodic at right hand side of


plotted figure.
LAB No.9: Continue by adding the signals
x1(n)= cos(2f1n) and x2(n)=Cos (2f2n) in a vector of
length 8192 with the adjacent frequencies f1
=1/81/8192 and f2 =1/8+1/8192. Listen and plot and
rewrite your program using the results from Lab 02.
Then, listen only to x1(n). Change the frequency f1
=1/2 1/8192 and listen again. Can you explain the
beating effect although there is only one cosine???

Solution:
n=0:8192;
f=1/8-1/8192;

f2=1/8+1/8192;
x1=cos(2*pi*f*n);
x2=cos(2*pi*f2*n);
subplot(2,2,1);
plot(n,x1,'k');
xlabel('time(t)');
ylabel('cose signal');
title('signal with frequency 2*(1/8-1/8192)[13-EE-25]');
subplot(2,2,2);
plot(n,x2,'g');
xlabel('time(t)');
ylabel('cos signal');
title('signal with frequency 2*(1/8-1/8192)');
subplot(2,2,3);
y=x1+x2;
% sum of signal by 'k'
plot(n,y,'b');
xlabel('time(t)');
ylabel('cos signal');
title('signal with addation of x1,x2)[13-EE-25]');
x3=cos(2*pi*(1/2-1/8192)*n);
subplot(2,2,4);
plot(x3);
xlabel('time(t)');
ylabel('cos signal');
title('signal with frequency (1/2-1/81``)');
play(recObj);
tic;
while toc < 3
end
sound(x1);
tic;
while toc < 2
end
play(recObj1);
tic;
while toc < 5
end
sound(y);
while toc < 7
end
play(recObj2);
tic;
while toc < 5
end
sound(x3);

Figure:

Conclusion:
The difference of beating effect of x1 with frequency
1/8-1/8192 and x1 with frequency 1/2-1/8192,is due to
the large difference in frequencies.
x1 with frequency 1/8-1/8192 is much intense sound then
x1 with frequency 1/2-1/8192.due to low frequency and
large wavelength.
Large wavelength make more loudly sound.

LAB No.10:

Generate a sequence x (n) = 1+cos (2f0n) of 8192 samples


with f0 =0.0625. Let a lter calculate the 1st deference, i.e. Y (n) =
x (n) x (n 1). Use loops for. end or use subtraction with two
vectors. Study the signal and explain the result. What is the impulse
response of the system calculating the 1 st deference? Is it causal??
Stable??

Program:
fo=0.0625;
n=1:8192;
x_n=1+cos(2*pi*fo*n);
x=[1 0 0 0 0 0 0 0 0 0 ];
for n=2:10
h(n)=x(n)-x(n-1);
end
subplot(2,1,1)
stem(x_n(1:10),'filled');
grid;
title('SEQUENCE OF SAMPLES[13-EE-25]')
subplot(2,1,2)
stem(h(2:10),'g');
title('SYTEM IMPULSE RESPONSE h(n)[[13-EE-25]')

FIGURE:

Conclusion
System is stable and casual. Because value of 'n' from
2:10 which make the system bounded output so system is
BIBO its block diagram can be given as:

Lab No.11 Convolve signals [12345]and[132] with the


Matlab func-tions conv and xcorr. Also, calculate the
correlation between the signals, both with conv, and
withxcorr.

Program:
x=[1 2 3 4 5];
h=[1 3 2];
convy= conv(h,x);
corrk= xcorr(h,x);
subplot(2,2,1)
plot(convy,'k');
subplot(2,2,3)
plot(corrk,'m');
subplot(2,2,2)
stem(corrk,'c');
subplot(2,2,4)
stem(corrk,'c');

Figure:

Conclusion:
Cross-correlation measures the similarity between x and
shifted (lagged) copies of y as a function of the lag.
If x and y have different lengths, the function appends
zeros at the end of the shorter vector so it has the
same length, N, as the other. Convolution is most
commonly used to find the response of system.