
Dear Experts, 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. ##########Matlab CODE################ function[an_fit,sam_fit,fit_both]=calcharm(t,ts,dt) subplot 211 plot(t,ts) legend('Orig Tser') xlabel('TIME') title('ORIGINAL TIMESERIES') figure(gcf) disp('Paused...');pause %% Prepare Annual signal fann = 2*pi/(365.25/dt); E = [ones(size(t)) cos(fann*t) sin(fann*t)]; a = E\ts; %dfit = a(1) + a(2)*cos(fann*t) + a(3)*sin(fann*t); % % or more easily: an_fit = E*a; #################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 with best regards, Sudheer *************************************************************** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.India@gmail.com;sudheer.joseph@yahoo.com Web- http://oppamthadathil.tripod.com ***************************************************************

I thought I should add the error I get too. From: Sudheer Joseph <sudheer.joseph@yahoo.com> To: "numpy-discussion@scipy.org" <numpy-discussion@scipy.org>
Sent: Saturday, 10 August 2013 8:40 PM Subject: [Numpy-discussion] solving matrix
Dear Experts, 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.
##########Matlab CODE################
function[an_fit,sam_fit,fit_both]=calcharm(t,ts,dt) subplot 211 plot(t,ts) legend('Orig Tser') xlabel('TIME') title('ORIGINAL TIMESERIES') figure(gcf) disp('Paused...');pause %% Prepare Annual signal fann = 2*pi/(365.25/dt); E = [ones(size(t)) cos(fann*t) sin(fann*t)]; a = E\ts; %dfit = a(1) + a(2)*cos(fann*t) + a(3)*sin(fann*t); % % or more easily: an_fit = E*a;
#################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

________________________________ From: Sudheer Joseph <sudheer.joseph@yahoo.com> To: Discussion of Numerical Python <numpy-discussion@scipy.org> Sent: Saturday, 10 August 2013 8:56 PM Subject: Re: [Numpy-discussion] solving matrix
I thought I should add the error I get too.
From: Sudheer Joseph <sudheer.joseph@yahoo.com>
To: "numpy-discussion@scipy.org" <numpy-discussion@scipy.org>
Sent: Saturday, 10 August 2013 8:40 PM Subject: [Numpy-discussion] solving matrix
Dear Experts, 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.
##########Matlab CODE################
function[an_fit,sam_fit,fit_both]=calcharm(t,ts,dt) subplot 211 plot(t,ts) legend('Orig Tser') xlabel('TIME') title('ORIGINAL TIMESERIES') figure(gcf) disp('Paused...');pause %% Prepare Annual signal fann = 2*pi/(365.25/dt); E = [ones(size(t)) cos(fann*t) sin(fann*t)]; a = E\ts; %dfit = a(1) + a(2)*cos(fann*t) + a(3)*sin(fann*t); % % or more easily: an_fit = E*a;
#################PYTHON CODE ################## import numpy as np from numpy import
Dear Admin, I found the the issue was a silly mistake of difference size of array and now would like to withdraw this post. Is there a way to delete this question. with best regards, Sudheer 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
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (1)
-
Sudheer Joseph