Sensor Calibration with Python’s Jupyter Notebook and the linear regression model

November 5, 2017 Oliver Prislan

Revision for “Sensor Calibration with Python’s Jupyter Notebook and the linear regression model” created on 5. November 2017, 13:55:04

Titel
Sensor Calibration with Python's Jupyter Notebook and the linear regression model
Inhalt
This example shows how Jupyter Notebook can be used to calibrate the metering of current. To install Jupyter follow the instruction on https://www.anaconda.com/download/ Let's assume we have a current sensor connected to an Ardunio performing the analog reading of the sensor and converting it into a digital signal that is sent to a Raspberry Pi via serial connection. <img class="aligncenter wp-image-700" src="http://prismatec.eu/wp-content/uploads/2017/11/sensorCalibCurrent.jpg" alt="" width="449" height="850" /> An analog sensor provides a contigeous set of values. The Arduino analog-digital converter can only represent values of 0 to 1023 (10bit) in the range [0..5]V. Zero Volt repersents "0" and 5V represents 1023. Applying a factor can turn this voltage into a corresponding current. Determining the factor requires a comparisson of readings with a 2nd source. Here, this is a clamp ampere meter. We created a text file with a list pairs of metered values (arduino / clamp ampere meter):<code> Phase Arduino AmpereMeter 0 L1    0.40     0.41 1 L1    6.40     8.80 2 L2    0.22     0.30 3 L2    0.36     0.47 4 L2    0.65     0.94 5 L2    4.60     6.90 6 L2    6.10     8.30 7 L3    0.70     1.10 8 L3    1.30     1.90 9 L3    5.00     6.90 10 L3   7.60    10.20 </code> This data is used in the script below: <code> %matplotlib inline import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn import datasets, linear_model import re <code> df = pd.read_csv("d:/data/ems/messung_enmon/Ard_vs_Amp.txt") x = df.Arduino y = df.AmpereMeter n = df.Arduino.count() x = x.values.reshape(n, 1) y = y.values.reshape(n, 1) plt.scatter(x,y, label='metered values', color='red') <code> regr = linear_model.LinearRegression() regr.fit(x, y) m = re.sub('[\[\]]', '', str(regr.coef_)) b = re.sub('[\[\]]', '', str(regr.intercept_)) func = "f(x) = "+ m +" * x + " + b <code> plt.plot(x, regr.predict(x), color='blue', linewidth=2, label=func) plt.legend() plt.show() <code> </code></code></code></code></code> <img class="aligncenter wp-image-700" src="http://prismatec.eu/wp-content/uploads/2017/11/linearRegessionCurrent.png" alt="" width="600" height="400" /> the X-Axis represents current sensorvalues, the Y-axis the metered values of the clamp-ampere-meter. The slope of the regression line m = 1.36 is used as calibration factor. An offset of b = 0.05 can be subtracted. The result will be better more values are metered with a finer resolution. Non-linear curves can be fitted with Python's more advanced "Fitting"-algorithms.
Auszug


OldNewDate CreatedAuthorActions
5. November 2017, 13:55:04 Oliver Prislan
5. November 2017, 13:53:49 Oliver Prislan
5. November 2017, 13:47:59 Oliver Prislan
5. November 2017, 13:46:24 Oliver Prislan
5. November 2017, 13:38:45 Oliver Prislan
5. November 2017, 13:38:10 Oliver Prislan
5. November 2017, 13:36:21 Oliver Prislan
5. November 2017, 13:25:35 Oliver Prislan
5. November 2017, 12:49:20 Oliver Prislan
5. November 2017, 12:47:10 Oliver Prislan
5. November 2017, 12:46:41 [Automatische Speicherung] Oliver Prislan
5. November 2017, 12:44:55 Oliver Prislan
5. November 2017, 12:33:34 Oliver Prislan