lasprs/examples_py/test_slm1.ipynb

149 lines
3.5 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"id": "bcd07c1e-6722-44c7-b162-267f1341c2fe",
"metadata": {},
"source": [
"# Test Sound Level Meter implementation - 1"
]
},
{
"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",
"# Only do the following if you are in develop mode\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._lasprs import StandardFilterDescriptor, SLMSettings, FreqWeighting, TimeWeighting, SLM\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.Overall()]+StandardFilterDescriptor.genOctaveFilterSet(16, 16e3)\n",
"\n",
"names = [str(o) for o in octaves]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab22d9cc-03ae-4d58-b730-599b659f9dc0",
"metadata": {},
"outputs": [],
"source": [
"fs = 48000\n",
"\n",
"tw = TimeWeighting.Impulse()\n",
"# tw = TimeWeighting.Fast()\n",
"N = fs\n",
"if tw == TimeWeighting.Slow():\n",
" tau = 1.0\n",
"elif tw == TimeWeighting.Fast():\n",
" tau = 1/8\n",
"elif tw == TimeWeighting.Impulse():\n",
" tau = 35e-3\n",
"\n",
"else:\n",
" raise NotImplementedError()\n",
"settings = SLMSettings(fs, FreqWeighting.Z, tw, filterDescriptors=octaves, Lref=1)\n",
"slm = SLM(settings)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1960b80-99f9-4744-9b0f-6651ed2693cd",
"metadata": {},
"outputs": [],
"source": [
"inp = np.ones(N)\n",
"# inp = np.zeros(N)\n",
"inp[0] = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "532539b8-7a5e-47c2-afb1-a72d62c445d4",
"metadata": {},
"outputs": [],
"source": [
"t = np.linspace(0, N/fs, N, endpoint=False)\n",
"out = slm.run(inp, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f446ac32-4ac2-4e85-87c8-43a06be9fb14",
"metadata": {},
"outputs": [],
"source": [
"plt.figure()\n",
"for o in out:\n",
" plt.plot(t,o)\n",
"plt.ylim(-60, 0)\n",
"plt.legend(names)"
]
}
],
"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
}