lasprs/examples_py/test_filterbank.ipynb

136 lines
3.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "bcd07c1e-6722-44c7-b162-267f1341c2fe",
"metadata": {},
"source": [
"# Standard filterbank frequency response and impulse response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0c4d6ef-69b2-4b5c-92fd-d987a11f3cbd",
"metadata": {},
"outputs": [],
"source": [
"# Prerequisites, uncomment below in case of errors. Also for ipympl, restart Jupyter Lab if it was not installed\n",
"\n",
"!cd .. && maturin develop -F python-bindings\n",
"#!pip install ipympl scipy matplotlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23b45cf6-85bc-4eaa-8f27-77c5b354e772",
"metadata": {},
"outputs": [],
"source": [
"# If this does not work, install ipympl and reboot Jupyter Lab\n",
"%matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96090ac9-2033-411d-8e45-0c6b375a268b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from numpy import log10\n",
"from lasprs import StandardFilterDescriptor\n",
"def level(a):\n",
" return 20*np.log10(np.abs(a))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52343e3d-63ef-4fc2-ad7e-85ca124efdac",
"metadata": {},
"outputs": [],
"source": [
"freq = np.logspace(log10(2), log10(2e4), 200)\n",
"octaves = StandardFilterDescriptor.genFilterSetInRange(1, 10, 16e3, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2788f062-34ab-4573-9849-459fdccc97d3",
"metadata": {},
"outputs": [],
"source": [
"hs = [ o.genFilter().tf(0,freq) for o in octaves]\n",
"names = [str(o) for o in octaves]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3366f635-29fe-4757-ab0c-9d4929f8cddb",
"metadata": {},
"outputs": [],
"source": [
"plt.figure()\n",
"# plt.subplot(211)\n",
"plt.title('Frequency response of filters - magnitude')\n",
"\n",
"for h in hs:\n",
" plt.semilogx(freq, level(h))\n",
"plt.legend(names)\n",
"plt.ylabel('Magnitude [dB]')\n",
"plt.ylim(-50, 1)\n",
"plt.xlabel('Freq. [Hz]')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b242187a-3449-4c30-8e26-28e75d59c414",
"metadata": {},
"outputs": [],
"source": [
"fs = 48000\n",
"tend = 0.01\n",
"t = np.linspace(0, tend, int(tend*fs), endpoint=False)\n",
"impulse = np.zeros(t.size)\n",
"impulse[0] = 1\n",
"plt.figure()\n",
"plt.title('Filter impulse response')\n",
"for o in octaves:\n",
" i = o.genFilter().bilinear(fs).filter(impulse)\n",
" plt.plot(t, i)\n",
"plt.legend(names, loc='upper right')\n",
"plt.ylabel('Filter output [-]')\n",
"plt.xlabel('Time [s]')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}