# Differences between DSP programming vs MPU programming

I'm moderately skilled in programming classic microcontrollers. Embedded C, built-in peripherals, interrupts, the daily bread of an Embedded programmer.

I'm interested in expanding my skillset onto programming DSP. Of course the basic steps would be grabbing some development kit, some manuals and tutorials and get to writing.

But I've seen microcontroller code written by a seasoned Java developer starting with embedded. I've seen an Embedded engineer trying to write web apps. I myself moved from desktop applications development to writing backends running on server farms with thousands requests per second.

The results are not pretty - the paradigms, habits, styles, hang-ups result in using design patterns simply not applicable, optimizing elements that given platform should not have optimized, and so on. It took some tutoring from someone experienced to learn the necessities and unlearn the wrong parts.

So - as a beginning programmer of DSP with background in standard MPUs (ARM7, 8052 etc) what must I learn, and what must I unlearn to write good DSP code? What are the basic differences between the styles and prerequisites of programming the two?

• i just realized that there are 3 close votes. i have absolutely no idea why this question should be closed. is it off-topic? – robert bristow-johnson Oct 7 '14 at 15:27

## 2 Answers

first of all, you certainly can use an ARM or Intel or Arduino or whatever. DSP chips are MPUs that are optimized for the most common DSP operations.

consider an FIR filter.

$$y[n] = \sum\limits_{i=0}^{N-1} h[i] x[n-i]$$

to perform that in $N$ instructions the DSP has to do, in one instruction, the following:

1. multiply coefficient $h[i]$ and sample $x[n-i]$.
2. add the product $h[i] x[n-i]$ into an accumulator.
3. pre-fetch, for the next instruction both $h[i+1]$ and $x[n-(i+1)]$ and
4. post-increment the pointers to point to $h[i+2]$ and $x[n-(i+2)]$

if the accumulator is fixed-point, the width of it should be wide enough that there is no rounding on the right and with enough guard bits on the left for overflow. then there should be a saturate instruction (or the saturation is done automatically when the word is moved to the output).

finally, the pointer that points to $x[n-i]$ must have a circular or modulo-addressing mode so that it "wraps around" since the data in the $x[n]$ is stored in a circular FIFO buffer.

now, a DSP chip will probably do all that with its specialized hardware, but you need not use a DSP as long as you, the programmer, are willing to deal with these details. given that some MPUs run at a much faster clock rate than most inexpensive DSPs, even if you must do all of these operations with multiple instructions, it still might run faster on, say, an ARM than on a SHArC. maybe not.

in doing circular addressing, the simplest way to do that is to use a regular array (like in C) but define the size of that array to be a power of 2 (say $2^p$). then use the AND operation (like "&" in C) to mask off all but the lower $p$ bits. that will accomplish the modulo arithmetic on the index or address of the data.

other than that, it's mostly a matter of knowing the math for DSP, understanding a little about the numerical issues regarding finite-width words, whether they be fixed or floating point and being careful. DSP programming is really no different than any other mathematical programming.

DSP algorithm are defined in equation and understanding those equation is important. The c/c++ coding complexity is not very high and it similar to driver code.