0% found this document useful (0 votes)
24 views3 pages

Lab 9

The document contains MATLAB code to perform Fourier series approximations of periodic signals with different numbers of terms. The code defines two periodic signals over different time periods, calculates their Fourier coefficients, and plots the original signals alongside the approximations for varying numbers of terms to compare the accuracy.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Lab 9

The document contains MATLAB code to perform Fourier series approximations of periodic signals with different numbers of terms. The code defines two periodic signals over different time periods, calculates their Fourier coefficients, and plots the original signals alongside the approximations for varying numbers of terms to compare the accuracy.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 10

Signal and system

14-fet/bsce/f22

Qno 1

Code.

T = 3; % Period of the function

t = linspace(0, T, 1000); % Time vector

x_t = zeros(size(t));

for k = 1:length(t)

if t(k) >= 0 && t(k) < 1

x_t(k) = t(k);

elseif t(k) >= 1 && t(k) < 2

x_t(k) = 1;

elseif t(k) >= 2 && t(k) <= 3

x_t(k) = 3 - t(k);

end

end

N_values = [3, 11, 51];

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

figure;

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)');

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)');

You might also like