question about numpy.polyval
sirvival
fpfeifer at hs.uni-hamburg.de
Mon Feb 28 10:34:32 EST 2011
Hi,
I have some simulated data of stellar absorption lines.
What I am trying to is the following:
I divide my data into chunks (each of the same size).
Then I let the code find the max y value in one of those chunks.
I got this working.
Then I put those value in a two column array (first column has the
position of the max value in the original data; second column the y
value at this position).
Then I use polyfit to fit the data.
At last I use polyval to get the fit.
The problem is now since I got about 70 chunks I am not sure how to
use polyfit to get the fit for the original data.
My simulated data is a one column array of 300000 data points. I am
only interested in a fit of values above 160000.
The data is data_mean.
My code:
import numpy as np
import matplotlib.pyplot as mpl
import scipy, pyfits
chunk = 2000
data_len = len(data_mean)
num_chunk = data_len/chunk
start_chunk = 160000
num_chunk = num_chunk - start_chunk/chunk # I defined num_chunk this
way so I can change the startvalue
data_chunk = np.zeros( (num_chunk, chunk))
data_mean_b = data_mean[start_chunk:len(data_mean)] # for fitting
purpose later in the code
for i in range(num_chunk):
data_chunk[i] = data_mean[start_chunk+i*2000:start_chunk
+2000+i*2000]
data_max = np.zeros(num_chunk)
for i in range(num_chunk):
data_max[i] = max(data_chunk[i]) # finding the max values inside a
chunk
data_max_pos = np.zeros(num_chunk) # the position of the max values
for i in range(num_chunk):
for position, item in enumerate(data_mean):
if item == data_max[i]:
data_max_pos[i] = position
data_fin = np.zeros((num_chunk,2))
for i in range(num_chunk): # final data two columns
data_fin[i,0] = data_max_pos[i]
data_fin[i,1] = data_max[i]
order = 2
x = np.arange(num_chunk)
y = data_fin[::,1]
coeff = np.polyfit(x, y, order)
fit = np.polyval(coeff,x)
xa = np.arange(len(data_mean_b))
fitb = np.zeros((num_chunk,2))
#end of code
Now fit does work fine but as len(num_chunk) = 70 it is no use for the
simulated data.
So I tried with xa and fitb.
But this just gives me somethin like this (plot of fita):
http://img40.imageshack.us/i/web01.png/
Plot of fit:
http://img196.imageshack.us/i/web02j.png/
Thanks
More information about the Python-list
mailing list