N = 8
y = [0 -1 -1 -1 1 1 0 1]

% Compute the matrices of trigonometric functions

p=1:N/2+1;

n=1:N;

C=cos(2*pi*n'*(p-1)/N);

S=sin(2*pi*n'*(p-1)/N);

 

% Compute Fourier Coefficients

A=2/N*y*C;

B=2/N*y*S

A(N/2+1)=A(N/2+1)/2;

 

% Reconstruct Signal - pmax is number of frequencies used in increasing order

pmax=13;

ynew=A(1)/2+C(:,2:pmax)*A(2:pmax)'+S(:,2:pmax)*B(2:pmax)';

 

% Plot Data

plot(y,'o')

 

% Plot reconstruction over data

hold on

plot(ynew,'r')

hold off   
