FIRST METHOD
%fourier series approximation for figure 1 (x(t)):

t = -5: 0.01 : 5;
xt = zeros (1, length(t));
N = 10;
for k = -N: N
xk = 0.25*(1-exp(-j*k*pi))/(j*(k+0.001)*pi/2);
xt =xt + xk*exp(k * j * 0.5 * pi * t);
end
xt = 0.5 + xt;

plot(t, xt)

-----------------------------------------------------
SECOND METHOD
Figure 1
% Define the time vector
t = -4:0.01:4;

% Define the square wave function x(t)
x = u(t) - u(t - 2);

% Define the Fourier series parameters
T = 4; % Period of x(t)
w = 2 * pi / T; % Angular frequency
N = 10; % Number of terms

% Initialize the synthesized signal vector
xt = zeros(size(t));

% Synthesize the signal using Fourier series
for k = -N:N
    xk = (1/T) * trapz(t, x .* exp(-1i * k * w * t));
    xt = xt + xk * exp(1i * k * w * t);
end
% Plot the synthesized signal
figure;
plot(t, real(xt)); % Plot the real 
