# Is it possible to fit with a Fourier series with more than 8 terms?

28 views (last 30 days)
Liam Morris on 15 Jul 2021
Commented: Walter Roberson on 15 Jul 2021
I am trying to build a Fourier series fitting with around 12 terms. I am using the 'fit' function and used 'Fourier8' as it won't allow me to go any higher.
I have something similar when I try to fit a polynomial of more than 9 degrees using the 'fit' function. However, this can be overcome by using the 'polyfit' function and you can use much higher polynomial degrees.
Is there an analogous equivalent for Fourier fitting to overcome the limit of 8 terms?

Walter Roberson on 15 Jul 2021
fft() and find the locations in the first half of the output that have the 12 largest absolute values. Extract the magnitude and phase at those locations.
Walter Roberson on 15 Jul 2021
Oh, interesting. @fittype/private/liblookup.m function iLookupFourier says
% Fourier terms
% Y = A0 + A(1)*cos(x*w) + B(1)*sin(x*w)+...
% ...
% + A(n)*cos(n*x*w) + B(n)*sin(n*x*w)
% where w is the nonlinear parameter to be fitted. Other
% parameters will be fitted linearly using backslash.
This implies that you can construct a fitting function with a single parameter, w, which is acting like a fundamental frequency, and that given that w you can construct the sin and cos terms, after which you can construct the vandermode matrix and use the \ operator to extract the A and B terms, which you then use to synthesize the signal and then calculate error relative to the original signal.
This is distinctly different than what I had been leading to; my suggestion with fft() was assuming that the purpose was to find the 12 peaks that best explained the signal, with the peaks being at independent frequencies... that would probably have lead to 12 frequencies, and 12 magnitudes and 12 phases. The approach they take instead would be to take a fundamental frequency and the first 11 harmonics of it, rather than scattered frequencies.

Steven Lord on 15 Jul 2021
Curve Fitting Toolbox includes standard library models that contain between 1 and 8 Fourier terms. You could define a custom fit type (perhaps using the "Create Custom Linear Model" example on the fittype documentation page as a model) though you would not be able to use the heuristics for the standard library models to generate starting points.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!