Skip to content

Commit

Permalink
[python] Improve DATS import
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Aug 18, 2024
1 parent f89cb08 commit 75ef8df
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.frd filter=lfs diff=lfs merge=lfs -text
*.zma filter=lfs diff=lfs merge=lfs -text
85 changes: 43 additions & 42 deletions src/python/akustik/speaker/dats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,7 @@
from akustik.plot.style import default_styles


def read_dats_frequency_response(path):
return pd.read_csv(
path,
sep="\t",
header=None,
names=["Frequency", "SPL", "Phase"]
)


def read_dats_impedance_response(path):
return pd.read_csv(
path,
sep="\t",
header=None,
names=["Frequency", "Impedance", "Phase"]
)


def axes_style(ax: Axes, fmin, fmax):
def _axes_style(ax: Axes, fmin, fmax):
formatter = ScalarFormatter()
formatter.set_scientific(False)
ax.xaxis.set_major_formatter(formatter)
Expand All @@ -49,13 +31,36 @@ def max_impedance(Z: np.ndarray, f: np.ndarray):
return Z[Zmax_index], f[Zmax_index]


def ensure_absolute_path(paths):
paths = [pathlib.Path(path).absolute() for path in paths]
return paths
def read_dats_frequency_response(path):
return pd.read_csv(
path,
sep="\t",
header=None,
names=["Frequency", "SPL", "Phase"]
)


def read_dats_impedance_response(path):
return pd.read_csv(
path,
sep="\t",
header=None,
names=["Frequency", "Impedance", "Phase"]
)


def read_dats_folder(folder):
folder = pathlib.Path(folder)
frd_files = glob.glob(str(folder/"FRD"/"*.frd"))
zma_files = glob.glob(str(folder/"ZMA"/"*.zma"))

name = pathlib.Path(zma_files[0]).stem
frd = read_dats_frequency_response(frd_files[0])
zma = read_dats_impedance_response(zma_files[0])
return name, frd, zma


def main(dats_dirs, fmin, fmax):
dats_dirs = ensure_absolute_path(dats_dirs)
data = {
"name": [],
"acoustic": {
Expand All @@ -70,24 +75,20 @@ def main(dats_dirs, fmin, fmax):
},
}

for dats_dir in dats_dirs:
frd_files = glob.glob(str(dats_dir/"FRD"/"*.frd"))
zma_files = glob.glob(str(dats_dir/"ZMA"/"*.zma"))

FR = read_dats_frequency_response(frd_files[0])
IR = read_dats_impedance_response(zma_files[0])
for folder in dats_dirs:
name, frd, zma = read_dats_folder(folder)

data["name"].append(pathlib.Path(zma_files[0]).stem)
data["acoustic"]["freq"].append(FR["Frequency"].to_numpy())
data["acoustic"]["spl"].append(FR["SPL"].to_numpy())
data["acoustic"]["phase"].append(FR["Phase"].to_numpy())
data["electric"]["freq"].append(IR["Frequency"].to_numpy())
data["electric"]["impedance"].append(IR["Impedance"].to_numpy())
data["electric"]["phase"].append(IR["Phase"].to_numpy())
data["name"].append(name)
data["acoustic"]["freq"].append(frd["Frequency"].to_numpy())
data["acoustic"]["spl"].append(frd["SPL"].to_numpy())
data["acoustic"]["phase"].append(frd["Phase"].to_numpy())
data["electric"]["freq"].append(zma["Frequency"].to_numpy())
data["electric"]["impedance"].append(zma["Impedance"].to_numpy())
data["electric"]["phase"].append(zma["Phase"].to_numpy())

Re = 3.24
Z: np.ndarray = IR["Impedance"].to_numpy()
f: np.ndarray = IR["Frequency"].to_numpy()
Z: np.ndarray = zma["Impedance"].to_numpy()
f: np.ndarray = zma["Frequency"].to_numpy()

assert f.shape == Z.shape
Zmax, freq = max_impedance(Z, f)
Expand Down Expand Up @@ -137,8 +138,8 @@ def plot(ax: Axes, x, y):
plot(ax, data["electric"]["freq"], data["electric"]["phase"])
ax.set_ylabel('Phase [Degree]')

axes_style(axs[0][0], fmin, fmax)
axes_style(axs[1][0], fmin, fmax)
axes_style(axs[0][1], fmin, fmax)
axes_style(axs[1][1], fmin, fmax)
_axes_style(axs[0][0], fmin, fmax)
_axes_style(axs[1][0], fmin, fmax)
_axes_style(axs[0][1], fmin, fmax)
_axes_style(axs[1][1], fmin, fmax)
plt.show()
3 changes: 3 additions & 0 deletions src/python/tests/data/dats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Dayton Audio Test System (DATS)

Folders are downloaded from the main [Dayton Audio](https://daytonaudio.com) website. Unused files are removed.
3 changes: 3 additions & 0 deletions src/python/tests/data/dats/RSS390HF-4/FRD/[email protected]
Git LFS file not shown
3 changes: 3 additions & 0 deletions src/python/tests/data/dats/RSS390HF-4/ZMA/RSS390HF-4.zma
Git LFS file not shown
18 changes: 18 additions & 0 deletions src/python/tests/test_speaker_dats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np

from akustik.speaker.dats import (
max_impedance,
read_dats_folder,
)


def test_max_sound_pressure():
folder = "src/python/tests/data/dats/RSS390HF-4"
name, frd, zma = read_dats_folder(folder)
assert name == "RSS390HF-4"
assert frd.shape == (416, 3)
assert zma.shape == (344, 3)

Z: np.ndarray = zma["Impedance"].to_numpy()
f: np.ndarray = zma["Frequency"].to_numpy()
assert np.allclose(max_impedance(Z, f), (21.173, 19.585))

0 comments on commit 75ef8df

Please sign in to comment.