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

64 views (last 30 days)
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?

Answers (2)

Steven Lord
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.

Walter Roberson
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.
  3 Comments
Walter Roberson
Walter Roberson on 15 Jul 2021
If your temperature values are equally spaced, then fft() your speed of sound data.
If your temperature values are not equally spaced, then https://www.mathworks.com/help/matlab/ref/double.nufft.html nufft() instead of fft()
Then pick out peaks.
... At least those should provide you with starting points for a nonlinear fitting. If your curve has significant power in frequencies beyond the 12 most significant peaks, then potentially the fitting values could end up fairly different. Once you had the guesses, you could use https://www.mathworks.com/help/optim/ug/lsqcurvefit.html lsqcurvefit() to refine the locations, using a model that synthesized a signal based upon the trial coefficients.
Walter Roberson
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.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!