w = np.linspace(1e-3, np.pi, 600); z1 = np.exp(-1j * w)
def lagrange3(d): # taps for delay 1 + d (centered), returns (filter, extra delay)
D_ = 1 + d; n_ = np.arange(4)
return np.array([np.prod([(D_ - m) / (n - m) for m in n_ if m != n]) for n in n_]), 1
filt = {"linear": lambda d: ((1 - d) + d * z1, 0), "Lagrange 3": lambda d: (np.polyval(lagrange3(d)[0][::-1], z1), 1),
"allpass": lambda d: (((1 - d) / (1 + d) + z1) / (1 + (1 - d) / (1 + d) * z1), 0)}
cols = {"linear": "0.55", "Lagrange 3": COL["flute"], "allpass": COL["exciter"]}
fig, axs = plt.subplots(2, 2, figsize=(5.2, 2.9))
for name, F in filt.items():
H, extra = F(0.5); axs[0, 0].plot(w / np.pi, np.abs(H), color=cols[name], lw=1.8, label=name)
H, extra = F(0.3); axs[0, 1].plot(w / np.pi, -np.unwrap(np.angle(H)) / w - extra, color=cols[name], lw=1.8)
axs[0, 0].set_title("$|H|$, $d=0.5$", fontsize=8.5); axs[0, 0].legend(fontsize=7)
axs[0, 1].axhline(0.3, color="k", ls=":", lw=0.8); axs[0, 1].set_ylim(0, 0.8)
axs[0, 1].set_title("phase delay, target $d=0.3$", fontsize=8.5)
for ax in axs[0]:
ax.set_xlabel("$\\omega/\\pi$", labelpad=0)
gs = axs[1, 0].get_gridspec(); axs[1, 0].remove(); axs[1, 1].remove()
ax = fig.add_subplot(gs[1, :])
f0s = np.geomspace(100, 2500, 2000); D_ = fs / f0s
ax.plot(f0s, 1200 * np.log2(D_ / np.round(D_)), color="0.4", lw=0.8)
ax.set_xscale("log"); ax.set_xlabel("$f_0$ (Hz)", labelpad=0); ax.set_ylabel("cents")
ax.set_title("pitch error if the loop delay is rounded to an integer", fontsize=8.5)
plt.tight_layout()
plt.show()
scale = 1046.5 * 2**(np.array([0, 2, 4, 5, 7, 9, 11, 12]) / 12)
for name, frac in [("int", False), ("frac", True)]:
save_wav(f"scale_{name}", np.concatenate([flute(f, 1.0, dur=0.32, frac=frac, noise=0.02, seed=j) for j, f in enumerate(scale)]))