Lab 9
Lab 9
14-fet/bsce/f22
Qno 1
Code.
x_t = zeros(size(t));
for k = 1:length(t)
x_t(k) = t(k);
x_t(k) = 1;
x_t(k) = 3 - t(k);
end
end
figure;
for i = 1:length(N_values)
N = N_values(i);
subplot(3, 1, i);
ylabel('x(t)');
legend('Original', 'Approximation');
grid on;
end
Q no 2.
Code>
T = 2;
t = linspace(0, T, 1000);
for k = 1:length(t)
if t(k) >= 0 && t(k) <= 1
x_t(k) = 2 * t(k);
elseif t(k) > 1 && t(k) < 2
x_t(k) = 0;
end
end
N_values = [7, 18, 61];
fourier_series_approximation = @(N, T, t, x_t) arrayfun(@(ti)
sum(fourier_coefficients(N, T, x_t) .* exp(1i * 2 * pi * (1:N) / T * ti)), t);
fourier_coefficients = @(N, T, x_t) arrayfun(@(n) (2 / T) * trapz(t, x_t .* exp(-1i *
2 * pi * n / T * t)), 1:N);
for i = 1:length(N_values)
N = N_values(i);
x_approx = fourier_series_approximation(N, T, t, x_t);
subplot(3, 1, i);
plot(t, x_t, 'k', 'LineWidth', 1.5); hold on;
plot(t, real(x_approx), 'r--', 'LineWidth', 1.5);
title(['Fourier Series Approximation with ', num2str(N), ' Terms']);
xlabel('t');
ylabel('x(t)');
legend('Original', 'Approximation');
grid on;
end
sgtitle('Fourier Series Approximations of x(t)');