Skip to content

Commit

Permalink
release 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
hofbi committed Mar 11, 2022
1 parent 958fa0a commit d55ca5b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_stages: [commit]

exclude: '^data/'
ci:
autoupdate_commit_msg: "chore(deps): pre-commit.ci autoupdate"
autoupdate_schedule: "monthly"
Expand Down
8 changes: 8 additions & 0 deletions examples/evaluate_wav.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"""Example to evaluate WAV files"""

import sys
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, FileType
from pathlib import Path

from scipy.io import wavfile

# This is to make the local vibromaf package available
try:
sys.path.append(str(Path(__file__).absolute().parents[1]))
except IndexError:
pass

from vibromaf.metrics.snr import snr
from vibromaf.metrics.spqi import spqi
from vibromaf.metrics.stsim import st_sim
Expand Down
9 changes: 9 additions & 0 deletions examples/white_noise.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
"""Simple get started example with white noise signals"""

import sys
from pathlib import Path

import numpy as np

# This is to make the local vibromaf package available
try:
sys.path.append(str(Path(__file__).absolute().parents[1]))
except IndexError:
pass

from vibromaf.metrics.snr import snr
from vibromaf.metrics.spqi import spqi
from vibromaf.metrics.stsim import st_sim
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ matplotlib
tikzplotlib
mkdocs
mkdocstrings
pandas
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setuptools.setup(
name="vibromaf",
version="0.0.5",
version="0.0.6",
author="Markus Hofbauer, Andreas Noll",
author_email="[email protected]",
description="Vibrotactile quality metrics and metric fusion",
Expand Down
8 changes: 8 additions & 0 deletions test/metrics/test_spqi.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ def test_spqi__dist_shorter_than_ref__should_throw(self):
dist = np.linspace(0, 1, 2800)
with self.assertRaisesRegex(ValueError, r"Distorted .* must not be shorter"):
spqi(dist, signal)

def test_spqi__input_signals_identical_zero__should_be_valid(self):
signal = np.zeros(1024)
distorted = np.zeros(1024)
signal[-1] = 0.00001
result = spqi(distorted, signal)
self.assertGreaterEqual(1, result)
self.assertGreaterEqual(result, 0)
10 changes: 8 additions & 2 deletions test/metrics/test_stsim.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Spectral Temporal SIMilarity Tests"""

import math
import unittest

import numpy as np
Expand Down Expand Up @@ -60,7 +60,13 @@ def test_compute_sim__values_larger_than_one_possible(self):
ref_block = np.array([[0.5] * 4])
dist_block = np.array([[0.5, 0.5, 0.5, 1]])
result = STSIM.compute_sim(ref_block, dist_block)
self.assertEqual(1.25, result)
self.assertAlmostEqual(1.25, result)

def test_compute_sim__input_signals_identical_zero__should_be_not_nan(self):
signal = np.zeros((4, 1024))
distorted = np.zeros((4, 1024))
result = STSIM.compute_sim(distorted, signal)
self.assertFalse(math.isnan(result))

def test_st_sim_init__eta_grater_one__should_throw(self):
with self.assertRaisesRegex(ValueError, "Eta must be between 0 and 1."):
Expand Down
8 changes: 8 additions & 0 deletions test/signal/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def test_divide_and_normalize__multiple_blocks__correct_reshaped(self):
np.array_equal(np.array([[-1, 1], [-1, 1], [-1, 1], [-1, 1]]), result)
)

def test_divide_and_normalize__input_signals_identical_zero__should_be_not_nan(
self,
):
input_signal = np.zeros(10)
unit = BlockBuilder(5)
result = unit.divide_and_normalize(input_signal)
self.assertFalse(np.isnan(result).any())


class PerceptualSpectrumBuilderTest(unittest.TestCase):
"""Perceptual Spectrum Builder Test"""
Expand Down
2 changes: 1 addition & 1 deletion vibromaf/metrics/stsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def compute_sim(reference: np.array, distorted: np.array) -> float:
return float(
np.mean(
np.sum(reference * distorted, axis=1)
/ np.sum(np.power(reference, 2), axis=1)
/ (np.sum(np.power(reference, 2), axis=1) + np.finfo(float).eps)
)
)

Expand Down
7 changes: 5 additions & 2 deletions vibromaf/signal/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def mag2db(power: np.array) -> np.array:

def signal_energy(signal: np.array) -> np.array:
"""Calculate the signal energy"""
return np.sum(np.power(signal, 2))
return np.sum(np.square(signal, dtype=np.float64))


def compute_normalized_spectral_difference(
Expand All @@ -40,7 +40,10 @@ def compute_normalized_spectral_difference(
difference = np.sum(
np.abs(db2pow(reference_spectrum) - db2pow(distorted_spectrum)), axis=1
)
return pow2db(difference / np.sum(np.abs(db2pow(reference_spectrum)), axis=1))
return pow2db(
difference
/ (np.sum(np.abs(db2pow(reference_spectrum)), axis=1) + np.finfo(float).eps)
)


def compute_spectral_support(spectrum: np.array, scale: float = 12) -> np.array:
Expand Down
4 changes: 2 additions & 2 deletions vibromaf/signal/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ def divide_and_normalize(self, signal: np.array) -> np.array:
blocks = self.divide(signal)
means = np.apply_along_axis(np.mean, 1, blocks).reshape((blocks.shape[0], 1))
stds = np.apply_along_axis(np.std, 1, blocks).reshape((blocks.shape[0], 1))
return (blocks - means) / stds
return (blocks - means) / (stds + np.finfo(float).eps)


@dataclass(frozen=True)
class PerceptualSpectrumBuilder:
"""Calculate perceptual spectrum"""

block_builder: BlockBuilder = BlockBuilder(512)
perceptual_threshold: PerceptualThreshold = PerceptualThreshold(2800)
perceptual_threshold: PerceptualThreshold = PerceptualThreshold(8000)
block_transform_strategy: Callable[[np.array], np.array] = compute_block_dct

def compute_perceptual_spectrum(self, signal: np.array) -> np.array:
Expand Down

0 comments on commit d55ca5b

Please sign in to comment.