• Stars
    star
    152
  • Rank 243,932 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created about 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DSP C++ audio filters

DSP filters in C++

dimtass

This repo contains some DSP biquad filters used in audio. I've extracted those filters from the Designing Audio Effect Plug-Ins in C++: With Digital Audio Signal Processing Theory book that you can find here.

I've also implemented a real-time testing on a Cortex-M4 MCU, using the on-chip ADC and DAC. You can find the post here.

This is the formula I'm using for the digital biquad filter in the source code:

y(n) = a0*x(n) + a1*x(n-1) + a2*x(n-2) - b*y(n-1) + b2*y(n-2)
  • First order all-pass filter (fo_apf)
  • First order high-pass filter (fo_hpf)
  • First order low-pass filter (fo_lpf)
  • First order high-shelving filter (fo_shelving_high)
  • First order low-shelving filter (fo_shelving_low)
  • Second order all-pass filter (so_apf)
  • Second order band-pass filter (so_bpf)
  • Second order band-stop filter (so_bsf)
  • Second order Butterworth band-pass filter (so_butterworth_bpf)
  • Second order Butterworth band-stop filter (so_butterworth_bsf)
  • Second order Butterworth high-pass filter (so_butterworth_hpf)
  • Second order Butterworth low-pass filter (so_butterworth_lpf)
  • Second order high-pass filter (so_hpf)
  • Second order Linkwitz-Riley high-pass filter (so_linkwitz_riley_hpf)
  • Second order Linkwitz-Riley low-pass filter (so_linkwitz_riley_lpf)
  • Second order Low-pass filter (so_lpf)
  • Second order parametric/peaking boost filter with constant-Q (so_parametric_cq_boost)
  • Second order parametric/peaking cut filter with constant-Q (so_parametric_cq_cut)
  • Second order parametric/peaking filter with non-constant-Q (so_parametric_ncq)

All the filters are now header files and they are located in the lib/ folder. In order to use them just copy the lib/ folder (and rename it if needed) into your project folder. There's an example how to build later in the README.

Build & run tests

You can use cmake to build the tests. On Linux, you can just run this:

./build_tests.sh

The above command will build the tests and run them.

Note: Tests are a bit naive in this case, since it doesn't make much sense for testing using unit-tests, but anyways, I've added them

Usage:

The filters can be used in your C++ code in the part where the audio sample is about to be processed. You need to include the filter_common.h and filter_includes.h files and the create an object with filter(s) you want to apply and calculate the coefficients with the calculate_coeffs() function. Then in the sample processing function run the filter() function with the current sample as a parameter.

I've used RackAFX to test these filters.

Code example

For example, to use the so-LPF filter then first create a main.cpp file in the top directory of this repo.

touch main.cpp

Then add this code inside:

#include <iostream>
#include <memory>
#include "filter_common.h"
#include "filter_includes.h"

int main() {
    std::unique_ptr<SO_LPF> filter (new SO_LPF);

    auto coeffs = filter->calculate_coeffs(1.0, 5000, 96000);
    auto yn = filter->process(0.303);

    std::cout << "Coeffs: " << std::endl;
    std::cout << "a0: " << coeffs.a0 << std::endl;
    std::cout << "a1: " << coeffs.a1 << std::endl;
    std::cout << "a2: " << coeffs.a2 << std::endl;
    std::cout << "b1: " << coeffs.b1 << std::endl;
    std::cout << "b2: " << coeffs.b2 << std::endl;

    std::cout << "yn: " << yn << std::endl;
    return 0;
}

Now to build the file run:

g++ main.cpp -I./lib

And then run the executable:

./a.out

This is will print the filter coefficients and then will process a sample with the value 0.303. You should see an output similar to this:

Coeffs: 
a0: 0.0228608
a1: 0.0457215
a2: 0.0228608
b1: -1.63163
b2: 0.723069
yn: 0.00692681

More Repositories

1

stm32f746-tflite-micro-mnist

MNIST inference on STM32F746 using TensorFlow Lite for Microcontrollers
C
23
star
2

dsp-c-filters

DSP filters in C
C
17
star
3

meta-allwinner-hx

Yocto meta layer that supports allwinner H2, H3, H5 and H6 cpus (unofficial)
PHP
16
star
4

stm32f103-ili9341-dma

STM32F103 & ILI9341 with SPI/DMA & overclocking
C
16
star
5

stm32f103-cmake-template

STM32F103 cmake template
C
14
star
6

imxrt1062-tflite-micro-mnist

MNIST inference on i.MT RT1062 (Teensy 4.0) using TensorFlow Lite for Microcontrollers
C
12
star
7

stm32f746-x-cube-ai-mnist

MNIST inference on STM32F746 using X-CUBE-AI
C
9
star
8

stm32mp1-rpmsg-netlink-example

C
7
star
9

stm32f303-adc-dsp-dac

Using an STM32F303CC with an ADC and DAC to process samples with digital biquad filters.
C
6
star
10

stm32mp1-rpmsg-adcsampler

A firmware for STM32MP1 that reads 4x ADC channels via DMA and sends the values to the Cortex-A using OpenAMP.
C
5
star
11

stm32mp1-cmake-template

CMake template for STM32MP157C
C
4
star
12

meta-elastic-beats

Yocto meta layer for elastic-beats
C++
3
star
13

stm32mp1-rpmsg-test

Benchmark tool for OpenAMP (tty) on STM32MP1
C
3
star
14

stm32-usb-fs-device-driver

STM32 USB FS Device Driver for STM32F10x, STM32L1xx and STM32F3xx
C
3
star
15

linux-arduino-spi-i2c

Linux and the I2C and SPI interface
C
3
star
16

meta-stm32mp1-bsp-base

Shell
3
star
17

jetson-nano-tflite-mnist

MNIST tflite cloud server with ESP8266 and Jetson nano
C++
2
star
18

stm32f103-wifi-usb-psu

Power supply with WiFi, USB and UART interface using STM32F103, ESP8266 and MCP41010
C
2
star
19

meta-nanopi-rockchip64

Pascal
2
star
20

stm32f303-cmake-template

CMake template for the STM32F303CC (black-pill from RoboDyn)
C
2
star
21

linux-stm32-spi-i2c

Linux, the I2C and SPI interface and the SMP and PREEMPT-RT kernel
C
2
star
22

stm32f4xx-cmake-template

This is a template cmake project for the stm32f4xx.
C
2
star
23

machine-learning-for-embedded

This repo is part of my blog series which is called Machine Learning on Embedded: https://www.stupid-projects.com/machine-learning-on-embedded-part-1/
C
2
star
24

docker-registry-sbc

Docker registry running on any SBC e.g. nanopi
1
star
25

go-config

Config parser for go that supports environment vars and multiple yaml files
Go
1
star
26

noarch_c_lib

This is an architecture independent C lib that can be used on various embedded projects and different MCUs.
C
1
star
27

stm32f103-i2c-io-expander

C
1
star
28

tflite-micro-python-comparison

Benchmarking TensorFlow Lite for microcontrollers on amd64
C++
1
star
29

teensy-hid-with-unity3d

Controlling a 3D object in Unity3D with Teensy and MPU-6050
C#
1
star
30

stm32f407_dds_dac

DDS generator using internal DAC on a STM32F407
C
1
star
31

stm32f103-usb-joystick

STM32F103 USB joystick with gestures
C
1
star
32

STM32F7xx_HAL_Driver

STM32F7xx HAL Library Driver
C
1
star