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

N_c = 2 * N - 1;                                     % Length of conv. singal in samples
n_c = 0 : N_c - 1;                                  % Time indices of conv signal

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

% Set v to rect pulse --------------------------------------------
v = zeros(N, 1);
v(1 : 10) = 1; 

% Set signal u ----------------------------------------------------
u = zeros(N, 1);
u(1) = 1;               % Unity impulse  
% u(10) = 1;             % Shifted unity impulse
% u(1 : 10) = 1;         % Short rect pulse
% u(1 : 15) = 1;         % Long rect pulse

% Compute convolution of v and u -------------------------
y = conv(v, u);

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

U = fftshift(fft(u) / N);

Y = fftshift(fft(y(n_c < N) / N));

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

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

subplot(3, 2, 1)
stem(n, v, 'LineWidth', 2)
hold on
grid on
xlabel('$n$', 'FontSize', 20);
ylabel('$v(n)$', 'FontSize', 20);
title('Discrete time signal $v(n)$', 'FontSize', 20)

%%
subplot(3, 2, 2)
stem(mu, abs(V), 'LineWidth', 2)
grid on
xlabel('$\mu$', 'FontSize', 20);
ylabel('$|V(\mu)|$', 'FontSize', 20);
title('Abs. DFT spectrum $|V(\mu)|$', 'FontSize', 20)

%%
subplot(3, 2, 3)
stem(n, u, 'LineWidth', 2, 'Color', cmap(2, :))
grid on
xlabel('$n$', 'FontSize', 20);
ylabel('$u(n)$', 'FontSize', 20);
title('Discrete time signal $u(n)$', 'FontSize', 20)

%%
subplot(3, 2, 4)
stem(mu, abs(U), 'LineWidth', 2, 'Color', cmap(2, :))
grid on
xlabel('$\mu$', 'FontSize', 20);
ylabel('$|U(\mu)|$', 'FontSize', 20);
title('Abs. DFT spectrum $|U(\mu)|$', 'FontSize', 20)

%%
subplot(3, 2, 5)
stem(n_c, y, 'LineWidth', 2, 'Color', cmap(3, :))
grid on
xlim([0, N - 1])
xlabel('$n$', 'FontSize', 20);
ylabel('$y(n)$', 'FontSize', 20);
title('Discrete time signal $y(n)$', 'FontSize', 20)

%%
subplot(3, 2, 6)
stem(mu, abs(Y), 'LineWidth', 2, 'Color', cmap(3, :))
grid on
xlabel('$\mu$', 'FontSize', 20);
ylabel('$|Y(\mu)|$', 'FontSize', 20);
title('Abs. DFT spectrum $|Y(\mu)|$', 'FontSize', 20)

