Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

LAB MODULE -1

Digital Signal Processing


CHETAN SHARMA Submitted to: Dr. HARIHARAN MUTHUSAMY
(BT16EEE042)

Q.1) Generate and plot the following sequences using MATLAB


a) −2.7 e (−0.4+ jπ /6)n for 0 ≤n ≤ 82
b) cos (0.6 πn+0.3 n)
c) sin ( 0.1 πn+ 0.75 π ) −3 cos ( 0.8 πn+ 0.2 π ) +cos (1.3 πn)
d) 2 δ ( n+2 ) −δ ( n−4 ) ,−5 ≤ n ≤5
e) n [ u ( n )−u ( n−10 ) ] +10 e(−0.3 (n−10)) [u ( n−10 )−u ( n−20 ) ] , 0 ≤ n≤ 20
MATLAB code:
%% (a) plot -2.7exp((-0.4+(j*pi/6))n), for n=[0,82]
n=0:82;
j=sqrt(-1);
y0 = -2.7*exp((-0.4+(j*pi/6))*n);
figure(1)
subplot(3,1,1)
stem(n,y0,'b','linewidth',1.2);xlabel('n');
title('(a) -2.7*exp((-0.4+(j*pi/6))*n)');ylabel('y0');

%% (b) plot cos(0.6*pi*n + 0.3pi)


n = 0:.1:10;
y1 = cos(0.6*pi*n + 0.3*pi);
subplot(3,1,2)
stem(n,y1,'b','linewidth',1.2);xlabel('n');
ylabel('y1'); title('(b) cos(0.6*pi*n + 0.3*pi)');

%% (c) plot sequence sin(0.1pi*n + 0.75pi) - 3cos(0.8*pi*n +


0.2pi) + cos(1.3*pi*n)
y2 = sin(0.1*pi*n + 0.75*pi) - 3*cos(0.8*pi*n + 0.2*pi) +
cos(1.3*pi*n);
subplot(3,1,3)
stem(n,y2,'b','linewidth',1.2); xlabel('n');
ylabel('y2');title('(c) sin(0.1*pi*n + 0.75*pi) - 3*cos(0.8*pi*n
+ 0.2*pi) + cos(1.3*pi*n)');

%% (d) plot 2*delta(t+2)-delta(t-4), for n=[-5,5]


figure(2)
subplot(2,1,1)
t = -5:5;
axis([-5 5 -2 2]);
y3 = 2*delta(t+2) - delta(t-4);
stem(t,y3,'b','linewidth',1.5);xlabel('n');
title('(d) 2*delta(t+2) - delta(t-4)'); ylabel('y3');

%% (e) plot n(u(n)-u(n-10))+(10exp(-0.3(n-10)))(u(n+10)-u(n-20)),


for n=[0,20]
n = 0:20;
y4 = n.*(U(n)-U(n-10)) + (10*exp(-0.3*(n-10))).*(U(n+10) - U(n-
20));
y5=U(-n+10);y6=U(-n);
subplot(2,1,2)
stem(n,y4,'b','linewidth',1.5); xlabel('n');ylabel('y4');
title('(e) n.*(U(n)-U(n-10)) + (10*exp(-0.3*(n-10))).*(U(n+10) -
U(n-20))');

%% function to find delta function


function Y = delta(X)
Y = zeros(size(X));
Y(X==0) = 1;
end

%% function to find unit step function


function Y2 = U(X1)
for i=1:size(X1,2)
if(X1(i)>=0)
Y2(i)=1; % for +ve input index(n1(i)>0), U(n1)=1;
else
Y2(i) = 0; % for -ve input index U(n1)=0;
end
end
end

Results:
Comment: In this first question we plot different-2 equation. In matlab code we define delta
function and unit step function, which used in d and e part.
Q.2) Write a MATLAB program to perform the following basic operation for the
sequence: x(n) = [2,1,-4,3,2,4,-3] and y(n) = [1,4,2,5].
a) Addition b) Multiplication
c) perform shifting after addition . Z(n+2) and Z(n-4)
d) Energy of a sequence after Multiplication.
e) Determine power of a sequence after Addition.
MATLAB program:
X = [2,1,-4,3,2,4,-3]; % sequence x(n)
Y = [1,4,2,5]; % sequence y(n)
s = add(X,Y);
m = multiply(X,Y);
[ns1,shift_2_left] = shift(s,2); % left shift by 2, ns1= new
indexing,z(n+2)
[ns2,shift_4_right] = shift(s,4); % right shift by 4, ns2= new
indexing,z(n-4)
E = Energy(m); % Energy calculation after multiplication
of sequence,
P = Energy(s)/(size(s,2)); % Power calculation after additon
of sequence, P=(E/N)
n1 = 0:size(s,2)-1; % index for addition output
n2 = 0:size(m,2)-1; % index for multiplication output

%%plot output figures


figure(1)
subplot(2,1,1)
stem(n1,s,'linewidth',1.5); %plot discreat addition
output vs their index
xlabel('sample time'); ylabel('addition');
subplot(2,1,2) %plot multipication vs their index
stem(n2,m,'linewidth',1.5);
xlabel('sample time'); ylabel('multiplication');
figure(2)
subplot(3,1,1)
stem(n1,s,'b','linewidth',1.5);
xlabel('n'); ylabel('Z');title('Original sequence,Z(n)');
subplot(3,1,2)
stem(ns1,shift_2_left,'b','linewidth',1.5);
xlabel('sample time shifted,n+2');
ylabel('output');title('z(n+2)');
subplot(3,1,3)
stem(ns2,shift_4_right,'b','linewidth',1.5);
xlabel('sample time shifted,n-4'); ylabel('output');
title('z(n-4)');

%% print the result


fprintf('Addition = ');disp(s);
fprintf('Multiplication = ');disp(m);
fprintf('Energy of sequence after Multiplication = %f\n',E);
fprintf('Power of sequence after Addition = %f\n',P);

%% addition program for discrete time signal


function Y_1 = add(x_n,y_n)
if size(x_n,2) == size(y_n,2)
Y_1 = x_n + y_n;
elseif size(x_n,2)>size(y_n,2)
m = zeros(1,size(x_n,2)-size(y_n,2));
yn = [y_n,m];
Y_1 = x_n + yn;
elseif size(x_n,2)<size(y_n,2)
m = zeros(1,size(y_n,2)-size(x_n,2));
xn = [x_n,m];
Y_1 = xn + y_n;
end
end
%% multiplication of two discrete signal
function Y_1 = multiply(x_n,y_n)
if size(x_n,2) == size(y_n,2)
Y_1 = x_n.*y_n;
elseif size(x_n,2)>size(y_n,2)
m = zeros(1,size(x_n,2)-size(y_n,2));
yn = [y_n,m];
Y_1 = x_n.*yn;
elseif size(x_n,2)<size(y_n,2)
m = zeros(1,size(y_n,2)-size(x_n,2));
xn = [x_n,m];
Y_1 = xn.*y_n;
end
end

%% function shifting
function [N1,Z1] = shift(Z,shift_value)
la = size(Z,2);
N1=0-shift_value:la-shift_value-1; %new indexing
Z1 = Z; %output sequence
end

%% Energy calculation
% Since energy of a sequence
function Y = Energy(Z);
Y = 0;
for i= 1:size(Z,2)
Y = Y + (Z(i))^2;
end
end
Results:

Addition and Multiplication:


Shifted signal after addition, z(n+2), z(n-4)

Output Energy and Power:

Comment: Since X and Y are finite sequence, so energy of their addition and
power of their multiplication is finite.
Q.3) Write a MATLAB program to plot the following sequence for
x[n] = [1,2,3,4,6,8,5,4,3,2,1]. Origin is at 6. x1(n) = 3*x(n-2) – 4*x(n+3) ,
x2(n) = 2*x(2-n) + x(n)*x(n-3)
MATLAB code:
X = [1,2,3,4,6,8,5,4,3,2,1]; % Origin at 6
n = -4:6;
[Z1,M1,N1] = sumNmul(3*X,n+2,-4*X,n-3); % Note for right
shift(in actual n-2) we use n+2,
[Z2,M2,N2] = sumNmul(X,n,X,n+3); %and for left shift(n+3)
we use n-3
[Z3,M3,N3] = sumNmul(2*X,-2-n,M2,N2);
subplot(3,1,1)
stem(n,X,'linewidth',1.5);grid on;
xlabel('n'); ylabel('X'); title('Original signal, X(n)')
subplot(3,1,2)
stem(N1,Z1,'linewidth',1.5); grid on;
xlabel('n'); ylabel('SUM Z1'); title('3X(n-2)-4X(n+3)');
subplot(3,1,3)
stem(N3,Z3,'linewidth',1.5);
xlabel('n');ylabel('output,Z3');title('2X(2-n)+X(n)*X(n+3)');

%%
% program to calculate addition and multiplication of two
sequence matrix
% here Y= X1+X2, M=X1*X2, and N= new indexing
function [Y,M,N] = sumNmul(X1,n1,X2,n2)
% here X1 is a sequence and n1 its corresponding index, similer
for X2,and n2
X1_new = X1;
X2_new = X2; % save input sequence in X2_new

if n1(1)>n1(end) % check for -n indexing


n_1 = flip(n1); % if first eliment of index is higher
than last eliment then flip the sequence
X1_new = flip(X1_new); % and filp the input sequence
matrix also
else
n_1 = n1;
end
if n2(1) > n2(end)
n_2 = flip(n2);
X2_new = flip(X2_new);
else
n_2 = n2;
end
lb = min(n_1(1),n_2(1)); % find minimum index between both
indexing
lm = max(n_1(end),n_2(end)); % find maximum index between both
indexing
N = lb:lm; % resulting index
if n_1(1)<n_2(1)
dif_1 = abs(n_2(1)-n_1(1));
% take absolute value of difference between primary index of
both array
X2_new = [zeros(1,dif_1),X2_new]; % match left dimention of
sequence
elseif n_1(1)>n_2(1)
dif_1 = abs(n_1(1)-n_2(1));
X1_new = [zeros(1,dif_1),X1_new];
end

if n_1(end)<n_2(end)
dif_2 = abs(n_1(end)-n_2(end)); %match right dimention of
sequence
X1_new = [X1_new,zeros(1,dif_2)];
elseif n_1(end)>n_2(end)
dif_2 = abs(n_1(end)-n_2(end));
X2_new = [X2_new,zeros(1,dif_2)];
end
Y = X1_new + X2_new; % addition of two discrete sequence
M = X1_new .*X2_new; % multiplication of two discrete
sequence
end

Result: We use user define function ‘ sumNmul()’ which gives us the addition
,multiplication and their new indexing of two sequen
Output result of command window:
Q.4) x(n) = U(n) – U(n-10). Determine and plot even and odd component using
MATLAB.

MATLAB code:
% Program to find odd and even component of a sequence
n=0:20;
X = U(n)-U(n-10);
% calling function sumNmul(), to find S1 = X(n)-X(-n); N1 is
resulting sequence
[S1 M1 N1] = sumNmul(X,n,-1*X,-n);
odd_cmpnt = 0.5*S1; % odd component = 0.5*S1, since
S1=X(n)-X(-n);
[S2 M2 N2] = sumNmul(X,n,X,-n); % S2 = X(n)+X(-n); N2=
resulting index
even_cmpnt = 0.5*S2; % even component = 0.5*S2;
subplot(3,1,1)
stem(n,X,'linewidth',1.5);ylabel('X');
title('original signal, X(n)=U(n)-U(n-10)');
subplot(3,1,2)
stem(N1,odd_cmpnt,'linewidth',1.5);ylabel('odd component');
title('odd component = (1/2)*[X(n)-X(-n)]');
subplot(3,1,3);
stem(N2,even_cmpnt,'linewidth',1.5);xlabel('n');ylabel('even
component');
title('even component = (1/2)*[X(n)+X(-n)]');

%% Defining unit step function


function Y2 = U(n1) % n1=index matrix
for i=1:size(n1,2)
if(n1(i)>=0) % for +ve input index(n1(i)>0),
U(n1)=1;
Y2(i)=1;
else
Y2(i) = 0; % for -ve input index U(n1)=0;
end
end
end

Result:
Comment : we define a unit step function named U(n) here, and a function
named ‘sumNmul()’ ,which we define in third question, are used here to find odd
and even component of X(n) sequence. Function ‘sumNmul()’ gives us the
addition ,multiplication of two sequence, and their new indexing value.
Q.5) Write a MATLAB program to generate the following data s[n] and the
noise d[n] and perform ensemble average.
s [ n ] =2 [ n ( 0.9 )n ] for 0 ≤ n ≤ 71

Assume there are 50 measurements. Calculate MSE and relative error after
ensemble averaging. Use randn() function to generate and add noise.
MATLAB code:
n=0:71;
S=2*(n.*((0.9).^n));
subplot(3,1,1)
plot(n,S,'b','linewidth',1);title('Original
signal,S=2[n(0.9)^n ]')
xlabel('n'); ylabel('S');
%stem(n,S);
for i=1:50 % for loop to creat 50 new signal
with random noise
delta = 0.2*randn(1,size(n,2)); %creating random signal of
dimention 1x72
Z(i,:)=S(1,:)+delta(1,:);
end
%Y1 = mean(Z); % find mean value using inbuilt matlab
function
Y2 = my_mean(Z); % find mean value by self built mean
function
subplot(3,1,2)
plot(n,Z);xlabel('n');ylabel('50 different noisy signals')
title('signal with 50 noise signal');
subplot(3,1,3)
plot(n,Y2,'b','linewidth',1);ylabel('mean(Z)');xlabel('n');
title('average of Z,ensemble average');
yy = immse(S,Y2); %(MSE) calculated by inbuild
function
mse_error = MSE_err(S,Y2) % find MSE error
Relative_error = relative_error(S,Y2);

%% print the results for MSE(maximum square error) and relative


error
fprintf('maximum square error(MSE)= %f\n',mse_error);
fprintf('Relative Error = %f\n',Relative_error);
fprintf('maximum square error(MSE) calculated by inbuild
function(immse)= %f\n',yy);

%% function to find mean value


function Y_mean = my_mean(Z1)
[row,column] = size(Z1); %find number of row and column
in Z1
for i=1:column
sum = 0;
for j=1:row
sum = sum+Z1(j,i);
end
Y_mean(i) = sum/row;
end
end

%% function to find mean square error


function ms_error = MSE_err(X,Y)
Za = X-Y;
square_sum = 0;
for i=1:size(X,2)
square_sum = square_sum+(Za(i))^2 ;
end
ms_error = (square_sum)/size(X,2);
% ms_error=(norm(X(:)-Y(:),2).^2)/numel(X);
end

%% function to find relative error


function relative_err = relative_error(X,Y)
Za = X-Y;
absolute_sum=0;
for i=1:size(X,2)
absolute_sum = absolute_sum +abs(Za(i)); % find absolute sum
end
relative_err = absolute_sum/size(X,2);
end
Results:
Command window outputs:
Here we get MSE error = 0.000824, and relative error = 0.022144 for ensemble
average filter output sequence, but for any random sequence (let we take Z(4,:))
we get MSE = 0.33684, and Relative error = 0.145947. So by comparing for both
we can say that after applying ensemble filtering we get more near to original
sequence or very less error compare to any random one.

You might also like