在MATLAB中编写n编号项傅里叶级数的函数?

时间:2018-06-07 22:09:55

标签: matlab fft curve-fitting

使用fit函数,在MATLAB中使用傅里叶拟合可以产生的最大项数为8:

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

不幸的是,一个8学期的系列不会为了我的目的而削减它。目标是使用傅里叶模型拟合数据集并提取系数,并且系数需要尽可能精确以便以后分析。我想知道是否有人知道如何使用以下方法修改预先存在的拟合函数:

fitoptions('fourier8')

将创建一个n项傅里叶级数,其中n是项数(有限)。如果这是不可行的,那么如何编写一个程序来创建一个带有可提取系数的n项傅里叶级数(有限),使用下面图像中的一般显示公式注意:结果不会是有效的,并且可能是不可能的是enter image description here

更新:我发现代码接收已知系数,但是如何调整它以便它在以前不知道的情况下主动调整/微调n个项的系数?另外,如何找到期间的上限和下限(因为这些是此脚本的输入)?

 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)

1 个答案:

答案 0 :(得分:1)

我很确定你做的事与我在研究中所做的相似。以下代码为n个点和2个hmodes + 1生成傅立叶矩阵。马上。你可以调整一下。因此,求解Ax = b给出了系数。如果没有,它有点方便。我只是注意到这很慢。有更快的FFT方法。我想如果你在我的问题的答案中看到某个地方有一个python问题列出one.这实际上是相同的研究。我把它固定在其他地方。

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