DSP C++ audio filters

Overview

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:

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; }">
#include <iostream>
#include <memory>
#include "filter_common.h"
#include "filter_includes.h"

int main() {
    std::unique_ptr 
   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
You might also like...
Easy and efficient audio synthesis in C++

Tonic Fast and easy audio synthesis in C++. Prefer coding to patching? Love clean syntax? Care about performance? That's how we feel too, and why we m

An audio mixer that supports various file formats for Simple Directmedia Layer.

An audio mixer that supports various file formats for Simple Directmedia Layer.

Explore fractals in an audio-visual sandbox
Explore fractals in an audio-visual sandbox

Fractal Sound Explorer Explore fractals in an audio-visual sandbox Download executable on my itch.io page: https://codeparade.itch.io/fractal-sound-ex

Audio out with an FTDI UART cable

Audio out with an FTDI UART cable This encodes audio as either PDM (using a first order sigma-delta stage), 32-bits PWM or 64-bits PWM and sends it as

Sexy, audio-responsive effects on LED strips.
Sexy, audio-responsive effects on LED strips.

Striptease Sexy, audio-responsive effects on LED strips. For Teensy 4 with Audio Adapter Board, by PJRC. Quick demo Shooting video of LEDs is very tri

audio monitor filter for OBS Studio
audio monitor filter for OBS Studio

Audio Monitor dock and filter for OBS Studio Plugin for OBS Studio to add Audio Monitor dock and filter. It allows you to put the audio of a OBS sourc

Lightweight Embedded Audio Framework

LEAF (Lightweight Embedded Audio Framework) is a C library for audio synthesis and processing created by Jeff Snyder, Mike Mulshine, and Matt Wang at Princeton University's New Instrument Research Lab. It was originally called OOPS when we started writing it in 2017, so you may see references to it under that name as well.

An Audio Steganography Tool, written in C++
An Audio Steganography Tool, written in C++

HiddenWave Hide your personal Data inside The Audio wav file Written in C++ by Gaurav Raj [TheHackersBrain] Hiddenwave is an audio steganography tool

C++ audio time-stretching implementation

Time Stretcher C++ audio time-stretching implementation, based on the algorithms presented in: Audio Time Stretching with an Adaptive Phase Vocoder, N

Comments
  • Difference between the Boost and cut parametric filters

    Difference between the Boost and cut parametric filters

    What is the difference between the two? I want to give the option to boost or cut a certain frequency, but usually, this is done using a multiplier with 0 being the middle. However, it seems that you have separate filters for boost and cut. Should I be selecting which one based on what the multiplier value is (> 0 or <0) or do I only need one that I can enter both a positive and negative multiplier in?

    opened by johnroper100 3
  • Second order All pass

    Second order All pass "so_apf.h" might have missing frequency

    I can see at top comment section mentioning frequency but parameters are only for Q and samplerate(fs). Then when I test filter it gives only abrupt bump. Happily nothing worse happens though. I think one of those fs's must be the frequency but I don't understand much about filters math so I cannot correct the situation my self. Maybe you could kindly review the second order all pass code to clarify which is the frequency - if any. It is this part which I feel is missing frequency parameter and corresponding code part in file so_apf.h:

    tp_coeffs& calculate_coeffs(float Q, int fs)

    opened by visuttaja 6
  • [REQ] Set license on GH

    [REQ] Set license on GH

    Hi there, this project is awesome !

    Since we've collected it in our HyMPS project list (in the Audio DSPs section), can you please set licensing terms on this git ?

    Our "problem" is that does not generates the correct badge:

    (generate string: https://flat.badgen.net/github/license/dimtass/DSP-Cpp-filters/?label=LICENSE)

    ...maybe it's a GH setup issue ?

    Thanks in advance.

    opened by forart 3
Owner
Dimitris Tassopoulos
Wannabe engineer.
Dimitris Tassopoulos
multichannel eurorack dsp platform

Nemesis multichannel dsp platform for eurorack \hardware panel, pcb and gerber files \software project template \doc datasheets and additional informa

Arev Imer 24 Oct 7, 2022
A lightweight music DSP library.

Soundpipe Soundpipe is a lightweight music DSP library written in C. It aims to provide a set of high-quality DSP modules for composers, sound designe

Paul Batchelor 102 Dec 14, 2021
cute_dsp is a C API for various DSP effects suitable for video games

cute_dsp is a C API for various DSP effects suitable for video games and meant to interface directly with the cute_sound library created by Randy Gaul

Matt Rosen 13 May 29, 2021
PortAudio is a portable audio I/O library designed for cross-platform support of audio

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.

PortAudio 750 Nov 19, 2022
A simple C++ library for reading and writing audio files.

AudioFile A simple header-only C++ library for reading and writing audio files. Current supported formats: WAV AIFF Author AudioFile is written and ma

Adam Stark 667 Nov 22, 2022
A C library for reading and writing sound files containing sampled audio data.

libsndfile libsndfile is a C library for reading and writing files containing sampled audio data. Authors The libsndfile project was originally develo

null 1.1k Nov 15, 2022
C library for cross-platform real-time audio input and output

libsoundio C library providing cross-platform audio input and output. The API is suitable for real-time software such as digital audio workstations as

Andrew Kelley 1.6k Nov 22, 2022
Single file audio playback and capture library written in C.

A single file library for audio playback and capture. Example - Documentation - Supported Platforms - Backends - Major Features - Building - Unofficia

David Reid 2.5k Nov 25, 2022
SimplE Lossless Audio

SELA SimplE Lossless Audio A lossless audio codec which aims to be as simple as possible while still having good enough compression ratios. Code Quali

Ratul Saha 207 Sep 13, 2022