Skip to content

Commit

Permalink
Update analysis example #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jun 18, 2023
1 parent 801de1f commit 4497596
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Thumbs.db

test.*.dft
test.*.wav

*.npy
2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ categories = ["algorithms", "mathematics", "multimedia", "science"]
num = "*"

[dev-dependencies]
ndarray = "0.15.*"
ndarray-npy = "0.8.*"
1 change: 1 addition & 0 deletions rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clean:
example:
@cargo build --examples
@cargo run --example analysis
@python examples/analysis.py

test:
@cargo build --tests
Expand Down
35 changes: 35 additions & 0 deletions rust/examples/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import matplotlib.pyplot as plot
import numpy as np
import os

npy = os.path.join("examples", "analysis.npy")
dft = np.load(npy)

sr = 44100
n = len(dft)

with np.errstate(all='ignore'):
db = 20 * np.log10(np.abs(dft))

roi = (0, n / sr, 0, 1)

args = dict(extent=roi,
origin='lower',
aspect='auto',
cmap='inferno',
interpolation='nearest')

plot.imshow(db.T, **args)
cbar = plot.colorbar()

plot.xlabel('s')
plot.ylabel('Hz')
cbar.set_label('dB')

plot.clim(-120, 0)

# freqs = qdft.frequencies
# ticks = np.linspace(0, 1, freqs.size, endpoint=True)
# plot.gca().yaxis.set_major_formatter(lambda tick, _: np.interp(tick, ticks, freqs).astype(int))

plot.show()
42 changes: 34 additions & 8 deletions rust/examples/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use num::Zero;
use num::complex::Complex;
use qdft::QDFT;

use ndarray::Array1 as Vector;
use ndarray::Array2 as Matrix;
use ndarray_npy::write_npy;

#[allow(non_camel_case_types)]
type c64 = num::complex::Complex<f64>;

fn phase(t: &Vector<f64>, f: &Vector<f64>) -> Vector<f64>
{
let pi = std::f64::consts::PI;
let mut cs = Vector::<f64>::zeros(t.len());

for i in 1 .. t.len() {
let dt = t[i] - t[i - 1];
let dp = 2.0 * pi * f[i] * dt;
cs[i] = dp + cs[i - 1];
}

return cs;
}

fn main() {
let samplerate = 44100.0;
let bandwidth = (50.0, samplerate / 2.0);
Expand All @@ -16,12 +35,19 @@ fn main() {
latency,
window);

let n = 1;
let m = n * qdft.size();
let n = 1 * samplerate as usize;
let m = qdft.size();

let t = Vector::<f64>::range(0.0, n as f64, 1.0) / samplerate;
let f = Vector::<f64>::ones(n) * 1000.0;

let x = phase(&t, &f).mapv_into(|v|v.sin());
let mut y = Matrix::<c64>::zeros((n, m));

qdft.qdft(x.as_slice().unwrap(), y.as_slice_mut().unwrap());

let mut samples = vec![f32::zero(); n];
let mut dfts = vec![Complex::<f64>::zero(); m];
let npy = std::path::Path::new("examples").join("analysis.npy");
let error = format!("Unable to create {}!", npy.to_str().unwrap());

qdft.qdft(&samples, &mut dfts);
qdft.iqdft(&dfts, &mut samples);
write_npy(npy, &y).expect(error.as_str());
}
5 changes: 2 additions & 3 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(unused)]

use num::Float;
use num::Zero;
use num::{Float, Zero};
use num::complex::Complex;
use std::collections::VecDeque;

Expand Down Expand Up @@ -278,6 +277,6 @@ mod tests {
}

assert_eq!(i, qdft.size);
assert_eq!(j, (qdft.size as isize) * 3 + 1);
assert_eq!(j, (qdft.size * 3 + 1) as isize);
}
}
18 changes: 10 additions & 8 deletions rust/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#[cfg(test)]
mod tests {

use num::One;
use num::Zero;
use num::complex::Complex;
use qdft::QDFT;

use num::{One, Zero};

#[allow(non_camel_case_types)]
type c64 = num::complex::Complex<f64>;

#[test]
fn test_one_in_zero_out() {
let samplerate = 44100.0;
Expand All @@ -22,16 +24,16 @@ mod tests {
window);

let n = 1;
let m = n * qdft.size();
let m = qdft.size();

let mut samples = vec![f32::zero(); n];
let mut dfts = vec![Complex::<f64>::zero(); m];
let mut dfts = vec![c64::zero(); n * m];

dfts[0] = Complex::one();
assert_ne!(dfts[0], Complex::zero());
dfts[0] = c64::one();
assert_ne!(dfts[0], c64::zero());

qdft.qdft(&samples, &mut dfts);
assert_eq!(dfts[0], Complex::zero());
assert_eq!(dfts[0], c64::zero());

samples[0] = f32::one();
assert_ne!(samples[0], f32::zero());
Expand Down

0 comments on commit 4497596

Please sign in to comment.