Fourier series data fit with fixed period?

8 visualizaciones (últimos 30 días)
VR
VR el 6 de Mzo. de 2019
Editada: Francesco Tricarico el 20 de Oct. de 2020
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.

Respuestas (1)

Francesco Tricarico
Francesco Tricarico el 11 de Oct. de 2020
Editada: Francesco Tricarico el 11 de Oct. de 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 comentario
Francesco Tricarico
Francesco Tricarico el 13 de Oct. de 2020
Editada: Francesco Tricarico el 20 de Oct. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by