This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

polynomial function in msp430f155??

Other Parts Discussed in Thread: MSP430F155

hi all is it possible to use a polynomial funtction to msp430f155 like this way

y = a+bx+cx2+dx3+ex4.. is there any special mathematical library is available for doing a polynomial function..please guide me

 

  • I don't know of a library for that - but yes you can calculate polynomials (x2 = x^2, that is x squared - I guess?).

    For this kind of computation you might want to have a look at the Horner scheme as it's more efficient than the standard formula.

  • hi my polynomial will be in the following format

    y =28.616+(-19.269*x)+(5.9017 * x^2)+(-0.87359*x^3)+(0.047738*x^4)

    where x = 6.014...

    kindly suggest me the fastest method to do this function in msp430f155 controller

  • Well it depends on your needs mainly - do you need floating point numbers or can it be done with fixed point numbers? Fixed point will be faster but you have to be careful with range checking and the resolution you need (basically there is a tradeoff between resolution and range), for fixed point you could use a hardware multiplier (but it's not available on MSP430F155). Floating point will be slower but it will be easier to implement (the floating point library will take care of the difficult things).

    A floating point implementation can be as simple as:

    float x = 6.104f;
    float y = (((((((0.047738f * x) - 0.87359f) * x) + 5.9017f) * x) - 19.269f) * x) + 28.616f;

    This will take quite a lot cycles and also considerable amount of flash (652 bytes in a minimal example I just tried) .

    Fixed point numbers will be faster and need less space - but to implement those you must specify the range of x first and the needed resolution - then a fitting fixed point format can be chosen and the calculation can be implemented.

    If fixed point is entirely new for you, it's basically integer calculations but with a scaling factor. So for example 6.104 could be expressed as 25002 as a Q3.12 number (12 fractional bits) that is to get the value you have in mind you have to divide by 2^12=4096, so it's 25002/4096 = 6.10400390625 so it's not exact, the least significant bit is 1/4096=0.000244140625. The range is -8 to 7.999755859375. If you want to get finer resolution you have to switch to another Q-format, but then the range decreases (if you stick to 16 bit).

    So to get range and resolution right, you have to tell us more about your application.

     

  • The MSPs 32 bit hardware multiplyer supports fractional integers as well as multiply-accumulate operations.
    So using Q-numbers is not only faster than float, it is MUCH faster. :)
    However, there is no library that does provide prebuilt functions for fractinal number handling.
    It needs to be done in C or better assembly language for best performance.

    But indeed, the range of x must be known and the error margin must be checked.

  • hi am very new to the fixed point calculation as well as programing.. so kindly send me any pdf link about fixed point calculation

  • karthik raja said:

    hi am very new to the fixed point calculation as well as programing.. so kindly send me any pdf link about fixed point calculation

     Fixed point calculus cannot be fit in a PDF document, is computer science so you can find some book from amazon or elsewhere in a bookstore here some title and one in particular i own from time of graduation.

    http://www.amazon.com/s/ref=nb_sb_ss_i_5_12?url=search-alias%3Daps&field-keywords=fixed+point+arithmetic&sprefix=fixed+point+

    http://www.amazon.com/Arithmetic-Cambridge-Monographs-Computational-Mathematics/dp/0521194695/ref=sr_1_7?ie=UTF8&qid=1322921304&sr=8-7

     Art of fixed point can help do thing requiring more calculus power on low budget uP and is Hacking art and skill.

     Regards

     Roberto

  • Jens-Michael Gross said:

    The MSPs 32 bit hardware multiplyer supports fractional integers as well as multiply-accumulate operations.
    So using Q-numbers is not only faster than float, it is MUCH faster. :)
    However, there is no library that does provide prebuilt functions for fractinal number handling.
    It needs to be done in C or better assembly language for best performance.

    But indeed, the range of x must be known and the error margin must be checked.

     Hi Jens, msp430F155 has no Hardware multiplier @all. Also F16x part has only integer multiplier accumulator, from datasheet:

    hardware multiplier (MSP430F16x/161x only)
    The multiplication operation is supported by a dedicated peripheral module. The module performs 16x16,
    16x8, 8x16, and 8x8 bit operations. The module is capable of supporting signed and unsigned multiplication
    as well as signed and unsigned multiply and accumulate operations. The result of an operation can be accessed
    immediately after the operands have been loaded into the peripheral registers. No additional clock cycles are
    required.

     I hope burdening forum is a bad way to help people, sorry to wrote this note but I see too much disorder and I love programming and uP coding disrupted from who just post and post....

     My MSP code was inspired from TI examples then completely rewrote and debugged to my best as reusable library. Don't waste time and resource from community

    Here your biography:

    2010 MSP430 Contributor of the Year
    First (and for 6 months only) Guru member in the TI E2E community.

    If you have an MSP430-related question, don't hesitate to ask.
    But sorry, while I'm glad to be helpful, I don't have the time to (nor do I want to) provide complete code solutions for free.

     Regards

     Roberto

  • Roberto Romano said:
    Hi Jens, msp430F155 has no Hardware multiplier @all.


    You're right. From my memory, I thought that the difference between 15x and 16x was the second USART (and the tiemr B3/TimerB7), while the HWM was the difference between the 13x/14x and the 15x/16x, but you're right, the 155 doesn't have one.
    However, using the HWM may speed calculations up significantly. And even the 16 bit HWM can be used for fractional digits. After all, there is no difference in halndling between normal integers or longs and Q numbers. Just the required shift after a multiplication/division.
    And updating from 155 to 167 does only cost <$0,50 more (single unit, Digi-key) for greatly improved multiplication performance and twice the flash and ram. Same footprint and pinout.
    Or the 147, which is a 167 without DAC, BOR, I2C and SVS (but with HWM), but for a significantly lower price. Same pinout too.

    For new designs, I'd recommend the 5418A, as it is significantly cheaper than the 15x/16x (unless you need the DAC or the smaller footprint). The 1x series, while not obsolete, is not recommended for new designs. And not the suitable processor for mathematical calculations. Especially the types without HWM.

    Roberto Romano said:
    Also F16x part has only integer multiplier accumulator

    Here is a big msiunderstanding. While he 16xx HWM does indeed only perform 16x16 operations at max, it will produce a 32 bit result (unlike a standard C int multiplication, which requires casting the operands to 32 bit before to get a 32 bit result).

    So unless you need 64 bit results, the 16 bit HWM is suitable for many more uses if NOT using the internal C multiplication mechanisms. This is a limitation of C language, and not the HWM itself.

    Roberto Romano said:
    My MSP code was inspired from TI examples then completely rewrote and debugged to my best as reusable library.


    The TI examples have proven not very useful in the past, if you didn't already understand (at least roughly) how things work. And if you do, you usually do not need them. So the only example code I ever used for inspiration was rathe rcomplex code form some application notes.
    During the last two years I met countless forum guests who has problems undestanding the examples and how to alter them for their own needs. Not always caused by lack of MCU experience by these peoples.

    Sorry if I have caused some confusion by not limiting my answer to the 155. This is partly caused by my attempt to provide more than jsut a narrowed-down answer to a question (a 'yes' or 'no' won't help in most cases, even if one could answer this short), partly by teh fact that I cannot read the whoel thread and write the answer without being interrupted several tiems and sometimes for a longer period. And it doesn't help that all you see when answering is one post and not the whole thread.

    Both together let me sometimes draw the wong frame around my answer. However, there are some people who really do look at the image inside the frame. :)

  • Jens-Michael Gross said:

    For new designs, I'd recommend the 5418A, as it is significantly cheaper than the 15x/16x (unless you need the DAC or the smaller footprint). The 1x series, while not obsolete, is not recommended for new designs. And not the suitable processor for mathematical calculations. Especially the types without HWM.

    Also F16x part has only integer multiplier accumulator

    Here is a big msiunderstanding. While he 16xx HWM does indeed only perform 16x16 operations at max, it will produce a 32 bit result (unlike a standard C int multiplication, which requires casting the operands to 32 bit before to get a 32 bit result).

    So unless you need 64 bit results, the 16 bit HWM is suitable for many more uses if NOT using the internal C multiplication mechanisms. This is a limitation of C language, and not the HWM itself.

    Both together let me sometimes draw the wong frame around my answer. However, there are some people who really do look at the image inside the frame. :)

    [/quote]

     Hi Jeans I just provide one Hour of my free time to try learn why MSP encounter resistance over Arduino using an oldest obsolete hardware and costing a fortune, I fully agree your point not to do complete solution, again software example need to be more refined and useful also to non new user of MSP.

     Few good usable example and probably a framework can boost Launchpad more high than Arduino can do teaching really how to do thing, bootloader is not burnable, it cost less is more powerful it consume nothing.. But people are wrong way ask just to have all done by other.. this is wrong many fold!!!

     Hardware multiplier compute just a 16x16 multiplication, is just the accumulator performing 32bit addition, this is useful on digital filtering but not on polynomial due to inaccessibility of result register that is read only so 32bit Hardware math is only possible on new 5/6xx series. Again in new TI policy Stellaris overlap MSP and in case no battery operation is needed low end computation power override high end MSP430 price.

     Again I abandoned 1xx series in favor of 2xx series, it cost less, more performance (16Mips to 8) more flexible peripheral but 25x and 26x doesn't exist so no 2xx counterpart are full 1:1 to 1xx migration path.. also this is a TI must do before to obsolete 1xx. Again 1xx 2xx are no directly compatible and some code rewrite is needed expecially for serial communication is completely different. All non serial comm 1xx can be obsoleted with no pain (just DCO wake more fast). Again on gxx part I note missed the extended footprint where in the 1xx series, so you never can mount a 20 pin part on a 28 footprint, pin are at different position.

     Again I cannot completely convert library where I used DAC in the past and also too many application exist using 1xx so obsolescence can be gradual where first part can be exchanged @ no cost.

     Regards

     Roberto

  • Roberto Romano said:
    software example need to be more refined and useful also to non new user of MSP.


    I wholeheartedly agree.
    Understandable and educational (and not just working) examples are the key to turn interested people into lastign customers. Especially if you cannot just throw a huge library into the device so all is just a function call.
    (some processors have been quite successful by providing hardware abstratcion libraries for everthing and compensating the efficiency loss by sheer processing power and memory size - which isn't suitable for a low-power device)

    For LaunchPad, the GRACE project surely is a step ahead. But at the cost of reduced efficiency, even if the generated framework code is specifically compiled for the made selections.

    Having examples that are not only working but also have a good documentaiton about what is done and why it works (instead of clever but undoumented code that uses implicit and non-obvious side-effects, but has two pages of legal stuff in the header for 10 lines of rather cryptic code) is vital to keep people interested isntead of running away scared after some wasted hours of fruitless attempts to customize the code for their needs.

    Roberto Romano said:
    also this is a TI must do before to obsolete 1xx.


    Well, for the smaller 1x members, the bigger ones from the G2 series are a good replacement.
    For the 16xx devices, the 5x family is nice. Sure, the 80pin 5418 is larger than the 64pin 16xx, but also offers much more. And is faster and cheaper. And the larger area under the chip makes routing much easier even if you don't need the additional pins.
    I only miss the DAC, which I used for a loopback-stabilized output. For the rest, well, much of my 1x library code has a 54xx counterpart now, and the next hardware revision f most projects will undergo a migration from 1232 and 1611 to 5438. I completely skipped the 2x and 4x so far, bu tthe G2x will be of interest for small individual projects where no PCBs are created, so the DIL format is a big plus (and the low price).

     

  • Based upon the values given, attached is some example code. The file main.txt compares the fixed point to floating point implementations. I ran it on a PC. using 32 bit Visual Studio. I believe the type long is 32 bits on both the PC and MSP. The file output.txt is what the MSP code might look like. In tests with the given values, you probably need at least 13 bits for fraction before your get anywhere close to floating point.

    #include <stdio.h>
    
    #define PRINT_INTERMEDIATE 1
    
    const double fk0 = 28.616;
    const double fk1 = -19.269;
    const double fk2 = 5.9017;
    const double fk3 = -0.87359;
    const double fk4 = 0.047738;
    
    int bits = 13;
    
    long ik0;
    long ik1;
    long ik2;
    long ik3;
    long ik4;
    
    long float2fixed(double f)
    {
      long   ix = 1;
      double fx;
      ix <<= bits;
      fx = (double)ix;
    
      f *= fx;
      return((int)f);
    }
    
    double fixed2float(long i)
    {
      long   ix = 1;
      double fx;
      ix <<= bits;
      fx = (double)ix;
      return((double)i/fx);
    }
    
    double fpoly(double x)
    {
      double y;
    //y = (((((((fk4*x)+fk3)*x)+fk2)*x)+fk1)*x)+fk0;
      y   = fk4;
      y  *= x;
    #if PRINT_INTERMEDIATE
      printf("fpoly ");
      printf("%10f ", y);
    #endif
      y  += fk3;
      y  *= x;
    #if PRINT_INTERMEDIATE
      printf("%10f ", y);
    #endif
      y  += fk2;
      y  *= x;
    #if PRINT_INTERMEDIATE
      printf("%10f ", y);
    #endif
      y  += fk1;
      y  *= x;
    #if PRINT_INTERMEDIATE
      printf("%10f ", y);
    #endif
      y  += fk0;
    #if PRINT_INTERMEDIATE
      printf("%10f\n", y);
    #endif
    
      return(y);
    }
    
    long ipoly(long x)
    {
      long y;
    //y = (((((((ik4*x)+ik3)*x)+ik2)*x)+ik1)*x)+ik0;
      y   = ik4;
      y  *= x;
      y >>= bits;
    #if PRINT_INTERMEDIATE
      printf("ipoly ");
      printf("%10f ", fixed2float(y));
    #endif
      y  += ik3;
      y  *= x;
      y >>= bits;
    #if PRINT_INTERMEDIATE
      printf("%10f ", fixed2float(y));
    #endif
      y  += ik2;
      y  *= x;
      y >>= bits;
    #if PRINT_INTERMEDIATE
      printf("%10f ", fixed2float(y));
    #endif
      y  += ik1;
      y  *= x;
      y >>= bits;
    #if PRINT_INTERMEDIATE
      printf("%10f ", fixed2float(y));
    #endif
      y  += ik0;
    #if PRINT_INTERMEDIATE
      printf("%10f\n", fixed2float(y));
    #endif
    
      return(y);
    }
    
    void print_code(void)
    {
      printf("\n", bits);
      printf("const int  bits= %d\n", bits);
      printf("const long ik0 = 0x%08x; // %10f, ideal %10f\n", ik0, fixed2float(ik0), fk0);
      printf("const long ik1 = 0x%08x; // %10f, ideal %10f\n", ik1, fixed2float(ik1), fk1);
      printf("const long ik2 = 0x%08x; // %10f, ideal %10f\n", ik2, fixed2float(ik2), fk2);
      printf("const long ik3 = 0x%08x; // %10f, ideal %10f\n", ik3, fixed2float(ik3), fk3);
      printf("const long ik4 = 0x%08x; // %10f, ideal %10f\n", ik4, fixed2float(ik4), fk4);
      printf("\n", bits);
      printf("long ipoly(long x)\n");
      printf("{\n");
      printf("  long y;\n");
      printf("  y   = ik4;\n");
      printf("  y  *= x;\n");
      printf("  y >>= bits;\n");
      printf("  y  += ik3;\n");
      printf("  y  *= x;\n");
      printf("  y >>= bits;\n");
      printf("  y  += ik2;\n");
      printf("  y  *= x;\n");
      printf("  y >>= bits;\n");
      printf("  y  += ik1;\n");
      printf("  y  *= x;\n");
      printf("  y >>= bits;\n");
      printf("  y  += ik0;\n");
      printf("  return(y);\n");
      printf("}\n");
    }
    
    int main(int argc, char *argv[])
    {
      double fx = 6.014;
      long   ix = float2fixed(fx);
      double fy;
      long   iy;
    
      ik0 = float2fixed(fk0);
      ik1 = float2fixed(fk1);
      ik2 = float2fixed(fk2);
      ik3 = float2fixed(fk3);
      ik4 = float2fixed(fk4);
    
      printf("/*----test results----\n");
      printf("x = 0x%08x; // actual %f, ideal %f\n", ix, fixed2float(ix), fx);
    
      fy = fpoly(fx);
      iy = ipoly(ix);
      printf("fpoly(x) = 0x%08x = %f\n", float2fixed(fy), fy);
      printf("ipoly(x) = 0x%08x = %f\n", iy, fixed2float(iy));
      printf("---------------------*/\n");
    
    
      print_code();
    
      if(argc) return(0); /* Quiet compiler */
      if(argv) return(0); /* Quiet compiler */
      return(0);
    }
    

    /*----test results----
    x = 0x0000c072; // actual 6.013916, ideal 6.014000
    fpoly   0.287096  -3.527173  14.280406 -30.001405  -1.385405
    ipoly   0.286987  -3.527466  14.277832 -30.015991  -1.400024
    fpoly(x) = 0xffffd3ab = -1.385405
    ipoly(x) = 0xffffd333 = -1.400024
    ---------------------*/
    
    const int  bits= 13
    const long ik0 = 0x000393b6; //  28.615967, ideal  28.616000
    const long ik1 = 0xfffd9765; // -19.268921, ideal -19.269000
    const long ik2 = 0x0000bcda; //   5.901611, ideal   5.901700
    const long ik3 = 0xffffe40c; //  -0.873535, ideal  -0.873590
    const long ik4 = 0x00000187; //   0.047729, ideal   0.047738
    
    long ipoly(long x)
    {
      long y;
      y   = ik4;
      y  *= x;
      y >>= bits;
      y  += ik3;
      y  *= x;
      y >>= bits;
      y  += ik2;
      y  *= x;
      y >>= bits;
      y  += ik1;
      y  *= x;
      y >>= bits;
      y  += ik0;
      return(y);
    }
    

  • Norman Wong said:
    In tests with the given values, you probably need at least 13 bits for fraction before your get anywhere close to floating point.

    Sure. But the question is whether it makes sense to demand a precision of a 13 bit fraction while not being content with a integer part of 19 bits. especially if your base values are 10 or 12 bit integers with a variation/noise in the LSB. (Since the ADC is the usual data source when someone wants to use floats - the MSP definitely isn't the processor for highspeed data encryption/decrytion services).

    Sure, if your possible value range ranges from infinitesimal small to unbelievable large, floating point math is the only thing that will be sufficient for a single algorithm. But again, the MSP isn't the right tool for the job - like using pincers for removing screws.

  • Yeah, I agree it all depends on the range of the input values. The original poster did not reveal the range. Did not reveal the source of numbers either. Quite probably an ADC. Might be something else entirely. I meant the code to be example of fixed point calculation. Something to play with to get a feel for possible overflows and loss of precision. It probably will not work for all the many reasons posted in this thread.

**Attention** This is a public forum