1

Using the fit function, the maximum number of terms that can be made using a Fourier fit in MATLAB is 8:

f = fit(xs,ys,'fourier8')

Unfortunately, an 8-term series isn't going to cut it for my purposes. The goal is to fit a data set with a Fourier model and extract the coefficients, and the coefficients need to be as precise as possible for later analysis. I was wondering if anyone had an idea of how to modify the pre-existing fit function using:

fitoptions('fourier8')

that would create a n-term Fourier series, where n is the number of terms (finite). If this is unfeasible, how one would go about writing a program that creates an n-term Fourier series (finite) with extractable coefficients using the general shown formula in the image below NOTE: THE BOUND WOULD NOT BE INF AND WOULD INSTEAD BE n.enter image description here

Update: I found code that takes in known coefficients, but how could this be adjusted so that it actively adjusts/fine-tunes the coefficients for n-numbered terms when they are not known prior? Also, how could one find the upper and lower bounds for the period (as those are inputs for this script)?

 a0 = input('a0: ');
 an = input('an: ');
 bn = input('bn: ');
 a = input('lower boundary: ');
 b = input('upper boundary: ');
 t = linspace(a,b,10000); % note: this is just for testing the code
 suma =0;

 for n=1:100 
     ebn = evalin('caller',bn);
     ean = evalin('caller',an);
     suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
 end

 series = a0 + suma;
 plot(t,series)
  • Why not compute the coefficients using their [formula](https://en.wikipedia.org/wiki/Fourier_series#Definition) in the Fourier series? Truncating the series to a finite sum [gives](https://en.wikipedia.org/wiki/Fourier_series#Approximation_and_convergence_of_Fourier_series) the best approximation in LS sense – Luis Mendo Jun 07 '18 at 22:25
  • @Luis Mendo I apologize, my question was not concise enough and the formula that I included is misleading, the series should be finite in order to calculate the necessary coefficients. –  Jun 07 '18 at 22:53
  • Worth checking out [Real Fourier Series Matlab file exchange](https://www.mathworks.com/matlabcentral/fileexchange/31013-simple-real-fourier-series-approximation) – StaticBeagle Jun 07 '18 at 23:25

1 Answers1

0

I am pretty sure you're doing something similar to what I did in my research. The following code generates a Fourier matrix for n points and 2 x hmodes+1. Right now. You can tweak that. So solving the Ax=b gives the coefficients for this. If not, it is kind of handy. I'll just note this is pretty slow. There are faster FFT methods. I think if you look somewhere in my answer for my questions there is a python question that lists one. This is effectively the same research. I fixed it somewhere else.

function FCMat = FCMatGen(pts,hmodes)
% Takes the following inputs
% pts: the number of pts
% hmodes: the numbers of modes
% Returns the followinmn
% The matrix A: npts x hmodes for the Fourier Continuation
% problem
A = dftmtx(2*pts);
A = A(1:pts,:);
Ar = real(A(:,(1:(hmodes+1))));
Ai = imag(A(:,(2:(hmodes+1))));
A = [Ar,Ai];
FCMat = A;
end