changeset 199:ca9d9104afba

added utility to calibrate polynoms and plot
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 20 Feb 2012 19:32:27 -0500
parents c91c8fd8bf1e
children 0a27fa343257
files python/utils.py
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/utils.py	Thu Feb 16 18:59:12 2012 -0500
+++ b/python/utils.py	Mon Feb 20 19:32:27 2012 -0500
@@ -187,6 +187,24 @@
     result[-halfWidth:] = inputSignal[-halfWidth:]
     return result
 
+def linearRegression(x, y, deg = 1, plotData = False):
+    '''returns the least square estimation of the linear regression of y = ax+b
+    as well as the plot'''
+    from numpy.lib.polynomial import polyfit
+    from matplotlib.pyplot import plot
+    from numpy.core.multiarray import arange
+    coef = polyfit(x, y, deg)
+    if plotData:
+        def poly(x):
+            result = 0
+            for i in range(len(coef)):
+                result += coef[i]*x**(len(coef)-i-1)
+            return result
+        plot(x, y, 'x')
+        xx = arange(min(x), max(x),(max(x)-min(x))/1000)
+        plot(xx, [poly(z) for z in xx])
+    return coef
+
 #########################
 # plotting section
 #########################