I am trying to write the below piece of matlab code to a python script. The objective of the code is to find a fit of annual harmonic to a time series and then remove it from the time series. It uses matlabs E\ts to find the coefficients of
harmonic fit, I used linlag.lstsq equivalent in python but feel the result is not correct. Can any one advice on this please.
#################PYTHON CODE ##################
import numpy as np
from numpy import
pi,c_,ones,size,cos,sin
t=np.arange(1,365.25*5)
dt=np.average(np.diff(t))
acyc = 2*pi/(365.25/dt)
365.25/4)/dt)
E1=c_[ones(size(t)),cos(acyc*t),sin(acyc*t)]
linalg.lstsq(E1,ts)
an_fit = E*a
###############ERROR##################
ts.shape
Out[243]: (1828, 1)
E1.shape
Out[244]: (1826, 3)
linalg.lstsq(E1,ss)
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
/home/sjo/work/PY_WORK/PY_TSER/<ipython-input-245-2eb3822d2564> in <module>()
----> 1 linalg.lstsq(E1,ss)
/usr/local/lib/python2.7/dist-packages/numpy-1.7.0-py2.7-linux-x86_64.egg/numpy/linalg/linalg.pyc in lstsq(a, b, rcond)
1801 ldb = max(n,
m)
1802 if m != b.shape[0]:
-> 1803 raise LinAlgError('Incompatible dimensions')
1804 t, result_t = _commonType(a, b)
1805 result_real_t = _realType(result_t)
LinAlgError: Incompatible dimensions
with best regards,
Sudheer