3
Exp.No:1 Generation of discrete time sequences and their basic
operations.
Aim: To generate discrete time signals and understand their
basic operations.
Software: MATLAB
i) Unit Sample sequence:
In MATLAB, the function zeros(1,N) generates a row vector
of N zeros which can be used to implement 𝜹(𝒏) over a finite
interval. However, the logical relation n==0 is an elegant
way of implementing 𝜹(𝒏).
function [x,n] = impulsesequence(n0,n1,n2)
%Generates x(n)= 𝜹(n-n0); n1<=n<=n2
%Impulse shifting can also be performed by varying n0.
%------------------------------------
%[x,n] = impulsesequence(n0,n1,n2)
n = [n1:n2];
x = [(n-n0)==0];
stem(n,x,'filled')
grid on;
ylim([0,1.5]);
xlabel('integer index');
ylabel('Amplitude');
title('Unit sample sequence');
Observation:
Type on Command Window as
4
ii) Unit Step Sequence:
In MATLAB, the function ones(1,N) generates a row vector
of N ones. It can be used to generate u(n) over a finite
interval. Once again an elegant approach is to use the
logical relation n>=0.
function [x,n] = stepsequence(n0,n1,n2)
%------------------------------------
%Generates x(n)= 𝒖(n-n0); n1<=n<=n2
%Step sequence shifting can also be performed by varying
n0.
%------------------------------------
%[x,n] = stepsequence(n0,n1,n2)
n = [n1:n2];
x = [(n-n0)>=0];
stem(n,x,'filled')
grid on;
ylim([0,1.5]);
xlabel('integer index');
ylabel('Amplitude');
title(‘Unit step sequence’);
5
Observation:
iii) Ramp sequence:
function [x,n] = rampsequence(n0,n1,n2)
%Generates x(n)=(n-n0)*𝒖(n-n0); n1<=n<=n2
%Ramp sequence shifting can also be performed by varying
n0.
%------------------------------------
%[x,n] = rampsequence(n0,n1,n2)
n = [n1:n2];
x = [(n-n0).*((n-n0)>=0)];
stem(n,x,'filled')
xlabel('integer index');
ylabel('Amplitude');
title(‘Ramp sequence’);
6
Observation:
iv) Real valued exponential sequence:
function [x,n] = exponentialseq(a,n0,n1,n2)
%Generates x(n)=a^n*𝒖(n); n1<=n<=n2
%Exponential sequence shifting can also be performed by
varying n0.
%------------------------------------
%[x,n] = exponentialseq(a,n0,n1,n2)
n = n1:n2;
x = a.^(n-n0).*[(n-n0)>=0];
stem(n,x,'filled')
ylim([0,1.2])
xlabel('integer index,n');
ylabel('Amplitude');
title(‘Real valued exponential sequence’);
7
v) Complex valued exponential sequence:
function [x,n] = complex_exposeq(a,b,n1,n2)
%Generates x(n)=exp(a+jb)*𝒖(n); n1<=n<=n2
%------------------------------------
n=n1:n2;
x = (exp((a+b*j)*n)).*[n>=0];
y = real(x);
subplot(2,1,1)
stem(n,y,'filled');
xlabel('integer index,n');
ylabel('Real part');
title({'Complex valued exponential sequence:';'Real and
Imaginary parts'},'Color','blue');
z=imag(x);
8
subplot(2,1,2);
stem(n,z,'filled');
xlabel('integer index,n');
ylabel('Imaginary part');
Observation:
vi) Sinusoidal Sequence:
function [x,n] = sinusoidalseq(a,b,theta,n1,n2)
%Generates x(n)=𝒂 ∗ 𝒔𝒊𝒏(𝝎𝒏 + 𝜽); n1<=n<=n2
%------------------------------------
n=n1:n2;
omega = b*pi;
x = a*sin(omega*n+theta);
stem(n,x, 'filled', 'Color', 'red');
xlabel('integer index,n');
ylabel('Amplitude');
9
title('Sinusoidal Sequence: x[n] =
a*sin(\omegan+\theta)','Color','blue')
Observation:
Operations on sequences:
vii) Signal Addition:
function [y,n] = sigadd(x1,n1,x2,n2)
% implements y(n) = x1(n)+x2(n)
% -----------------------------
% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be diff from n1)
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration
of y(n)
y1 = zeros(1,length(n)); % initialization
y2 = y1; % initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with
duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with
duration of y
10
y = y1+y2; % sequence addition
Observation:
viii) Signal Multiplication:
function [y,n] = signal_multiplication(x1,n1,x2,n2)
% implements y(n) = x1(n)*x2(n)
% -----------------------------
% [y,n] = signal_multiplication (x1,n1,x2,n2)
% y = product sequence over n, which includes n1 & n2
11
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be diff from n1)
n = min(min(n1),min(n2)):max(max(n1),max(n2)); %
duration of y(n)
y1 = zeros(1,length(n)); %initialization
y2 = y1; %initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with
duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with
duration of y
y = y1.*y2; % sequence multiplication
Observation:
12
ix) Sequence Shifting:
function [y,n] = sequence_shift(x,m,l)
%y[n] = x[n-l]
%y[m+l] = x[m]
n = m+l;
y = x;
subplot(211);
stem(m,x,'filled','LineWidth',2);
title('x[m]','FontSize',11)
xlim([-length(x) 1.5*length(x)])
subplot(212);
stem(n,y,'filled','LineWidth',2);
xlabel('integer index,n');
title('y[n]=x[n-l])
xlim([-length(x) 1.5*length(x)])
Observation:
13
x) Sequence Folding:
function [y,n] = sequence_fold(x,n)
y = fliplr(x);
subplot(211);
stem(n,x,'filled','LineWidth',2);
title('x[n]','FontSize',11)
xlim([-length(x) 1.5*length(x)])
n=-fliplr(n);
subplot(212);
stem(n,y,'filled','LineWidth',2);
xlabel('integer index,n');
title('y[n]=x[-n]');
xlim([-length(x) 1.5*length(x)]);
Observation:
Conclusion: