finished exercise except for some parts of number 1

This commit is contained in:
2026-05-09 17:16:35 +02:00
parent a4f2001671
commit 99b68b10eb
14 changed files with 301 additions and 217 deletions

View 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)]);

View File

@@ -0,0 +1,39 @@
clear
close all
clc
% Convolution function
function [ Y ] = convolution( h, x )
Ny = length(x)+length(h)-1
Y = zeros(1, Ny);
for n = 1:Ny
for i = 1:length(h)
if (n-i+1 >= 1) && (n-i+1 <= length(x))
Y(n) = Y(n) + h(i) * x(n-i+1)
end
end
end
end
n = 0:49
hn = [0.25, 0.5, 0.25]
xn = cos(((2*pi)/20)*n)
% Perform convolution of xn and hn
Y = convolution(hn, xn);
Y2 = conv(xn, hn)
% Plot the results
plot(Y);
xlabel('Sample Index');
ylabel('Amplitude');
title('Convolution Result');
grid on;
% Plot Y2 on same graph
hold on;
plot(Y2, '--');
legend('Custom Convolution', 'MATLAB conv Function');
hold off;

View File

@@ -0,0 +1,29 @@
function X = dtft ( x , n , w )
% DTFT Computes Discrete-time Fourier transform
% @param x : finite duration sequence (vector) over n
% @param n : sample indices vector
% @param w : frequency vector
% @return X : DTFT of x[n] at frequencies w
% this function does not use for loops
X = sum(x(:) .* exp(-1j * n(:) * w), 1);
end
n = -10:10;
x = (0.8) .^ abs(n) .* ((n >= -10) & (n < 11));
w = linspace(-pi, pi, 1000);
X = dtft(x, n, w);
magnitude = abs(X);
phase = angle(X);
figure;
subplot(2, 1, 1);
plot(w, magnitude);
title('Magnitude Response');
xlabel('Frequency (rad/sample)');
ylabel('|X(w)|');
subplot(2, 1, 2);
plot(w, phase);
title('Phase Response');
xlabel('Frequency (rad/sample)');
ylabel('Phase(rad)');

View File

@@ -0,0 +1,42 @@
% Read and play original audio
[audio, fs] = audioread('./audio.wav');
%sound(audio, fs);
% Read and play room impulse response (RIR)
[rir, fs_rir] = audioread('./rir.wav');
%sound(rir, fs_rir);
% Apply convolution (reverb effect)
audio_reverb = conv(audio, rir);
%sound(audio_reverb, fs);
% Plot the waveforms
figure;
% Original audio
subplot(2,1,1);
plot(audio);
title('Original Audio');
xlabel('Sample Number');
ylabel('Amplitude');
% Reverberated audio
subplot(2,1,2);
plot(audio_reverb);
title('Audio with Reverb');
xlabel('Sample Number');
ylabel('Amplitude');
% Calculate signal energy
function [ W ] = energy( x )
W = sum(abs(x).^2);
end
energy_rir = energy(rir)
energy_original = energy(audio)
energy_reverb = energy(audio_reverb)
% Display energy values
fprintf('Original Audio Energy: %.4f\n', energy_original);
fprintf('Reverberated Audio Energy: %.4f\n', energy_reverb);

BIN
Assignment2_211/audio.wav Normal file

Binary file not shown.

BIN
Assignment2_211/rir.wav Normal file

Binary file not shown.