Fourier series data fit with fixed period?
6 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Francesco Tricarico
on 11 Oct 2020
Edited: Francesco Tricarico
on 11 Oct 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 Comment
Francesco Tricarico
on 13 Oct 2020
Edited: Francesco Tricarico
on 20 Oct 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
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!