%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  fourier_series.m                                                       %
%  Euler Method Example                                                   %
%  Created by : Brenden  Epps,    bepps@mit.edu,  28 February 2007        %
%  2.23 - Hydrofoils & Propellers                                         %
%                                                                         %
%  All rights reserved                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear, close all, clc,
set(0,'DefaultFigureWindowStyle','docked') 
set(0,'defaultaxesfontsize',14);
linewidth = 1.5;

%%%%%%%%%%%%%      Fourier Series example         %%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Find the Fourier series representation for f(x) = x on x=[-pi,pi].
% (i.e. saw-tooth wave)
%
% f(x) = 1/2 a_0 + sum(a_n*cos(nx)+b_n*sin(nx))

%%% solve for a single value of delta_t %%%
X = -pi:0.01:pi;

figure
plot(X,X,'k','LineWidth',linewidth), 
xlabel('x','Fontsize',14), ylabel('f(x)','Fontsize',14), 
title('f(x)','Fontsize',16)
hold on;

a_0 = 0;

f = (1/2)*a_0;

for n = 1:20
    a(n) = 0;
    b(n) = 2*(-1)^(n+1)/n;
    f = f + a(n)*cos(n*X) + b(n)*sin(n*X);
    plot(X,f,'r')
    pause
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% t = 0:0.001:0.6;
% x = sin(2*pi*50*t)+sin(2*pi*120*t);
% y = x + 2*randn(size(t));
% plot(1000*t(1:50),y(1:50))
% title('Signal Corrupted with Zero-Mean Random Noise')
% xlabel('time (milliseconds)')
% 
% Y = fft(y,512);
% 
% Pyy = Y.* conj(Y) / 512;
% 
% f = 1000*(0:256)/512;
% plot(f,Pyy(1:257))
% title('Frequency content of y')
% xlabel('frequency (Hz)')