Fourier series data fit with fixed period?
8 ビュー (過去 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.
0 件のコメント
回答 (1 件)
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
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 Center および File Exchange で Linear and Nonlinear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!