close all hidden
clear
clc

set(groot, 'DefaultTextInterpreter', 'LaTeX');
set(groot, 'DefaultAxesTickLabelInterpreter', 'LaTeX');
set(groot, 'DefaultAxesFontName', 'LaTeX');
set(groot, 'DefaultAxesFontSize', 14);
set(groot, 'DefaultLegendInterpreter', 'LaTeX'); 
cmap = lines;

%**************************************************************************
% Basic paramters
%**************************************************************************
f_s_h = 200;    % Sampling freq for "continuous" signal in Hz
T_0 = 4;          % Fundamental period for normalization in s
T_max = 4;      % Signal duration in seconds

%**************************************************************************
% Calculate support variables
%**************************************************************************
% Continuous signal --------------------------------------
N_h = T_max * f_s_h + 1;    % Duration in samples
t_h = linspace(0, T_max / T_0, T_max * f_s_h + 1); % Time scale in seconds

%**************************************************************************
% Signal generation
%**************************************************************************

% Rect signal -----------------------------------------------
v = ones(N_h, 1);
v(ceil(N_h/2) : end) = v(ceil(N_h/2) : end) * -1;

% Fourier series approximation
y = zeros(N_h, 1);

% Number of trigonometric coefficients
M = 10;

% Offset
c_0 = 0;

% Even coefficients
a = zeros(M, 1);

% Odd coefficients
b = zeros(M, 1);

% Calculate odd coefficients according to equation
% b_mu = (4 * h) / (pi * mu)
% Also calculate limited Fourier series approx
for mu = 1 : M 
    if mod(mu, 2)
        b(mu) = (4*1) / (pi * mu);
        y = y + b(mu) * sin(2*pi*mu*t_h)';
    else
        b(mu) = 0;
    end
end

% Calculate complex coefficients from trigonometric ones
% c_mu = 1/2 * (a_mu + j * b_mu) with c(-mu) = c(mu)*
c = 1/2 * ([flip(a); c_0; a] + 1i*[-flip(b); 0; b]);

%**************************************************************************
% Plots
%**************************************************************************
% Evaluate section by section to get plots after each other

figure
% set(gcf, 'Position', [2000 0 1400 1350]) % Set window size


subplot(3, 1, 1)
plot(t_h, v, 'LineWidth', 2)
hold on
grid on
xlabel('$\frac{t}{T_0}$', 'FontSize', 30);
ylabel('$v(t)$', 'FontSize', 20);
title('Continuous rect signal', 'FontSize', 20)

%%
plot(t_h, y, 'LineWidth', 2);
legend('Original signal', 'Finite Fourier series approx.', 'FontSize', 20)

subplot(3, 1, 2)
stem(0, c_0, 'LineWidth', 2)
hold on
stem(a, 'LineWidth', 2)
stem(b, 'LineWidth', 2)
grid on
xlim([0 max(10, M)])
xlabel('$\mu$', 'FontSize', 20);
ylabel('$a_\mu$, $b_\mu$', 'FontSize', 25);
title(['Trigonometric Fourier coefficients ($\mu \leq$ ', num2str(M), ')'], 'FontSize', 20)
legend({'$c_0 = \frac{a_0}{2}$', '$ a_\mu$', '$ b_\mu$'}, 'FontSize', 20)

%%
subplot(3, 1, 3)
stem(-M : M, real(c), 'LineWidth', 2)
hold on
stem(-M : M, imag(c), 'LineWidth', 2)
grid on
xlim([-max(10, M) max(10, M)])
xlabel('$\mu$', 'FontSize', 20);
ylabel('$c_\mu$', 'FontSize', 25);
title(['Complex Fourier coefficients ($\mu \leq$ ', num2str(M), ')'], 'FontSize', 20)
legend({'$\Re\big\{c_\mu\big\}$', '$\Im\big\{c_\mu\big\}$'}, 'FontSize', 20)

