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

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

% Default for all exercises
T0 = 2;         % Periodos shmatos (se sec) (epilegw opoia 8elw)
d = T0/1200;    % Posa shmeia ana periodo 8elw?
D = 4;          % Poses periodous 8elw na exw?
N = 40;         % Posous orous sth seira Fourier na ypologisw?
t = 0:d:D*T0;   % O xronos t
w0 = 2*pi/T0;   % H kyklikh syxnothta (w0 = 2*pi*f0) - boleuei gia symbolo


if flag == 3
    A0 = 1;                 % O prwtos oros
    k = 1:N-1;              % 8elw N orous ths seiras
    Ak = 2./(pi*k);         % Oi 40 prwtoi oroi ths seiras
    x = A0 + Ak * cos((k'*w0)*t - pi/2); % To shma!!! :)
    plot(t,x);              % Sxhma
    xlabel('Time (sec)');
    ylabel('Amplitude');
    title('Result of Exercise 3');
elseif flag == 5
    A0 = 1/pi;                  % O prwtos oros
    k = 1:N;                    % 8elw N orous ths seiras
    Ak = 2./(pi*(4.*k.^2-1));   % Oi 40 prwtoi oroi ths seiras
    x = A0 + Ak*cos((2*k'*w0)*t - pi); % To shma!!! :)
    x = x + (1/2)*cos(w0*t - pi/2);    % Pros8etw kai ton oro gia k=1
    plot(t,x)                   % Sxhma
    xlabel('Time (sec)');
    ylabel('Amplitude');
    title('Result of Exercise 5');    
elseif flag == 8
    disp('Enjoy Beethoven!');
    fs = 11025;
    f_LA = 440;
    notes = [56 55 56 55 56 51 54 52 49 0 40 44 49 51 0];
    dur = [160*ones(1,8) 320 160*ones(1,4) 320 160];
    sig = cell(1, length(notes));   % initialize
    fr = zeros(1, length(notes));   % initialize
    music = [];                     % initialize
    % Find frequencies and synthesize signals
    for i = 1:length(notes)
        fr(i) = f_LA*2.^((notes(i)-49)/12);
        if notes(i) ~= 0
            t = 0:1/fs:dur(i)/1000;
            sig{i} = cos(2*pi*fr(i)*t);
        else
            t = 0:1/fs:dur(i)/1000;
            sig{i} = zeros(1, length(t));
        end
        music = [music sig{i}];
    end
    %plot(music);
    soundsc(music, fs);
end