Fourier series data fit with fixed period?

7 次查看(过去 30 天)
I am trying to fit a data set of one year to Fourier series and I want to fix the period to be one year. So far, it seems that functions like 'fit' gives you w (i.e.,period) as output but you can fix it as an input. I read somewhere that it is possible to fix w if you choose your lower bound and upper bound to be the same. However, they did not specify how to do it with a MWE. Any recommendations would be helpful.

回答(1 个)

Francesco Tricarico
Francesco Tricarico 2020-10-11
编辑:Francesco Tricarico 2020-10-11
Probably VR solved it but for MWE would be useful in the future, so:
% Sinusoid to sample data.
omega = 1;
N_sp = 10; % Number of sampling points.
t = linspace(0,2*pi/omega,N_sp)';
y = sin(omega*t);
% Fit Options setting. Pay attention to the bound definition.
FitOpts = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[-Inf*ones(1,5), omega],'Upper',[Inf*ones(1,5), omega]);
% Fit results.
UnboundedFit = fit(t,y,'fourier2')
BoundedFit = fit(t,y,'fourier2',FitOpts)
In UnboundedFit, the fundamental angular frequency is choosen by fit (w = 0.5). In BoundedFit, the fundamental angular frequency is that specified by the user (w = 1).
  1 个评论
Francesco Tricarico
Francesco Tricarico 2020-10-13
编辑:Francesco Tricarico 2020-10-20
Trying to improve flexibility of the code i posted, let's go mat-friends!
% Declaring the type of fit.
FitType = 'fourier2';
% Creating and showing a table array to specify bounds.
CoeffNames = coeffnames(fittype(FitType));
CoeffBounds = array2table([-Inf(1,length(CoeffNames));...
Inf(1,length(CoeffNames))],'RowNames',...
["lower bound", "upper bound"],'VariableNames',CoeffNames);
% Sinusoid to sample data.
omega = 1;
N_samplinpts = 10;
t = linspace(0,2*pi/omega,N_samplinpts)';
y = sin(omega*t);
% Specifying bounds according to the position shown by the table.
CoeffBounds(:,6) = [{omega}; {omega}]
% Fit Options setting.
FitOpts = fitoptions('Method','NonlinearLeastSquares','Lower',...
table2array(CoeffBounds(1,:)),'Upper',table2array(CoeffBounds(2,:)));
% Fit results.
UnboundedFit = fit(t,y,FitType)
BoundedFit = fit(t,y,FitType,FitOpts)
Francesco

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by