function FourierSeries(ex)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         Fourier Series Examples
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FOURIERSERIES(EX) executes simple Fourier Series examples. Two examples are 
% implemented. A simple +-1 pulse in a period (example 1), and a ramp function
% (example 2).
% 
% Input:
% -----
%      ex: [1x1] example number (ex = 1 or 2)
%
% Example:
% --------
%      FourierSeries(1);
%      FourierSeries(2);
%
% @ George Kafentzis, SigProcLab, CSD, UoC, 2010.
% e-mail : kafentz@csd.uoc.gr

if ex < 1 || ex > 2
	error('Only Two examples are implemented. Use ex = 1 or ex = 2.');
end

%
% Notes :
% -------
% 1)To see the result of each statement, just supress the ; at the end of each line.
% 2)To clear your workspace from all variables, type 'clear all'.
% 3)To close all your plots at once, just type 'close all'.
%
%
%

if ex == 1
	% FOURIER SERIES EXAMPLE 1:
	% -------------------------------------------------------------------------
	% In this lab, we will see "in action" the Fourier Series theory. In the
	% last lectures, you have seen that a periodic signal can be written as an infinite
	% sum of exponentials. This representation is called Fourier Series and it
	% informs us about the frequency content of the signal, that is which 
	% frequencies are present in the signal.
	% You've also seen that a periodic pulse signal defined as:
	%
	%                  {  A, 0 <= t <= T0/2
	%           x(t) = {
	%                  { -A, T0/2 <= t <= T0
	%
	% can be written as:
	%
	%           x(t) =  Ó { 2A/(jð(2k+1)) * exp(j(2k+1)ù0t) }
	%                   k
	%
	%
	% We will see now how we can "shape" this periodic signal using Fourier
	% Analysis in MATLAB.
	%
	%
	%
	% Let's define our periodic signal. Let's say that we want to plot only 2
	% periods:
	D = 2;

	% Let's say that the period of the signal is T0 = 2 sec.
	T0 = 2;

	% Let's say that the pulse amplitude is A = 2.
	A = 2;

	% Let's define the time axis now:
	Ts = T0/200;    % I want 200 samples per period (good enough, eh? :-) )
	t = 0:Ts:D*T0;  % Our time axis (it will be used later)
	
	% Our periodic signal is :
	plot([0 T0/2], [A A], 'LineWidth', 2); hold on; 
	plot([T0/2 T0], [-A -A], 'LineWidth', 2); hold on;
	plot([T0 (3*T0)/2], [A A], 'LineWidth', 2); hold on; 
	plot([(3*T0)/2 2*T0], [-A -A], 'LineWidth', 2); hold on; 
	grid; axis([0 2*T0 -2.5 2.5]); hold off; xlabel('Time'); ylabel('Amplitude');
	title('Periodic Pulse Signal (2 periods)');

	% A few comments about the code (62-67) above:
	% --------------------------------------------
	% This is a way to plot straight lines in MATLAB. It is not the only way
	% but it is simple and convenient. Take a look at the syntax of the
	% statements and the arguments of the plot functions. 'LineWidth' and
	% number 2 are just to make lines thicker, so don't bother with it.
	% Also, the axis statement just defines the limits (min and max) of the two
	% axis in the plot. For example, the time axis will be from 0 to 2T0 and
	% the amplitude axis will be from -2.5 to 2.5.


	% The above signal was represented as a Fourier Series in lecture 7.
	% Observe that in theory, the series has infinite terms. In practice, we
	% can use only a finite number of terms, let's say it is N.
	N = 10;
	
	% The mean value X0 is:
	X0 = 0;

	% Our k will be from -N/2 to N/2:
	k = -N/2:N/2;

	% We proved (in theory) that our Fourier coefficients will be:
	Xk = 2*A./(j*pi*(2*k+1));

	% The fundamental circular frequency will be:
	w0 = 2*pi/T0;
	
	% So, our signal will be:
	x = X0 + Xk * exp((j*(2*k'+1)*w0)*t);	% Take a look at the syntax. Is it
        	                                % correct? Why is it so? Xk and exp(...) are matrices!

	% Let's see it:
	figure; plot(t, real(x), 'LineWidth', 2); grid; xlabel('Time'); ylabel('Amplitude');
	title('Fourier Series of a periodic signal');

	% Let's play a little with the number of terms we use. Let's see what
	% happens when N = 10, 20, 40, 80, 160.

	N = 5;
	while(1)
	    N = 2*N; 
	    k = -N/2:N/2;
	    Xk = 2*A./(j*pi*(2*k+1)); 
	    x = X0 + Xk * exp((j*(2*k'+1)*w0)*t);
	    figure; plot(t, real(x), 'LineWidth', 2); grid; xlabel('Time'); ylabel('Amplitude');
	    title(['Fourier Series of a periodic signal for N = ', int2str(N)]);
	    if N == 160
        	break;
	    end
	end

	% What can you say about the series, when N increases?

else
	% FOURIER SERIES EXAMPLE 2:
	% -------------------------------------------------------------------------
	% Let's define the periodic signal x(t):
	%
	%       x(t) = At/T0, 0 <= t <= T0.
	%
	% The Fourier coefficients will be: (try it!)
	%
	%       Xk = -A/2jðk êáé X0 = A/2.
	%
	% Let's implement the Fourier Series for this signal in MATLAB:
	
	% Let's say we want to plot 4 periods:
	D = 4;

	% Let's say that our period is T0 = 2 sec:
	T0 = 2;

	% Let's say that A = 2:
	A = 4;

	% Defining the time axis:
	Ts = T0/200;    % I want 200 samples per period
	t = 0:Ts:D*T0;  % The time axis

	% Our periodic signal will be:
	figure; plot(0:Ts:T0, A*[0:Ts:T0]/T0, 'LineWidth', 2); hold on; 
	plot(T0:Ts:2*T0, A*([T0:Ts:2*T0]-T0)/T0, 'LineWidth', 2); hold on;
	plot(2*T0:Ts:3*T0, A*([2*T0:Ts:3*T0]-2*T0)/T0, 'LineWidth', 2); hold on; 
	plot(3*T0:Ts:4*T0, A*([3*T0:Ts:4*T0]-3*T0)/T0, 'LineWidth', 2); hold on; 
	grid; axis([0 4*T0 -0.5 4.5]); hold off; xlabel('Time in sec'); ylabel('Amplitude');
	title('Periodic Pulse Signal (4 periods)');

	% Let's calculate the Fourier Series for different values of N:

	% Mean value:
	X0 = A/2;

	% Fundamental circular requency:
	w0 = 2*pi/T0;

	N = 5;
	while(1)
	    N = 2*N; 
	    k = [-N/2:1:-1 1:1:N/2];    % Attention: k must be nonzero! We must not
        	                        % include zero in our k vector!
	    Xk = -A./(j*2*pi*k); 
	    x = X0 + Xk * exp(j*(k'*w0)*t);
	    figure; plot(t, real(x), 'LineWidth', 2); 
	    grid; xlabel('Time'); ylabel('Amplitude');
	    title(['Fourier Series of a periodic signal for N = ', int2str(N)]);
	    if N == 160
        	break;
	    end
	end
end
