finished exercise except for some parts of number 1
This commit is contained in:
62
Assignment2_211/assignment2_1.m
Normal file
62
Assignment2_211/assignment2_1.m
Normal file
@@ -0,0 +1,62 @@
|
||||
clear
|
||||
close all
|
||||
clc
|
||||
|
||||
% x1[n], -5 <= n <= 10
|
||||
n1 = -5:10;
|
||||
x1 = -4*(n1 == -3) + 4*(n1 == 0) - (n1 == 3) + 2*(n1 == 7);
|
||||
|
||||
% x2[n], -5 <= n <= 10
|
||||
n2 = -5:10;
|
||||
x2 = exp(-0.31*n2);
|
||||
|
||||
% x3[n], 0 <= n <= 256
|
||||
n3 = 0:256;
|
||||
x3 = 3*sin(2*pi*(3.5/64)*n3);
|
||||
|
||||
% x4[n], 0 <= n <= 256
|
||||
n4 = 0:256;
|
||||
x4 = -cos((9/64)*n4);
|
||||
|
||||
|
||||
% Visualization
|
||||
figure;
|
||||
subplot(121); hold on; grid on;
|
||||
stem(n1, x1);
|
||||
stem(n2, x2);
|
||||
xlabel('n');
|
||||
ylabel('x[n]');
|
||||
legend('x_1[n]', 'x_2[n]');
|
||||
|
||||
subplot(122); hold on; grid on;
|
||||
plot(n3, x3);
|
||||
plot(n4, x4);
|
||||
xlabel('n');
|
||||
ylabel('x[n]');
|
||||
legend('x_3[n]', 'x_4[n]');
|
||||
|
||||
% Custom power function
|
||||
function [ P ] = custom_power( x )
|
||||
P = (1/length(x)) * sum(abs(x).^2);
|
||||
end
|
||||
|
||||
% Calculate and display the power
|
||||
P3 = custom_power(x3);
|
||||
% Display the calculated powers
|
||||
disp(['Power of x_3[n]: ', num2str(P3)]);
|
||||
|
||||
% Energy function
|
||||
function [ W ] = energy( x )
|
||||
W = sum(abs(x).^2);
|
||||
end
|
||||
|
||||
% Calculate and display the energy of each signal
|
||||
P1 = energy(x1);
|
||||
P2 = energy(x2);
|
||||
P3 = energy(x3);
|
||||
P4 = energy(x4);
|
||||
% Display the calculated powers
|
||||
disp(['Energy of x_1[n]: ', num2str(P1)]);
|
||||
disp(['Energy of x_2[n]: ', num2str(P2)]);
|
||||
disp(['Energy of x_3[n]: ', num2str(P3)]);
|
||||
disp(['Energy of x_4[n]: ', num2str(P4)]);
|
||||
Reference in New Issue
Block a user