Skip to content

Instantly share code, notes, and snippets.

@jurihock
Last active August 26, 2023 19:29
Show Gist options
  • Save jurihock/599ee96a359589982a036fbb4b09a655 to your computer and use it in GitHub Desktop.
Save jurihock/599ee96a359589982a036fbb4b09a655 to your computer and use it in GitHub Desktop.
SDFT vs. QDFT
# Basic SDFT vs. QDFT showcase based on figures from
# "Sliding with a constant Q" by Russell Bradford
import matplotlib.pyplot as plot
import matplotlib.ticker as ticker
import numpy as np
from sdft import SDFT
from qdft import QDFT
sr = 44100 # sample rate in hertz
n = sr//2 # test signal length in samples
t = np.arange(n) / sr # test signal timestamps in seconds
f = np.array([100, 110, 120, 1000, 10000, 11000, 12000]) # test signal frequencies in hertz
x = np.mean(np.sin(2 * np.pi * f[:, None] * t), axis=0) # test signal samples of shape (n)
sdft = SDFT(500) # sdft plan
qdft = QDFT(sr, (50, sr/2)) # qdft plan
ylin = sdft.sdft(x) # total sdft matrix of shape (n, sdft.size)
ylog = qdft.qdft(x) # total qdft matrix of shape (n, qdft.size)
ylin = np.abs(ylin)[-1] # last sdft vector of shape (sdft.size)
ylog = np.abs(ylog)[-1] # last qdft vector of shape (qdft.size)
xlin = [np.arange(sdft.size), np.linspace(0, sr / 2, sdft.size)] # sdft bin numbers and frequencies
xlog = [np.arange(qdft.size), qdft.frequencies] # qdft bin numbers and frequencies
figure, axes = plot.subplots(2, 1)
axes[0].plot(ylin)
axes[0].set_title('SDFT')
axes[0].set_xlabel('Bin number / frequency')
axes[0].set_ylabel('Magnitude response')
axes[1].plot(ylog)
axes[1].set_title('QDFT')
axes[1].set_xlabel('Bin number / frequency')
axes[1].set_ylabel('Magnitude response')
axes[0].xaxis.set_major_formatter(ticker.FuncFormatter(
lambda t, p: f'{int(t)}\n{int(np.interp(t, xlin[0], xlin[1]))} Hz'))
axes[1].xaxis.set_major_formatter(ticker.FuncFormatter(
lambda t, p: f'{int(t)}\n{int(np.interp(t, xlog[0], xlog[1]))} Hz'))
plot.tight_layout()
plot.show()
@jurihock
Copy link
Author

jurihock commented Aug 26, 2023

sdft_vs_qdft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment