-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
edition = "2021" | ||
name = "qdft" | ||
version = "0.1.0" | ||
authors = ["Juergen Hock <[email protected]>"] | ||
repository = "https://github.com/jurihock/qdft" | ||
description = "Constant-Q Sliding DFT" | ||
readme = "README.md" | ||
license = "MIT" | ||
keywords = ["cqt", "constant-q", "sliding", "dft", "discrete", "fourier", "transform", "audio", "signal-processing", "dsp", "spectrum", "frequencies", "hz"] | ||
categories = ["algorithms", "mathematics", "multimedia", "science"] | ||
|
||
[dependencies] | ||
num = "*" | ||
|
||
[dev-dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.PHONY: help build clean example test | ||
|
||
help: | ||
@echo build | ||
@echo clean | ||
|
||
build: | ||
@cargo build | ||
|
||
clean: | ||
@rm -rf target | ||
|
||
example: | ||
@cargo build --examples | ||
@cargo run --example analysis | ||
|
||
test: | ||
@cargo build --tests | ||
@cargo test --workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use num::Zero; | ||
use num::complex::Complex; | ||
use qdft::QDFT; | ||
|
||
fn main() { | ||
let mut qdft = QDFT::new(44100.0, (100.0, 20000.0)); | ||
|
||
let n = 1; | ||
let mut samples = vec![f32::zero(); n]; | ||
let mut dfts = vec![Complex::<f64>::zero(); n]; | ||
|
||
qdft.qdft(&samples, &mut dfts); | ||
qdft.iqdft(&dfts, &mut samples); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#![allow(unused)] | ||
|
||
use num::Float; | ||
use num::Zero; | ||
use num::complex::Complex; | ||
use std::marker::PhantomData; | ||
|
||
pub struct QDFT<T, F> | ||
where T: Float, | ||
F: Float { | ||
samplerate: f64, | ||
bandwidth: (f64, f64), | ||
t: PhantomData<T>, | ||
f: PhantomData<F>, | ||
} | ||
|
||
pub type QDFT32 = QDFT<f32, f64>; | ||
pub type QDFT64 = QDFT<f64, f64>; | ||
|
||
impl<T, F> QDFT<T, F> | ||
where T: Float, | ||
F: Float { | ||
|
||
pub fn new(samplerate: f64, | ||
bandwidth: (f64, f64) | ||
) -> Self { | ||
QDFT { | ||
samplerate: samplerate, | ||
bandwidth: bandwidth, | ||
t: PhantomData, | ||
f: PhantomData, | ||
} | ||
} | ||
|
||
pub fn qdft_scalar(&mut self, sample: &T, dft: &mut Complex::<F>) { | ||
*dft = Complex::<F>::zero(); | ||
} | ||
|
||
pub fn iqdft_scalar(&mut self, dft: &Complex::<F>, sample: &mut T) { | ||
*sample = T::zero(); | ||
} | ||
|
||
#[inline] | ||
pub fn qdft_vector(&mut self, samples: &[T], dfts: &mut [Complex::<F>]) { | ||
for i in 0..samples.len() { | ||
self.qdft_scalar(&samples[i], &mut dfts[i]); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn iqdft_vector(&mut self, dfts: &[Complex::<F>], samples: &mut [T]) { | ||
for i in 0..samples.len() { | ||
self.iqdft_scalar(&dfts[i], &mut samples[i]); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn qdft(&mut self, samples: &[T], dfts: &mut [Complex::<F>]) { | ||
self.qdft_vector(samples, dfts); | ||
} | ||
|
||
#[inline] | ||
pub fn iqdft(&mut self, dfts: &[Complex::<F>], samples: &mut [T]) { | ||
self.iqdft_vector(dfts, samples); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
|
||
use num::One; | ||
use num::Zero; | ||
use num::complex::Complex; | ||
use qdft::QDFT; | ||
|
||
#[test] | ||
fn test() { | ||
let mut qdft = QDFT::new(44100.0, (100.0, 20000.0)); | ||
|
||
let n = 1; | ||
let mut samples = vec![f32::zero(); n]; | ||
let mut dfts = vec![Complex::<f64>::zero(); n]; | ||
|
||
dfts[0] = Complex::one(); | ||
assert_ne!(dfts[0], Complex::zero()); | ||
|
||
qdft.qdft(&samples, &mut dfts); | ||
assert_eq!(dfts[0], Complex::zero()); | ||
|
||
samples[0] = f32::one(); | ||
assert_ne!(samples[0], f32::zero()); | ||
|
||
qdft.iqdft(&dfts, &mut samples); | ||
assert_eq!(samples[0], f32::zero()); | ||
} | ||
} |