Code Matlab
Code Matlab
m 1 of 2
%question 1
%A
n = 0:1:82;%0<=n<=82
subplot(3,2,1);
y = (-2.7)*exp((-0.4+1i*pi/6)*n); %to plot exponetial function
stem(n,y); % used to plot discrete signal
xlabel('n');
ylabel("amplitude");
title("exp signal");
%B to plot cosine
subplot(3,2,2);
n = -10:0.1:10;
y = cos(0.6*pi*n +0.3*pi);
stem(n,y);
hold on;% hold used to hold y curve
plot(n,y,'LineWidth',2);%for compare i also plot continuous signal with discrete
xlabel('n');
ylabel("amplitude");
title("cos signal");
%C to plot sinosudial
subplot(3,2,3);
n = -10:0.1:10;
y = sin(0.1*pi*n+0.75*pi)-3*cos(0.8*pi*n+0.2*pi)+cos(1.3*pi*n);
stem(n,y);
hold on;
plot(n,y,'LineWidth',2);%"LineWidth" use for increasing the thickness of curve
xlabel('n');
ylabel('amplitude');
title('combine signal');
%D to plot impulse signal
subplot(3,2,4);
n =-5:1:5;
impulse=2*(n==-2)-(n==4); %impulse at n=-2 and n=4 only
stem(n,impulse);
xlabel('n');
ylabel("amplitude");
title("impluse signal");
%E
subplot(3,2,5);
n=0:1:20;
a=(n>=0); %a=u[n]
b=(n>=10);%b=u[n-10]
c=(n>=20); %c=u[n-20]
d=10*exp(-0.3*(n-10));
y=n.*(a-b)+d.*(a-c); %y=n*(u[n]-u[n-10])+10*exp(-0.3*(n-10))*(u[n]-u[n-20])
stem(n,y);
xlabel('n');
ylabel("amplitude");
title("compound signal");
29/8/24 12:10 AM Q_2.m 1 of 2
%a %Addition
l_x=length(x);%length function to find the length of array
l_y=length(y);
l_z=max(l_x+y_origin,l_y+x_origin);
x_padded=zeros(1,l_z);
y_padded=zeros(1,l_z);
x_padded(y_origin + (1:length(x))) = x;
y_padded(x_origin + (1:length(y))) = y;
z_padded=x_padded+y_padded;
new_origin=max(x_origin,y_origin);
n=1:l_z;
subplot(2,2,1);
stem(n,z_padded);
xlabel("n");
ylabel("z[n]")
title('addition');
%b %%Multiplication
subplot(2,2,4);
n_min = min(min(n1), min(n2));
n_max = max(max(n1), max(n2));
N = n_min:n_max;
% Initialize the resulting sequence with zeros
Z = zeros(1, length(N));
% Perform the multiplication with alignment
for i = 1:length(N)
if ismember(N(i), n1) && ismember(N(i), n2)
Z(i) = x(n1 == N(i)) * y(n2 == N(i));
end
end
stem(N,Z);
xlabel("N");
ylabel("Z")
title('Multiplication');
disp('multilication');
disp(Z);
ylabel("z[n+2]")
title('addition');
n_shift_2=n-4;%shift z(n) to z(n-4)
subplot(2,2,3);
stem(n_shift_2,z_padded);
xlabel("n");
ylabel("z[n-4]")
title('addition');
disp('addition of given sequences=' )
disp(z_padded);
%d %ENERGY
E=sum(abs(Z).^2);
disp('energy of sequence=');
disp(E);
%e %powers
P = sum(abs(z_padded).^2) / l_z;
disp('power=')
disp(P);
28/8/24 11:57 PM q3.m 1 of 1
%Q3
close all;
x=[1 2 3 4 6 8 5 4 3 2 1]; % given discrete sequence
n=-4:1:6; %origin at 6
x_n_minus_2 = circshift(x, 2); %%"circshift" usefor circular shifting
a=x_n_minus_2; %%a=x(n-2)
x_n_plus_3 = circshift(x, -3);
b=x_n_plus_3; %b=x(n+3)
x_n_minus_3 = circshift(x, 3);
c=x_n_minus_3; %c=x(n-3)
x_reversed = fliplr(x); %"fliplr use for reverse of x[n]
%fliplr(x(n))=x(-n);
x_minus_n_plus_2 = circshift(x_reversed, 2);
d=x_minus_n_plus_2;%d=x(2-n)
x1=3.*a-4.*b;%x1(n)=3*x(n-3)-4*x(n-2)
x2=2.*d+x.*c;%x2(n)=2*x(2-n)+x(n)
subplot(2,1,1);
stem(n,x1);
xlabel('n');
ylabel("amplitude");
title('x1[n]');
subplot(2,1,2);
stem(n,x2);
xlabel('n');
ylabel("amplitude");
title('x2[n]');
28/8/24 11:35 PM Q_4.m 1 of 1
close all;
n=-20:1:20;%interval
a=(n>=0); %a=u[n]
b=(n>=10) ; %b=u[n-10]
x=a-b; %x[n]=u[n]-u[n-10]
subplot(3,1,1);
stem(n,x);
xlabel('n');
ylabel('amplitude');
title('original signal');
y=fliplr(x); %here flip function used for x[-n]=y
%odd
x_odd=(x-y)/2; % x_odd=(x[n]-x[-n])/2
subplot(3,1,2);
stem(n,x_odd);
xlabel('n');
ylabel('amplitude');
title('odd signal');
%even
x_even=(x+y)/2; % x_even=(x[n]+x[-n])/2
subplot(3,1,3);
stem(n,x_even);
xlabel('n');
ylabel('amplitude');
title('even signal');
29/8/24 12:22 AM Q5.m 1 of 1
n=0:1:71;
s=2.*(n.*(0.9).^n);
stem(n,s);
num_measurements = 50;
measurements = zeros(num_measurements, length(s)); %%initialize measurments matrix
with 50 row and as many column
for i = 1:num_measurements
noise = randn(size(s)); % Generate noise using random function of the length of s
measurements(i, :) = s + noise; % Add noise to the signal % i=1 to 50 and : for
access all the column
end
ensemble_average = mean(measurements, 1);
mse = mean((ensemble_average - s).^2);
relative_error = mse / mean(s.^2);
disp('Mean Squared Error=');
disp(mse);
disp('relative_error');
disp(relative_error);