Assignment DSP 1
Assignment DSP 1
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
%% 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:
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 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)]');
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);