# Is there any way to do LFIT (general linear least-squares fit ) on the time series data using MATLAB?

1 view (last 30 days)
DIPENDRA007 on 27 Jul 2021
Commented: Star Strider on 28 Jul 2021
How to do the attched linear least-square fitting equation on time series data in matlab?
y = a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2)
a,b,c ... are parameters. t is time and y is dependent variable.

Star Strider on 27 Jul 2021
That depends entirely on what you want to do.
One approach:
syms a b c d e f g t
y = a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2);
yfcn = matlabFunction(y, 'Vars',{[a b c d e f g],t})
yfcn = function_handle with value:
@(in1,t)in1(:,1)+in1(:,2).*t+in1(:,5).*cos(t.*pi.*2.0)+in1(:,7).*cos(t.*pi.*4.0)+in1(:,4).*sin(t.*pi.*2.0)+in1(:,6).*sin(t.*pi.*4.0)+in1(:,3).*t.^2
This will work in the Optimization Toolbox and Statictics and Machine Learning Toolbox nonlinear parameter estimation funcitons. Specify the initial parameter estimates as a row vector.
tv = linspace(0, 10, 50); % Create Data
yv = exp(-(tv-5).^2) + randn(size(tv))/10; % Create Data
B0 = rand(1,7); % Choose Appropriate Initial Parameter Estimates
B = nlinfit(tv(:), yv(:), yfcn, B0)
B = 1×7
-0.1691 0.2401 -0.0259 -0.0227 -0.0012 0.0266 -0.0076
The Curve Fitting Toolbox does it differently:
yfit = fittype('a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2)', 'independent',{'t'});
yfit = fit(tv(:) ,yv(:), yfit,'start',B0)
yfit =
General model: yfit(t) = a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2) Coefficients (with 95% confidence bounds): a = -0.1691 (-0.4125, 0.07436) b = 0.2401 (0.1275, 0.3526) c = -0.02595 (-0.03684, -0.01506) d = -0.02275 (-0.1431, 0.09761) e = -0.001153 (-0.1193, 0.117) f = 0.02665 (-0.09346, 0.1468) g = -0.007593 (-0.1257, 0.1105)
Experiment to get the result you want.
.
Star Strider on 28 Jul 2021
I still do not understand what you want to do.
If you want to filter the data, use the lowpass (or bandpass, to eliminate the baseline offset and drift) functions. I have no idea what you want to do, however
[yr_filt,lpdf] = lowpass(yr, 4E-3, Fs, 'ImpulseResponse','iir');
will work for the lowpass filter, and
[yr_filt,bpdf] = bandpass(yr,[1E-5, 4E-3], Fs, 'ImpulseResponse','iir');
for the bandpass design (although it might be necessary to experiment with the lower passband edge).
.