smooth bezier equivalent plotting in matplotlib - python-3.x

I need a help to plot a spectra with plt.steps function in matplotlib. As the data is noisy, in gnuplot I used smooth bezier option to smoothen. Is there any similar option in matplotlib? spline is not able to smoothen the data in desired level. Here is a sample data set https://drive.google.com/open?id=0B4shfFfM7MOqV2h3ZDA0RXlOa2M
I have plotted by plt.steps(data[:,0],data[:,1]) with log x value.
Plot is attached.matplotlib image
. how to smoothen the entire data with matplotlib.(I am quite new to python plotting)

A trivial low-pass filter seems effective. Tune alpha as desired.
#! /usr/bin/env python3
import matplotlib.pyplot as plt
def smooth(y, alpha=0.1):
ret = []
sm = y[0] # smoothed value, a moving average
for val in y:
ret.append(sm)
sm = alpha * val + (1 - alpha) * sm
return ret
def plot(points):
x = [a for a, b in points]
y = [b for a, b in points]
plt.subplot(211)
plt.semilogx(x, y)
plt.subplot(212)
plt.semilogx(x, smooth(y))
plt.savefig('/tmp/smoothed.png')
if __name__ == '__main__':
plot([
(0.25, 0.000),
(0.35, 0.055),
(0.45, 0.103),
(0.55, 0.104),
(0.65, 0.143),
(0.75, 0.140),
(0.85, 0.143),
(0.95, 0.143),
(1.05, 0.126),
(1.15, 0.223),
(1.25, 0.217),
(1.35, 0.232),
(1.45, 0.225),
(1.55, 0.219),
(1.65, 0.223),
(1.75, 0.236),
(1.85, 0.216),
(1.95, 0.222),
(2.05, 0.244),
(2.15, 0.244),
(2.25, 0.257),
(2.35, 0.221),
(2.45, 0.221),
(2.55, 0.213),
(2.65, 0.238),
(2.75, 0.209),
(2.85, 0.223),
(2.95, 0.226),
(3.05, 0.212),
(3.15, 0.247),
(3.25, 0.247),
(3.35, 0.236),
(3.45, 0.215),
(3.55, 0.218),
(3.65, 0.241),
(3.75, 0.209),
(3.85, 0.239),
(3.95, 0.221),
(4.05, 0.169),
(4.15, 0.246),
(4.25, 0.230),
(4.35, 0.229),
(4.45, 0.242),
(4.55, 0.264),
(4.65, 0.188),
(4.75, 0.182),
(4.85, 0.248),
(4.95, 0.172),
(5.05, 0.189),
(5.15, 0.228),
(5.25, 0.183),
(5.35, 0.272),
(5.45, 0.201),
(5.55, 0.204),
(5.65, 0.203),
(5.75, 0.198),
(5.85, 0.187),
(5.95, 0.244),
(6.05, 0.229),
(6.15, 0.202),
(6.25, 0.234),
(6.35, 0.231),
(6.45, 0.173),
(6.55, 0.206),
(6.65, 0.173),
(6.75, 0.178),
(6.85, 0.183),
(6.95, 0.188),
(7.05, 0.181),
(7.15, 0.153),
(7.25, 0.150),
(7.35, 0.183),
(7.45, 0.188),
(7.55, 0.111),
(7.65, 0.145),
(7.75, 0.195),
(7.85, 0.192),
(7.95, 0.156),
(8.05, 0.126),
(8.15, 0.095),
(8.25, 0.151),
(8.35, 0.127),
(8.45, 0.130),
(8.55, 0.095),
(8.65, 0.127),
(8.75, 0.219),
(8.85, 0.122),
(8.95, 0.094),
(9.05, 0.128),
(9.15, 0.054),
(9.25, 0.122),
(9.35, 0.080),
(9.45, 0.139),
(9.55, 0.107),
(9.65, 0.097),
(9.75, 0.087),
(9.85, 0.050),
(9.95, 0.090),
(10.05, 0.053),
(10.15, 0.121),
(10.25, 0.055),
(10.35, 0.056),
(10.45, 0.014),
(10.55, 0.087),
(10.65, 0.044),
(10.75, 0.150),
(10.85, 0.077),
(10.95, 0.140),
(11.05, 0.064),
(11.15, 0.065),
(11.25, 0.132),
(11.35, 0.050),
(11.45, 0.068),
(11.55, 0.017),
(11.65, 0.000),
(11.75, 0.072),
(11.85, 0.110),
(11.95, 0.056),
(12.05, 0.057),
(12.15, 0.115),
(12.25, 0.098),
(12.35, 0.060),
(12.45, 0.101),
(12.55, 0.041),
(12.65, 0.062),
(12.75, 0.063),
(12.85, 0.064),
(12.95, 0.065),
(13.05, 0.066),
(13.15, 0.157),
(13.25, 0.023),
(13.35, 0.093),
(13.45, 0.094),
(13.55, 0.072),
(13.65, 0.048),
(13.75, 0.098),
(13.85, 0.125),
(13.95, 0.101),
(14.05, 0.051),
(14.15, 0.104),
(14.25, 0.053),
(14.35, 0.054),
(14.45, 0.054),
(14.55, 0.083),
(14.65, 0.112),
(14.75, 0.113),
(14.85, 0.115),
(14.95, 0.087),
(15.05, 0.029),
(15.15, 0.000),
(15.25, 0.091),
(15.35, 0.031),
(15.45, 0.124),
(15.55, 0.031),
(15.65, 0.032),
(15.75, 0.065),
(15.85, 0.033),
(15.95, 0.033),
(16.05, 0.000),
(16.15, 0.068),
(16.25, 0.000),
(16.35, 0.070),
(16.45, 0.141),
(16.55, 0.143),
(16.65, 0.072),
(16.75, 0.073),
(16.85, 0.000),
(16.95, 0.037),
(17.05, 0.113),
(17.15, 0.077),
(17.25, 0.039),
(17.35, 0.078),
(17.45, 0.079),
(17.55, 0.040),
(17.65, 0.041),
(17.75, 0.082),
(17.85, 0.041),
(17.95, 0.042),
(18.05, 0.042),
(18.15, 0.043),
(18.25, 0.043),
(18.35, 0.000),
(18.45, 0.133),
(18.55, 0.134),
(18.65, 0.045),
(18.75, 0.091),
(18.85, 0.046),
(18.95, 0.093),
(19.05, 0.236),
(19.15, 0.048),
(19.25, 0.145),
(19.35, 0.049),
(19.45, 0.000),
(19.55, 0.050),
(19.65, 0.000),
(19.75, 0.101),
(19.85, 0.205),
(19.95, 0.155),
(20.05, 0.052),
(20.250, 0.034),
(21.750, 0.029),
(23.250, 0.036),
(24.750, 0.033),
(26.250, 0.048),
(27.750, 0.051),
(29.250, 0.033),
(30.750, 0.033),
(32.250, 0.089),
(33.750, 0.084),
(35.250, 0.068),
(36.750, 0.089),
(38.250, 0.017),
(39.750, 0.049),
(41.250, 0.093),
(42.750, 0.043),
(44.250, 0.069),
(45.750, 0.049),
(47.250, 0.096),
(48.750, 0.065),
(50.250, 0.098),
(51.750, 0.042),
(53.250, 0.088),
(54.750, 0.105),
(56.250, 0.074),
(57.750, 0.065),
(59.250, 0.096),
(60.750, 0.129),
(62.250, 0.075),
(63.750, 0.142),
(65.250, 0.116),
(66.750, 0.035),
(68.250, 0.091),
(69.750, 0.170),
(71.250, 0.119),
(72.750, 0.082),
(74.250, 0.086),
(75.750, 0.223),
(77.250, 0.163),
(78.750, 0.097),
(80.250, 0.175),
(81.750, 0.182),
(83.250, 0.108),
(84.750, 0.196),
(86.250, 0.145),
(87.750, 0.090),
(89.250, 0.372),
(90.750, 0.224),
(92.250, 0.132),
(93.750, 0.171),
(95.250, 0.141),
(96.750, 0.146),
(98.250, 0.225),
(99.750, 0.503),
(101.250, 0.199),
(102.750, 0.123),
(104.250, 0.169),
(105.750, 0.174),
(107.250, 0.224),
(108.750, 0.368),
(110.250, 0.284),
(111.750, 0.243),
(113.250, 0.150),
(114.750, 0.256),
(116.250, 0.263),
(117.750, 0.540),
(119.250, 0.332),
(120.750, 0.567),
(122.250, 0.407),
(123.750, 0.477),
(125.250, 0.122),
(126.750, 0.313),
(128.250, 0.384),
(129.750, 0.328),
(131.250, 0.335),
(132.750, 0.411),
(134.250, 0.351),
(135.750, 0.574),
(137.250, 0.367),
(138.750, 0.150),
(140.250, 0.536),
(141.750, 0.391),
(143.250, 0.479),
(144.750, 0.489),
(146.250, 0.333),
(147.750, 0.510),
(149.250, 0.520),
(150.750, 0.796),
(152.250, 0.090),
(153.750, 0.092),
(155.250, 0.563),
(156.750, 0.478),
(158.250, 0.487),
(159.750, 0.298),
(161.250, 0.405),
(162.750, 0.206),
(164.250, 0.735),
(165.750, 0.428),
(167.250, 0.653),
(168.750, 0.332),
(170.250, 0.113),
(171.750, 0.459),
(173.250, 0.117),
])

Related

Surface triangulation and interpolation in python 3

I have 3 lists of equal length of x, y and z coordinates.
With them, I need to triangulate a surface, and retrieve values that lie in a line over that surface. In other words, I need the values that lie on that surface that intersect a given plane.
Problem is, I have no idea where to start.
I have tried scipy interp2d, but it seems I need more z values them what I actually have (like shown in this answer: Python interpolation and extracting value of z for x and y?.
# this is the data I have
x = [0.0, 17.67599999997765, 49.08499999996275, 90.57299999985844, 136.60500000044703]
y = [0.0, 45.22349889159747, 66.50303846438841, 114.04427618243405, 187.7707039612985]
z = [0.0, 1.8700000000000045, 1.9539999999999509, 1.3929999999999154, 1.6299999999999955]
I need a final grid with x y z values that look something like this:
I don't really need too much resolution
My desired final result is to be able to retrieve specific values on top of that surface
Like the point line in this image:
I have also tried looking at geospatial libraries, but I couldn't find a solution either.
Maybe it's possible to interpolate the z values that I need? But I'm not really sure how to do this. I have never used scipy library before, and I'm still struggling to understand it.
I'm using python 3.9
You barely have any data, so if you don't choose your intersecting plane carefully, you'll get no results back (or nonsense back). This includes the case of x=y; you can't do that at all - so the graph you've shown is entirely inapplicable to your data.
import numpy as np
import scipy.interpolate
x = [0.0, 17.6759999999776500, 49.0849999999627500, 90.5729999998584400, 136.6050000004470300]
y = [0.0, 45.2234988915974700, 66.5030384643884100, 114.0442761824340500, 187.7707039612985000]
z = [0.0, 1.8700000000000045, 1.9539999999999509, 1.3929999999999154, 1.6299999999999955]
xyi = np.empty((200, 2))
xyi[:, 0] = np.arange(200)
xyi[:, 1] = xyi[:, 0] * 1.374
zi = scipy.interpolate.griddata(
points=(x, y), values=z,
xi=xyi,
method='cubic',
)
good_vals = ~np.isnan(zi)
xyz = np.empty((np.count_nonzero(good_vals), 3))
xyz[:, :2] = xyi[good_vals, :]
xyz[:, 2] = zi[good_vals]
print(xyz)
[[0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.00000000e+00 1.37400000e+00 4.68988354e-02]
[2.00000000e+00 2.74800000e+00 9.44855957e-02]
[3.00000000e+00 4.12200000e+00 1.42698116e-01]
[4.00000000e+00 5.49600000e+00 1.91474231e-01]
[5.00000000e+00 6.87000000e+00 2.40751776e-01]
[6.00000000e+00 8.24400000e+00 2.90468585e-01]
[7.00000000e+00 9.61800000e+00 3.40562494e-01]
[8.00000000e+00 1.09920000e+01 3.90971337e-01]
[9.00000000e+00 1.23660000e+01 4.41632950e-01]
[1.00000000e+01 1.37400000e+01 4.92485168e-01]
[1.10000000e+01 1.51140000e+01 5.43465824e-01]
[1.20000000e+01 1.64880000e+01 5.94512755e-01]
[1.30000000e+01 1.78620000e+01 6.45563795e-01]
[1.40000000e+01 1.92360000e+01 6.96556779e-01]
[1.50000000e+01 2.06100000e+01 7.47429542e-01]
[1.60000000e+01 2.19840000e+01 7.98119919e-01]
[1.70000000e+01 2.33580000e+01 8.48565744e-01]
[1.80000000e+01 2.47320000e+01 8.98704854e-01]
[1.90000000e+01 2.61060000e+01 9.48475082e-01]
[2.00000000e+01 2.74800000e+01 9.97814263e-01]
[2.10000000e+01 2.88540000e+01 1.04666023e+00]
[2.20000000e+01 3.02280000e+01 1.09495083e+00]
[2.30000000e+01 3.16020000e+01 1.14262388e+00]
[2.40000000e+01 3.29760000e+01 1.18961722e+00]
[2.50000000e+01 3.43500000e+01 1.23586870e+00]
[2.60000000e+01 3.57240000e+01 1.28131613e+00]
[2.70000000e+01 3.70980000e+01 1.32589737e+00]
[2.80000000e+01 3.84720000e+01 1.36955024e+00]
[2.90000000e+01 3.98460000e+01 1.41221257e+00]
[3.00000000e+01 4.12200000e+01 1.45382221e+00]
[3.10000000e+01 4.25940000e+01 1.49431699e+00]
[3.20000000e+01 4.39680000e+01 1.53363474e+00]
[3.30000000e+01 4.53420000e+01 1.57171329e+00]
[3.40000000e+01 4.67160000e+01 1.60849049e+00]
[3.50000000e+01 4.80900000e+01 1.64390417e+00]
[3.60000000e+01 4.94640000e+01 1.67789216e+00]
[3.70000000e+01 5.08380000e+01 1.71039230e+00]
[3.80000000e+01 5.22120000e+01 1.74134241e+00]
[3.90000000e+01 5.35860000e+01 1.77068035e+00]
[4.00000000e+01 5.49600000e+01 1.79834394e+00]
[4.10000000e+01 5.63340000e+01 1.82427101e+00]
[4.20000000e+01 5.77080000e+01 1.84839941e+00]
[4.30000000e+01 5.90820000e+01 1.87066696e+00]
[4.40000000e+01 6.04560000e+01 1.89101151e+00]
[4.50000000e+01 6.18300000e+01 1.90937088e+00]
[4.60000000e+01 6.32040000e+01 1.92570604e+00]
[4.70000000e+01 6.45780000e+01 1.94056076e+00]
[4.80000000e+01 6.59520000e+01 1.95421968e+00]
[4.90000000e+01 6.73260000e+01 1.96719695e+00]
[5.00000000e+01 6.87000000e+01 1.97928362e+00]
[5.10000000e+01 7.00740000e+01 1.98942207e+00]
[5.20000000e+01 7.14480000e+01 1.99700320e+00]
[5.30000000e+01 7.28220000e+01 2.00180807e+00]
[5.40000000e+01 7.41960000e+01 2.00361776e+00]
[5.50000000e+01 7.55700000e+01 2.00221333e+00]
[5.60000000e+01 7.69440000e+01 1.99737586e+00]
[5.70000000e+01 7.83180000e+01 1.98888640e+00]
[5.80000000e+01 7.96920000e+01 1.97652604e+00]
[5.90000000e+01 8.10660000e+01 1.96007583e+00]
[6.00000000e+01 8.24400000e+01 1.93931685e+00]
[6.10000000e+01 8.38140000e+01 1.91403017e+00]
[6.20000000e+01 8.51880000e+01 1.88497097e+00]
[6.30000000e+01 8.65620000e+01 1.85740456e+00]
[6.40000000e+01 8.79360000e+01 1.83466390e+00]
[6.50000000e+01 8.93100000e+01 1.81970145e+00]
[6.60000000e+01 9.06840000e+01 1.81546971e+00]
[6.70000000e+01 9.20580000e+01 1.82177337e+00]
[6.80000000e+01 9.34320000e+01 1.82842946e+00]
[6.90000000e+01 9.48060000e+01 1.83439551e+00]
[7.00000000e+01 9.61800000e+01 1.83969621e+00]
[7.10000000e+01 9.75540000e+01 1.84435625e+00]
[7.20000000e+01 9.89280000e+01 1.84840034e+00]
[7.30000000e+01 1.00302000e+02 1.85185316e+00]
[7.40000000e+01 1.01676000e+02 1.85473940e+00]
[7.50000000e+01 1.03050000e+02 1.85708377e+00]
[7.60000000e+01 1.04424000e+02 1.85891096e+00]
[7.70000000e+01 1.05798000e+02 1.86024566e+00]
[7.80000000e+01 1.07172000e+02 1.86111256e+00]
[7.90000000e+01 1.08546000e+02 1.86153636e+00]
[8.00000000e+01 1.09920000e+02 1.86154176e+00]
[8.10000000e+01 1.11294000e+02 1.86115344e+00]
[8.20000000e+01 1.12668000e+02 1.86039610e+00]
[8.30000000e+01 1.14042000e+02 1.85929444e+00]
[8.40000000e+01 1.15416000e+02 1.85787402e+00]
[8.50000000e+01 1.16790000e+02 1.85624017e+00]
[8.60000000e+01 1.18164000e+02 1.85445481e+00]
[8.70000000e+01 1.19538000e+02 1.85252055e+00]
[8.80000000e+01 1.20912000e+02 1.85043999e+00]
[8.90000000e+01 1.22286000e+02 1.84821574e+00]
[9.00000000e+01 1.23660000e+02 1.84585039e+00]
[9.10000000e+01 1.25034000e+02 1.84334656e+00]
[9.20000000e+01 1.26408000e+02 1.84070685e+00]
[9.30000000e+01 1.27782000e+02 1.83793385e+00]
[9.40000000e+01 1.29156000e+02 1.83503019e+00]
[9.50000000e+01 1.30530000e+02 1.83199845e+00]
[9.60000000e+01 1.31904000e+02 1.82884125e+00]
[9.70000000e+01 1.33278000e+02 1.82556119e+00]
[9.80000000e+01 1.34652000e+02 1.82216087e+00]
[9.90000000e+01 1.36026000e+02 1.81864290e+00]
[1.00000000e+02 1.37400000e+02 1.81500988e+00]
[1.01000000e+02 1.38774000e+02 1.81126441e+00]
[1.02000000e+02 1.40148000e+02 1.80740911e+00]
[1.03000000e+02 1.41522000e+02 1.80344657e+00]
[1.04000000e+02 1.42896000e+02 1.79937940e+00]
[1.05000000e+02 1.44270000e+02 1.79521020e+00]
[1.06000000e+02 1.45644000e+02 1.79094157e+00]
[1.07000000e+02 1.47018000e+02 1.78657613e+00]
[1.08000000e+02 1.48392000e+02 1.78211648e+00]
[1.09000000e+02 1.49766000e+02 1.77756521e+00]
[1.10000000e+02 1.51140000e+02 1.77292494e+00]
[1.11000000e+02 1.52514000e+02 1.76819827e+00]
[1.12000000e+02 1.53888000e+02 1.76338780e+00]
[1.13000000e+02 1.55262000e+02 1.75849613e+00]
[1.14000000e+02 1.56636000e+02 1.75352588e+00]
[1.15000000e+02 1.58010000e+02 1.74847964e+00]
[1.16000000e+02 1.59384000e+02 1.74336002e+00]
[1.17000000e+02 1.60758000e+02 1.73816962e+00]
[1.18000000e+02 1.62132000e+02 1.73291105e+00]
[1.19000000e+02 1.63506000e+02 1.72758692e+00]
[1.20000000e+02 1.64880000e+02 1.72219982e+00]
[1.21000000e+02 1.66254000e+02 1.71675236e+00]
[1.22000000e+02 1.67628000e+02 1.71124714e+00]
[1.23000000e+02 1.69002000e+02 1.70568677e+00]
[1.24000000e+02 1.70376000e+02 1.70007386e+00]
[1.25000000e+02 1.71750000e+02 1.69441100e+00]
[1.26000000e+02 1.73124000e+02 1.68870081e+00]
[1.27000000e+02 1.74498000e+02 1.68294588e+00]
[1.28000000e+02 1.75872000e+02 1.67714882e+00]
[1.29000000e+02 1.77246000e+02 1.67131224e+00]
[1.30000000e+02 1.78620000e+02 1.66543873e+00]
[1.31000000e+02 1.79994000e+02 1.65953091e+00]
[1.32000000e+02 1.81368000e+02 1.65359138e+00]
[1.33000000e+02 1.82742000e+02 1.64762273e+00]
[1.34000000e+02 1.84116000e+02 1.64162758e+00]
[1.35000000e+02 1.85490000e+02 1.63560853e+00]
[1.36000000e+02 1.86864000e+02 1.62956819e+00]]

How to efficiently use "LMFIT" in python curve fitting to a damped cosine wave

I've the dataset(x & y) that I want to fit as a damped cosine like (1-A+Acos(Kx))exp(-B*x) by using LMFIT ("non-linear least squares method") as the link (https://lmfit.github.io/lmfit-py/intro.html). To do this, I've tried with the following piece of code, but can't fit properly. What is going wrong with my code? Any help or suggestions will be appreciated.
x = [0, 1.3, 1.7, 1.72, 1.84, 1.98, 2.02, 2.16, 2.2, 2.2, 2.3, 2.38, 2.5, 2.55, 2.75, 2.8, 2.82, 2.84, 2.9, 2.92, 3.1, 3.13, 3.18, 3.19, 3.22, 3.3, 3.38, 3.44, 3.49, 3.62, 3.64, 3.72, 3.72, 3.75, 3.8, 3.82, 3.86, 3.92, 4.0, 4.07, 4.1, 4.1, 4.13, 4.14, 4.14, 4.17, 4.21, 4.24, 4.24, 4.24, 4.28, 4.3, 4.38, 4.49, 4.62, 4.62, 4.67, 4.72, 4.73, 4.74, 4.76, 4.76, 4.81, 4.81, 4.88, 4.89, 4.9, 4.9, 4.94, 4.96, 5.03, 5.05, 5.06, 5.1, 5.1, 5.15, 5.16, 5.16, 5.19, 5.22, 5.22, 5.3, 5.37, 5.41, 5.46, 5.56, 5.63, 5.65, 5.65, 5.73, 5.76, 5.81, 5.86, 5.91, 5.98, 6.03, 6.05, 6.05, 6.06, 6.11, 6.14, 6.22, 6.25, 6.27, 6.27, 6.3, 6.3, 6.31, 6.36, 6.42, 6.42, 6.47, 6.48, 6.5, 6.51, 6.58, 6.59, 6.62, 6.65, 6.66, 6.67, 6.69, 6.72, 6.77, 6.8, 6.84, 6.87, 6.91, 6.94, 6.94, 6.94, 7.05, 7.14, 7.17, 7.22, 7.23, 7.24, 7.32, 7.32, 7.35, 7.38, 7.4, 7.41, 7.42, 7.44, 7.45, 7.49, 7.5, 7.52, 7.54, 7.6, 7.72, 7.75, 7.81, 7.9, 7.92, 7.95, 7.97, 7.98, 7.99, 8.02, 8.03, 8.03, 8.05, 8.06, 8.07, 8.1, 8.12, 8.14, 8.19, 8.2, 8.21, 8.24, 8.25, 8.28, 8.28, 8.29, 8.32, 8.38, 8.38, 8.43, 8.49, 8.52, 8.54, 8.54, 8.57, 8.7, 8.75, 8.75, 8.78, 8.79, 8.88, 8.88, 8.93, 8.95, 9.0, 9.01, 9.02, 9.03, 9.06, 9.07, 9.11, 9.14, 9.16, 9.17, 9.18, 9.19, 9.2, 9.3, 9.33, 9.44, 9.46, 9.59, 9.62, 9.62, 9.64, 9.66, 9.71, 9.73, 9.73, 9.75, 9.76, 9.76, 9.79, 9.88, 9.9, 9.93, 9.93, 9.95, 9.99, 10.01, 10.03, 10.04, 10.05, 10.07, 10.11, 10.13, 10.18, 10.22, 10.22, 10.31, 10.37, 10.38, 10.41, 10.42, 10.44, 10.5, 10.52, 10.55, 10.56, 10.56, 10.58, 10.6, 10.66, 10.68, 10.68, 10.69, 10.7, 10.73, 10.75, 10.81, 10.93, 10.96, 10.98, 10.98, 11.02, 11.04, 11.1, 11.14, 11.15, 11.15, 11.17, 11.19, 11.21, 11.23, 11.24, 11.28, 11.3, 11.31, 11.32, 11.33, 11.4, 11.42, 11.48, 11.5, 11.51, 11.6, 11.62, 11.62, 11.63, 11.65, 11.72, 11.74, 11.74, 11.94, 11.95, 11.98, 12.02, 12.02, 12.03, 12.04, 12.09, 12.11, 12.17, 12.2, 12.23, 12.26, 12.3, 12.31, 12.33, 12.33, 12.37, 12.38, 12.61, 12.63, 12.69, 12.7, 12.74, 12.79, 12.8, 12.84, 12.87, 12.9, 12.91, 12.92, 12.94, 13.0, 13.19, 13.2, 13.26, 13.29, 13.3, 13.31, 13.31, 13.34, 13.35, 13.36, 13.44, 13.48, 13.52, 13.59, 13.78, 13.83, 13.88, 13.98, 14.02, 14.05, 14.07, 14.1, 14.14, 14.19, 14.25, 14.33, 14.36, 14.38, 14.41, 14.46, 14.47, 14.53, 14.54, 14.57, 14.69, 14.72, 14.77, 14.78, 14.78, 14.8, 14.82, 14.82, 14.91, 14.92, 14.96, 14.96, 15.05, 15.09, 15.17, 15.2, 15.21, 15.25, 15.26, 15.31, 15.32, 15.36, 15.36, 15.4, 15.4, 15.4, 15.41, 15.47, 15.52, 15.6, 15.61, 15.61, 15.63, 15.65, 15.71, 15.77, 15.8, 15.84, 15.86, 15.88, 15.94, 15.94, 15.97, 15.98, 16.02, 16.03, 16.27, 16.43, 16.56, 16.64, 16.64, 16.64, 16.68, 16.88, 16.91, 16.92, 16.93, 16.97, 16.99, 17.0, 17.01, 17.02, 17.05, 17.13, 17.21, 17.32, 17.45, 17.59, 17.79, 17.8, 17.81, 17.87, 17.9, 17.92, 17.93, 17.93, 17.97, 17.98, 18.02, 18.05, 18.08, 18.11, 18.2, 18.24, 18.4, 18.48, 18.5, 18.51, 18.59, 18.65, 18.76, 18.76, 18.86, 18.86, 18.86, 18.87, 18.9, 18.92, 18.93, 19.05, 19.06, 19.17, 19.26, 19.27, 19.41, 19.47, 19.48, 19.54, 19.6, 19.66, 19.67, 19.68, 19.8, 19.9, 20.01, 20.04, 20.1, 20.49, 20.49, 20.5, 20.56, 20.65, 20.65, 20.7, 20.71, 20.78, 20.91, 21.11, 21.19, 21.2, 21.28, 21.29, 21.58, 21.62, 21.7, 21.7, 21.76, 21.76, 21.84, 21.85, 21.87, 21.9, 21.94, 22.0, 22.02, 22.09, 22.16, 22.3, 22.3, 22.41, 22.51, 22.53, 22.71, 22.77, 22.94, 23.17, 23.25, 23.33, 23.72, 23.87, 24.12, 24.14, 24.19, 24.34, 24.4, 24.6, 24.62, 24.62, 24.8, 25.01, 25.13, 25.4, 25.42, 25.81, 25.85, 25.89, 26.03, 26.17, 26.22, 26.41, 26.98, 27.01, 27.02, 27.06, 27.17, 27.49, 27.73, 28.14, 28.23, 28.37, 28.56, 28.83, 28.84, 30.32, 30.57, 31.95, 33.23, 33.46, 33.81, 33.85, 34.44]
y = [1, 0.96826171875, 0.9541015625, 0.99658203125, 0.98828125, 0.98046875, 0.9931640625, 0.99365234375, 0.96435546875, 0.98388671875, 0.984375, 0.98486328125, 0.97412109375, 0.986328125, 0.900390625, 0.662109375, 0.994140625, 0.59716796875, 0.9833984375, 0.97802734375, 0.94873046875, 0.99169921875, 0.9755859375, 0.88330078125, 0.98583984375, 0.96630859375, 0.974609375, 0.9677734375, 0.9345703125, 0.7783203125, 0.95556640625, 0.90576171875, 0.9765625, 0.9794921875, 0.896484375, 0.984375, 0.97314453125, 0.96240234375, 0.96044921875, 0.95361328125, 0.6123046875, 0.919921875, 0.9833984375, 0.90869140625, 0.98095703125, 0.890625, 0.96923828125, 0.425048828125, 0.611328125, 0.96484375, 0.9560546875, 0.464111328125, 0.97705078125, 0.50634765625, 0.841796875, 0.93896484375, 0.96240234375, 0.9501953125, 0.8857421875, 0.806640625, 0.884765625, 0.96533203125, 0.677734375, 0.9169921875, 0.88427734375, 0.90966796875, 0.91259765625, 0.97021484375, 0.9736328125, 0.9619140625, 0.841796875, 0.9736328125, 0.87353515625, 0.79931640625, 0.92626953125, 0.966796875, 0.95263671875, 0.966796875, 0.96240234375, 0.552734375, 0.9443359375, 0.9248046875, 0.91748046875, 0.97021484375, 0.85400390625, 0.70751953125, 0.9658203125, 0.90185546875, 0.916015625, 0.91650390625, 0.95654296875, 0.953125, 0.77294921875, 0.814453125, 0.40673828125, 0.8583984375, 0.8779296875, 0.89892578125, 0.31005859375, 0.495849609375, 0.9189453125, 0.94921875, 0.89990234375, 0.6923828125, 0.7294921875, 0.85302734375, 0.95947265625, 0.890625, 0.9384765625, 0.8779296875, 0.93359375, 0.391357421875, 0.779296875, 0.93505859375, 0.9423828125, 0.93359375, 0.841796875, 0.9345703125, 0.58154296875, 0.92919921875, 0.947265625, 0.94580078125, 0.84375, 0.93359375, 0.921875, 0.9189453125, 0.8955078125, 0.61474609375, 0.8369140625, 0.87060546875, 0.8740234375, 0.356201171875, 0.892578125, 0.73876953125, 0.93310546875, 0.88330078125, 0.826171875, 0.80810546875, 0.984375, 0.919921875, 0.1849365234375, 0.89404296875, 0.83984375, 0.91455078125, 0.2366943359375, 0.73193359375, 0.65380859375, 0.779296875, 0.94140625, 0.8564453125, 0.79150390625, 0.67041015625, 0.266357421875, 0.290771484375, 0.677734375, 0.89501953125, 0.4091796875, 0.87255859375, 0.87255859375, 0.389892578125, 0.90478515625, 0.403076171875, 0.68896484375, 0.83642578125, 0.8486328125, 0.5771484375, 0.8642578125, 0.93359375, 0.7734375, 0.72900390625, 0.853515625, 0.87890625, 0.7587890625, 0.2391357421875, 0.8671875, 0.9501953125, 0.92041015625, 0.869140625, 0.90576171875, 0.95947265625, 0.77734375, 0.2017822265625, 0.82568359375, 0.6015625, 0.8935546875, 0.12481689453125, 0.89697265625, 0.6240234375, 0.86865234375, 0.9130859375, 0.470947265625, 0.75439453125, 0.8486328125, 0.67529296875, 0.87451171875, 0.9755859375, 0.71826171875, 0.91796875, 0.70751953125, 0.80908203125, 0.89208984375, 0.79541015625, 0.7236328125, 0.81103515625, 0.84814453125, 0.59765625, 0.7529296875, 0.234375, 0.68896484375, 0.849609375, 0.69970703125, 0.82861328125, 0.87939453125, 0.322509765625, 0.84326171875, 0.4404296875, 0.78369140625, 0.58154296875, 0.796875, 0.84814453125, 0.28759765625, 0.5263671875, 0.71044921875, 0.8388671875, 0.94580078125, 0.3125, 0.1378173828125, 0.81591796875, 0.85009765625, 0.84521484375, 0.779296875, 0.45751953125, 0.84033203125, 0.58447265625, 0.921875, 0.51123046875, 0.82421875, 0.80224609375, 0.2000732421875, 0.90771484375, 0.4267578125, 0.58154296875, 0.78173828125, 0.736328125, 0.8173828125, 0.65087890625, 0.66357421875, 0.900390625, 0.923828125, 0.66162109375, 0.76513671875, 0.73974609375, 0.6943359375, 0.96044921875, 0.4091796875, 0.529296875, 0.82861328125, 0.7333984375, 0.81689453125, 0.73583984375, 0.66259765625, 0.72607421875, 0.8125, 0.47802734375, 0.646484375, 0.1658935546875, 0.80615234375, 0.59716796875, 0.724609375, 0.2113037109375, 0.301025390625, 0.45947265625, 0.22216796875, 0.62744140625, 0.61865234375, 0.8662109375, 0.7998046875, 0.7333984375, 0.771484375, 0.57373046875, -0.135009765625, 0.6455078125, 0.39404296875, 0.76513671875, 0.603515625, 0.76025390625, 0.630859375, 0.114013671875, 0.63671875, 0.75537109375, 0.1390380859375, 0.27001953125, 0.73681640625, 0.78271484375, 0.7646484375, -0.005977630615234375, 0.402099609375, 0.7119140625, 0.75244140625, 0.71484375, 0.64990234375, 0.3974609375, 0.333251953125, -0.004974365234375, 0.94873046875, 0.348876953125, 0.27880859375, 0.77783203125, 0.67529296875, 0.421875, 0.66357421875, 0.73388671875, 0.6640625, 0.70263671875, 0.48486328125, 0.25537109375, 0.6337890625, 0.4765625, 0.560546875, 0.75439453125, 0.1290283203125, 0.75390625, 0.67138671875, -0.06591796875, 0.7421875, 0.671875, 0.62109375, 0.487548828125, 0.61181640625, 0.56787109375, -0.158447265625, 0.767578125, 0.2235107421875, 0.701171875, 0.05010986328125, 0.60986328125, 0.7255859375, 0.646484375, 0.368896484375, 0.623046875, 0.7177734375, -0.1337890625, 0.69677734375, 0.68408203125, 0.64794921875, 0.433349609375, 0.5302734375, 0.8076171875, 0.39306640625, 0.70361328125, 0.55810546875, 0.7392578125, 0.59765625, 0.91748046875, 0.2086181640625, -0.00395965576171875, 0.11541748046875, 0.4501953125, 0.9111328125, 0.5673828125, 0.278076171875, 0.55029296875, 0.53271484375, 0.6103515625, 0.2529296875, 0.374755859375, 0.6416015625, -0.0623779296875, 0.6845703125, 0.75537109375, 0.853515625, -0.1517333984375, 0.77294921875, 0.55517578125, 0.51318359375, 0.62353515625, 0.5771484375, 0.53515625, 0.324951171875, 0.1741943359375, 0.619140625, -0.174560546875, 0.1729736328125, 0.33642578125, 0.58544921875, 0.59033203125, 0.66845703125, 0.50244140625, 0.01486968994140625, 0.3740234375, 0.442138671875, 0.249755859375, 0.52685546875, 0.17822265625, 0.036163330078125, 0.0174560546875, -0.272705078125, 0.072998046875, 0.14208984375, 0.81298828125, 0.28955078125, 0.371337890625, 0.499755859375, 0.311767578125, 0.63330078125, 0.7138671875, 0.358154296875, -0.04681396484375, -0.1251220703125, 0.32763671875, 0.049652099609375, 0.449951171875, 0.469970703125, 0.1090087890625, 0.34326171875, 0.040740966796875, 0.43017578125, 0.311767578125, 0.043548583984375, -0.2294921875, 0.2281494140625, -0.26611328125, 0.5087890625, 0.76904296875, 0.058685302734375, 0.6572265625, 0.51318359375, 0.7578125, -0.036712646484375, 0.311767578125, 0.025726318359375, 0.041351318359375, 0.0687255859375, 0.342529296875, 0.375732421875, 0.316162109375, 0.466796875, 0.371337890625, 0.07318115234375, 0.6201171875, 0.63623046875, 0.425048828125, 0.38134765625, -0.263916015625, -0.26513671875, 0.364990234375, 0.454833984375, -0.27001953125, 0.43017578125, 0.2164306640625, 0.335693359375, 0.310302734375, 0.447998046875, -0.1202392578125, 0.33740234375, 0.383544921875, -0.447509765625, -0.076904296875, 0.0176544189453125, 0.52587890625, 0.6865234375, 0.59375, -0.2413330078125, 0.38671875, 0.355224609375, 0.403564453125, 0.58154296875, 0.426025390625, 0.0280609130859375, -0.342041015625, -0.06524658203125, 0.2734375, 0.01151275634765625, -0.00833892822265625, 0.2269287109375, -0.1380615234375, 0.55712890625, -0.01837158203125, 0.36474609375, 0.52099609375, -0.353759765625, 0.356689453125, -0.11865234375, 0.2734375, 0.07598876953125, -0.207763671875, 0.1864013671875, 0.11090087890625, 0.08856201171875, -0.441162109375, 0.197998046875, -0.11285400390625, 0.1878662109375, 0.50537109375, -0.40576171875, -0.2386474609375, 0.0860595703125, -0.38720703125, -0.192626953125, -0.313232421875, 0.478759765625, -0.4306640625, -0.0682373046875, 0.01105499267578125, -0.14013671875, 0.23583984375, 0.26025390625, -0.255859375, -0.37548828125, 0.0772705078125, -0.21728515625, 0.279296875, 0.153564453125, -0.389892578125, 0.45361328125, -0.11083984375, 0.466064453125, 0.44287109375, -0.3984375, -0.16552734375, 0.3984375, -0.1669921875, -0.11944580078125, -0.1561279296875, 0.441650390625, -0.194091796875, -0.429443359375, 0.00981903076171875, 0.2122802734375, -0.151123046875, 0.019500732421875, -0.192138671875, -0.1365966796875, 0.1016845703125, -0.10931396484375, 0.139404296875, -0.454833984375, -0.353515625, 0.26953125, -0.41650390625, -0.41162109375, 0.303955078125, 0.0335693359375, -0.1527099609375, -0.396728515625, 0.1541748046875, 0.302001953125, 0.461181640625, -0.26953125, -0.244873046875, -0.128173828125, -0.1346435546875, -0.2939453125, 0.1932373046875, 0.0312042236328125, 0.04608154296875, -0.351806640625, -0.11907958984375, -0.005191802978515625, -0.29296875, -0.1134033203125, -0.09002685546875]
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares
from lmfit import minimize, Parameters, Parameter, report_fit
x = np.asarray(x); y = np.asarray(y)
def fit_fc(params, t, data):
A = params['A'].value
K = params['K'].value
B = params['B'].value
model = (1-A+A*np.cos(K * t))*np.exp(-1 * B * t)
return model - data
params = Parameters()
params.add('A', value=.9, min =0, max =1)
params.add('K', value=0.42, min=-0.2, max=0.8)
params.add('B', value=0.1, min=.01, max=.1)
result = minimize(fit_fc, params, args=(x, y), method='leastsq')
report_fit(params) # write error report
y_lsq = (1-result.params['A'] + (result.params['A'] *np.cos(result.params['K']*x))*np.exp(result.params['B'])*x*(-1))
#plot results
plt.plot(x, y, 'o', label='Data')
plt.plot(x, y_lsq, label='Least_Square_Method')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
I changed your script to use lmfit.Model, which is sort of easier for curve fitting. That would look like (removing the data for simplicity):
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model, Parameters
x = np.asarray(x)
y = np.asarray(y)
def damped_cosine(t, a, k, b):
return (1-a+a*np.cos(k*t))*np.exp(-b*t)
params = Parameters()
params.add('a', value=0.9, min=0)
params.add('k', value=0.42)
params.add('b', value=0.1)
dmodel = Model(damped_cosine)
result = dmodel.fit(y, params, t=x)
print(result.fit_report())
result.plot_fit(show_init=True)
plt.show()
I've removed most of the Parameters bounds -- don't use arbitrary and tight bounds unless you know you need to do so, and if you have not even plotted your data, then you definitely do not know that.
The report printed for this fit is
[[Model]]
Model(damped_cosine)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 42
# data points = 562
# variables = 3
chi-square = 42.2473566
reduced chi-square = 0.07557667
Akaike info crit = -1448.43356
Bayesian info crit = -1435.43905
[[Variables]]
a: 1.1104e-10 +/- 0.00205742 (1852835584.87%) (init = 0.9)
k: 0.45910709 +/- 170843.547 (37212134.66%) (init = 0.42)
b: 0.05936966 +/- 0.00309385 (5.21%) (init = 0.1)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.707
and the plot of the data and fit (and initial guess) is
I think that the most obvious conclusion is that a damped oscillatory function is not a very convincing representation of your data. There is not anything like a clear complete oscillation.
Have you tried to plot the figure ? Your data is really "noisy" compared to the fit you are attempting.

Pan Tompkins Lowpass filter overflow

The Pan Tompkins algorithm1 for removing noise from an ECG/EKG is cited often. They use a low pass filter, followed by a high pass filter. The output of the high pass filter looks great. But (depending on starting conditions) the output of the low pass filter will continuously increase or decrease. Given enough time, your numbers will eventually get to a size that the programming language cannot handle and rollover. If I run this on an Arduino (which uses a variant of C), it rolls over on the order of 10 seconds. Not ideal. Is there a way to get rid of this bias? I've tried messing with initial conditions, but I'm fresh out of ideas. The advantage of this algorithm is that it's not very computationally intensive and will run comfortably on a modest microprocessor.
1 Pan, Jiapu; Tompkins, Willis J. (March 1985). "A Real-Time QRS Detection Algorithm". IEEE Transactions on Biomedical Engineering. BME-32 (3): 230–236.
Python code to illustrate problem. Uses numpy and matplotlib:
import numpy as np
import matplotlib.pyplot as plt
#low-pass filter
def lpf(x):
y = x.copy()
for n in range(len(x)):
if(n < 12):
continue
y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1]
return y
#high-pass filter
def hpf(x):
y = x.copy()
for n in range(len(x)):
if(n < 32):
continue
y[n,1] = y[n-1,1] - x[n,1]/32 + x[n-16,1] - x[n-17,1] + x[n-32,1]/32
return y
ecg = np.loadtxt('ecg_data.csv', delimiter=',',skiprows=1)
plt.plot(ecg[:,0], ecg[:,1])
plt.title('Raw Data')
plt.grid(True)
plt.savefig('raw.png')
plt.show()
#Application of lpf
f1 = lpf(ecg)
plt.plot(f1[:,0], f1[:,1])
plt.title('After Pan-Tompkins LPF')
plt.xlabel('time')
plt.ylabel('mV')
plt.grid(True)
plt.savefig('lpf.png')
plt.show()
#Application of hpf
f2 = hpf(f1[16:,:])
print(f2[-300:-200,1])
plt.plot(f2[:-100,0], f2[:-100,1])
plt.title('After Pan-Tompkins LPF+HPF')
plt.xlabel('time')
plt.ylabel('mV')
plt.grid(True)
plt.savefig('hpf.png')
plt.show()
raw data in CSV format:
timestamp,ecg_measurement
96813044,2.2336266040
96816964,2.1798632144
96820892,2.1505377292
96824812,2.1603128910
96828732,2.1554253101
96832660,2.1163244247
96836580,2.0576734542
96840500,2.0381231307
96844420,2.0527858734
96848340,2.0674486160
96852252,2.0283479690
96856152,1.9648094177
96860056,1.9208210945
96863976,1.9159335136
96867912,1.9208210945
96871828,1.8768328666
96875756,1.7986314296
96879680,1.7448680400
96883584,1.7155425548
96887508,1.7057673931
96891436,1.6520038604
96895348,1.5591397285
96899280,1.4809384346
96903196,1.4467253684
96907112,1.4369501113
96911032,1.3978494453
96914956,1.3440860509
96918860,1.2952101230
96922788,1.3000977039
96926684,1.3343108892
96930604,1.3440860509
96934516,1.3489736318
96938444,1.3294233083
96942364,1.3782991170
96946284,1.4222873687
96950200,1.4516129493
96954120,1.4369501113
96958036,1.4320625305
96961960,1.4565005302
96965872,1.4907135963
96969780,1.5053763389
96973696,1.4613881111
96977628,1.4125122070
96981548,1.4076246261
96985476,1.4467253684
96989408,1.4809384346
96993324,1.4760508537
96997236,1.4711632728
97001160,1.4907135963
97005084,1.5444769859
97008996,1.5982404708
97012908,1.5835777282
97016828,1.5591397285
97020756,1.5786901473
97024676,1.6324535369
97028604,1.6911046504
97032516,1.6959922313
97036444,1.6764417648
97040364,1.6813293457
97044296,1.7155425548
97048216,1.7448680400
97052120,1.7253177165
97056048,1.6911046504
97059968,1.6911046504
97063880,1.7302052974
97067796,1.7741935253
97071724,1.7693059444
97075644,1.7350928783
97079564,1.7595307826
97083480,1.8719452857
97087396,2.0381231307
97091316,2.2482893466
97095244,2.4828934669
97099156,2.7468230724
97103088,2.9960899353
97106996,3.0987291336
97110912,2.9178886413
97114836,2.5171065330
97118756,2.0185728073
97122668,1.5053763389
97126584,1.1094819307
97130492,0.8015640258
97134396,0.5767350673
97138308,0.4545454502
97142212,0.4349951267
97146124,0.4692081928
97150020,0.4887585639
97153924,0.4594330310
97157828,0.4105571746
97161740,0.3861192512
97165660,0.3763440847
97169580,0.3714565038
97173492,0.3225806236
97177404,0.2639296054
97181316,0.2394916772
97185236,0.2297165155
97189148,0.2443792819
97193060,0.2248289346
97196972,0.1857282543
97200900,0.1808406734
97204812,0.2199413537
97208732,0.2492668628
97212652,0.2443792819
97216572,0.2199413537
97220484,0.2248289346
97224404,0.2834799575
97228316,0.3274682044
97232228,0.3665689229
97236132,0.3861192512
97240036,0.4398827075
97243936,0.5083088874
97247836,0.6109481811
97251748,0.7086998939
97255660,0.7771260738
97259568,0.8553275108
97263476,0.9775171279
97267392,1.1094819307
97271308,1.1974585056
97275228,1.2512218952
97279148,1.2952101230
97283056,1.3734115362
97286992,1.4760508537
97290900,1.5493645668
97294820,1.5738025665
97298740,1.5982404708
97302652,1.6471162796
97306584,1.7106549739
97310500,1.7546432018
97314420,1.7546432018
97318340,1.7644183635
97322272,1.8084066390
97326168,1.8621701240
97330072,1.8963831901
97333988,1.8817204475
97337912,1.8572825431
97341840,1.8670577049
97345748,1.8866080284
97349668,1.8768328666
97353580,1.8230694770
97357500,1.7595307826
97361424,1.7302052974
97365332,1.7350928783
97369252,1.6959922313
97373168,1.6226783752
97377092,1.5298142433
97381012,1.4613881111
97384940,1.4320625305
97388860,1.4076246261
97392780,1.3440860509
97396676,1.2658846378
97400604,1.2121212482
97404532,1.1974585056
97408444,1.1779080629
97412356,1.1192570924
97416264,1.0361680984
97420164,0.9628542900
97424068,0.9286412239
97427988,0.9042033195
97431892,0.8406646728
97435804,0.7575757503
97439708,0.6940371513
97443628,0.6793744087
97447540,0.6793744087
97451452,0.6549364089
97455356,0.6060606002
97459240,0.5767350673
97463140,0.6011730194
97467044,0.6451612472
97470964,0.6842619895
97474884,0.6891495704
97478796,0.7184750556
97482700,0.8064516067
97486612,0.8846529960
97490516,0.9335288047
97494428,0.9530791282
97498340,0.9481915473
97502256,0.9726295471
97506156,0.9921798706
97510060,0.9726295471
97513980,0.8846529960
97517884,0.7869012832
97521796,0.7086998939
97525692,0.6549364089
97529604,0.5913978099
97533516,0.4887585639
97537428,0.3567937374
97541348,0.2639296054
97545260,0.2003910064
97549148,0.1417399787
97553060,0.0928641223
97556980,0.0537634420
97560892,0.0342130994
97564804,0.0146627559
97568708,0.0244379281
97572628,0.0048875851
97576500,0.0000000000
97580324,0.0000000000
97584172,0.0097751703
97588060,0.0244379281
97591980,0.0195503416
97595900,0.0146627559
97599812,0.0488758563
97603724,0.1319648027
97607628,0.2248289346
97611548,0.3030303001
97615444,0.3665689229
97619364,0.4496578693
97623276,0.5718474864
97627176,0.7038123130
97631076,0.8064516067
97634988,0.8699902534
97638900,0.9384163856
97642816,1.0361680984
97646720,1.1485825777
97650644,1.2365591526
97654572,1.2658846378
97658492,1.2805473804
97662404,1.3294233083
97666324,1.3782991170
97670244,1.3831867027
97674148,1.3489736318
97678068,1.3049852848
97681988,1.2903225421
97685908,1.3000977039
97689812,1.3098728656
97693728,1.2463343143
97697648,1.1876833438
97701568,1.1681329011
97705488,1.1876833438
97709412,1.1827956390
97713328,1.1339198350
97717244,1.0752688646
97721144,1.0557184219
97725056,1.0703812837
97728972,1.0850440216
97732872,1.0752688646
97736788,1.0459432601
97740696,1.0508308410
97744600,1.0948191833
97748520,1.1290322542
97752444,1.1192570924
97756364,1.0850440216
97760272,1.0801564407
97764168,1.1094819307
97768072,1.1339198350
97771996,1.1143695116
97775920,1.0557184219
97779840,1.0166177749
97783756,0.9970674514
97787668,0.9921798706
97791580,0.9530791282
97795500,0.8846529960
97799412,0.8504399299
97803316,0.8455523490
97807212,0.8699902534
97811124,0.8699902534
97815028,0.8308895111
97818940,0.8064516067
97822844,0.8211143493
97826756,0.8651026725
97830668,0.9042033195
97834572,0.8895405769
97838476,0.8993157386
97842396,0.9530791282
97846304,1.0410556793
97850204,1.0850440216
97854112,1.0899316024
97858020,1.1192570924
97861940,1.2267839908
97865860,1.4320625305
97869772,1.6911046504
97873688,1.9892473220
97877604,2.3020527362
97881524,2.6197457313
97885452,2.8299119949
97889372,2.7761485576
97893292,2.4535679817
97897216,1.9745845794
97901136,1.4956011772
97905052,1.0899316024
97908964,0.8260019302
97912864,0.6695992469
97916772,0.6353860855
97920684,0.7038123130
97924588,0.8553275108
97928496,1.0215053558
97932408,1.1388074159
97936324,1.2023460865
97940228,1.2463343143
97944148,1.3098728656
97948064,1.3734115362
97951988,1.3929618644
97955912,1.3734115362
97959836,1.3636363744
97963764,1.3782991170
97967684,1.4173997879
97971612,1.4173997879
97975540,1.3880742835
97979460,1.3831867027
97983372,1.4027370452
97987292,1.4467253684
97991216,1.4565005302
97995124,1.4320625305
97999036,1.4173997879
98002964,1.4662756919
98006884,1.5249266624
98010812,1.5689149856
98014740,1.5689149856
98018668,1.5689149856
98022592,1.6129032135
98026516,1.6715541839
98030436,1.6911046504
98034348,1.6617790222
98038268,1.6422286987
98042192,1.6715541839
98046124,1.7204301357
98050032,1.7399804592
98053952,1.7155425548
98057880,1.6911046504
98061800,1.7106549739
98065716,1.7595307826
98069636,1.7937438488
98073556,1.7888562679
98077476,1.7741935253
98081408,1.8084066390
98085312,1.8719452857
98089228,1.9305962562
98093144,1.9257086753
98097048,1.9257086753
98100968,1.9599218368
98104884,2.0332355499
98108804,2.0967741012
98112724,2.1016616821
98116652,2.0869989395
98120564,2.0967741012
98124484,2.1456501483
98128404,2.1847507953
98132324,2.1749756336
98136252,2.1212120056
98140156,2.0967741012
98144068,2.1114368438
98147996,2.0967741012
98151908,2.0430107116
98155824,1.9501466751
98159748,1.8817204475
98163664,1.8475073814
98167584,1.8377322196
98171508,1.7937438488
98175440,1.7253177165
98179364,1.7057673931
98183296,1.7106549739
98187200,1.7448680400
98191108,1.7546432018
98195032,1.7302052974
98198952,1.7302052974
98202868,1.7741935253
98206800,1.8426198005
98210712,1.8866080284
98214628,1.8914956092
98218548,1.8914956092
98222472,1.9403715133
98226396,2.0087976455
98230308,2.0527858734
98234212,2.0527858734
98238132,2.0527858734
98242044,2.0869989395
98245964,2.1407625675
98249892,2.1798632144
98253812,2.1749756336
98257740,2.1652004718
98261660,2.1896383762
98265588,2.2385141849
98269516,2.2678396701
98273444,2.2385141849
98277364,2.1896383762
98281292,2.1798632144
98285212,2.1994135379
98289140,2.2091886997
98293052,2.1798632144
98296980,2.1212120056
98300892,2.0918865203
98304804,2.1114368438
98308732,2.1163244247
98312660,2.0674486160
98316572,2.0087976455
98320480,1.9892473220
98324392,1.9892473220
98328308,2.0087976455
98332216,1.9892473220
98336132,1.9501466751
98340048,1.9354838371
98343972,1.9696969985
98347888,1.9843597412
98351812,1.9696969985
98355736,1.9159335136
98359664,1.8866080284
98363576,1.9012707710
98367484,1.9305962562
98371408,1.9208210945
98375324,1.8817204475
98379240,1.8719452857
98383156,1.8817204475
98387072,1.9305962562
98390984,1.9403715133
98394904,1.9159335136
98398832,1.9012707710
98402744,1.9354838371
98406672,1.9794721603
98410584,1.9941349029
98414492,1.9696969985
98418416,1.9550342559
98422336,1.9843597412
98426260,2.0430107116
98430164,2.0723361968
98434076,2.0527858734
98437988,2.0381231307
98441900,2.0625610351
98445820,2.1065492630
98449740,2.1309874057
98453660,2.1065492630
98457572,2.0869989395
98461492,2.0918865203
98465404,2.1456501483
98469324,2.1847507953
98473236,2.1749756336
98477148,2.1505377292
98481052,2.1652004718
98484972,2.1945259571
98488900,2.2287390232
98492820,2.2091886997
98496732,2.1700880527
98500644,2.1652004718
98504556,2.2091886997
98508476,2.2531769275
98512404,2.2336266040
98516324,2.1994135379
98520244,2.2043011188
98524152,2.2531769275
98528068,2.2873899936
98531988,2.2727272510
98535908,2.2238514423
98539836,2.1994135379
98543764,2.2336266040
98547676,2.2580645084
98551588,2.2482893466
98555508,2.1994135379
98559436,2.1652004718
98563356,2.1603128910
98567268,2.1700880527
98571164,2.1309874057
98575068,2.0527858734
98578992,1.9843597412
98582920,1.9648094177
98586840,1.9696969985
98590756,1.9501466751
98594680,1.8963831901
98598596,1.8523949623
98602528,1.8572825431
98606456,1.8621701240
98610376,1.8670577049
98614292,1.8328446388
98618204,1.8132943153
98622132,1.8426198005
98626048,1.8963831901
98629968,1.9257086753
98633892,1.8914956092
98637808,1.8670577049
98641716,1.8914956092
98645640,1.9941349029
98649556,2.1456501483
98653476,2.3313782215
98657404,2.5708699226
98661316,2.8885631561
98665236,3.2306940555
98669148,3.4799609184
98673064,3.4604105949
98676972,3.1769306659
98680884,2.7614858150
98684796,2.2678396701
98688720,1.8230694770
98692628,1.4418377876
98696556,1.2023460865
98700476,1.1241446733
98704400,1.1876833438
98708316,1.3098728656
98712228,1.4125122070
98716148,1.4858260154
98720060,1.5493645668
98723988,1.6275659561
98727908,1.6764417648
98731836,1.6911046504
98735748,1.6617790222
98739676,1.6471162796
98743608,1.6715541839
98747532,1.7057673931
98751452,1.7057673931
98755372,1.6568914413
98759300,1.6275659561
98763220,1.6422286987
98767140,1.6862169265
98771056,1.6911046504
98774964,1.6617790222
98778884,1.6568914413
98782812,1.6911046504
98786728,1.7448680400
98790632,1.7790811061
98794544,1.7693059444
98798452,1.7644183635
98802384,1.7888562679
98806312,1.8279570579
98810224,1.8426198005
98814132,1.8181818962
98818044,1.7986314296
98821972,1.8181818962
98825900,1.8523949623
98829832,1.8719452857
98833760,1.8377322196
98837684,1.8035190582
98841596,1.7986314296
98845528,1.8377322196
98849456,1.8670577049
98853368,1.8523949623
98857292,1.8181818962
98861220,1.8328446388
98865140,1.8866080284
98869048,1.9305962562
98872968,1.9305962562
98876888,1.9012707710
98880800,1.9208210945
98884704,1.9599218368
98888624,1.9892473220
98892544,1.9599218368
98896464,1.8866080284
98900376,1.8426198005
98904296,1.8377322196
98908216,1.8328446388
98912132,1.7839686870
98916040,1.7008798122
98919956,1.6471162796
98923884,1.6373411178
98927812,1.6324535369
98931740,1.5982404708
98935644,1.5151515007
98939564,1.4613881111
98943492,1.4418377876
98947424,1.4271749496
98951348,1.3685239553
98955260,1.2707722187
98959180,1.1925709247
98963092,1.1534701585
98967008,1.1339198350
98970932,1.1045943498
98974840,1.0215053558
98978748,0.9677418708
98982660,0.9579667091
98986572,0.9775171279
98990492,0.9824047088
98994396,0.9237536430
98998308,0.8748778343
99002212,0.8797654151
99006132,0.9188660621
99010036,0.9286412239
99013956,0.9090909004
99017836,0.8895405769
99021740,0.9042033195
99025644,0.9530791282
99029556,0.9921798706
99033468,0.9970674514
99037380,0.9872922897
99041296,1.0166177749
99045196,1.0752688646
99049100,1.1192570924
99053016,1.1290322542
99056940,1.0997067642
99060840,1.1094819307
99064744,1.1485825777
99068668,1.1925709247
99072588,1.2023460865
99076508,1.1925709247
99080428,1.2023460865
99084340,1.2658846378
99088252,1.3343108892
99092168,1.3587487936
99096084,1.3343108892
99100004,1.3294233083
99103924,1.3636363744
99107860,1.4027370452
99111772,1.3831867027
99115700,1.3343108892
99119628,1.3147605657
99123556,1.3343108892
99127480,1.3587487936
99131404,1.3538612127
99135324,1.3049852848
99139236,1.2756597995
99143156,1.2903225421
99147076,1.3196481466
99151012,1.3147605657
99154924,1.2707722187
99158844,1.2072336673
99162764,1.2023460865
99166676,1.2267839908
99170588,1.2365591526
99174508,1.1974585056
99178420,1.1632453203
99182340,1.1534701585
99186248,1.1876833438
99190164,1.1974585056
99194072,1.1583577394
99197996,1.1192570924
99201916,1.1192570924
99205832,1.1730204820
99209748,1.2072336673
99213668,1.2023460865
99217588,1.1779080629
99221488,1.1876833438
99225412,1.2267839908
99229332,1.2707722187
99233244,1.2609970569
99237152,1.2365591526
99241068,1.2463343143
99244988,1.2805473804
99248900,1.2952101230
99252820,1.2805473804
99256732,1.2316715717
99260660,1.2316715717
99264588,1.2854349613
99268512,1.3391984701
99272436,1.3538612127
99276364,1.3343108892
99280292,1.3391984701
99284212,1.3782991170
99288116,1.4271749496
99292040,1.4369501113
99295964,1.4076246261
99299892,1.4076246261
99303816,1.4662756919
99307740,1.5395894050
99311652,1.5640274047
99315564,1.5444769859
99319484,1.5444769859
99323412,1.5786901473
99327332,1.6275659561
99331252,1.6520038604
99335156,1.6422286987
99339076,1.6275659561
99343004,1.6422286987
99346924,1.6666666030
99350844,1.6568914413
99354764,1.6031280517
99358676,1.5542521476
99362604,1.5542521476
99366532,1.5835777282
99370460,1.5982404708
99374372,1.5835777282
99378300,1.5640274047
99382204,1.5835777282
99386132,1.6373411178
99390056,1.6715541839
99393980,1.6520038604
99397892,1.6275659561
99401812,1.6422286987
99405736,1.6862169265
99409664,1.7106549739
99413580,1.6911046504
99417500,1.6568914413
99421432,1.6715541839
99425348,1.7204301357
99429256,1.8084066390
99433164,1.9208210945
99437068,2.0918865203
99440980,2.3655912876
99444912,2.7321603298
99448828,3.0596284866
99452752,3.2453567981
99456680,3.1867058277
99460600,2.9374389648
99464516,2.5610947608
99468428,2.1163244247
99472356,1.6813293457
99476284,1.3343108892
99480200,1.1436949968
99484112,1.1339198350
99488036,1.2365591526
99491956,1.3440860509
99495864,1.4320625305
99499780,1.5298142433
99503708,1.6422286987
99507636,1.7350928783
99511556,1.7644183635
99515480,1.7399804592
99519396,1.7350928783
99523320,1.7448680400
99527220,1.7350928783
99531140,1.6862169265
99535064,1.5933528900
99538980,1.5102639198
99542892,1.4711632728
99546820,1.4467253684
99550748,1.3978494453
99554668,1.3049852848
99558588,1.2072336673
99562504,1.1485825777
99566428,1.1192570924
99570348,1.0752688646
99574256,1.0068426132
99578176,0.9384163856
99582084,0.9188660621
99585988,0.9188660621
99589900,0.9188660621
99593812,0.8895405769
99597716,0.8748778343
99601636,0.8651026725
99605552,0.9090909004
99609436,0.9481915473
99613356,0.9530791282
99617268,0.9237536430
99621180,0.9335288047
99625080,1.0019550323
99628980,1.0752688646
99632888,1.0801564407
99636792,1.0703812837
99640704,1.0899316024
99644616,1.1436949968
99648536,1.2170088291
99652444,1.2170088291
99656356,1.2023460865
99660268,1.2072336673
99664180,1.2561094760
99668084,1.3000977039
99671980,1.3147605657
99675900,1.2952101230
99679820,1.3000977039
99683728,1.3587487936
99687652,1.4027370452
99691568,1.4222873687
99695484,1.3978494453
99699404,1.3880742835
99703328,1.4173997879
99707248,1.4565005302
99711156,1.4760508537
99715064,1.4271749496
99718988,1.3929618644
99722908,1.3929618644
99726828,1.4076246261
99730748,1.3831867027
99734668,1.3147605657
99738580,1.2561094760
99742492,1.2414467334
99746420,1.2658846378
99750340,1.2658846378
99754252,1.2365591526
99758168,1.2121212482
99762084,1.2365591526
99766012,1.3000977039
99769916,1.3538612127
99773856,1.3685239553
99777780,1.3929618644
99781704,1.4662756919
99785620,1.5640274047
99789532,1.6568914413
99793460,1.6959922313
99797392,1.7057673931
99801312,1.7399804592
99805228,1.7937438488
99809148,1.8377322196
99813072,1.8377322196
99816996,1.8230694770
99820920,1.8475073814
99824840,1.9061583518
99828756,1.9501466751
99832680,1.9599218368
99836608,1.9501466751
99840536,1.9599218368
99844452,2.0087976455
99848364,2.0527858734
99852268,2.0527858734
99856184,2.0283479690
99860092,2.0185728073
99864012,2.0576734542
99867932,2.0967741012
99871836,2.0869989395
99875740,2.0478982925
99879652,2.0234603881
99883564,2.0527858734
99887484,2.1065492630
99891404,2.1163244247
99895332,2.0772237777
99899236,2.0527858734
99903156,2.0821113586
99907076,2.1065492630
99910996,2.1016616821
99914916,2.0576734542
99918828,2.0283479690
99922740,2.0430107116
99926652,2.0821113586
99930572,2.1016616821
99934492,2.0576734542
99938404,2.0332355499
99942316,2.0674486160
99946220,2.1309874057
99950124,2.1749756336
99954052,2.1652004718
99957972,2.1260998249
99961892,2.1456501483
99965804,2.1945259571
99969732,2.2336266040
99973644,2.2189638614
99977564,2.1945259571
99981492,2.2043011188
99985404,2.2482893466
99989332,2.2922775745
99993252,2.2580645084
99997164,2.2238514423
100001084,2.2189638614
100005044,2.2678396701
100009004,2.2776148319
100012956,2.2385141849
100016924,2.1798632144
100020892,2.1603128910
100024844,2.1798632144
100028804,2.2140762805
100032756,2.1798632144
100036716,2.1358749866
100040676,2.1163244247
100044644,2.1358749866
100048604,2.1603128910
100052556,2.1407625675
100056516,2.0967741012
100060468,2.0918865203
100064420,2.1163244247
100068384,2.1407625675
100072340,2.1065492630
100076292,2.0478982925
100080244,2.0332355499
100084196,2.0478982925
100088156,2.0674486160
100092100,2.0332355499
100096056,1.9696969985
100100004,1.9110459327
100103968,1.9061583518
100107928,1.9208210945
100111884,1.8768328666
100115844,1.8181818962
100119812,1.7888562679
100123776,1.8084066390
100127712,1.8475073814
100131672,1.8523949623
100135636,1.8181818962
100139604,1.8035190582
100143560,1.8377322196
100147524,1.8768328666
100151488,1.8719452857
100155448,1.8523949623
100159404,1.8132943153
100163376,1.8426198005
100167328,1.8963831901
100171276,1.9110459327
100175232,1.9061583518
100179188,1.9501466751
100183132,2.1016616821
100187084,2.3216030597
100191036,2.5904202461
100194996,2.8787879943
100198956,3.1769306659
100202916,3.4555230140
100206876,3.5826001167
100210836,3.4115347862
100214804,3.0205278396
100218748,2.5317692756
100222708,2.1016616821
100226676,1.7937438488
100230640,1.5933528900
100234592,1.4858260154
100238548,1.5053763389
100242500,1.6422286987
100246456,1.8377322196
100250424,1.9990224838
100254380,2.0967741012
100258340,2.1652004718
100262284,2.2434017658
100266236,2.3411533832
100270196,2.4242424964
100274140,2.4731183052
100278112,2.4975562095
100282068,2.5562071800
100286020,2.6441838741
100289992,2.7028348445
100293948,2.7077224254
100297908,2.7126100063
100301868,2.7468230724
100305820,2.8054740905
100309764,2.8250244140
100313724,2.7908113002
100317676,2.7370479106
100321636,2.7223851680
100325600,2.7419354915
100329556,2.7517106533
100333516,2.7126100063
100337468,2.6735093593
100341428,2.6686217784
100345392,2.7028348445
100349348,2.7272727489
100353316,2.6881721019
100357260,2.6441838741
100361212,2.6588466167
100365180,2.6832845211
100369140,2.7077224254
100373100,2.6783969402
100377052,2.6148581504
100381012,2.6001954078
100384960,2.6246333122
100388916,2.6490714550
100392860,2.6197457313
100396828,2.5659823417
100400788,2.5562071800
100404740,2.5806450843
100408692,2.6099705696
100412644,2.5904202461
100416588,2.5366568565
100420548,2.5268816947
100424508,2.5610947608
100428460,2.5953078269
100432412,2.5757575035
100436372,2.5171065330
100440324,2.4926686286
100444276,2.5219941139
100448228,2.5366568565
100452196,2.5073313713
100456148,2.4389052391
100460108,2.3949170112
100464068,2.3753666877
100468028,2.3655912876
100471988,2.3069403171
The trick seems to be the initial conditions. Load the first 13 values of input and output of low pass filter to zero and the bias goes away.
#low-pass filter
def lpf(x):
y = x.copy()
for n in range(13):
y[n,1] = 0
x[n,1] = 0
for n in range(len(x)):
if(n < 12):
continue
y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1]
return y

How to record the value of a variable within odeint?

I would like to know if there is a way to record the value of a specific variable within the function of integration, without having to print it within the definition of the function, which in many cases, due to the algorithm of prediction-correction, lead to more or less values than the final vector returned by the function?
Example let's try with this code:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def essai(y, t):
a = y[0]
c1 = a
a = c1 / a**2
return [a]
# Solving
essai0 = [10]
t = np.linspace(0, 2000, 10)
y = odeint(essai, essai0, t)
a = y[:, 0]
# Graphs
fig, ax = plt.subplots()
ax.plot(t, a, 'k--', label='a')
legend = ax.legend(loc='lower right', shadow=True, fontsize='x-large')
legend.get_frame().set_facecolor('#FFFCCC') # 00FFCC
plt.xlabel('x')
plt.ylabel('y')
plt.title('y vs x')
plt.show()
I would like to record the values of c1 which depends on a. What should I do?
If I print, it I get (because of pred-corr algorithm):
10.0
10.001203411814794
10.00120326701222
10.002406534059283
10.00240638930896
10.031168251789499
10.03116843523562
10.059847893733858
10.059848247411573
10.088446178306066
10.088446526968276
10.178981333917179
10.1789826635142
10.26872274187664
10.268720875457465
10.251795853148066
10.251794757670828
10.324093402400061
10.324093338929458
10.395889284010963
10.395889126663482
10.467192620394076
10.467192470562162
10.60836217080531
10.608361512785885
10.747675991273601
10.747676529983982
10.885208084361661
10.88520861500753
11.021024408838219
11.021024559158226
11.15518691385528
11.15518704871583
11.389028983440005
11.389029612664437
11.618166387462095
11.618166372845774
11.842871925632974
11.842870666797078
12.063390475531826
12.0633901508557
12.279950446401756
12.279950250452782
12.492757035192547
12.492756877414479
12.790475076345272
12.79047467718475
13.081418818481728
13.081418595295522
13.366029970579808
13.366030900758636
13.644707388512776
13.644707798536366
13.917805722870085
13.917805853240296
14.185647189512732
14.185647276304193
14.448524340486092
14.44852440612534
14.849045554474056
14.849045812160185
15.239043242348172
15.239044113472564
15.619306858637934
15.619307570817467
15.990530200625596
15.990530706701604
16.353328829257094
16.35332918566708
16.70825155213741
16.708251810028536
17.055790075751844
17.055790265472186
17.52054793291328
17.520548366986496
17.97329155702487
17.97329263337524
18.414908470097206
18.41490919183692
18.84617978510828
18.846180323693773
19.26780035288661
19.26780072790131
19.68039039537204
19.680390669145883
20.084506483562638
20.084506685872917
20.63204921728682
20.632049705019547
21.165431430483114
21.16543268212929
21.685699626883885
21.685700483180575
22.193774842932424
22.193775478119036
22.69047628806277
22.69047673120133
23.176535191516802
23.1765355148269
23.652607704971896
23.652607943862492
24.296731084127696
24.296731656936466
24.92421316694978
24.924214631653445
25.536282592848192
25.536283593100098
26.134020839947766
26.134021582629195
26.718389929663125
26.718390447872228
27.290248649274574
27.290249027491374
27.8503676838429
27.85036796338048
28.60821935477876
28.608220025227006
29.346505899333515
29.346507613905608
30.066670806260635
30.066671977520553
30.769984796557875
30.769985666417984
31.457578314647648
31.457578921761066
32.13046057231114
32.13046101551341
32.78953730742519
32.789537635058444
33.68118868621462
33.68118947182226
34.54983545122736
34.549837459883506
35.39717380841791
35.397175180698845
36.22469707822626
36.224698097642104
37.033733817898586
37.03373452954837
37.82547018189015
37.82547070150822
38.60097077071101
38.60097115490064
39.65004988104156
39.650050802111195
40.67207751401193
40.67207986867377
41.669047220267416
41.66904882908885
42.64271422854618
42.64271542393563
43.594640193459966
43.59464102811222
44.52621945824691
44.52622006777859
45.43870353935591
45.438703990091476
46.67300975177773
46.673010832232926
47.87550305124021
47.87550581301012
49.04852683447106
49.04852872160157
50.194144483083306
50.19414588551954
51.3141919066777
51.31419288605143
52.41030839692969
52.41030911225109
53.48396538985435
53.483965918885744
54.9362075454971
54.93620881348237
56.35103457439806
56.35103781516747
57.73120149400896
57.731203708595864
59.07913425147381
59.07913589751868
60.39699143853227
60.39699258818307
61.68670054765226
61.68670138744394
62.94999176730058
62.949992388453296
64.65865496068966
64.65865644932029
Which is much more values than I may expect with t = np.linspace(0, 2000, 10) which divide the intervale of time in tenth of 200.
I have thought to this problem for a long time without find a really good way to do it and I would be delighted to know how to bypass this problem.
There is no relation between the evaluation points of the ODE function in the internal solver steps and the requested sample points of the solution for the output. Moreover, the evaluation points can deviate from the solution trajectory with some error of an order lower than the order of the integration method.
The easiest way to do what you want in a structured fashion is to define the c1 function as a separate function and then to call it on the results
def c1_func(y): return y[0]
def essai(y, t):
a = y[0]
c1 = c1_func(y)
a = c1 / a**2
return [a]
...
y = odeint(...
c1_val = c1_func(y.T)
plt.plot(x, c1_val)
or so.

How to fit multiple spectra to a spectrum by extracting the coefficients from a least-squares linear fit?

I want to fit two different spectra to my original spectrum. The two different spectra have x and y values of:
x_1 = 1700.42
1700.9
1701.38
1701.86
1702.34
1702.83
1703.31
1703.79
1704.27
1704.75
1705.24
1705.72
1706.2
1706.68
1707.17
1707.65
1708.13
1708.61
1709.09
1709.58
1710.06
1710.54
1711.02
1711.5
1711.99
1712.47
1712.95
1713.43
1713.91
1714.4
1714.88
1715.36
1715.84
1716.33
1716.81
1717.29
1717.77
1718.25
1718.74
1719.22
1719.7
1720.18
1720.66
1721.15
1721.63
1722.11
1722.59
1723.08
1723.56
1724.04
1724.52
1725
1725.49
1725.97
1726.45
1726.93
1727.41
1727.9
1728.38
1728.86
1729.34
1729.82
1730.31
1730.79
1731.27
1731.75
1732.24
1732.72
1733.2
1733.68
1734.16
1734.65
1735.13
1735.61
1736.09
1736.57
1737.06
1737.54
1738.02
1738.5
1738.98
1739.47
1739.95
1740.43
1740.91
1741.4
1741.88
1742.36
1742.84
1743.32
1743.81
1744.29
1744.77
1745.25
1745.73
1746.22
1746.7
1747.18
1747.66
1748.14
1748.63
1749.11
1749.59
1750.07
1750.56
y_1 = 0.00285
0.00289
0.00290
0.00292
0.00297
0.00304
0.00310
0.00314
0.00319
0.00323
0.00327
0.00333
0.00340
0.00344
0.00347
0.00352
0.00358
0.00364
0.00369
0.00374
0.00382
0.00388
0.00392
0.00397
0.00403
0.00408
0.00414
0.00420
0.00428
0.00436
0.00444
0.00451
0.00456
0.00461
0.00468
0.00474
0.00480
0.00486
0.00493
0.00501
0.00509
0.00517
0.00524
0.00530
0.00535
0.00543
0.00551
0.00558
0.00564
0.00571
0.00578
0.00587
0.00594
0.00599
0.00607
0.00615
0.00623
0.00631
0.00636
0.00645
0.00657
0.00666
0.00673
0.00682
0.00688
0.00695
0.00704
0.00713
0.00722
0.00732
0.00741
0.00750
0.00758
0.00768
0.00777
0.00783
0.00788
0.00792
0.00795
0.00799
0.00803
0.00806
0.00807
0.00804
0.00800
0.00795
0.00787
0.00779
0.00767
0.00753
0.00737
0.00719
0.00699
0.00677
0.00652
0.00626
0.00599
0.00572
0.00546
0.00519
0.00492
0.00465
0.00437
0.00413
0.00391
and x_2 = 1700.42 1700.9 1701.38 1701.86 1702.34 1702.83 1703.31 1703.79 1704.27 1704.75 1705.24 1705.72 1706.2 1706.68 1707.17 1707.65 1708.13 1708.61 1709.09 1709.58 1710.06 1710.54 1711.02 1711.5 1711.99 1712.47 1712.95 1713.43 1713.91 1714.4 1714.88 1715.36 1715.84 1716.33 1716.81 1717.29 1717.77 1718.25 1718.74 1719.22 1719.7 1720.18 1720.66 1721.15 1721.63 1722.11 1722.59 1723.08 1723.56 1724.04 1724.52 1725 1725.49 1725.97 1726.45 1726.93 1727.41 1727.9 1728.38 1728.86 1729.34 1729.82 1730.31 1730.79 1731.27 1731.75 1732.24 1732.72 1733.2 1733.68 1734.16 1734.65 1735.13 1735.61 1736.09 1736.57 1737.06 1737.54 1738.02 1738.5 1738.98 1739.47 1739.95 1740.43 1740.91 1741.4 1741.88 1742.36 1742.84 1743.32 1743.81 1744.29 1744.77 1745.25 1745.73 1746.22 1746.7 1747.18 1747.66 1748.14 1748.63 1749.11 1749.59 1750.07 1750.56
y_2 = 0.00182478
0.00198449
0.0021542
0.00230491
0.00248363
0.00269334
0.00289705
0.00308676
0.00330747
0.00358919
0.0038779
0.00415561
0.00444332
0.00474103
0.00507474
0.00542346
0.00576517
0.00613688
0.00651859
0.0068873
0.00727502
0.00767773
0.00808544
0.00851815
0.00894486
0.00935658
0.00979429
0.010245
0.0106727
0.0110844
0.0115191
0.0119878
0.0124556
0.0128823
0.013274
0.0137237
0.0142374
0.0147181
0.0151798
0.0156495
0.0160963
0.016534
0.0169657
0.0173574
0.0177211
0.0180818
0.0184125
0.0187012
0.0189339
0.0191077
0.0192454
0.0193291
0.0193638
0.0193495
0.0192672
0.0191119
0.0188696
0.0185614
0.0181941
0.0176948
0.0170465
0.0162762
0.0153449
0.0142406
0.0129863
0.0115801
0.0100468
0.00844248
0.00692419
0.0055719
0.00435861
0.00340132
0.00270704
0.00213775
0.00168046
0.00134117
0.00109188
9.16595E-4
7.80307E-4
6.65019E-4
5.62731E-4
4.75443E-4
4.42155E-4
4.49867E-4
4.29579E-4
3.9929E-4
3.83002E-4
3.51714E-4
3.38426E-4
3.40138E-4
3.2985E-4
3.27562E-4
3.24274E-4
3.06986E-4
2.92698E-4
3.0041E-4
3.12121E-4
2.84833E-4
2.47545E-4
2.41257E-4
2.34969E-4
2.27681E-4
2.47393E-4
2.60105E-4
2.25817E-4
My original data:
x_orig = 1700.42
1700.9
1701.38
1701.86
1702.34
1702.83
1703.31
1703.79
1704.27
1704.75
1705.24
1705.72
1706.2
1706.68
1707.17
1707.65
1708.13
1708.61
1709.09
1709.58
1710.06
1710.54
1711.02
1711.5
1711.99
1712.47
1712.95
1713.43
1713.91
1714.4
1714.88
1715.36
1715.84
1716.33
1716.81
1717.29
1717.77
1718.25
1718.74
1719.22
1719.7
1720.18
1720.66
1721.15
1721.63
1722.11
1722.59
1723.08
1723.56
1724.04
1724.52
1725
1725.49
1725.97
1726.45
1726.93
1727.41
1727.9
1728.38
1728.86
1729.34
1729.82
1730.31
1730.79
1731.27
1731.75
1732.24
1732.72
1733.2
1733.68
1734.16
1734.65
1735.13
1735.61
1736.09
1736.57
1737.06
1737.54
1738.02
1738.5
1738.98
1739.47
1739.95
1740.43
1740.91
1741.4
1741.88
1742.36
1742.84
1743.32
1743.81
1744.29
1744.77
1745.25
1745.73
1746.22
1746.7
1747.18
1747.66
1748.14
1748.63
1749.11
1749.59
1750.07
1750.56
y_orig = 0.011507
0.0121121
0.0127542
0.0132673
0.0137554
0.0143684
0.0148995
0.0154036
0.0159997
0.0165907
0.0172408
0.0178499
0.018388
0.019089
0.0197701
0.0203572
0.0210393
0.0216564
0.0222324
0.0228305
0.0233166
0.0238667
0.0244387
0.0248918
0.0254159
0.025865
0.026158
0.0265131
0.0267652
0.0269333
0.0271824
0.0273214
0.0274515
0.0274626
0.0271257
0.0269957
0.0270148
0.0267899
0.026651
0.026427
0.0260381
0.0257212
0.0252253
0.0247254
0.0243314
0.0237925
0.0233076
0.0227997
0.0221607
0.0216288
0.0210079
0.020299
0.019702
0.0189881
0.0182382
0.0175053
0.0165944
0.0157524
0.0149355
0.0139746
0.0131167
0.0122307
0.0112948
0.0105009
0.00964397
0.00886105
0.00821613
0.0074542
0.00685928
0.00640136
0.00589444
0.00568351
0.00555559
0.00529467
0.00514074
0.00495682
0.0047789
0.00469697
0.00453005
0.00441613
0.0042912
0.00408328
0.00409536
0.00412444
0.00400951
0.00397959
0.00389367
0.00375074
0.00372082
0.0036819
0.00365497
0.00363905
0.00353413
0.00348721
0.00346528
0.00336936
0.00334044
0.00331251
0.00322459
0.00316767
0.00308874
0.00304882
0.0030859
0.00301798
0.00287005
How do I fit the two spectra to the original spectrum by extracting the coefficients from a least-squares linear fit? I use scipy.optimize.curve_fit to fit using gaussians, but now I need to just fit data.
When I scatterplot the data, it looks like three separate, somewhat asymmetrical peaks - I would think this requires one peak fit for each set of data. Please see my image and code below.
import numpy, matplotlib
import matplotlib.pyplot as plt
x_1 = numpy.array([1700.42, 1700.9, 1701.38, 1701.86, 1702.34, 1702.83, 1703.31, 1703.79, 1704.27, 1704.75, 1705.24, 1705.72, 1706.2, 1706.68, 1707.17, 1707.65, 1708.13, 1708.61, 1709.09, 1709.58, 1710.06, 1710.54, 1711.02, 1711.5, 1711.99, 1712.47, 1712.95, 1713.43, 1713.91, 1714.4, 1714.88, 1715.36, 1715.84, 1716.33, 1716.81, 1717.29, 1717.77, 1718.25, 1718.74, 1719.22, 1719.7, 1720.18, 1720.66, 1721.15, 1721.63, 1722.11, 1722.59, 1723.08, 1723.56, 1724.04, 1724.52, 1725, 1725.49, 1725.97, 1726.45, 1726.93, 1727.41, 1727.9, 1728.38, 1728.86, 1729.34, 1729.82, 1730.31, 1730.79, 1731.27, 1731.75, 1732.24, 1732.72, 1733.2, 1733.68, 1734.16, 1734.65, 1735.13, 1735.61, 1736.09, 1736.57, 1737.06, 1737.54, 1738.02, 1738.5, 1738.98, 1739.47, 1739.95, 1740.43, 1740.91, 1741.4, 1741.88, 1742.36, 1742.84, 1743.32, 1743.81, 1744.29, 1744.77, 1745.25, 1745.73, 1746.22, 1746.7, 1747.18, 1747.66, 1748.14, 1748.63, 1749.11, 1749.59, 1750.07, 1750.56])
y_1 = numpy.array([0.00285, 0.00289, 0.00290, 0.00292, 0.00297, 0.00304, 0.00310, 0.00314, 0.00319, 0.00323, 0.00327, 0.00333, 0.00340, 0.00344, 0.00347, 0.00352, 0.00358, 0.00364, 0.00369, 0.00374, 0.00382, 0.00388, 0.00392, 0.00397, 0.00403, 0.00408, 0.00414, 0.00420, 0.00428, 0.00436, 0.00444, 0.00451, 0.00456, 0.00461, 0.00468, 0.00474, 0.00480, 0.00486, 0.00493, 0.00501, 0.00509, 0.00517, 0.00524, 0.00530, 0.00535, 0.00543, 0.00551, 0.00558, 0.00564, 0.00571, 0.00578, 0.00587, 0.00594, 0.00599, 0.00607, 0.00615, 0.00623, 0.00631, 0.00636, 0.00645, 0.00657, 0.00666, 0.00673, 0.00682, 0.00688, 0.00695, 0.00704, 0.00713, 0.00722, 0.00732, 0.00741, 0.00750, 0.00758, 0.00768, 0.00777, 0.00783, 0.00788, 0.00792, 0.00795, 0.00799, 0.00803, 0.00806, 0.00807, 0.00804, 0.00800, 0.00795, 0.00787, 0.00779, 0.00767, 0.00753, 0.00737, 0.00719, 0.00699, 0.00677, 0.00652, 0.00626, 0.00599, 0.00572, 0.00546, 0.00519, 0.00492, 0.00465, 0.00437, 0.00413, 0.00391])
x_2 = numpy.array([1700.42, 1700.9, 1701.38, 1701.86, 1702.34, 1702.83, 1703.31, 1703.79, 1704.27, 1704.75, 1705.24, 1705.72, 1706.2, 1706.68, 1707.17, 1707.65, 1708.13, 1708.61, 1709.09, 1709.58, 1710.06, 1710.54, 1711.02, 1711.5, 1711.99, 1712.47, 1712.95, 1713.43, 1713.91, 1714.4, 1714.88, 1715.36, 1715.84, 1716.33, 1716.81, 1717.29, 1717.77, 1718.25, 1718.74, 1719.22, 1719.7, 1720.18, 1720.66, 1721.15, 1721.63, 1722.11, 1722.59, 1723.08, 1723.56, 1724.04, 1724.52, 1725, 1725.49, 1725.97, 1726.45, 1726.93, 1727.41, 1727.9, 1728.38, 1728.86, 1729.34, 1729.82, 1730.31, 1730.79, 1731.27, 1731.75, 1732.24, 1732.72, 1733.2, 1733.68, 1734.16, 1734.65, 1735.13, 1735.61, 1736.09, 1736.57, 1737.06, 1737.54, 1738.02, 1738.5, 1738.98, 1739.47, 1739.95, 1740.43, 1740.91, 1741.4, 1741.88, 1742.36, 1742.84, 1743.32, 1743.81, 1744.29, 1744.77, 1745.25, 1745.73, 1746.22, 1746.7, 1747.18, 1747.66, 1748.14, 1748.63, 1749.11, 1749.59, 1750.07, 1750.56])
y_2 = numpy.array([0.00182478, 0.00198449, 0.0021542, 0.00230491, 0.00248363, 0.00269334, 0.00289705, 0.00308676, 0.00330747, 0.00358919, 0.0038779, 0.00415561, 0.00444332, 0.00474103, 0.00507474, 0.00542346, 0.00576517, 0.00613688, 0.00651859, 0.0068873, 0.00727502, 0.00767773, 0.00808544, 0.00851815, 0.00894486, 0.00935658, 0.00979429, 0.010245, 0.0106727, 0.0110844, 0.0115191, 0.0119878, 0.0124556, 0.0128823, 0.013274, 0.0137237, 0.0142374, 0.0147181, 0.0151798, 0.0156495, 0.0160963, 0.016534, 0.0169657, 0.0173574, 0.0177211, 0.0180818, 0.0184125, 0.0187012, 0.0189339, 0.0191077, 0.0192454, 0.0193291, 0.0193638, 0.0193495, 0.0192672, 0.0191119, 0.0188696, 0.0185614, 0.0181941, 0.0176948, 0.0170465, 0.0162762, 0.0153449, 0.0142406, 0.0129863, 0.0115801, 0.0100468, 0.00844248, 0.00692419, 0.0055719, 0.00435861, 0.00340132, 0.00270704, 0.00213775, 0.00168046, 0.00134117, 0.00109188, 9.16595E-4, 7.80307E-4, 6.65019E-4, 5.62731E-4, 4.75443E-4, 4.42155E-4, 4.49867E-4, 4.29579E-4, 3.9929E-4, 3.83002E-4, 3.51714E-4, 3.38426E-4, 3.40138E-4, 3.2985E-4, 3.27562E-4, 3.24274E-4, 3.06986E-4, 2.92698E-4, 3.0041E-4, 3.12121E-4, 2.84833E-4, 2.47545E-4, 2.41257E-4, 2.34969E-4, 2.27681E-4, 2.47393E-4, 2.60105E-4, 2.25817E-4])
x_orig = numpy.array([1700.42, 1700.9, 1701.38, 1701.86, 1702.34, 1702.83, 1703.31, 1703.79, 1704.27, 1704.75, 1705.24, 1705.72, 1706.2, 1706.68, 1707.17, 1707.65, 1708.13, 1708.61, 1709.09, 1709.58, 1710.06, 1710.54, 1711.02, 1711.5, 1711.99, 1712.47, 1712.95, 1713.43, 1713.91, 1714.4, 1714.88, 1715.36, 1715.84, 1716.33, 1716.81, 1717.29, 1717.77, 1718.25, 1718.74, 1719.22, 1719.7, 1720.18, 1720.66, 1721.15, 1721.63, 1722.11, 1722.59, 1723.08, 1723.56, 1724.04, 1724.52, 1725, 1725.49, 1725.97, 1726.45, 1726.93, 1727.41, 1727.9, 1728.38, 1728.86, 1729.34, 1729.82, 1730.31, 1730.79, 1731.27, 1731.75, 1732.24, 1732.72, 1733.2, 1733.68, 1734.16, 1734.65, 1735.13, 1735.61, 1736.09, 1736.57, 1737.06, 1737.54, 1738.02, 1738.5, 1738.98, 1739.47, 1739.95, 1740.43, 1740.91, 1741.4, 1741.88, 1742.36, 1742.84, 1743.32, 1743.81, 1744.29, 1744.77, 1745.25, 1745.73, 1746.22, 1746.7, 1747.18, 1747.66, 1748.14, 1748.63, 1749.11, 1749.59, 1750.07, 1750.56])
y_orig = numpy.array([0.011507, 0.0121121, 0.0127542, 0.0132673, 0.0137554, 0.0143684, 0.0148995, 0.0154036, 0.0159997, 0.0165907, 0.0172408, 0.0178499, 0.018388, 0.019089, 0.0197701, 0.0203572, 0.0210393, 0.0216564, 0.0222324, 0.0228305, 0.0233166, 0.0238667, 0.0244387, 0.0248918, 0.0254159, 0.025865, 0.026158, 0.0265131, 0.0267652, 0.0269333, 0.0271824, 0.0273214, 0.0274515, 0.0274626, 0.0271257, 0.0269957, 0.0270148, 0.0267899, 0.026651, 0.026427, 0.0260381, 0.0257212, 0.0252253, 0.0247254, 0.0243314, 0.0237925, 0.0233076, 0.0227997, 0.0221607, 0.0216288, 0.0210079, 0.020299, 0.019702, 0.0189881, 0.0182382, 0.0175053, 0.0165944, 0.0157524, 0.0149355, 0.0139746, 0.0131167, 0.0122307, 0.0112948, 0.0105009, 0.00964397, 0.00886105, 0.00821613, 0.0074542, 0.00685928, 0.00640136, 0.00589444, 0.00568351, 0.00555559, 0.00529467, 0.00514074, 0.00495682, 0.0047789, 0.00469697, 0.00453005, 0.00441613, 0.0042912, 0.00408328, 0.00409536, 0.00412444, 0.00400951, 0.00397959, 0.00389367, 0.00375074, 0.00372082, 0.0036819, 0.00365497, 0.00363905, 0.00353413, 0.00348721, 0.00346528, 0.00336936, 0.00334044, 0.00331251, 0.00322459, 0.00316767, 0.00308874, 0.00304882, 0.0030859, 0.00301798, 0.00287005])
plt.plot(x_1, y_1, 'o')
plt.plot(x_2, y_2, 'o')
plt.plot(x_orig, y_orig, 'o')
plt.xlabel('X Data') # X axis data label
plt.ylabel('Y Data') # Y axis data label
plt.show()

Resources