% script: DFTtest.m
% description: perform some approximation experiments with complex
% Fourier polynomials on the function myfcn defined externally.
 
N = 3; % use odd value to get perfect match with real coefs
n = round(N/2) - 1
a = zeros(1,n+1); 
b = zeros(1,n);
c = zeros(1,N);
% construct the coefficients for myfcn 
xnodes = linspace(0,2*pi,N+1); 
xnodes = xnodes(1:N); % trim off 2*pi 
f = myfcn(xnodes); 
% dispose of 0th coefficients
a(n+1) = 2*sum(f)/N;
% now the rest of real Fourier coefs
for j = 1:n 
  a(j) = 2*f*cos(j*xnodes)'/N; 
  b(j) = 2*f*sin(j*xnodes)'/N;
end
% next construct the complex coefficients
for j = 1:N 
  c(j) = f*exp(-i*(j-1)*xnodes).'/N; 
end
% ok, let's compare
% a(n+1) is exceptional Fourier a_0
disp('|a0 - 2*c0| =')
disp(abs(a(n+1) - 2*c(1)))
% rest of the a(k) are Fourier a_k
ac = c(2:n+1)+c(N:-1:n+2);
disp('norm(a(1:n) - (c(2:n+1)+c(N:-1:n+2)),inf) =')
disp(norm(a(1:n) - ac,inf))
ac(n+1) = 2*c(1);
% the b(k) are Fourier b_k
bc = i*(c(2:n+1)-c(N:-1:n+2));
disp('norm(b(1:n) - i*(c(2:n+1)-c(N:-1:n+2)),inf) =')
disp(norm(b(1:n) - bc,inf))
