Reputation: 23
I have the problem to take the derivative of some experimental data. I tried to smooth the data I got because the original derivative made no sense to me. I attached a picture with my experimental data and a theoretical curve how the data should look like. Then I took the derivative of the experimental and the theoretical data for comparison. This is my current code:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import scipy.stats as stats
import math
from scipy.optimize import curve_fit
bias = pd.read_csv('bias_voltage_2024_12_12__13_17_47.csv', delimiter=',', header=None)
data1 = pd.read_csv('sipm_dark_DD_microfc_v1_2024_12_12__14_57_27.csv', delimiter=',', header=None)
bias_voltage = bias[1][:425].values
dark_current = data1[1]
filt = np.ones(15)/15
smooth = np.convolve(dark_current, filt, 'valid')
deriv = np.gradient(smooth, bias_voltage[7:-7])
x = np.linspace(0, 32, 100)
mu = 25.4
y = np.where(x < mu, 0, 0.0002*(x - 8) * (x - mu))
deriv2 = np.gradient(y, x)
#plt.scatter(bias_voltage[7:-7], deriv, s=8, label='derivative')
plt.scatter(x, deriv2, s=8, label='theoretical data')
plt.xlabel('Bias Voltage (V)')
plt.ylabel('Current (A) / Derivative of Current (A/V)')
plt.legend()
plt.savefig('theo_deriv.png')
plt.show()
The last image shows roughly how the derivative of the experimental data should look like . To smooth the data did not worj for me until now. Am I doing something wrong?
Here the link with the two csv files used in my example code: experimental data files
Upvotes: 0
Views: 75
Reputation: 2055
Because of the noise in the experimental data, I thought it would be easier to work with np.interp() to interpolate the data:
x = np.linspace(0, 32, 100)
interCurve = np.interp(x, bias_voltage, dark_current)
derivB = np.gradient(interCurve[:-1], x[:-1])
plt.plot(x, interCurve, label='interpolated curve')
plt.scatter(bias_voltage, dark_current, marker='x', color='g', s=6, label='experimental points')
plt.plot(x[:-1], derivB, label='derivative of interpolated curve')
plt.legend()
plt.show()
Upvotes: 0