function ask3_hy215(flag)
% 3nd set of exercises - HY215 - 2014
%
% Input:
% ------
%    flag [1x1] : integer number 8, 9 (corresponds to exercise number)
%
% Output:
% ------
%    plots
%
% Example: ask2_hy215(9);
%
% @ George Kafentzis, Computer Science Department, UoC, 2014

if flag ~= 8 && flag ~= 9
    error('Invalid Argument! Please type help ask3_hy215 for instructions.');
end

if flag == 8
    dt = 1/100;             % To bhma mas
    D = 20;                 % H diarkeia tou shmatos sto xrono
    t = 0:dt:D;             % O a3onas tou xronou
    df = 1/D;               % To bhma mas sth syxnothta
    f = -5*pi:df:5*pi;      % O a3onas twn syxnothtwn [-5pi, 5pi]
    M = exp(-j*2*pi*t'*f);
    %Minv = exp(j*2*pi*f'*t);
    a = 0.2;
    x = exp(-a*t);
    figure; plot(t,x); xlabel('Time'); ylabel('Amplitude'); title('e^{-at}\epsilon(t)');
    X = dt*x*M;
    Xtheoretic = 1./(a + j*2*pi*f);
    figure; plot(f, abs(X)); hold on; plot(f, abs(Xtheoretic), 'm--'); hold off;
    xlabel('Frequency (Hz)'); ylabel('Magnitude'); title('Theoretic and MATLAB-based Magnitude Spectrum');
    legend('MATLAB result', 'Theoretic');
elseif flag == 9
    dt = 1/100;             % To bhma mas
    df = 1/10;              % To bhma mas sth syxnothta
    t = -5:dt:5;            % O a3onas tou xronou [-5,5]
    f = -5*pi:df:5*pi;      % O a3onas twn syxnothtwn [-5pi, 5pi]
    x = rectpuls(t);        % To x(t)
    y = (1 + cos(pi*t)).*rectpuls(t); % To y(t)
    subplot(211); plot(t,x, 'LineWidth', 2);
    xlabel('Time (s)'); ylabel('Amplitude'); title('x(t)');
    subplot(212); plot(t,y, 'LineWidth', 2);
    xlabel('Time (s)'); ylabel('Amplitude'); title('y(t)');
    M = exp(-j*2*pi*t'*f);
    X = dt*x*M;             % O met. Fourier tou x(t)
    Y = dt*y*M;             % O met. Fourier tou y(t)
    figure; plot(f, abs(X)); hold on; plot(f, abs(Y), 'm--'); hold off;
    xlabel('Frequency (Hz)'); ylabel('Magnitude'); Title('Magnitude Spectra');
    legend('X(f)', 'Y(f)');
    x_norm = x./sqrt(sum(x.^2));  % Kanonikopoihsh
    y_norm = y./sqrt(sum(y.^2));  % Kanonikopoihsh
    Xn = dt*x_norm*M;             % O met. Fourier tou kanonikopoihmenou x(t) 
    Yn = dt*y_norm*M;             % O met. Fourier tou kanonikopoihmenou y(t)
    figure; plot(f, abs(Xn)); hold on; plot(f, abs(Yn), 'm--'); hold off;
    xlabel('Frequency (Hz)'); ylabel('Magnitude'); Title('Magnitude Spectra from normalized signals');
    legend('Xn(f)', 'Yn(f)');
end

