Code Monkey home page Code Monkey logo

qdft's Introduction

Constant-Q Sliding DFT in C++, Rust, and Python

language license pypi creates

Forward and inverse Constant-Q Sliding DFT (QDFT) according to [1] with following features:

  • Arbitrary octave resolution (quarter tone by default)
  • Built-in parameterizable cosine family window (Hann by default)
  • Customizable time and frequency domain data type in C++
  • Endless single or multiple sample processing at once
  • Optional quality control parameter to smoothly reduce low frequency bandwidth and improve the time resolution
  • Optional analysis latency control parameter
  • Real-time analysis and synthesis capability

The Constant-Q Sliding Discrete Fourier Transform (QDFT) is a recursive approach to compute the Fourier transform sample by sample. This is an efficient implementation without the FFT calculus. Just define an arbitrary frequency range and octave resolution to obtain the corresponding DFT estimate. In contrast to the linear SDFT, frequency bins of the QDFT are logarithmically spaced. Thus, both high and low frequencies are resolved with the same quality, which is particularly useful for audio analysis. Based on the QDFT, a chromagram feature with detailed instantaneous frequency estimation is planned for the future release.

WIP

  • Readme
  • Docstrings
  • PyPI package qdft
  • Rust package qdft
  • Sliding chromagram as a bonus (a draft is already included in the Python package)

Basic usage

C++

#include <qdft/qdft.h> // see also cpp folder

double sr = 44100;                             // sample rate in hertz
std::pair<double, double> bw = { 50, sr / 2 }; // lowest and highest frequency in hertz to be resolved
double r = 24;                                 // octave resolution, e.g. number of DFT bins per octave

QDFT<float, double> qdft(sr, bw, r); // create qdft plan for custom time and frequency domain data types

size_t n = ...;         // number of samples
size_t m = qdft.size(); // number of dft bins

float* x = ...; // analysis samples of shape (n)
float* y = ...; // synthesis samples of shape (n)

std::complex<double>* dft = ...; // dft matrix of shape (n, m)

qdft.qdft(n, x, dft);  // extract dft matrix from input samples
qdft.iqdft(n, dft, y); // synthesize output samples from dft matrix

The time domain data type defaults to float and the frequency domain data type to double.

Rust

use qdft::QDFT; // see also rust folder

// just a shortcut for our complex number type
#[allow(non_camel_case_types)]
type c64 = num::complex::Complex<f64>;

// zero number trait, e.g. c64::zero()
use num::Zero;

let samplerate = 44100.0;                 // sample rate in hertz
let bandwidth = (50.0, samplerate / 2.0); // lowest and highest frequency in hertz to be resolved
let resolution = 24.0;                    // octave resolution, e.g. number of DFT bins per octave
let latency = 0.0;                        // analysis latency adjustment between -1 and +1
let window = Some((0.5, -0.5));           // hann window coeffs

// create qdft plan for custom time and frequency domain data types
let mut qdft = QDFT::<f32, f64>::new(
    samplerate,
    bandwidth,
    resolution,
    latency,
    window);

let n = ...;         // number of samples
let m = qdft.size(); // number of dft bins

let mut x = vec![f32::zero(); n]; // analysis samples of shape (n)
let mut y = vec![f32::zero(); n]; // synthesis samples of shape (n)

let mut dft = vec![c64::zero(); n * m]; // dft matrix of shape (n, m)

qdft.qdft(&x, &mut dft);  // extract dft matrix from input samples
qdft.iqdft(&dft, &mut y); // synthesize output samples from dft matrix

Alternatively use ndarray instead of a flat array to allocate the DFT matrix, as shown in the analysis.rs example.

Python

from qdft import QDFT # see also python folder

sr = 44100        # sample rate in hertz
bw = (50, sr / 2) # lowest and highest frequency in hertz to be resolved
r = 24            # octave resolution, e.g. number of DFT bins per octave

qdft = QDFT(sr, bw, r) # create qdft plan

n = ...       # number of samples
m = qdft.size # number of dft bins (if need to know in advance)

x = ... # analysis samples of shape (n)

dft = qdft.qdft(x)  # extract dft matrix of shape (n, m) from input samples
y = qdft.iqdft(dft) # synthesize output samples from dft matrix

Feel free to obtain current version from PyPI by executing pip install qdft.

Examples

QDFT Chroma12
SDFT STFT
face.py cmajor.py
SDFT STFT

See also

If you're interested in Sliding DFT with linear frequency resolution, don't forget to browse my jurihock/sdft project!

References

  1. Russell Bradford et al. (2008). Sliding with a Constant Q. International Conference on Digital Audio Effects. https://www.dafx.de/paper-archive/2008/papers/dafx08_63.pdf

  2. Russell Bradford et al. (2005). Sliding is Smoother Than Jumping. International Computer Music Conference Proceedings. http://hdl.handle.net/2027/spo.bbp2372.2005.086

  3. Eric Jacobsen and Peter Kootsookos (2007). Fast, Accurate Frequency Estimators. IEEE Signal Processing Magazine. https://ieeexplore.ieee.org/document/4205098

License

github.com/jurihock/qdft is licensed under the terms of the MIT license. For details please refer to the accompanying LICENSE file distributed with it.

qdft's People

Contributors

jurihock avatar stellarpower avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qdft's Issues

IIR windowing

It is worth noting that asymmetric window functions are impossible to implement, not even the window skew parameter outlined in my collection of audio analysis functions on a sliding DFT, but what you can do is make the sDFT an IIR filter bank (which is a damped sine wave). which uses less RAM (since it doesn't need ring buffers) and it can be cascaded (just like biquad filters) to achieve a steeper rolloff at the cost of greater delay (thus, bad time resolution) on long window sizes

Variable-Q transform and arbitrary frequency scale

Variable-Q transform is very similar to constant-Q transform except the Q value is lower as the frequency decreases, which is useful if you want to have better time resolution on lower frequencies at the expense of frequency resolution at bass frequencies (like a logarithmic-frequency spectrogram with ERB frequency resolution).

Arbitrary frequency scale for bin spacing also enables variable-Q transform and with that, it can directly calculate Mel spectrogram using VQT with Mel-frequency bin spacing and resolution.

Optimize circular buffer shift in the C++ implementation

As mentioned in my comment, the std::rotate is most likely a major performance issue.

Actually there is no measurable difference between the following variations:

  • std::rotate(inputs.begin(), inputs.begin() + 1, inputs.end());
  • std::copy(inputs.begin() + 1, inputs.end(), inputs.begin());
  • std::shift_left(inputs.begin(), inputs.end(), 1);

But instead of std::vector use std::deque to shift the buffered values like so:

  • inputs.pop_front();
  • inputs.push_back(sample);

The complexity of the linear/random element access as well as insertion or removal of elements at both ends of the std::deque should be still O(1).

Improvements

  • Basic usage as a standalone example
  • Additional getters
  • C++ namespace qdft
  • Rework convolution loop
  • Try to build all C++ examples as possible, regardless of the Python availability
  • Bin latency values in seconds

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.