# Fitting data with Fourier Series coefficients

Suppose I have some data, given by $\boldsymbol{x}$ and $\boldsymbol{y}$ pairs, for instance$\{(\boldsymbol{x_1}, \boldsymbol{y_1}), (\boldsymbol{x_2},\boldsymbol{y_2}),...,(\boldsymbol{x_n},\boldsymbol{y_n})\}$ for $n\in\mathbb{N}$.

I assume that the data comes from some function $f(\boldsymbol{x})$ with some noise, I don't know the function, so we don't know whether it is periodic (very likely, it is NOT periodic).

This means $\boldsymbol{y_i} = f(\boldsymbol{x_i}) + \boldsymbol{\epsilon_i}$ where $\boldsymbol{\epsilon_i} \sim \textbf{N}(\textbf{0}, \boldsymbol{\sigma}^2))$.

Now I know that the Fourier Series is defined only for periodic functions. However (here) it is possible to "extend" a non-periodic function defined on an interval, to a periodic function.

The amount of data that I have is finite, so will lie in an interval.

So my question is:

Does it make sense (is it even possible) to try and fit a Fourier Series to the data, by considering the data (which is multi-dimensional, both x and y are vectors) in a multi-dimensional interval , by finding the coefficients that minimize some loss function? (for instance Mean Squared Error) How would someone define this fourier series for a multi-dimensional, non-periodic, noisy function?

(by using only a finite number of fourier terms!)

• This is a standard approach in linear regression and should work OK for non-periodic functions. In general, you can choose a variety of basis functions and periodic basis functions can fit an interval (although they are also an obvious choice to fit a periodic target function). – Neil Slater Nov 16 '17 at 10:15
• @NeilSlater Thank you! Do you know of any package for python? At the moment I am trying something similar to method1 given here: stackoverflow.com/questions/23561856/… – Euler_Salter Nov 16 '17 at 10:59
• Sorry no I do not know a suitable library. Hopefully someone can give you a proper answer, all I have done is try to calm your worries in the comment. Basically, you are not attempting something unusual or without precedent – Neil Slater Nov 16 '17 at 12:00
• I agree with @NeilSlater. Rather than finding a package specific to this problem, it's possible to use a neural network framework like Keras and build a network where the input is your X values and the output is not Y, but instead coefficients (i.e. weights of your sinusoids) of a linear combination sinusoids. You'd need to build a custom loss function to compare the target with the predicted weighted (and shifted) sinusoids, but that is totally possible in Keras or Tensorflow in general. – tom Nov 16 '17 at 18:15
• @tom: It's even easier than that. Your input can be flattened [cos(x), cos(2x), cos(3x)] (where actual multiples of x chosen so that you get a sensible spread of frequencies over your interval). Then perform linear regression with those inputs. No neural network required. – Neil Slater Nov 16 '17 at 18:23