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 parameters
%**************************************************************************
f_s = 200;        % Sampling freq for "continuous" signal in Hz
T_0 = 2;          % Fundamental period for signal description in s
T_max = 100;      % Signal duration in seconds

%**************************************************************************
% Calculate support variables
%**************************************************************************
N = T_max * f_s + 1;                     % Duration in samples
t = linspace(-T_max/2, T_max/2, N);      % Time scale in s
w = 2 * pi * linspace(-f_s/2, f_s/2, N); % Frequency scale in s^-1

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

signalSel = 1;

switch(signalSel)
    case 1
    % Rect signal -----------------------------------------------
    % Set time signal using ones and zeros
    v = ones(N, 1);                 % Fill signal with ones
    v(t < -T_0) = 0 * v(t < -T_0);  % Set signal before rect pulse to 0
    v(t > T_0) = 0 * v(t > T_0);    % Set signal after rect pulse to 0

    % Set discretized Fourier spectrum to sin(x)/x function
    V = abs(2 ./ w .* sin(w * T_0)); 

    % Calculate approx abs Fourier spectrum based on DFT
    V_dft = abs(fftshift(fft(v) / f_s));
    
    case 2
    % Sinc signal ----------------------------------------------
    % Hints: Define required auxiliary parameters first!
    %        Use sinc for sin(x)/x!
    
    case 3
    % Exp signal ----------------------------------------------
    % Hints: What does the step function in this case actually do?
    %        Use 1i for j!
    
    case 4
    % Cos signal ----------------------------------------------
    % Hints: Use min(abs(w - w_0)) to find freq index closest to w_0!
    %        Scaling of the fft has to be different here. How and why?
    
    case 5
    % Multiple rect signal ------------------------------------
    % Hints: Use v(t - t_x) indexing!
    %        Use a combination of the previous signals/results!
        
end

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

w_lim = 20;

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

subplot(3, 1, 1)
plot(t, v, 'LineWidth', 2, 'Color', cmap(1, :))
hold on
grid on
xlabel('$t$ / $\mathrm{s}$', 'FontSize', 20);
ylabel('$v(t)$', 'FontSize', 20);
title('Continuous time signal (discretized)', 'FontSize', 20)

subplot(3, 1, 2)
plot(w, V, 'LineWidth', 2, 'Color', cmap(2, :))
grid on
xlabel('$\omega$ / $\mathrm{s^{-1}}$', 'FontSize', 20);
ylabel('$|V(j\omega)|$', 'FontSize', 20);
title('Fourier spectrum (discretized)', 'FontSize', 20)
xlim([-w_lim, w_lim])

subplot(3, 1, 3)
plot(w, V_dft, 'LineWidth', 2, 'Color', cmap(3, :))
grid on
xlabel('$\omega$ / $\mathrm{s^{-1}}$', 'FontSize', 20);
ylabel('$|V_{dft}(j\omega)|$', 'FontSize', 20);
title('Fourier spectrum approx. using DFT', 'FontSize', 20)
xlim([-w_lim, w_lim])
