# Curve Fitting Toolbox Fourier Coefficient 9+?

2 views (last 30 days)
Isaias Trevino on 30 Jun 2022
Commented: William Rose on 13 Jul 2022
Hello, so there isn't a code in particular that I would like to share; however, I'm trying to use the fit function to create a fourier fit. For exploratory purposes I would like to see if there is a way to implement a fourier series with at least 9 coefficients. I don't know if there's any way that I'd be able to do this as the fit function only goes up to 'fourier8'. If I were to create a custom equation it wouldn't work and I don't know if this is because there isn't a normalized mean. Is there any way to normalize the mean and standard deviation so that I may have a higher order fourier series fit?

William Rose on 30 Jun 2022
@Isaias Trevino, The Fourier decomposition of a signal is orthogonal. That means that the best fit coefficients at the different frequencies are independent. That means that if you find the best fit for the first 9 coefficients, and then try again with 10 or 20 coefficients, your answer for the first 9 will not be affected, because the fit is independent at the different harmonics. This is not true if you fit a poynomial, because the value you get for the slope term of a straight line will usually change if you then add a squared term to your fitting function. So if you want to fit with nine Fourier terms, you can just compute Y=fft(x), and then ignore all terms above the ninth harmonic.
If you want to reconstruct the fitted signal with only the first 9 frequencies, then set the values in Y (the FFT array) to zero at the higher frequencies, and then do the inverse FFT. If you do this, make sure to keep the elements that correspond to the "negative" first 9 frequencies, and zero out the higher negative frequencies. If you don't do this, the result of the inverse FFT will almost certainly be complex, and will not be what you want.
I hope that helps. Good luck.
##### 2 CommentsShowHide 1 older comment
William Rose on 13 Jul 2022
I assume that x is the independent variable, which you have measured, and that w is frequency. Your formula suggests that frequency of each component is the same, since you do not have a subscript "i" on the w's. I assume that is an oversight, and that you intend for the frequency to be a multiple of i, since that is Fourier's formula. In other words, we should replace ω with , where , where is the fundamental frequency, in radians per unit of x. You have to decide, based on your knowledge of your data, what value to use for . If you have a signal that you recorded for duration T, then it is common to choose , so that one cycle of the fundamenal frequency is exactly as long as the signal you recorded.
Notice that a0 and c0 in the formula are redundant: they are both constants. Therefore you would get the exact same fit with a0=1,c0=0; and with a0=0,c0=1; and with a0=-100,c0=101; and so on. If you do not eliminate one of these terms, you will not be able to find a unique best fit, because there are an infinite number of combinations of a0 and c0 that give the exact same fit. Therefore you must eliminate one of them from the fitting function. Let's eliminate c0.
The same comment as above also applies to ai versus cj, and to bi versus dj. Notice that is identical to , expect for the label on the constant, a_i versus c_j. Therefore you must eliminate ai or cj, and you must eliminate bi or dj. Let's eliminate cj and dj.
Therore, the formula which I think you should use, to get a unique fit, is
I have used index k instead of i, to avoid confusion with .
How does this sound to you? I want to know your opinion, before moving to the next step, which is finding the a's and b's. Perhaps you would be willing to share some example data.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by