Skip to content

Commit

Permalink
experiment with crossover
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreaubert committed May 31, 2023
1 parent a4755ec commit 73f1563
Showing 1 changed file with 382 additions and 0 deletions.
382 changes: 382 additions & 0 deletions experimental/play_crossover1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "84b374cc-97d2-4a6e-be40-d8eb9004560a",
"metadata": {},
"source": [
"Compute an optimal crossover"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e59c1538-1033-45e1-a0a0-cd9660ff7b76",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import math\n",
"import time\n",
"import bisect\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import plotly as plt\n",
"import ray\n",
"\n",
"pd.options.plotting.backend = \"plotly\"\n",
"\n",
"import sys, os, os.path\n",
"\n",
"sys.path.append(os.path.expanduser(\"../src\"))\n",
"\n",
"from generate_common import custom_ray_init, cache_load\n",
"from spinorama.filter_iir import Biquad\n",
"from spinorama.filter_peq import peq_print, peq_format_apo, peq_build, peq_linkwitzriley_lowpass, peq_linkwitzriley_highpass\n",
"from spinorama.compute_cea2034 import compute_cea2034, estimated_inroom_hv\n",
"from spinorama.plot import plot_spinorama, common_layout\n",
"from spinorama.load import shift_spl\n",
"import scipy.optimize as opt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb77fd16-ef8c-4bf3-91e3-c22c0928bc38",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"custom_ray_init({\"--log-level\": \"INFO\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64c83b5f-5252-46d4-9e6d-0156db41cd56",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"speaker_name_lf = \"Dolby CS 136LF\"\n",
"speaker_name_mh = \"Dolby CS 136MH\"\n",
"speaker_origin = \"Vendors-Dolby\"\n",
"speaker_version = \"vendor\"\n",
"lf_speaker = cache_load({\"speaker_name\": speaker_name_lf, \"origin\": speaker_origin}, False)\n",
"mh_speaker = cache_load({\"speaker_name\": speaker_name_mh, \"origin\": speaker_origin}, False)\n",
"\n",
"ray.shutdown()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f320851-54d4-49fa-b81b-b9b47f8d2751",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"crossover_freq = 365\n",
"crossover_order_lp = 8\n",
"crossover_order_hp = 8"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9542d37-d43f-4219-af85-fa2c47a93e1a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"spl_h_lf = lf_speaker[speaker_name_lf][speaker_origin][speaker_version][\"SPL Horizontal_unmelted\"]\n",
"spl_v_lf = lf_speaker[speaker_name_lf][speaker_origin][speaker_version][\"SPL Vertical_unmelted\"]\n",
"\n",
"on_lf_mean = spl_h_lf[\"On Axis\"].loc[((spl_h_lf.Freq>100)) & (spl_h_lf.Freq<crossover_freq)].mean()\n",
"\n",
"# normalise except Freq\n",
"spl_h_lf = shift_spl(spl_h_lf, on_lf_mean)\n",
"spl_v_lf = shift_spl(spl_v_lf, on_lf_mean)\n",
"\n",
"spl_h_mh = mh_speaker[speaker_name_mh][speaker_origin][speaker_version][\"SPL Horizontal_unmelted\"]\n",
"spl_v_mh = mh_speaker[speaker_name_mh][speaker_origin][speaker_version][\"SPL Vertical_unmelted\"]\n",
"\n",
"lf_spin = compute_cea2034(spl_h_lf, spl_v_lf)\n",
"lf_pir = estimated_inroom_hv(spl_h_lf, spl_v_lf)\n",
"\n",
"mh_spin = compute_cea2034(spl_h_mh, spl_v_mh)\n",
"mh_pir = estimated_inroom_hv(spl_h_mh, spl_v_mh)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecb20c81",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plot_params_local = common_layout({\n",
" \"xmin\": 20,\n",
" \"xmax\": 20000,\n",
" \"ymin\": -40,\n",
" \"ymax\": 10,\n",
" \"width\": 800,\n",
" \"height\": 500, \n",
"})\n",
"mh_plot = plot_spinorama(mh_spin, plot_params)\n",
"mh_plot.update_layout({\n",
" \"legend\": dict(\n",
" x=1.4,\n",
" y=1,\n",
" xanchor=\"right\",\n",
" yanchor=\"top\",\n",
" orientation=\"v\",\n",
" ),\n",
"})\n",
"mh_plot.show()\n",
"lf_plot = plot_spinorama(lf_spin, plot_params)\n",
"lf_plot.update_layout({\n",
" \"legend\": dict(\n",
" x=1.4,\n",
" y=1,\n",
" xanchor=\"right\",\n",
" yanchor=\"top\",\n",
" orientation=\"v\",\n",
" ),\n",
"})\n",
"lf_plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14d4a902-cd2e-4bd4-aedc-296c47be2186",
"metadata": {},
"outputs": [],
"source": [
"freq = spl_h_lf.Freq\n",
"on_mh = spl_h_mh[\"On Axis\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6cf3b5c-c403-47ff-8f8c-543d5f06a4e2",
"metadata": {},
"outputs": [],
"source": [
"lr4_lp = peq_linkwitzriley_lowpass(crossover_order_lp, crossover_freq, 48000)\n",
"lr4_hp = peq_linkwitzriley_highpass(crossover_order_hp, crossover_freq, 48000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d703a60-41b3-4254-896d-99f5db160ff6",
"metadata": {},
"outputs": [],
"source": [
"spl_lp = peq_build(freq, lr4_lp)\n",
"spl_hp = peq_build(freq, lr4_hp)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7195f401-7a2d-4e7e-bcf2-7e03a92d5ac7",
"metadata": {},
"outputs": [],
"source": [
"def spl2pressure(spl):\n",
" return np.power(10, np.divide(np.subtract(spl, 105),20))\n",
"\n",
"def pressure2spl(pressure):\n",
" return np.add(105, np.multiply(20, np.log10(pressure)))\n",
"\n",
"def spl_add(spl_a, spl_b):\n",
" return pressure2spl(spl2pressure(spl_a)+spl2pressure(spl_b))\n",
"\n",
"def apply_crossover(spls, spl_crossover):\n",
" df = pd.DataFrame()\n",
" for key in spls.keys():\n",
" if key == \"Freq\":\n",
" df[key] = spls[key]\n",
" continue\n",
" df[key] = spl_add(spls[key], spl_crossover)\n",
" return df\n",
"\n",
"def merge_axis(spl_h, spl_v, spl_crossover):\n",
" return apply_crossover(spl_h, spl_crossover), apply_crossover(spl_v, spl_crossover)\n",
"\n",
"def merge_spl(spl_a, spl_b):\n",
" df = pd.DataFrame()\n",
" for key in spl_a.keys():\n",
" if key == \"Freq\":\n",
" df[key] = spl_a[key]\n",
" continue\n",
" df[key] = spl_add(spl_a[key], spl_b[key])\n",
" return df\n",
"\n",
"def merge_measurements(\n",
" spl_h_lf, \n",
" spl_v_lf,\n",
" spl_lp,\n",
" spl_h_mh,\n",
" spl_v_mh,\n",
" spl_hp\n",
"):\n",
" spl_h_lf, spl_v_lf = merge_axis(spl_h_lf, spl_v_lf, spl_lp)\n",
" spl_h_mh, spl_h_mh = merge_axis(spl_h_mh, spl_v_mh, spl_hp)\n",
" return merge_spl(spl_h_lf, spl_h_mh), merge_spl(spl_v_lf, spl_v_mh)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75f23cf9-55b5-41b6-b80c-0dd18093119c",
"metadata": {},
"outputs": [],
"source": [
"spl_mh = spl_add(on_mh, spl_hp)\n",
"spl_lf = spl_add(on_lf, spl_lp)\n",
"\n",
"fig = pd.DataFrame({\n",
" \"Freq\": freq,\n",
" \"lp\": spl_lp,\n",
" \"hp\": spl_hp,\n",
" \"mh\": spl_mh,\n",
" \"lf\": spl_lf,\n",
"}).plot.line(x=\"Freq\", y=[\"lp\", \"hp\", \"mh\", \"lf\"])\n",
"fig.update_xaxes(type=\"log\")\n",
"fig.update_yaxes(type=\"linear\", range=[-10, 10])\n",
"fig.update_layout(plot_params_local)\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8ab21e2-f1f6-4f38-a227-cea90fa61eff",
"metadata": {},
"outputs": [],
"source": [
"on = spl_add(spl_mh, spl_lf)\n",
"\n",
"fig = pd.DataFrame({\n",
" \"Freq\": freq,\n",
" \"on\": on,\n",
"}).plot.line(x=\"Freq\", y=[\"on\"])\n",
"fig.update_xaxes(type=\"log\")\n",
"fig.update_yaxes(type=\"linear\", range=[-10, 10])\n",
"fig.update_layout(plot_params_local)\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2077c3-47ff-4358-8f15-f604d5afa8d9",
"metadata": {},
"outputs": [],
"source": [
"spl_h, spl_v = merge_measurements(spl_h_lf, spl_v_lf, spl_lp, spl_h_mh, spl_v_mh, spl_hp)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd1bcfa7-6100-49e8-9482-77592e87c2be",
"metadata": {},
"outputs": [],
"source": [
"spin = compute_cea2034(spl_h, spl_v)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "045518f9-2fde-4eca-b635-ff8374019dd1",
"metadata": {},
"outputs": [],
"source": [
"spin_plot = plot_spinorama(spin, plot_params)\n",
"spin_plot.update_layout({\n",
" \"legend\": dict(\n",
" x=1.4,\n",
" y=1,\n",
" xanchor=\"right\",\n",
" yanchor=\"top\",\n",
" orientation=\"v\",\n",
" ),\n",
"})\n",
"spin_plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08aff11d-421f-4985-98d4-eed33b0d9bb7",
"metadata": {},
"outputs": [],
"source": [
"speaker_name = \"Dolby CS 133\"\n",
"\n",
"def spl2files(spls, speaker_name: str, orientation: str):\n",
" header = \"Freq[Hz] dBSPL Phase[Deg]\\n\"\n",
" freq = spls.Freq\n",
" for spl in spls:\n",
" angle = 0\n",
" if spl == 'Freq':\n",
" continue\n",
" if spl == 'On Axis':\n",
" angle = 0\n",
" else:\n",
" angle = int(spl[:-1])\n",
" filename = f\"{speaker_name} {orientation} {angle}.txt\"\n",
" with open(filename, \"w\", encoding=\"utf8\") as fd:\n",
" fd.writelines([header])\n",
" fd.writelines([\"{} {} {}\\n\".format(f, s, 0.0) for f,s in zip(freq, spls[spl].values)])\n",
"\n",
"spl2files(spl_h, speaker_name, \"_H\")\n",
"spl2files(spl_v, speaker_name, \"_V\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6133083-9ae8-4c8b-a1c3-4bda2d010b29",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 73f1563

Please sign in to comment.