Вы находитесь на странице: 1из 22

LAB No.

1: Write a MATLAB Program for generation of 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 plotting sequence

[y,t]=impsignal(-1,12,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-33');

Figure:

LAB No.2: Write a MATLAB Program for generation of 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 plot of sequence

[y,n]=stepseq(-3,4,2);
stem(n,y)
xlabel('n');
ylabel('amplitude');
title('unit step sequence[12-EE-33]');

FIGURE:

Q.no 3. Write a MATLAB Program for generation of Sine


Wave
First making function of sinegeneration()
Program:
function [y,time]=singeneration(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*sin(2*pi*F*time);
end
Now we plot a wave given below:
Program:

[x,t]=singeneration(5,60);
%f=60Hz & amplitude=10V
%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(second)');
ylabel('amplitude(V)');
title('sine wave[13-EE-33]');
zoom xon;

Figure
sine wave[13-EE-33]

5
4
3

amplitude(V)

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

0.005

0.01
0.015
time(second)

0.02

0.025

LAB No.4: Write a MATLAB Program for generation of


Cosine Wave
First making function of cosegeneration()

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
Now we plot a wave given below:
[y,t]=cosgeneration(3,60);
%f=60Hz & 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,y)
xlabel('time(second(s))');
ylabel('amplitude(Volt)');
title('cos wave[13-EE-33]');

zoom xon;
Figure:

LAB No.5: Write a MATLAB Program for generation of


Square Wave
First we will make a function of squrewave() as
given:
Program:
function [y,t]=squrewave(a,d)
%D represent Duty cycle in percentage 1% to 100%
%A represent amplitude
time=linspace(0,10*pi,400);
y= a*square(t,d);
end
And using this function plotting it:
Program:
[x,t]=squrewave(4,50);
of wave is =4

%dutycycle=40%

& Amplitude

%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([-12 35 0 4.5]);
xlabel('time(second(s))');
ylabel('amplitude(v)');
title('Squre wave[13-EE-33]');
zoom xon;

Figure

LAB No.6: Write a MATLAB Program for generation of Ramp


Sequence
First making a function of rampseq() as given:

function [x,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)
x(i)=0;
else
x(i)=m;
end
m=m+1;
end
end
if(n0<=1)
for j=1:k
if(m<=0)
x(j)=0;
else
x(j)=m;
end
m=m+1;
end

end
end
Now we will plot ramp sequence AS given:

[x,n]=rampseq(-5,45,0);
% n1=-1 & n2= 6 is length of n in x-axis
% n0= 0 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,x,'filled,r')
xlabel('n');
ylabel('Amplitude');
title('ramp sequence[13-EE-33]');

Figure:

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;
ta=0:0.5:10;
tb=0:1:10;
y=sin(t);y1=sin(ta);y2=sin(tb);
subplot(2,2,1);
plot(t,y,'m');
xlabel('time');
ylabel('sine function ');
title('original signal[13-EE-33]');
subplot(2,2,2);
stem(t,y,'b');
xlabel('time');

ylabel('sine function');
title('sampling at t=0.25[13-EE-33]');
subplot(2,2,3);
stem(ta,y1,'m');
xlabel('time');
ylabel('sine function');
title('sampling at t=0.5[13-EE-33]');
subplot(2,2,4);
stem(tb,y2,'filled');
title('sampling at t=1[13-EE-33]');
xlabel('time');
ylabel('sine function ');

Figure:

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-EE-33]');
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[13-ee-33]');
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

RESULTS:

By entering f=1/4 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. For each frequency
sound is heard.
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???
Program:

n1n=0:8192;
f=1/8-1/8192;
f2=1/8+1/8192;
x1=cos(2*pi*f*n1n);
x2=cos(2*pi*f2*n1n);
subplot(2,2,1);
plot(n1n,x1,'k');
xlabel('time(t)');
ylabel('cose signal');
title('signal with frequency 2*(1/8-1/8192)[13-EE-33]');
subplot(2,2,2);
plot(n1n,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(n1n,y,'b');
xlabel('time(t)');
ylabel('cos signal');
title('signal with addation of x1,x2)[13-EE-33]');
x3=cos(2*pi*(1/2-1/8192)*n1n);
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 <3
end
sound(x3);

Output figure:

LAB No.10: Generate a sequence x (n) = 1+cos (2f0n) of 8192 samples with f0
=0.0625. Let a filter 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 1st 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');
title('SEQUENCE OF SAMPLES[13-EE-33]')
subplot(2,1,2)
stem(h(2:10),'b');

title('SYTEM IMPULSE RESPONSE h(n)[[13-EE-33]')

FIGURE:

Result: System is casual and is stable.

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

Вам также может понравиться