# [Numpy-discussion] solving matrix

Sudheer Joseph sudheer.joseph@yahoo....
Sat Aug 10 10:10:50 CDT 2013

```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