Author Topic: Lowcost DSP for beginner  (Read 29122 times)

0 Members and 1 Guest are viewing this topic.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #50 on: June 17, 2013, 12:05:01 am »
do you use fixed point math with blackfin ?

snoopy

• Frequent Contributor
• Posts: 644
• Country:
Re: Lowcost DSP for beginner
« Reply #51 on: June 22, 2013, 12:17:12 pm »
do you use fixed point math with blackfin ?

Yes I used fixed point arithmetic. You can do floating point arithmetic but it's not very efficient.

regards

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #52 on: June 23, 2013, 12:05:40 am »
what fixedpoint library have you used ?

also 32bit ? 64bit ?

nctnico

• Super Contributor
• Posts: 18002
• Country:
Re: Lowcost DSP for beginner
« Reply #53 on: June 23, 2013, 12:42:42 am »
Why would you need a library? Fixed point is just a matter of choosing a multiplication ratio for shifting the 'point'.

So if you want to multiply by 0.5 and choose the multiplier at 32768 then the calculation is:
a= (a *  16384) / 32768
There are small lies, big lies and then there is what is on the screen of your oscilloscope.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #54 on: June 23, 2013, 09:01:13 am »
because in the real word it is not so easy, you need to consider a lot things about fixedpoint math, Microchip has introduced a dedicated hw and library to handle it (dsPIC3* -> fractional engine), also i'd like to have 64 bit of fixed point in order to have a better precision and to avoid precision lost.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #55 on: June 23, 2013, 09:03:55 am »
I mean, with 32 bit, this is not enough

Code: [Select]
/* *  fx2804 *  =================== *  integer    28 bits *  fractional  4 bits */#define itofx2804(x)    (fixedpoint_t) (((fixedpoint_t) x) << 4)#define ftofx2804(x)    (fixedpoint_t) (((float) x) * (16))#define dtofx2804(x)    (fixedpoint_t) (((double) x) * (16))#define fx2804toi(x)    (signed long)  ((fixedpoint_t) x) >> 4)#define fx2804tof(x)    (float)        (((fixedpoint_t) x) / (16))#define fx2804tod(x)    (double)       (((fixedpoint_t) x) / (16))#define mulfx2804(x,y)  (fixedpoint_t) ((((fixedpoint_t) x) * ((fixedpoint_t) y)) >> (4))#define divfx2804(x,y)  (fixedpoint_t) (((fixedpoint_t) x << (4)) / ((fixedpoint_t) y))#define fx2804_get_n(x) (fixedpoint_t) (((fixedpoint_t) x) >> 4)#define fx2804_get_d(x) (fixedpoint_t) (( 1 *((fixedpoint_t) x & (0xf) )) >> 4)/* *  fx2408 *  =================== *  integer    24 bits *  fractional  8 bits */#define itofx2408(x)    (fixedpoint_t) (((fixedpoint_t) x) << 8)#define ftofx2408(x)    (fixedpoint_t) (((float) x) * (256))#define dtofx2408(x)    (fixedpoint_t) (((double) x) * (256))#define fx2408toi(x)    (signed long)  ((fixedpoint_t) x) >> 8)#define fx2408tof(x)    (float)        (((fixedpoint_t) x) / (256))#define fx2408tod(x)    (double)       (((fixedpoint_t) x) / (256))#define mulfx2408(x,y)  (fixedpoint_t) ((((fixedpoint_t) x) * ((fixedpoint_t) y)) >> (8))#define divfx2408(x,y)  (fixedpoint_t) (((fixedpoint_t) x << (8)) / ((fixedpoint_t) y))#define fx2408_get_n(x) (fixedpoint_t) (((fixedpoint_t) x) >> 8)#define fx2408_get_d(x) (fixedpoint_t) (( 100 *((fixedpoint_t) x & (0xff) )) >> 8)/* *  fx2012 *  =================== *  integer    20 bits *  fractional 12 bits */#define itofx2012(x)    (fixedpoint_t) (((fixedpoint_t) x) << 12)#define ftofx2012(x)    (fixedpoint_t) (((float) x) * (4096))#define dtofx2012(x)    (fixedpoint_t) (((double) x) * (4096))#define fx2012toi(x)    (signed long)  ((fixedpoint_t) x) >> 12)#define fx2012tof(x)    (float)        (((fixedpoint_t) x) / (4096))#define fx2012tod(x)    (double)       (((fixedpoint_t) x) / (4096))#define mulfx2012(x,y)  (fixedpoint_t) ((((fixedpoint_t) x) * ((fixedpoint_t) y)) >> (12))#define divfx2012(x,y)  (fixedpoint_t) (((fixedpoint_t) x << (12)) / ((fixedpoint_t) y))#define fx2012_get_n(x) (fixedpoint_t) (((fixedpoint_t) x) >> 12)#define fx2012_get_d(x) (fixedpoint_t) (( 10000 *((fixedpoint_t) x & (0xfff) )) >> 12)/* *  fx1616 *  =================== *  integer    16 bits *  fractional 16 bits */#define itofx1616(x)    (fixedpoint_t) (((fixedpoint_t) x) << 16)#define ftofx1616(x)    (fixedpoint_t) (((float) x) * (65536))#define dtofx1616(x)    (fixedpoint_t) (((double) x) * (65536))#define fx1616toi(x)    (signed long)  ((fixedpoint_t) x) >> 16)#define fx1616tof(x)    (float)        (((fixedpoint_t) x) / (65536))#define fx1616tod(x)    (double)       (((fixedpoint_t) x) / (65536))#define mulfx1616(x,y)  (fixedpoint_t) ((((fixedpoint_t) x) * ((fixedpoint_t) y)) >> (16))#define divfx1616(x,y)  (fixedpoint_t) (((fixedpoint_t) x << (16)) / ((fixedpoint_t) y))#define fx1616_get_n(x) (fixedpoint_t) (((fixedpoint_t) x) >> 16)#define fx1616_get_d(x) (fixedpoint_t) (( 1000000 *((fixedpoint_t) x & (0xffff) )) >> 16)/* *  fx1220 *  =================== *  integer    12 bits *  fractional 20 bits */#define itofx1220(x)    (fixedpoint_t) (((fixedpoint_t) x) << 20)#define ftofx1220(x)    (fixedpoint_t) (((float) x) * (1048576))#define dtofx1220(x)    (fixedpoint_t) (((double) x) * (1048576))#define fx1220toi(x)    (signed long)  ((fixedpoint_t) x) >> 20)#define fx1220tof(x)    (float)        (((fixedpoint_t) x) / (1048576))#define fx1220tod(x)    (double)       (((fixedpoint_t) x) / (1048576))#define mulfx1220(x,y)  (fixedpoint_t) ((((fixedpoint_t) x) * ((fixedpoint_t) y)) >> (20))#define divfx1220(x,y)  (fixedpoint_t) (((fixedpoint_t) x << (20)) / ((fixedpoint_t) y))#define fx1220_get_n(x) (fixedpoint_t) (((fixedpoint_t) x) >> 20)#define fx1220_get_d(x) (fixedpoint_t) (( 100000000 *((fixedpoint_t) x & (0xfffff) )) >> 20)

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #56 on: June 23, 2013, 09:21:07 am »
Code: [Select]
void eval_pi_float(){    fp64_t   sign;    fp64_t   i, pi;    uint32_t iteractions;    iteractions = 20000;    sign = -1;    printf("[*] fp64 computing pi with %ld iteractions\n", iteractions);    pi = 0;    for (i = 1; i < iteractions; i += 2)    {        sign = -1 * sign;        pi  += 4 * (sign / i);    }    printf("pi is equal to ");    printf("%f ", pi);    printf("\n");}
Code: [Select]
void eval_pi_fx1616(){    sint32_t     sign;    fixedpoint_t i, pi;    uint32_t     iteractions;    iteractions = 20000;    sign = -1;    printf("[*] fixed_point fx16.16 computing pi with %ld iteractions\n", iteractions);    pi = 0;    for (i = 1; i < iteractions; i += 2)    {        sign = -1 * sign;        if (sign > 0)        {            pi = pi + (4 * divfx1616(1, i));        }        else        {            pi = pi - (4 * divfx1616(1, i));        }    }    printf("pi is equal to ");    printffx1616(pi);    printf("\n");}

fp64 computing pi with 20000 iteractions
pi is equal to 3.141493

fixed_point fx16.16 computing pi with 20000 iteractions
pi is equal to  3.1411

fixed_point fx24.08 computing pi with 20000 iteractions
pi is equal to  3.10

fixed_point fx12.20 computing pi with 20000 iteractions
pi is equal to  3.1309

 math engine evaluated pi fp64 3.141493 fx16.16 3.1411 fx24.08 3.10 fx12.20 3.1309

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #57 on: June 23, 2013, 09:34:03 am »
Code: [Select]
void eval_pi_i64(){    char     result[100];    sint32_t sign;    uint32_t i;    bigint_t pi;    bigint_t remainder;    bigint_t quotient;    bigint_t dividend;    bigint_t divisor;    bigint_t scale;    bigint_t one;    uint32_t iteractions;    iteractions = steps;    printf("[*] i64 computing pi with %ld iteractions\n", iteractions);    pi       = i64_atoi("0");    one      = i64_atoi("1");    dividend = i64_atoi("4");    divisor  = i64_atoi("1");    scale    = i64_atoi("10000000");    dividend = i64_mul(dividend, scale);    sign        = -1;    for (i = 1; i < iteractions; i += 2)    {        sign = -1 * sign;        i64_div(dividend, divisor, &quotient, &remainder);        if (sign > 0)        {            pi = i64_add(pi, quotient);        }        else        {            pi = i64_sub(pi, quotient);        }        i64_itoa(quotient, result, sizeof(result) - 1);        i64_itoa(pi, result, sizeof(result) - 1);        divisor = i64_add(divisor, one);        divisor = i64_add(divisor, one);    }    i64_itoa(pi, result, sizeof(result) - 1);    printf("with a scale of %lu ", scale);    printf("pi is equal to %s\n", result);}
i64 computing pi with 20000 iteractions
with a scale of  10000000, pi is equal to 31414944

31414944 / 10000000 = 3.1414944

the difference between float (fp32_t) and double (fp64_t)

float fp32 computing pi with 20000 iteractions
pi is equal to 3.141499

float fp64 computing pi with 20000 iteractions
pi is equal to 3.141493

Final report

 math engine evaluated pi fp32 3.141499 fp64 3.141493 fx16.16 3.1411 fx24.08 3.10 fx12.20 3.1309 fx.i64 3.1414944

snoopy

• Frequent Contributor
• Posts: 644
• Country:
Re: Lowcost DSP for beginner
« Reply #58 on: June 23, 2013, 01:38:31 pm »
Why would you need a library? Fixed point is just a matter of choosing a multiplication ratio for shifting the 'point'.

So if you want to multiply by 0.5 and choose the multiplier at 32768 then the calculation is:
a= (a *  16384) / 32768

because fixed fractional numbers are not a standard type in C or C++. They may be supported by the hardware and accessible using assembly language but this doesn't help people writing efficient dsp code in C or C++. Analog devices Visual DSP now supports the fract type in C++

regards

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #59 on: June 23, 2013, 02:56:35 pm »
Exactly. Fractional is a particular kind of fixed point, it is Q(1,N-1) and this means it needs a lot of renormalization during calculations, this is done by the special library provided, also C++ is able to "overload" math operators (+,-.*./,%).

To avoid precision lost you need to perform fixedpoint with 64bit at least.

Both of these means: a bit of homeworks you have to write and test by your own if there is no support provided.

andersm

• Super Contributor
• Posts: 1076
• Country:
Re: Lowcost DSP for beginner
« Reply #60 on: June 23, 2013, 06:30:27 pm »
Fixed-point math was proposed for C11 as part of TR 18037 (extensions to support embedded processors), but did not make it into this revision of the standard. If your target uses a reasonably modern version of GCC (from ca. 2008 or newer) it can probably generate code for fixed-point math, but library support may be missing.

Quickly testing Microchip's XC32 compiler, it will take advantage of the MIPS DSP extensions if they are enabled. It also appears to have full library support via Dinkumware's libraries.

snoopy

• Frequent Contributor
• Posts: 644
• Country:
Re: Lowcost DSP for beginner
« Reply #61 on: June 24, 2013, 03:33:45 am »
Exactly. Fractional is a particular kind of fixed point, it is Q(1,N-1) and this means it needs a lot of renormalization during calculations, this is done by the special library provided, also C++ is able to "overload" math operators (+,-.*./,%).

To avoid precision lost you need to perform fixedpoint with 64bit at least.

Both of these means: a bit of homeworks you have to write and test by your own if there is no support provided.

best to use a micro or dsp that provides native support for fixed point numbers otherwise you will waste a lot of clock cycles emulating it.

regards

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #62 on: June 24, 2013, 08:20:33 am »
the problem is ... what happens if you have not.

nctnico

• Super Contributor
• Posts: 18002
• Country:
Re: Lowcost DSP for beginner
« Reply #63 on: June 24, 2013, 09:35:46 am »
Exactly. Fractional is a particular kind of fixed point, it is Q(1,N-1) and this means it needs a lot of renormalization during calculations, this is done by the special library provided, also C++ is able to "overload" math operators (+,-.*./,%).

To avoid precision lost you need to perform fixedpoint with 64bit at least.

Both of these means: a bit of homeworks you have to write and test by your own if there is no support provided.

best to use a micro or dsp that provides native support for fixed point numbers otherwise you will waste a lot of clock cycles emulating it.

regards
A multiplication or divide by a power of 2 is just a barrel shift. I use fixed point math all the time but never had performance issues.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.

snoopy

• Frequent Contributor
• Posts: 644
• Country:
Re: Lowcost DSP for beginner
« Reply #64 on: June 24, 2013, 12:33:48 pm »
the problem is ... what happens if you have not.

then its is not suitable as a dsp.

nctnico

• Super Contributor
• Posts: 18002
• Country:
Re: Lowcost DSP for beginner
« Reply #65 on: June 24, 2013, 12:51:32 pm »
the problem is ... what happens if you have not.

then its is not suitable as a dsp.
Thats total crap and you know it!
There are small lies, big lies and then there is what is on the screen of your oscilloscope.

snoopy

• Frequent Contributor
• Posts: 644
• Country:
Re: Lowcost DSP for beginner
« Reply #66 on: June 24, 2013, 02:32:51 pm »
the problem is ... what happens if you have not.

then its is not suitable as a dsp.
Thats total crap and you know it!

so how many compares, shifts and adds does it take to multiply a 24 bit fixed point number by another 24 bit fixed point number  ?

and that's only for a single multiply !!

wait till you start implementing a dozen or so biquads at say 96 KHz !! Throw in a 200 tap FIR filter as well !!

you obviously have never done any serious dsp work.

regards

nctnico

• Super Contributor
• Posts: 18002
• Country:
Re: Lowcost DSP for beginner
« Reply #67 on: June 24, 2013, 04:00:21 pm »
You obviously have no idea what fixed point is about. Fixed point is no more than a number multiplied by a factor to get an integer. Multiplying integers is dead easy (especially if you have a hardware multiplier). The only thing to watch out for are overflows.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #68 on: June 24, 2013, 04:19:26 pm »
The only thing to watch out for are overflows.

Exactly, you have to care about overflows
and precision lost -> you have to tune your software very accurately.

And THIS is the reason why i need much more than 32bits!!!!
This is the question!

marshallh

• Supporter
• Posts: 1462
• Country:
Re: Lowcost DSP for beginner
« Reply #69 on: June 24, 2013, 04:19:40 pm »
Hell, fpgas didn't even have floating point DSP units until the new Gen10 series from Altera was revealed weeks ago.
Fixed point is perfectly useful and much simpler/faster to work with in low level hardware. The downside is ex. for 32bits fixedpoint you get a very limited range compared to a 32bit IEEE754 floating point number. The upside is multiplies, adds, substracts can be single cycle.
I've written a MPEG audio decoder on fpga using fixed point multiplies. Takes just nanoseconds, you can clock them faster than you can route nearby logic to them!
Verilog tips
BGA soldering intro

11:37 <@ktemkin> c4757p: marshall has transcended communications media
11:37 <@ktemkin> He speaks protocols directly.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #70 on: June 24, 2013, 04:24:22 pm »
Well, as you can see in the previous report i post, i need 64bit or more at least ... so i am really thinking about implementing
- sub128bit
- mul128bit (256bit internal register)
- div128bit (256bit internal register -> also used for modulus)
- shiftL/R 128bit
- cmp128bit
into a spartan3E, also i wandering how good can "cordic" libraries go if implemented with fixedpoint.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #71 on: June 24, 2013, 04:28:19 pm »
multiplies, adds, substracts can be single cycle

with 128bit multiplies should take 4 clocks to complete

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #72 on: June 24, 2013, 04:41:58 pm »
More specifically i have to port this code to a dsp

Code: [Select]
void gps_calc(){    double a, b, c;    // Degree    a  = acos(sin(Lat1 / RADS) * sin(Lat2 / RADS) + cos(Lat1 / RADS) * cos(Lat2 / RADS) * cos((Lon1 - Lon2) / RADS));    b  = a * Mt_Per_Rad;    c  = acos((sin(Lat2 / RADS) - sin(Lat1 / RADS) * cos(a)) / (cos(Lat1 / RADS) * sin(a)));    c *= RADS;    GPS_dist = (float) b;    GPS_ang  = (float) c;}
Please note that the precision requested is satisfied only with floating point 64bit, so ... i need fixedpoint at 128bit at least, and ... cordic implementation of all these trigonometric functions.

Am i right ? Or should i use a "big iron" dsp (magellan, shark, ...) ?

p.s. as "chip" dsp i have got only a blackfin BF537

nctnico

• Super Contributor
• Posts: 18002
• Country:
Re: Lowcost DSP for beginner
« Reply #73 on: June 24, 2013, 04:54:28 pm »
I'd start with re-evaluating the precision especially considering the accuracy of GPS data (worst case +/-100meters). The numbers you posted earlier contained less significant numbers than the type could hold and the fixed point wasn't choosen very well. You can use a lookup table for the sine function (0 to pi/2 is sufficient if space is constrained). In think you can use 32 bit integers but you have to choose the point well. The maximum distance around the globe is 40k km which is 40 million meters. In a 32 bit integer this gives you better than 10 centimeters of resolution (integer part shifted by 5 bits / multiplied by 32).
« Last Edit: June 24, 2013, 05:14:07 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.

legacy

• Super Contributor
• Posts: 4346
• Country:
Re: Lowcost DSP for beginner
« Reply #74 on: June 24, 2013, 05:42:45 pm »
Well ... the problem is related to the error propagation (rounding/truncating) about the primary evaluating values, please not they are double the precision of the final result: double a, b, c. If i use "float a,b,c" instead of "double a,b,c" i got a lot of errors in the final gps distance and angle estimation due to the trigonometic functions involved in formula.

I have also posted how pi has been calculated with fixedpoint at 32bit, what is wrong with it ?

Smf