Professional Documents
Culture Documents
Name: Zulfiqar Ali.: Department of Electronic Engineering University of Engineering and Technology Abbottabad Campus
Name: Zulfiqar Ali.: Department of Electronic Engineering University of Engineering and Technology Abbottabad Campus
Name: Zulfiqar Ali.: Department of Electronic Engineering University of Engineering and Technology Abbottabad Campus
REPORT VERIFICATION
Date: 8/19/2020
Signature:
Inlab Task:
Graph:
Graph:
Task-3: Write a MATLAB program for the ‘running average’, a running total is a sequence of
partial sum of a given sequence/signal. For example, the running totals of the signal {a, b, c,
…}are a, a+b, a+b+c, ... Use that program to find the running total of the discrete time signal of
length N=100. Write your program so that it is flexible. That is, you should be able to invoke
your program from the command window as follows:
>> y=running_averagel(x)
where x is the input signal, and y is the running total of that signal.
m-File Code:
%Moving Average Or Running Average
function y= runningaverage(x)
sum=0;
x=[1:100];
for i=1:100;
sum=sum+x(i);
y(i)=sum;
end
end
ans =
Columns 1 through 7
1 3 6 10 15
21 28
Columns 8 through 14
36 45 55 66 78
91 105
Columns 15 through 21
Columns 22 through 28
253 276 300 325 351
378 406
Columns 29 through 35
Columns 36 through 42
Columns 43 through 49
Columns 50 through 56
Columns 57 through 63
Columns 64 through 70
2080 2145 2211 2278 2346
2415 2485
Columns 71 through 77
Columns 78 through 84
Columns 85 through 91
Columns 92 through 98
4950 5050
Task-4: Write a program to compute the variance and mean of a signal x. The variance σ is
defined to be:
where ‘m’ is the mean value of the signal x. For signal x, use all the integers from 1 to 1000.
Code:
t=1:0.01:20;
%Let x be equal to cos(nt)
x=cos(0.01*t);
sum=0;
for i=1:length(x)
sum=sum+x(i);
end
mean=sum./length(x)
sum=0;
for i=1:length(x)
sum=sum+(x(i)-mean);
end
variance = sum./length(x)
Output:
>> t=1:0.01:20;
%Let x be equal to cos(nt)
x=cos(0.01*t);
sum=0;
for i=1:length(x)
sum=sum+x(i);
end
mean=sum./length(x)
sum=0;
for i=1:length(x)
sum=sum+(x(i)-mean);
end
variance = sum./length(x)
mean =
0.9930
variance =
1.7133e-15
end
Output:
>> x=[-5,-4,-3,-2,-1,0,1,2,3,4,5];
L = length(x);
for I = 1: L
if x (I) < 0
x (I) = -1;
else
x(I) =1
end
end
x =
-1 -1 -1 -1 -1 1 1 2 3 4
5
x =
-1 -1 -1 -1 -1 1 1 2 3 4
5
x =
-1 -1 -1 -1 -1 1 1 1 3 4
5
x =
-1 -1 -1 -1 -1 1 1 1 1 4
5
x =
-1 -1 -1 -1 -1 1 1 1 1 1
5
x =
-1 -1 -1 -1 -1 1 1 1 1 1 1
Task-6: Generate a step sequence u [n] as described in In-Lab section, use it to generate an
Code:
n=-10:10
figure(1)
u0= n>=0; %u(n)
stem(n,u0,'linewidth',3);
legend('u[n]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'u0'},'FontSize',14,'FontName','Times New Roman')
title({'u[n]'},'FontSize',14,'FontName','Times New Roman')
figure(2)
u1= n>=1; %u(n-1)
stem(n,u1,'linewidth',3);
legend('u[n-1]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'u1'},'FontSize',14,'FontName','Times New Roman')
title({'u[n-1]'},'FontSize',14,'FontName','Times New Roman')
figure(3)
y=u0-u1; %dellta(n)=u(n)-u(n-1)
stem(n,y,'linewidth',3);
legend('&[n]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'y'},'FontSize',14,'FontName','Times New Roman')
title({'&[n]'},'FontSize',14,'FontName','Times New Roman')
Graph:
u[n]
1
u[n]
0.9
0.8
0.7
0.6
u0
0.5
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
u[n-1]
1
u[n-1]
0.9
0.8
0.7
0.6
u1
0.5
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
&[n]
1
&[n]
0.9
0.8
0.7
0.6
0.5
y
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Task-7: Generate an impulse sequence δ [n] as described in In-Lab section, use it to generate a
step sequence .
Code:
n=-10:10;
k=0:10;
u0= n>=0; %u(n)
u1= n>=length(k); %u(n-k)
y= (u0-u1); %dellta(n)=u(n)-u(n-1)
stem(n,y,'linewidth',3);
legend('&[n-k]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'y'},'FontSize',14,'FontName','Times New Roman')
title({'&[n-k]'},'FontSize',14,'FontName','Times New Roman')
Output:
&[n-k]
1
&[n-k]
0.9
0.8
0.7
0.6
0.5
y
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Task-8: Generate the following sequences using step and impulse sequence.
a. x[n] = u[n] – u[n-10]
Code:
n=-20:20
figure(1)
u0= n>=0; %u(n)
stem(n,u0,'linewidth',3);
legend('u[n]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'u0'},'FontSize',14,'FontName','Times New Roman')
title({'u[n]'},'FontSize',14,'FontName','Times New Roman')
figure(2)
u1= n>=10; %u(n-10)
stem(n,u1,'linewidth',3);
legend('u[n-10]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'u1'},'FontSize',14,'FontName','Times New Roman')
title({'u[n-10]'},'FontSize',14,'FontName','Times New Roman')
figure(3)
y=u0-u1; %dellta(n)=u(n)-u(n-1)
stem(n,y,'linewidth',3);
legend('&[n]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'y'},'FontSize',14,'FontName','Times New Roman')
title({'&[n]'},'FontSize',14,'FontName','Times New Roman')
Graph:
u[n]
1
u[n]
0.9
0.8
0.7
0.6
u0
0.5
0.4
0.3
0.2
0.1
0
-20 -15 -10 -5 0 5 10 15 20
n
u[n-10]
1
u[n-10]
0.9
0.8
0.7
0.6
u1
0.5
0.4
0.3
0.2
0.1
0
-20 -15 -10 -5 0 5 10 15 20
n
&[n]
1
&[n]
0.9
0.8
0.7
0.6
0.5
y
0.4
0.3
0.2
0.1
0
-20 -15 -10 -5 0 5 10 15 20
n
b. x[n] = an u[n]
Code:
n=-10:10
a= 2;
figure(1)
Graph:
u[n]
1200
u[n]
1000
800
u0
600
400
200
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Code:
n=-10:10;
k=0:10;
a=2;
u0= n>=0; %u(n)
u1= n>=length(k); %u(n-k)
y= (u0-u1).*(a.^n); %Σa
n δ[n-k] , k = -10 to 10
stem(n,y,'linewidth',3);
legend('&[n-k]','FontSize',14,'FontName','Times New Roman')
xlabel({'n'},'FontSize',14,'FontName','Times New Roman')
ylabel({'y'},'FontSize',14,'FontName','Times New Roman')
title({'&[n-k]'},'FontSize',14,'FontName','Times New Roman')
Graph:
&[n-k]
1200
&[n-k]
1000
800
600
y
400
200
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n