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

0 Members and 1 Guest are viewing this topic.

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #75 on: June 24, 2013, 05:51:38 pm »
i mean, is this wrong ?

Code: [Select]
/*
 *  fx1220
 *  ===================
 *  integer    12 bits
 *  fractional 20 bits
 */
#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))

it should be much more precise than 1616 because it has 20bit for the fractional part of PI instead of 16bit

Code: [Select]
/*
 *  fx1616
 *  ===================
 *  integer    16 bits
 *  fractional 16 bits
 */
#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))
/*

but if you look at the report table

fx16.16   pi=3.1411
fx12.20   pi=3.1309

you find that it is not
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: fi
Re: Lowcost DSP for beginner
« Reply #76 on: June 24, 2013, 06:46:35 pm »
Do you need perform millions of these GPS-calculations per second? Or why not just use software double precision floating point if it gets the job done?

Also, your formula may not be in optimal form to squeeze every bit of precision out of underlying numeric representation. It is a bit of an art, see something like Numerical Recipes in C if you haven't already done so. For example, similar "precision problem" occurs when calculating a vector length using trivial formula sqrt(x^2+y^2). It has poor utilization of the value range since it is not possible to calculate the result using double precision floating point if x or y is greater than about 1.4e154 since intermediate results will overflow unless the formula is converted to an another form. So we have wasted quite a bit of our numeric range.

Regards,
Janne
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #77 on: June 24, 2013, 06:55:20 pm »
Its hard to tell what goes wrong with calculating pi without knowing the internals of the library.

There is some interesting reading material here:
http://www.mathworks.nl/help/fixedpoint/examples/calculate-fixed-point-sine-and-cosine.html
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #78 on: June 24, 2013, 07:03:44 pm »
Do you need perform millions of these GPS-calculations per second?

it should be use on a flying machine, the GPS calculation is easier than the IMU calculation which needs to be evaluate 50 times a second at least.

Or why not just use software double precision floating point if it gets the job done?

Cause my Blackfin BF537 has no fpu, i can only use softfloat to emulate double/float, also i have to evaluate the IMU &C staff.
I am not sure this  low cost DSP is good for my goal, just doing considerations due to the low cost of a tiny BF537 board sold by SoC Robotics.

Also, your formula may not be in optimal form to squeeze every bit of precision out of underlying numeric representation. It is a bit of an art, see something like Numerical Recipes in C if you haven't already done so.

I have placed a new order on Amazon, thanks for the trips  ;D
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #79 on: June 24, 2013, 07:14:18 pm »
Its hard to tell what goes wrong with calculating pi without knowing the internals of the library.

there is no library, i have posted everything you need about fixedpoint 32bit
- add/sub are common addiction and subtract of integer number
- fxmul/fxdiv have been provided as "C macro" (see my previous quote)
- the pi algorithm has been provided, both with the use of fixedpoint32 and float

You are missing the library of fixedpoint64bit: i haven't posted it.



 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #80 on: June 24, 2013, 07:33:28 pm »
Do you need perform millions of these GPS-calculations per second?

it should be use on a flying machine, the GPS calculation is easier than the IMU calculation which needs to be evaluate 50 times a second at least.
Sounds like soft float will do just fine. Dunno if a Blackfin has any advantages over an ARM; ARM is more widespread. If its for controlling an airplane you might need a certified compiler and follow certain programming rules.

If I where you I implement the algorithm using (soft) float first and optimise if it is too slow.

A couple of years ago I designed an echo canceller. Since I had no idea how fast it would run on a microcontroller I just started with the (soft) floating point version and measured how long it took to process one sample. Then I measured in which part most of the time was spend. That gave me an idea where I could gain the most from optimisation efforts. It went from 1.5ms per sample to 100us per sample but some part of it is still soft floating point!
« Last Edit: June 24, 2013, 08:10:14 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: fi
Re: Lowcost DSP for beginner
« Reply #81 on: June 24, 2013, 07:41:46 pm »
Do you need perform millions of these GPS-calculations per second?

it should be use on a flying machine, the GPS calculation is easier than the IMU calculation which needs to be evaluate 50 times a second at least.

Or why not just use software double precision floating point if it gets the job done?

Cause my Blackfin BF537 has no fpu, i can only use softfloat to emulate double/float, also i have to evaluate the IMU &C staff.
I am not sure this  low cost DSP is good for my goal, just doing considerations due to the low cost of a tiny BF537 board sold by SoC Robotics.

Have you timed how long it takes to calculate using software double precision floating point, then? Using software DP FP sounds feasible if you need just few tens of calculations per second. Depending of course what else you also need to do. You can also possibly save substantial amount of computation time by calculating sin/cos values only once per function call and converting angles to radians only once.

Regards,
Janne
 

Offline senso

  • Frequent Contributor
  • **
  • Posts: 951
  • Country: pt
    • My AVR tutorials
Re: Lowcost DSP for beginner
« Reply #82 on: June 24, 2013, 09:22:17 pm »
Plain atmegas have been running thousands of tri,quad, octo, etc copters just fine, doing waypoint navigation, sensor fusion, inertial calcs, reading the PPM values from the RC transmitters, generating PPM for the ESC's, even data-logging and talking down to a ground station, all that in a 8bit micro running at 16Mhz with crappy Arduino libs that slow everything down.

I understand the need for better and faster, so why not a cortex M4 that has hardware FPU?
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Lowcost DSP for beginner
« Reply #83 on: June 24, 2013, 10:02:13 pm »
Quote
gps_calc()
This really needs to re-implemented using integer math SO BADLY.
The real problem is probably due to the relevant range of answers being SO much smaller than the mathematical reality of the situation...  You want better than 1km of accuracy on a circumference of 40k km.
 

Offline snoopy

  • Frequent Contributor
  • **
  • Posts: 767
  • Country: au
    • Analog Precision
Re: Lowcost DSP for beginner
« Reply #84 on: June 25, 2013, 03:02:52 am »
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.

fixed fraction numbers never have numbers greater than one and the multiplier does shift-right and adds instead of shift-left and adds as in an integer multiplier. Try multiplying 0.5 by 0.5 and see the results ;)

If you your micro doesn't  have the hardware to do it quickly then how are you supposed to do it ?
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #85 on: June 25, 2013, 10:23:43 am »
IMHO fixed fractions from 0..1 have very little practical use.

Anyway I'll show you how its done with integers: Shift 15 bits left (*32768) so 0.5 becomes 16384 -> 16384*16384=268.43 M Now shift the result 15 bits to the right and you get 8192. If you divide 8192 by 32768 you get 0.25 so the result is correct. If you have to add several multiplication results (like in a direct form IIR filter) the right shift can be performed on the final result so you keep maximum resolution and speed.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Rasz

  • Super Contributor
  • ***
  • Posts: 2616
  • Country: 00
    • My random blog.
Who logs in to gdm? Not I, said the duck.
My fireplace is on fire, but in all the wrong places.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #87 on: June 25, 2013, 11:47:03 am »
Interesting, but my actual target board is based on BlackFin BF537, i have to understand if it could satisfy my constraints before opening a fork to an other target platform (e.g. cortex M4)
 

Offline snoopy

  • Frequent Contributor
  • **
  • Posts: 767
  • Country: au
    • Analog Precision
Re: Lowcost DSP for beginner
« Reply #88 on: June 25, 2013, 12:58:14 pm »
IMHO fixed fractions from 0..1 have very little practical use.

Anyway I'll show you how its done with integers: Shift 15 bits left (*32768) so 0.5 becomes 16384 -> 16384*16384=268.43 M Now shift the result 15 bits to the right and you get 8192. If you divide 8192 by 32768 you get 0.25 so the result is correct. If you have to add several multiplication results (like in a direct form IIR filter) the right shift can be performed on the final result so you keep maximum resolution and speed.

If you use any fixed point DSP, fixed fractional arithmetic is what they support in hardware. Also when implementing IIR filters such as a biquad the coefficients are rarely just a power of two.

regards
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #90 on: June 25, 2013, 01:56:21 pm »
If you use any fixed point DSP, fixed fractional arithmetic is what they support in hardware. Also when implementing IIR filters such as a biquad the coefficients are rarely just a power of two.

hardware tricks to support fixed fractional arithmetic in hardware: what are they exactly ? and what is the best support around ?
Microchip dsPIC has "fractional engine" in her dsPIC3x series.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #91 on: June 25, 2013, 02:23:44 pm »
You mean something like that implemented in hardware ?

Code: [Select]
/*
 * to add two Q31 values
 * and saturate the result to the maximum or the minimum
 * without the fixed-point arithmetic supports
 */
#define MIN_32    0x80000000
#define MAX_32    0x7fffffff

sint32_t Q31_add_sat1(sint32_t a, sint32_t b)
{
    sint32_t c;

    c = a + b;

    if (((a ^ b) & MIN_32) == 0)
    {
        if ((c ^ a) & MIN_32)
        {
            if (c < 0)
            {
                c = MIN_32;
            }
            else
            {
                c = MAX_32
            }
        }
    }
    return c;
}
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #92 on: June 25, 2013, 03:00:31 pm »
IMHO fixed fractions from 0..1 have very little practical use.

Anyway I'll show you how its done with integers: Shift 15 bits left (*32768) so 0.5 becomes 16384 -> 16384*16384=268.43 M Now shift the result 15 bits to the right and you get 8192. If you divide 8192 by 32768 you get 0.25 so the result is correct. If you have to add several multiplication results (like in a direct form IIR filter) the right shift can be performed on the final result so you keep maximum resolution and speed.
If you use any fixed point DSP, fixed fractional arithmetic is what they support in hardware. Also when implementing IIR filters such as a biquad the coefficients are rarely just a power of two.
regards
That doesn't matter at all. They are constant so multiply them with a factor (like 32768 from my example). Just remember 0.5 (or any fractional number) doesn't exist in the digital domain so no matter how you put it a fractional number is always multiplied by a factor (whether fixed or adjustable) to get a binary representation.

@legacy: saturated instructions are standard on many modern controllers including the ARM Cortex M4. But then again they probably have limited use. If a signal gets saturated then something gets overdriven and you get false results. I always check whether my signal processing algorithms can handle signals with maximum amplitude without clipping.
« Last Edit: June 25, 2013, 03:14:34 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: Lowcost DSP for beginner
« Reply #93 on: June 25, 2013, 03:41:38 pm »
hardware tricks to support fixed fractional arithmetic in hardware: what are they exactly ? and what is the best support around ?
Microchip dsPIC has "fractional engine" in her dsPIC3x series.
Why not start by studying the CPU you already have.

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #94 on: June 25, 2013, 06:00:50 pm »
work in progress  :scared:
 

Offline Paul Price

  • Super Contributor
  • ***
  • Posts: 1419
Re: Lowcost DSP for beginner
« Reply #95 on: June 25, 2013, 10:04:04 pm »
Assuming someone has found or not found the perfect DSP for a beginner, would anyone be so brave to tell what practical application or device they managed to get working using one?
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #96 on: June 25, 2013, 11:37:46 pm »
First you need to define DSP. Usually this is a convoluted CPU with some peripherals to connect codecs (A/D + D/A) aimed to crunch as much numbers as possible in a given time without being easy to program.

The thing is that you don't need a DSP to do signal processing so I wouldn't start with a real DSP to do signal processing unless you need a lot of speed. IIRC my first signal processing project was to convert the analog X-Y output of my oscilloscope into serial data. That project consisted of an 68HC11 (@2MHz) and an A/D converter. Later on I programmed an ADSP2181 in asm for processing audio but that wasn't a pleasant job at all. However since audio is still confined to 20kHz (or 4kHz when it comes to phone lines) and microcontrollers have become 100 times more powerful there is no need to jump through asm hoops any more to get a project going.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline snoopy

  • Frequent Contributor
  • **
  • Posts: 767
  • Country: au
    • Analog Precision
Re: Lowcost DSP for beginner
« Reply #97 on: June 28, 2013, 03:04:35 am »
First you need to define DSP. Usually this is a convoluted CPU with some peripherals to connect codecs (A/D + D/A) aimed to crunch as much numbers as possible in a given time without being easy to program.

The thing is that you don't need a DSP to do signal processing so I wouldn't start with a real DSP to do signal processing unless you need a lot of speed. IIRC my first signal processing project was to convert the analog X-Y output of my oscilloscope into serial data. That project consisted of an 68HC11 (@2MHz) and an A/D converter. Later on I programmed an ADSP2181 in asm for processing audio but that wasn't a pleasant job at all. However since audio is still confined to 20kHz (or 4kHz when it comes to phone lines) and microcontrollers have become 100 times more powerful there is no need to jump through asm hoops any more to get a project going.

and DSP's usually have additional hardware to interface directly to ADC's and DAC's with serial interfaces such as I2S, left justified etc. These also have hardware to transfer data to and from the serial interface directly into memory without CPU intervention. A lot of micros such as the ARM have these features as well but these days the converse is true where a lot of DSP's such as the Blackfin have a lot of features you find on embedded micros so it's horses for courses ;)

Yes and you are right. You don't need to use a DSP to do DSP. You could use a Z80 to do DSP as long as the sampling rate is very low ;)
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Lowcost DSP for beginner
« Reply #98 on: June 28, 2013, 08:55:23 am »
Again, the problem is the math support!

Supposing you can have MAC on a MPU, a low cost dsp is offering you "saturated additions" while MPU is offering you "overflowed addition", a low cost dsp is offering you "fractional engine" while MPU is offering you ... nothing more than an ALU to do dirty pretty & rude fixedpoint with a scaled integers.

 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26895
  • Country: nl
    • NCT Developments
Re: Lowcost DSP for beginner
« Reply #99 on: June 28, 2013, 12:15:16 pm »
Again, the problem is the math support!

Supposing you can have MAC on a MPU, a low cost dsp is offering you "saturated additions" while MPU is offering you "overflowed addition", a low cost dsp is offering you "fractional engine" while MPU is offering you ... nothing more than an ALU to do dirty pretty & rude fixedpoint with a scaled integers.
You see problems which aren't there at all...
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf