Filter löschen
Filter löschen

Fourier series data fit with fixed period?

7 Ansichten (letzte 30 Tage)
VR
VR am 6 Mär. 2019
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.

Antworten (1)

Francesco Tricarico
Francesco Tricarico am 11 Okt. 2020
Bearbeitet: Francesco Tricarico am 11 Okt. 2020
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 Kommentar
Francesco Tricarico
Francesco Tricarico am 13 Okt. 2020
Bearbeitet: Francesco Tricarico am 20 Okt. 2020
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by