Skip to content

Instantly share code, notes, and snippets.

@qqrs
Created November 18, 2012 22:20
Show Gist options
  • Save qqrs/4107845 to your computer and use it in GitHub Desktop.
Save qqrs/4107845 to your computer and use it in GitHub Desktop.
Uses matplotlib to plot linearity error in softpot output with pulldown resistor
# coding=utf-8
#!ipython --pylab
from pylab import *
def format_resistor_value(value):
value = int(value) * 1000
for unit in (u"Ω", u"kΩ", u"MΩ", u"GΩ"):
value = value / 1000
if value < 1000:
break
return "%d %s" % (value, unit)
fig = figure()
fig.suptitle("SoftPot with pulldown: error relative to linear response")
plot1 = fig.add_subplot(2,1,1)
plot2 = fig.add_subplot(2,1,2)
plot1.set_xlabel("Position (x)")
plot1.set_ylabel("Vout/Vref")
plot2.set_xlabel("Position (x)")
plot2.set_ylabel("Error (%)")
x = np.linspace(0, 1, 100, endpoint=True)
x[0] = x[1] / 100 # prevent divide by zero
for Ro_val in (1000, 10000, 100000, 1000000):
Ro = np.linspace(Ro_val,Ro_val, len(x), endpoint=True)
R1 = 10000 * x
R2 = 10000 * (1-x)
Rp = 1/(1/R1 + 1/Ro)
Vout = Rp / (Rp+R2)
Videal = R1/(R1+R2)
error = 100*abs(Vout - Videal)/Videal
plot1.plot(x, Vout, label="Pulldown Ro=%s" % format_resistor_value(Ro_val))
plot2.plot(x, error)
print "Ro = %6s max error = %4.1f%%" % (format_resistor_value(Ro_val),
max(error))
plot1.legend(loc="upper left", prop={"size":12})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment