I have a little bit of problem figuring out the relation between Fourier series and Least Squares.
As far as I understand, LS is a way of minimizing the quadratic error between a measured value $y_i$ and a linear combination of functions evaluated at points $x_i$. So, in order to solve for least squares we have to have the $(x_i,y_i)$ and the functions $f_j(\cdot)$ that are going to be linearly combined.
If I would like to approximate a set of points $(x_i,y_i)$ using Fourier basis functions, I could assemble a matrix as
$$ \begin{pmatrix} 1 & \cos(\omega x_1) & \sin(\omega x_1) \\ 1 & \cos(\omega x_2) & \sin(\omega x_2) \\ \vdots & \vdots & \vdots\\ 1 & \cos(\omega x_n) & \sin(\omega x_n) \\ \end{pmatrix} \begin{pmatrix} A_0 \\ A_1 \\ A_2 \\ \end{pmatrix} = \begin{pmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \\ \end{pmatrix}$$
To obtain the values of the coefficients $A_0,A_1,A_2$ using least-squares I can solve for $x = (A^TA)^{-1}A^Tb$
Now, my doubt arises since it seems to me that the coefficients $A_0,A_1,A_2$ computed with the least-squares formula can be found with the Fourier transform. Are the coefficients $A_0,A_1,A_2$ the same as the Fourier coefficients $C_k$ ?
EDIT
clc; close all;
N = 10;
x = 1:N;
%y = rand(1,N);
y = [0.7099 0.6745 0.7374 0.1144 0.7732 0.0021 0.0357 0.4804 0.8107 0.0410];
% Polynomial Fit
A = [(x.*x)' x' ones(N,1)];
c = A\y'
% Fourier Fit - K+1 Coefficients
A = [];
K = 8;
for k = 0:K
A = [A exp(-1j*2*pi*k*x/N)'];
end
c = A\y'
c = (fft(y)/length(y))'
% Test the approximation on a bigger range
x_nodes = linspace(0,10,1000);
f = c(1);
for i = 2:K
f = f + c(i)*exp(1j*2*pi*i*x_nodes/N);
end
If I try to use the complex form of the Fourier Series $ f = C_0 + \sum_k C_ke^{-j2\pi kx/L}$ and follow the least-squares approach and compare the results to the coefficients of the FFT, the coefficients seem to be different. What am I missing?


For real valued functions: $C_0=A_0$ and $C_k=(A_k-iB_k)/2$ for $k\ge 1$. Also, $C_{-k}=Conj(C_k)$. In the case of an even number of points the extra (highest wave number) coefficient is purely real and given by $C_{k_{\max}}=A_{k_{\max}}$.
– Doug Lipinski May 20 '14 at 16:30