# SimpleDSP

SimpleDSP is a basic DSP library which is for Arduino and most of the microcontrollers which can be programmed in C/C++

## Performance:

Here is some examples for its performance. Actually it is not optimized for performance. Its main focuses are portability and education.

### Arduino Nano

- FFT 16 points: 2 ms
- FFT 32 points: 6 ms
- FFT 64 points: 16 ms
- fir filter 10 coefficients: 190 us
- fir filter 23 coefficients: 453 us
- fir filter 46 coefficients: 900 us

10 coefficients FIR filter can be run at 5khz max frequency on an Arduino Nano.

## Arduino Due

- FFT 64 points: 2 ms
- FFT 128 points: 6 ms
- FFT 256 points: 10 ms

## Fast Fourier Transform (FFT) and Inverse Fast Fourier Transform (IFFT)

### Arduino Example

FFT and IFFT functions require 2 arguments.

data

data length

Do not forget to add `#include "simpleDSP_FFT.h"`

Full example for FFT and IFFT please refer here

1 | FFT(data,DATA_LEN); |

## FIR Filter

### Theory

### Arduino Example

FIR is filter structure which keep delays and coefficients of filter. There are 2 public functions. `firInit`

initializes the structure according to parameters and makes required memory allocations. Coefficients and its length are given as parameter to this function

Do not forget to add `#include "simpleDSP_fir.h"`

1 | void firInit(FIR *fir, int coefBLen, float *coefsB); |

1 | Serial.begin(9600); |

## IIR Filter

### Theory

### Arduino Example

IIR is filter structure which keep delays and coefficient of filter. There are 2 functions to implement IIR filter using SimpleDSP library. initIIR function is the constructor function for the filter. Filter coefficients for a and b should be given as arguments to this function. After init, filtIIR must be used as filtering function. This function requires 2 arguments. Object instance and data. It returns the filtered output.

1 | void iirInit(IIR *iir, int coefBLen, float *coefsB, int coefALen, float *coefsA); |

Do not forget to add `#include "simpleDSP_iir.h"`

to your code.

1 | Serial.begin(9600); |

## Octave Test Code

This code create sample data and plot the signal and its FFT. The octave code only needed for testing of function.

Create sample signal which is at 10 kHz sample rate and it is the combination of 3.2 kHz and 800 Hz sine waves.

1 | N=255; |

Calculate and plot x and FFT of x. For fft x axis values are calculated using `kor=(1:N)*fo/N;`

1 | plot(x); |

**Output from octave**

Graph of Raw Signal

Graph of FFT Signal

### Filter Design Using Octave

output of fir functions can be used as filter coefficients

Examples:

1 | freqz (fir1 (40, 0.3)); |

1 | figure |

filter frequency response

1 | figure |

1 | octave:> b |

**Outputs after filtering**

Time Domain Signal

FFT of Output Signal