Shop OBEX P1 Docs P2 Docs Learn Events
Interpolation/linearization — Parallax Forums

Interpolation/linearization

ArchiverArchiver Posts: 46,084
edited 2001-02-15 13:33 in General Discussion
I am trying to use the Basic 2 stamp to perform curve fitting of a
non-linear function.

I am generating an analog voltage proportional to a measurement that
will have 40 steps. This voltage is then fed into either a 8 bit or
12 bit A/D. I then intend to use the PWM function to then generate a
0 to 5 volt linearized output.

The output range of the A/D is 78 to 116 or 1261 to 1858, depending
on the A/D used. I have thought about using the lookup and lookdown
function. The problem is that if the 8 bit A/D is used, due to
rounding errors I will obtain 2 bit patterns. If I use the 12 Bit A/D
the lookup/lookdown will exceed the 255-element size.

I think interpolation is the way to go. But due to the non-linear
function I have not been able to my routine to work. Has any one
written a routine that I may be able to use?

Please any help is appreciated.

John

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-02-13 17:02
    Hi John,

    Maybe the following URL would help. Look at "tables and interpolation"

    http://www.emesystems.com/BS2math3.htm

    - Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2001-02-14 13:50
    Tracy,

    Thanks for your input.

    My question with your routine is how can you make it
    work when the width of the catagories 'K' is not
    evenly spaced, as in your example.

    John


    --- Tracy Allen <tracy@e...> wrote:
    > Hi John,
    >
    > Maybe the following URL would help. Look at "tables
    > and interpolation"
    >
    > http://www.emesystems.com/BS2math3.htm
    >
    > - Tracy
    >
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Get personalized email addresses from Yahoo! Mail - only $35
    a year! http://personal.mail.yahoo.com/
  • ArchiverArchiver Posts: 46,084
    edited 2001-02-14 19:39
    >>John wrote:
    >>My question with your routine is how can you make it
    >>work when the width of the catagories 'K' is not
    >>evenly spaced, as in your example.
    >
    >>--- Tracy Allen <tracy@e...> wrote:
    >> > Hi John,
    >> >
    >> > Maybe the following URL would help. Look at "tables
    >> > and interpolation"
    >> >
    >> > http://www.emesystems.com/BS2math3.htm
    >> > - Tracy
    >John wrote:
    >>>I am trying to use the Basic 2 stamp to perform curve fitting of a
    >>>non-linear function.
    >>>
    >>>I am generating an analog voltage proportional to a measurement that
    >>>will have 40 steps. This voltage is then fed into either a 8 bit or
    >>>12 bit A/D. I then intend to use the PWM function to then generate a
    >>>0 to 5 volt linearized output.
    >>>
    >>>The output range of the A/D is 78 to 116 or 1261 to 1858, depending
    >>>on the A/D used. I have thought about using the lookup and lookdown
    >>>function. The problem is that if the 8 bit A/D is used, due to
    >>>rounding errors I will obtain 2 bit patterns. If I use the 12 Bit A/D
    >>>the lookup/lookdown will exceed the 255-element size.
    >>>
    >>>I think interpolation is the way to go. But due to the non-linear
    >>>function I have not been able to my routine to work. Has any one
    >>>written a routine that I may be able to use?

    Interpolation is best done with the READ command, using data you have
    stored as DATA using two tables, breakpoints for the input and
    output. It is possible to do it using LOOKDOWN and LOOKUP,but I have
    found that implementation using those commands will eat up a lot, lot
    more memory. You need to do this with the 12-bit ADC, or else there
    will be no grist for the interpolation. The following example uses
    just 5 points, purely random values on my part. You would want more
    breakpoints.

    Xtable DATA word 1261, word 1356, word 1555, word 1781, word 1858
    Ytable DATA 0, 37, 145, 195, 255
    ' note entries in Xtable are the input breakpoints
    ' for PWM breakpoints to output in the Ytable
    Ntries DATA 4 ' number of table entries minus 1
    X var word ' input data 12 bits from ADC
    X0 var word ' breakpoints in Xtable
    X1 var word
    Y0 var byte ' breakpoints in Ytable
    Y1 var byte
    Z var byte ' interpolated value for PWM
    i var byte ' index for table lookup


    loop:
    gosub getX ' get X data 12 bits, not shown
    X = X min 1261 max 1858 ' clamp the X value
    X0=1261 ' initial value for interpolation
    for i=1 to Ntries
    ' need two reads to get a word variable:
    read Xtable+(i*2),X1,byte0
    read Xtable+(i*2)+1,X1,byte1
    if X<=X1 then foundcatagory
    X0=X1
    next
    foundcatagory:
    ' now read the corresponding Y values (bytes for PWM):
    read Ytable+i-1,Y0
    read Ytable+i,Y1
    ' interpolate:
    Z= (Y1-Y0)*(X-X0)/(X1-X0)+Y0
    PWM 0,Z,255 ' make PWM on P0
    goto loop

    There is probably a typo or error in there somewhere, but I think
    that is pretty close. It is important that the product
    (Y1-Y0)*(X-X0) has to be less than 65536. That is not a problem
    here, but it is a caveat depending on the input and output data
    requirements.

    I hope that helps,

    -- Tracy Allen
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-02-15 13:33
    Tracy,

    Thanks for you help.

    John
    --- Tracy Allen <tracy@e...> wrote:
    > >>John wrote:
    > >>My question with your routine is how can you make
    > it
    > >>work when the width of the catagories 'K' is not
    > >>evenly spaced, as in your example.
    > >
    > >>--- Tracy Allen <tracy@e...> wrote:
    > >> > Hi John,
    > >> >
    > >> > Maybe the following URL would help. Look at
    > "tables
    > >> > and interpolation"
    > >> >
    > >> > http://www.emesystems.com/BS2math3.htm
    > >> > - Tracy
    > >John wrote:
    > >>>I am trying to use the Basic 2 stamp to perform
    > curve fitting of a
    > >>>non-linear function.
    > >>>
    > >>>I am generating an analog voltage proportional to
    > a measurement that
    > >>>will have 40 steps. This voltage is then fed into
    > either a 8 bit or
    > >>>12 bit A/D. I then intend to use the PWM function
    > to then generate a
    > >>>0 to 5 volt linearized output.
    > >>>
    > >>>The output range of the A/D is 78 to 116 or 1261
    > to 1858, depending
    > >>>on the A/D used. I have thought about using the
    > lookup and lookdown
    > >>>function. The problem is that if the 8 bit A/D is
    > used, due to
    > >>>rounding errors I will obtain 2 bit patterns. If
    > I use the 12 Bit A/D
    > >>>the lookup/lookdown will exceed the 255-element
    > size.
    > >>>
    > >>>I think interpolation is the way to go. But due
    > to the non-linear
    > >>>function I have not been able to my routine to
    > work. Has any one
    > >>>written a routine that I may be able to use?
    >
    > Interpolation is best done with the READ command,
    > using data you have
    > stored as DATA using two tables, breakpoints for the
    > input and
    > output. It is possible to do it using LOOKDOWN and
    > LOOKUP,but I have
    > found that implementation using those commands will
    > eat up a lot, lot
    > more memory. You need to do this with the 12-bit
    > ADC, or else there
    > will be no grist for the interpolation. The
    > following example uses
    > just 5 points, purely random values on my part. You
    > would want more
    > breakpoints.
    >
    > Xtable DATA word 1261, word 1356, word 1555,
    > word 1781, word 1858
    > Ytable DATA 0, 37, 145,
    > 195, 255
    > ' note entries in Xtable are the input
    > breakpoints
    > ' for PWM breakpoints to output in the Ytable
    > Ntries DATA 4 ' number of table entries minus
    > 1
    > X var word ' input data 12 bits from ADC
    > X0 var word ' breakpoints in Xtable
    > X1 var word
    > Y0 var byte ' breakpoints in Ytable
    > Y1 var byte
    > Z var byte ' interpolated value for PWM
    > i var byte ' index for table lookup
    >
    >
    > loop:
    > gosub getX ' get X data 12 bits, not shown
    > X = X min 1261 max 1858 ' clamp the X value
    > X0=1261 ' initial value for interpolation
    > for i=1 to Ntries
    > ' need two reads to get a word variable:
    > read Xtable+(i*2),X1,byte0
    > read Xtable+(i*2)+1,X1,byte1
    > if X<=X1 then foundcatagory
    > X0=X1
    > next
    > foundcatagory:
    > ' now read the corresponding Y values (bytes
    > for PWM):
    > read Ytable+i-1,Y0
    > read Ytable+i,Y1
    > ' interpolate:
    > Z= (Y1-Y0)*(X-X0)/(X1-X0)+Y0
    > PWM 0,Z,255 ' make PWM on P0
    > goto loop
    >
    > There is probably a typo or error in there
    > somewhere, but I think
    > that is pretty close. It is important that the
    > product
    > (Y1-Y0)*(X-X0) has to be less than 65536. That is
    > not a problem
    > here, but it is a caveat depending on the input and
    > output data
    > requirements.
    >
    > I hope that helps,
    >
    > -- Tracy Allen
    > http://www.emesystems.com
    >
    >
    >
    >
    >
    >
    >
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Get personalized email addresses from Yahoo! Mail - only $35
    a year! http://personal.mail.yahoo.com/
Sign In or Register to comment.