Shop OBEX P1 Docs P2 Docs Learn Events
Complex SX math (is this possible) — Parallax Forums

Complex SX math (is this possible)

edited 2006-02-03 15:12 in General Discussion
I need to calculate a ratio and then figure out from that ratio what the value of the data is based upon the known quotient data that the ratio was derived from.
·
I am curious as to what the best way to approach this in ASM is. Even if someone can suggest a way in SX/B so I can get the idea I should be able to get the theory working and port it over to ASM.
·
For an example, if the ratio is based on lets say a quotient of 64.000. I need to calculate what 1 : 0.75 of 64.000 is or 75% which would be 48.000. The quotient that the data will be derived from will always be a whole number.
·
The data that is collected is two bytes in a 5.11 format. Where the first 5 bits represent the integer portion of the ratio and the following 11 bits represents the fraction portion.
·
Ratio = 1 : IIIII.FFFFFFFFFFF······················ ;I = Integer, F = Fraction
·
Example 1:0.75 = %0000011000000000
·
Resolution of each value
F = 0 to 31
I = 2048/0 to 2047· or· ·································· ;approximately 0.0005 to 0.9995
·
I can separate the Integer and Fraction by a simple masking and rotation of the bits into 3 bytes. The First byte holds the integer. Second byte holds the upper bits of the fraction and the Third byte holds the lower 8 bits of the fraction.
·
I do not need the full 11 bit resolution of the fraction and would be happy with an 8 bit fraction as this would obviously make the calculations much easier. Would simple truncation of the 3 LSB’s work?
·
Where I = 255/0 to 254·································· ;approximately 0.0039 to 0.9961
·
Also the largest output value in my application of the Integer would be 215 so there is no worry of overflowing an 8 bit value on the Integer portion of the output.
·
I do not need the Ratio just the final number (such as 48.000 in my example). Number of clock cycles is not a huge factor as this is a non time critical calculation and I should have a whole page or two available for code and a bank or two of RAM if need be in an SX48.
·
Is this even possible with the limited math functions of the SX?
·
I’m not asking for code just curious on theories on how this could be done.
·
-Dan-

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-02-02 11:26
    I suppose that you could do anything you mathematically want with the SX if you allow yourself enough time.

    And therein lies the dilemma - time.

    If you want the SX to be a dedicated math co-processor it is only 8 bit and there is a perfectly good 32 bit co-processor sold by Parallax.

    If you want to NOT use a coprocessor, it depends entirely on the timing of the task.

    Most of the problem is time consumed in making an 8 bit device handle·32 bit mathematics. You need to move 4 bytes about to get each denominator and numerator into an appropriated position.

    Then because all multiply and divide is binary [noparse][[/noparse]done by shift left and shift right], these 'shift functions' must carry through 4 registers. A lot of housekeeping.

    Of course, there is your pesky little decimal point. That decimal point is Base 10, not Binary. So, you must convert all your decimal numbers to binary before you do the math rather than just try to convienently 'pop in a decimal point'. That requires another set of registers and additional math to keep track to the Decimal point.

    Seems that you are trying to mix Decimal and Binary operations.

    Is that enough theory?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan

    Post Edited (Kramer) : 2/2/2006 11:32:21 AM GMT
  • BeanBean Posts: 8,129
    edited 2006-02-02 13:14
    What is the range of the "quotient" ?
    Your example says 64.000.
    So does that mean that the quotient has a fractional part also ?
    Is it also 5.11 format ?
    Assuming that the quotient is 5.11 also, you want to do a 16x16 multiply and the 32-bit result will be 10.22 format.

    SXList has two programs for 16x16 mult: http://www.sxlist.com/techref/ubicom/lib/math/mul.htm

    [noparse][[/noparse]edit] Oops, just realized that 64.000 will not fit a 5.11 format.·So what format is it ?

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."


    Post Edited (Bean (Hitt Consulting)) : 2/2/2006 1:26:12 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-02 15:07
    Anything is possible, as much as youd like to stay in the theoretical, when you are talking about assembly, the most expediant method of calculation requires discussing the particulars of the algorithm. Can you post a formula of the calculation you want to do, explain the format for each term (are they all 5.11 fixed point or do some terms have a different format), and whether each term is a constant or a variable (and state the value for any constant). All of these factors will affect what the most efficient means for calculation will be.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-02-02 16:09
    BTW, aren't 'complex numbers' supposed to be those nasty ones that have an imaginary value?
    For example X = Y + Zi

    Such math is used for calculating Impedance rather than Resistance in filters and tuned circuits.

    I don't think you really what to do that on an SX chip.

    It really comes down to the 'mission' of the microcontroller is not to be a numerical calculator. It has all those 'autonomous' I/O pins to control things or distribute data serially in several directions.

    It can do it. It can teach you how to do such programing. But starting with your 'formating idea' is really a hard way to learn. You should do some 8 bit math, then some 16 bit math, and so on. Walk before you run. In this way you will appreciate the limitations and ability to use data tables for repetative tasks rather than brute force calculations. The coding is much simpler too.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-02 17:05
    I think he only means complex math, as in complicated math.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • edited 2006-02-02 17:48
    Bean (Hitt Consulting) said...
    What is the range of the "quotient" ?
    Your example says 64.000.
    So does that mean that the quotient has a fractional part also ?
    Is it also 5.11 format ?

    I have a total of 6 static quotients 4 of which are whole numbers with no fractional and 2·others that end in .1 and .2
    ·
    The simplest form of the calculations is as follows
    ·
    f/2048 + i * q = n
    ·
    Where f = fractional, I = integer, q = quotient and n being the desired result.

    I’d be happy with a result that went out only to the hundredths.
    ·
    I’ve been using stamps for over 5 years and just got on board with the SX about a year ago and am familiar with most math functions on the SX but the simplest way to handle this to me is unclear.

    Seeing now the math is not that complex however dealing with the large fractional and decimal point is.

    Thanks for all the feedback!

    -Dan-

    Post Edited (Direct Digital Labs) : 2/2/2006 5:53:34 PM GMT
  • BeanBean Posts: 8,129
    edited 2006-02-02 17:56
    Dan,
    I assume you meant "(f/2048 + i) * q = n" ?

    What are the 6 static quotents ? What is the range of i ?

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."
    ·
  • edited 2006-02-02 18:02
    Bean (Hitt Consulting) said...
    Dan,
    I assume you meant "(f/2048 + i) * q = n" ?

    What are the 6 static quotents ? What is the range of i ?

    Bean.

    Yes you are correct
    "(f/2048 + i) * q = n"

    the 6 statics "q"·are· 32, 44.1, 48, 64, 88.2, 96

    The range of "i" is 0 to 6

    -Dan-
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-02 18:17
    The 44.1 and 88.2 quotients are the most problematic, what degree of error (in percentage) is acceptable in the final calculation for n?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • edited 2006-02-02 18:28
    Paul Baker said...
    The 44.1 and 88.2 quotients are the most problematic, what degree of error (in percentage) is acceptable in the final calculation for n?

    I'd say +/- 5% would be acceptable. Also as stated before I'd be willing to truncate the fractional value to 8-bits if it would simplify things.

    -Dan-
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-02 18:38
    Accepting that level of error means that the .1 and the .2 don't affect the result, using a quotient of 44 and 88 results in a 0.226757 % error in both equations.

    http://www.piclist.com/techref/piclist/codegen/constdivmul.htm·will generate code for you to do the equation in the shortest amount of time (be sure to select the SX for the processor). Depending on how the quotients are used, you can reduce the amount of code by recognising that many of the quotients are multiples of 2 of other quotients, meaning you can use code that does a (f/2048 + i)* 44 and shift the result to the left once to perform the same calculation of the quotient 88.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-02-02 19:19
    Rewrite your equation as

    (f + 2048*i) * q = 2048*n

    n = ((f + (i<<11))*q)>>11

    Simple integer add, multiply and shifts!
    Like Paul said, q·being integer only gives little error

    regards peter
  • edited 2006-02-02 19:33
    Brilliant! Now that is what a forum is for! Between all your suggestions I should be able to lick this thing in no time.
    Thank you very much for your time.

    -Dan-
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-02-02 19:44
    Simplify further

    n = ((f*q)>>11) + (i*q)

    n = (((f>>3)*q)>>8) + (i*q)

    max value: (((2048>>3)*96)>>8) + (6*96) = (24576>>8) + (576) = 672
    16bit math should suffice

    regards peter
  • edited 2006-02-02 20:20
    Peter Verkaik said...

    n = (((f>>3)*q)>>8) + (i*q)

    I'll go with this formula as it requires the least amount of bits to work with.
    ·
    Thanks again!
    ·
    -Dan-
  • BebopALotBebopALot Posts: 79
    edited 2006-02-03 13:40
    I didn't know Parallax offered a 32 bit co-processor. I have an app that needs Sin, Cos, Tan functions from SX renedered data - can the Parallax processor handle these functions?

    Thanks, BBAL
  • BeanBean Posts: 8,129
    edited 2006-02-03 13:52
    Yes it will do Sin, Cos, and Tan and alot more.
    Here is the link to it: www.parallax.com/detail.asp?product_id=604-00030a

    I'm not sure about Tan, but I know you can do SIN, COS, and ATAN with CORDIC functions.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."


    Post Edited (Bean (Hitt Consulting)) : 2/3/2006 1:56:07 PM GMT
  • BebopALotBebopALot Posts: 79
    edited 2006-02-03 15:12
    Bean that is just too cool. I mean, that is awesome.... I'll make it my "SX assistant" or "SXA..." I'll give it a shot.

    Danke!

    BBAL
Sign In or Register to comment.