I am working with 10,000 data points from a .csv file and need to fit constant functions to two specific ranges (of time here) so that I can average their y-intercepts and subtract that from the y-values of the data (voltage in this case).
My fits, range1fit and range2fit, are apparently of size 1 and I am getting a size error when I try to plot the trendlines because of a size difference between the elements I am trying to plot.
Here is my full code:
import numpy as np
import pandas
import matplotlib.pyplot as plt
import scipy.stats as sps
# r1: run 1, r2: run 2, etc
r1 = pandas.read_csv("9Vrun1.csv")
r2 = pandas.read_csv("9Vrun2.csv")
r3 = pandas.read_csv("9Vrun3.csv")
r4 = pandas.read_csv("9Vrun4.csv")
r5 = pandas.read_csv("9Vrun5.csv")
r = (r1 + r2 + r3 + r4 +r5)/5
time = r["TIME"]
voltage = r["CH1"]
n = 10E3 # number of recordings per sec
# ranges on flat areas either side of the peak
range1t = time[time.between(-0.0572061,0.016112)]
range1v = voltage[time.between(-0.0572061,0.016112)]
range2t = time[time.between(0.0737799,0.142302)]
range2v = voltage[time.between(0.0737799,0.142302)]
# fit ranges with constant lines
range1fit = np.polyfit(range1t,range1v,0)
range2fit = np.polyfit(range2t,range2v,0)
plt.plot(time, voltage)
plt.plot(range1t, range1fit)
plt.plot(range2t, range2fit)
plt.title('Voltage vs. Time with Target (power supply range: [-9.0, 9.0 V])' )
plt.xlabel('Time (s)')
plt.ylabel('Voltage (V)')
plt.show()
Any advice as to how to proceed would be greatly appreciated!