【发布时间】:2015-03-24 22:25:23
【问题描述】:
如何将傅里叶级数曲线拟合到超过 8 个级数的部分?
我尝试了这个程序,但我得到的系数值不一样,而且拟合不好
%% Create a Fourier series
AnCosWnx='A0';
BnSinWnx='';
lower=1;
upper=1;
startpoint=0;
for n=1:9
LCos=AnCosWnx;
LSin=BnSinWnx;
ACosWx=strcat('A',num2str(n),'*cos(w*',num2str(n),'*x)');
AnCosWnx=strcat(LCos,'+',ACosWx);
BSinWx=strcat('B',num2str(n),'*sin(w*',num2str(n),'*x)');
BnSinWnx=strcat(LSin,'+',BSinWx);
Fx=strcat(AnCosWnx,BnSinWnx);
end
%% build the fit function
options = fitoptions(Fx);
lower=zeros(1,2+n*2);
upper=zeros(1,2+n*2);
startpoint=zeros(1,2+n*2);
lower(1:end)=-inf;
upper(1:end)=inf;
startpoint(end)=6.17*10^-3;
set(options,'Lower',lower,...
'Upper',upper...
,'StartPoint',startpoint...
,'Algorithm','Trust-Region');
%%
ft = fittype(Fx);
% Fit this model using new data
[fitobject ,gof,output] = fit(sat,dat,ft,options);
%% 创建适合字符串的函数
cvalues = coeffvalues(fitobject);
cnames = coeffnames(fitobject);
output = formula(fitobject);
for ii=1:1:numel(cvalues)
cname = cnames{ii};
cvalue = num2str(cvalues(ii));
output = strrep(output, cname , cvalue);
end
%% 将输出字符串转换为函数 foutput= strcat('@(x)',output);
Fout= regexprep(Foutput,'\r\n|\n|\r','')
g=str2func(Fout);
hold on
h_ = plot(sat,g(sat),'r')
【问题讨论】:
标签: matlab fft curve-fitting