% Function To Generate Unit Sample Sequence: Types of Sequences
% Function To Generate Unit Sample Sequence: Types of Sequences
TYPES OF SEQUENCES
1. Unit sample sequence:
Question 1:
Write a MATLAB function to implement unit sample sequence δ (n)over theinterval
n1 ≤n0 ≤n2
Write your own matlab function having 3 input arguments n0, n1, n2. This function
should return two vectors x, n. Do not use zeros command.
% Function to generate unit sample sequence
function [x,n]=impseq(no,n1,n2)
n=[n1:n2];
x=[(n-no) == 0];
end
In Command Window:
[x,n]=impseq(3,-50,50)
stem(n,x);
xlabel('n')
ylabel('x')
title('Unit Impulse');
Unit Impulse
1
0.9
0.8
0.7
0.6
0.5
x
0.4
0.3
0.2
0.1
0
-50 0 50
n
Question 1 part 2:
. Generate and plot the following sequence over the indicated interval
Muhammad Usman Ali 14-EE-26
n=[-5:5];
x=[2*impseq(-2,-5,5)-impseq(4,-5,5)];
stem(n,x)
Unit impulse
2
1.5
0.5
x
-0.5
-1
-5 0 5
n
u(n−no)= 1 ,∧n≥ n 0
{
0 ,∧n< n 0
Question 2:
. Write a MATLAB function to implement unit step sequence u( n)over theinterval
n1 ≤n0 ≤n2
and having 3 input arguments n0, n1, n2. This function should return two vectors x, n. Do
not use zeros and ones command.
% Function to generate unit step function
function [x,n]=stepseq(no,n1,n2)
n=[n1:n2];
x=[(n-no) >= 0];
Muhammad Usman Ali 14-EE-26
end
In Command Window:
[x,n]=stepseq(0,-20,20)
stem(n,x);
xlabel('n')
ylabel('x')
title('Unit Step');
Unit Step
1
0.8
0.6
x
0.4
0.2
0
-20 -15 -10 -5 0 5 10 15 20
n
Question 3:
Define x1=5exp(i*n*pi/4) in MATLAB and plot using n = -50:50.
n = -50:50;
x1=5*exp(i*n*pi/4);
plot(n,x1)
Muhammad Usman Ali 14-EE-26
-1
-2
-3
-4
-5
-50 0 50
400 400
300 300
200 200
100 100
Amplitude
Amplitude
0 0
-100 -100
-200 -200
-300 -300
-400 -400
-500 -500
-50 0 50 -50 0 50
N N
Question 3 part 2:
Generate the complex-valued signal
x(n) = e(−0.1+j0.3)n, −10 ≤ n ≤ 10
and plot its magnitude, phase, the real part, and the imaginary part in four separate
subplots.
4. Periodic sequence:
stem(n,abs(x));
title('Magnitude');
xlabel('Samples(n)');
ylabel('Amplitude');
subplot(2,2,4);
stem(n,angle(x));
title('Phase');
xlabel('Samples(n)');
ylabel('Amplitude');
5 2
Amplitude
Amplitude
0 0
-5 -2
-10 -4
-10 -5 0 5 10 -10 -5 0 5 10
Samples(n) Samples(n)
Magnitude Phase
10 1
0
Amplitude
Amplitude
5 -1
-2
0 -3
-10 -5 0 5 10 -10 -5 0 5 10
Samples(n) Samples(n)
Question 4:
. Generate and plot the following sequence over the indicated interval
x(n) = {..., 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, ...}; −10 ≤ n ≤ 9.
n=-10:9;
a=[5 4 3 2 1];
X=repmat(a,1,4);
stem(n,X);
Muhammad Usman Ali 14-EE-26
title('Sequence');
xlabel('Number of samples');
ylabel('Amplitude');
Sequence
5
4.5
3.5
3
Amplitude
2.5
1.5
0.5
0
-10 -5 0 5 10
Number of samples
Question 5:
Generate two 3000 hertz sinusoids with different amplitudes and phases.
x1(t) = A1 cos(2π(3000)t + α) x2(t) = A2 cos(2π(3000)t + β)
(a) Select the value of the amplitudes as follows: let A1 = 13 and use your age for A2. For
the phases, use the last two digits of your telephone number for α(in degrees), and take β=
-30. computations
(b) Make a plot of both signals over a range of t that will exhibit approximately 3 cycles.
Make sure the plot starts at a negative time so that it will include t = 0, and make sure
that you have at least 20 samples per period of the wave.
(c) Verify that the phase of the two signals x1(t) and x2(t) is correct at t = 0, and also verify
that each one has the correct maximum amplitude.
(d) Use subplot (3,1,1) and subplot(3,1,2) to make a three-panel subplot that puts both of
these plots on the same window.
(e) Create a third sinusoid as the sum: x3(t) = x1(t) + x2(t). In Matlab this amounts to
summing the vectors that hold the samples of each sinusoid. Make a plot of x3(t) over
the same range of time as used in the previous two plots. Include this as the third panel
in the window by using subplot (3,1,3).
Muhammad Usman Ali 14-EE-26
(f) Measure the magnitude and phase of x3(t) directly from the plot. Explain how the
magnitude and phase were measured by making annotations on each of the plots.
f0 = 3;
A = 5;
t = -1:0.005:1;
y = A*cos(2*pi*f0*t);
figure, plot(t, y,'*:');
xlabel('Time, sec'), ylabel('Amplitude');
title('Graph of sinusoid');
M=10; %samples/sec
n=-3:1/M:3;
A=2;
phase=0;
f=1;
x=A * sin(2*pi*f*n + phase);
stem(n,x,'linewidth', 2)
title('Discrete-Time Sine Wave: A sin(2*\pi*f*n + \phi)')
xlabel('Time Index')
Muhammad Usman Ali 14-EE-26
ylabel('Signal Amplitude')
axis([n(1) n(end) -A A])
grid
t = 0:0.0005:1;
f = 13;
xa = cos(2*pi*f*t);
subplot(2,1,1)
plot(t,xa);grid
xlabel('Time, msec');
ylabel('Amplitude');
title('Continuous-time signal x_{a}(t)');
axis([0 1 -1.2 1.2])
subplot(2,1,2);
T = 0.1;
n = 0:T:1;
xs = cos(2*pi*f*n);
k = 0:length(n)-1;
stem(k,xs); grid
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete-time signal x[n]');
axis([0 (length(n)-1) -1.2 1.2])
Muhammad Usman Ali 14-EE-26
ADDITION OF SINUSOIDS
CASE 1: When Frequency, Phases, and amplitude of the sinusoids are same
t=-2:0.01:2;
x1=cos(2*pi*0.5*t);
x2=cos(2*pi*0.5*t);
x3=x1+x2;
subplot(3,1,1);
plot(t,x1,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('COS WAVE , AMPLITUDE = 1, FREQ = 0.5 HZ, Phase = 0 RADIAN');
subplot(3,1,2);
plot(t,x2,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('COS WAVE , AMPLITUDE = 1, FREQ = 0.5 HZ, Phase= 0 RADIAN');
subplot(3,1,3);
plot(t,x3,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('SUM OF THE ABOVE TWO COSINE SIGNALS');
Muhammad Usman Ali 14-EE-26
CASE 2: When Frequencies and Phases of the sinusoids are same but
Amplitudes are different.
t=-2:0.01:2;
x1=2*cos(2*pi*0.5*t);
x2=cos(2*pi*0.5*t);
x3=x1+x2;
subplot(3,1,1);
plot(t,x1,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('COS WAVE , AMPLITUDE = 2, FREQ = 0.5 HZ, Phase = 0 RADIAN');
subplot(3,1,2);
plot(t,x2,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('COS WAVE , AMPLITUDE = 1, FREQ = 0.5 HZ, Phase= 0 RADIAN');
subplot(3,1,3);
plot(t,x3,'linewidth',3);
grid;
ylabel('Amplitude');
xlabel('Time');
title('SUM OF THE ABOVE TWO COSINE SIGNALS');
Muhammad Usman Ali 14-EE-26