DSP

From Flowcode Help
Jump to navigationJump to search

What is DSP

DSP or Digital Signal Processing is a way of processing data in way that provides some kind of additional functionality. DSP usually takes the form of an input, a process on the input and an output corresponding to the process. As microcontrollers are generally quite fast devices it is the act of repeating the process at high speed that allows us to perform the task in hand.


These are all examples of DSP systems at work:

Guitar Distortion

Audio effects e.g. Echo, Reverb, etc.

Digital Filtering

Frequency Spectrum Analysis

Baby Monitors

Speed Control

Position Control

Temperature Control


The DSP System Component

There are a number of components built into Flowcode to help you to create a DSP system specific for what you need.


The DSP components all centre around the DSP System component which is basically the buffer manager for the system. For each link between components we need a buffer, think of each buffer as a pipe that the data can flow along. The size of each buffer dictates the number of operations or "ticks" that have to happen before the data reaches the end of the buffer. If we know the "tick" rate of the system and the size of the buffer then we know the delay that will be presented when the data is travelling through the buffer.


For example if we have an input component connected directly to an output component then we need only one buffer.

DSP1.jpg


If we have two input components connected via a sum component and then onto an output component, we need three buffers. Naming the buffers can help when it comes to connecting everything up.

DSP3.jpg


By default all the buffers have the same bit depth and sign but by disabling the simple mode property these can be manually edited for each individual buffer.

DSP4.jpg


Components are connected up to the buffers by specifying the buffer manager and then selecting the buffer. The arrows automatically connect themselves up as you set the connection properties and move the component around on the panel.

DSP2.jpg


The DSP system initialise function must be called in your program before any other DSP component macros are called as it sets up the buffers ready to be used. The initialise function also sets up the scope window so if you like you can see the DSP signals during simulation to help debug or visualise what's going on.

DSP5.jpg

DSP Components

More detailed explanations, examples and help can be found for each of the DSP components by following the links provided below.

Input

The DSP Input component is used to pass data from another source into the DSP system. This data could come from an analogue source, a external sensor on an SPI or I2C bus, a data file, the possibilities are endless.

Output

The DSP Output component is used to pass data to another source from the DSP system. This data could go to an digital to analogue converter, a external device on an SPI or I2C bus, a data file, the possibilities are endless.

Frequency Generator

The DSP Frequency Generator component is used to pass fixed waveform data into the DSP system. This data is generated by the component depending on the component's various properties. The waveform data is stored into ROM in the embedded target to allow the RAM memory to be retained for the DSP buffers and other system variables.

Delay

The DSP Delay component is used to create an additional buffered delay. Useful for a wide variety of task such as filtering, phase synchronising, echo effects etc.

Scale

The DSP Scale component is used to alter the data in a buffer by allowing a scale or offset to be applied.

Level

The DSP Level component is used to detect peaks, troughs and averages in the data contained in a buffer. This comes with a decay property which allows you to maintain record values long after they have left the contents of the buffer.

Sum

The DSP Sum component is used to allow two buffers to be merged into one using several different techniques such as addition, subtraction, take the max, take the min etc.

Filter

The DSP Filter component is used to attenuate unwanted frequencies from the data in a buffer leaving the buffer full of only the frequencies we are interested in.

FFT

The DSP FFT component is used to convert a time based signal into a frequency based spectrum by dishing out the frequencies present in the buffer into a number of frequency bins.

Buffer vs Tick

A typical DSP system will use a timer interrupt macro to maintain a steady rate on which everything happens. The timer interrupt will do things like any analogue sampling and may also call the DSP component functions in order. Based on this methodology we have created two variants of most of the DSP component macros in the form of Function and FunctionTick. These allow two different ways of working with the DSP component. These methods can be mixed and matched as necessary to allow your program to run at the optimum speed and throughput.


Buffer

The Function type hardware macros allow the entire DSP buffer to be processed at once. Behind the scenes this involves a loop and we cycle through all of the values in the buffer performing the hardware macro's functionality. Some components such as the DSP FFT only have the non tick version of the hardware macros as the FFT calculations only work with a buffer full of data.


The standard trigger based functions are designed to process a complete buffer in one operation. Care should be taken that the processing time is no longer then a single interrupt period.

DSP6.jpg


This method gives you free time between interrupts where other functions can be performed such as updating the GUI on a display. The longer the buffers, the more time you get between having to process the trigger code but the downside is that the trigger code will take more time to process.


For systems with higher sample rates where missing some data is not important the interrupt can be halted by disabling the timer interrupt within the trigger code and then re-enabling when you are done.

DSP7.jpg


If the processing time is longer then a single interrupt period and the interrupt is not disabled then you are likely to get corruption in the data buffer. You may be able to circumvent this by processing the buffers that may get corrupted by the interrupt first. Here you would have to ensure that the entire DSP operations are processed before the trigger mechanism is fired again or data corruption is inevitable. You can test this in your application by checking to see if the trigger variable has been set again at the end of the trigger code.


Tick

The FunctionTick type hardware macros allow a single value in the DSP buffer to be processed. Behind the scenes this performs the hardware macro's functionality on a single value. To move to the next value in the buffer ready for the next operation we need to call the DSP system macro to move to the next value in a specific buffer or in all buffers.


The tick based functions are designed to operate inside the interrupt function at sample rate. This allows your DSP system to utilize more of the system runtime but means you have less time for other functions such as refreshing a display. Because you are only processing a single value in a buffer rather then an entire buffer the function code is much shorter. This also has a second impact that there is no longer a delay in data being held in the buffer before being processed. The location inside the buffer is automatically tracked for you by the master buffer index. Calling the DSP System tick component macro at the end of your interrupt function will increment the master buffer index for you. This component macro also has a return value which signifies the end of the buffer has been reached and is what is used to control the state of the trigger variable.

DSP8.jpg

Combining Buffer and Tick

Both methods of DSP processing have upsides and downsides which cause problems and headaches when dealing with a finite amount of processing runtime and RAM. Before designing a entire DSP system it is important to play around with the individual components to get a feel for what is needed.


For example a system which performs a FFT and then plots the signal data onto a display needs to sample data regularly when populating the buffer so the tick method is ideal. When performing the FFT conversion and writing to the display we no longer need to have constant data and so the trigger approach can be used.


Alternatively an audio system producing an effect onto an output stream needs to have fairly high sample rates, constant data and minimal lag so the tick style approach is more suited.


The tick style approach is similar to using the trigger style approach with a buffer size of one so really the design of the system is entirely up to what you feel most comfortable with and what best suits your application.