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 = 10;
f_s_h = 200;    % Sampling freq for "continuous" signal in Hz
T_0 = 4;          % Fundamental period for normalization in s
f_1 = 1;           % Fundamental freq for sin signal in Hz
T_max = 4;      % Signal duration in seconds

%**************************************************************************
% Calculate support variables
%**************************************************************************
% Discrete signal --------------------------------------
N = T_max * f_s + 1;                             % Duration in samples
n = 0 : N - 1;                                         % Time indices
mu = -floor((N - 1) / 2) : floor((N - 1) / 2);  % Spectral indices
mu_abs = 0 : floor((N - 1) / 2);                % One-sided spectral indices
%**************************************************************************
% Signal generation
%**************************************************************************

% Unity impulse -----------------------------------------------
% v = zeros(N, 1);
% v(1) = 1;

% Offset ---------------------------------------------------------
v = ones(N, 1);

% Sine signal ------------------------------------------------
% m = 1;
% h = 0;
% v = sin(2*pi*m/N * n) + h;

% Rect signal ------------------------------------------------
% v = ones(N, 1);
% v(n > N / 2) = -v(n > N/2);
% v(1) = 0;


% Compute DFT / FFT
V = fftshift(fft(v) / N);

% Compute one-sided spectrum
V_abs = abs(V(mu >= 0));
V_abs(mu_abs > 0) = 2 * V_abs(mu_abs > 0);

%**************************************************************************
% 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)
stem(n, v, 'LineWidth', 2)
hold on
grid on
xlabel('$n$', 'FontSize', 20);
ylabel('$v(n)$', 'FontSize', 20);
title('Discrete signal', 'FontSize', 20)

%%

subplot(3, 1, 2)
stem(mu, real(V), 'LineWidth', 2)
hold on
stem(mu, imag(V), 'LineWidth', 2)
grid on
xlabel('$\mu$', 'FontSize', 20);
ylabel('$V(\mu)$', 'FontSize', 20);
title('DFT spectrum', 'FontSize', 20)
legend({'$\Re\big\{V(\mu)\big\}$', '$\Im\big\{V(\mu)\big\}$'}, 'FontSize', 20)

%%
subplot(3, 1, 3)
stem(mu_abs, V_abs, 'LineWidth', 2)
grid on
xlabel('$\mu$', 'FontSize', 20);
ylabel('$|V(\mu)|$', 'FontSize', 20);
title('One-sided DFT spectrum', 'FontSize', 20)

