A basic (soft) dsp library for arduino and most of the microcontrollers which can be programmed in C/C++
SimpleDSP is a basic DSP library which is for arduino and most of the microcontrollers which can be programmed in C/C++
Here is some examples for its performance. Actually it is not optimized for performance. Its main focuses are portability and education.
10 coeficients FIR filter can be run at 5khz max frequency on an Arduino Nano.
FFT and IFFT functions require 2 arguments.
data
data lenght
Do not forget to add #include "simpleDSP_FFT.h"
Full example for FFT and IFFT please refer here
FFT(data,DATA_LEN);
IFFT(data,DATA_LEN);
calcTime = millis()-startTime;
Serial.print("Total calculation time: ");
Serial.println(calcTime);
FIR is filter structure which keep delays and coeficients of filter. There are 2 public functions. firInit
initiliazes the structure according to parameters and makes required memory allocations. Coeficients and its lenght are given as parameter to this function
Do not forget to add #include "simpleDSP_fir.h"
void firInit(FIR *fir, int coefBLen, float *coefsB);
float firFilt(FIR *fir, int input);
Serial.begin(9600);
firInit(&fir1, 46, coef);
Serial.println("FIR filter initiliaze finished");
float a;
startTime = micros();
for (int i = 0; i < 255; i++)
{
a = firFilt(&fir1, input[i]);
}
calcTime = micros() - startTime;
Serial.print("Total calculation time: ");
Serial.println(calcTime);
IIR is filter structure which keep delays and coeficient of filter. There are 2 functions to implement IIR filter using SimpleDSP library. initIIR function is the constructor function for the filter. Filter coeficients 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.
void iirInit(IIR *iir, int coefBLen, float *coefsB, int coefALen, float *coefsA);
float iirFilt(IIR *iir, int input);
Do not forget to add #include "simpleDSP_iir.h"
to your code.
Serial.begin(9600);
iirInit(&iir1, 4, coefB, 4, coefA);
Serial.println("IIR filter initiliaze finished");
float a;
startTime = micros();
for (int i = 0; i < 255; i++)
{
a = iirFilt(&iir1, input[i]);
}
calcTime = micros() - startTime;
Serial.print("Total calculation time: ");
Serial.println(calcTime);
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.
N=255;
f1=800;
f2=3200;
fo=10000;
for i=1:1:N
x(i)=1000*cos(2*pi*f1*i/fo)+1000*cos(2*pi*f2*i/fo);
printf("%d\n",x(i));
end
Calculate and plot x and FFT of x. For fft x axis values are calculated using kor=(1:N)*fo/N;
plot(x);
X=abs(FFT(x));
figure;
kor=(1:N)*fo/N;
plot(kor,X);
Output from octave
Graph of Raw Signal
Graph of FFT Signal
output of fir functions can be used as filter coeficients
Examples:
freqz (fir1 (40, 0.3));
freqz (fir1 (15, [0.2, 0.5], "stop")); # note the zero-crossing at 0.1
freqz (fir1 (15, [0.2, 0.5], "stop", "noscale"));
figure
b= fir1 (20, 0.3, "low");
y = filter(b,1,x);
plot(y)
filter frequency response
figure
[b, a] = butter(3, 0.3, "low");
y = filter(b,a,x);
plot(y)
octave:> b
b =
0.049533 0.1486 0.1486 0.049533
octave:> a
a =
1.00000 -1.16192 0.69594 -0.13776
Outputs after filtering
Time Domain Signal
FFT of Output Signal