Skip to content

Commit

Permalink
add energy(), spl(), and rms() methods to Signal and FramedSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Böck committed Feb 24, 2017
1 parent 28bf298 commit 3c3727b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ API relevant changes:

Other changes:

* SPL and RMS can be computed on `Signal` and `FramedSignal` (#208)
* `num_threads` is passed to `ParallelProcessor` in single mode (#217)
* Use `install_requires` in `setup.py` to specify dependencies (#226)
* Use new Cython build system to build extensions (#227)
* Allow initialisation of previous/hidden states in RNNs (#243)
* Forward path of `HMM` can be computed stepwise (#244)



Version 0.14.1 (release date: 2016-08-01)
-----------------------------------------

Expand Down
32 changes: 32 additions & 0 deletions madmom/audio/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ def write(self, filename):
"""
return write_wave_file(self, filename)

def energy(self):
"""Energy of signal."""
return energy(self)

def root_mean_square(self):
"""Root mean square of signal."""
return root_mean_square(self)

rms = root_mean_square

def sound_pressure_level(self):
"""Sound pressure level of signal."""
return sound_pressure_level(self)

spl = sound_pressure_level


class SignalProcessor(Processor):
"""
Expand Down Expand Up @@ -1298,6 +1314,22 @@ def ndim(self):
"""Dimensionality of the FramedSignal."""
return len(self.shape)

def energy(self):
"""Energy of the individual frames."""
return energy(self)

def root_mean_square(self):
"""Root mean square of the individual frames."""
return root_mean_square(self)

rms = root_mean_square

def sound_pressure_level(self):
"""Sound pressure level of the individual frames."""
return sound_pressure_level(self)

spl = sound_pressure_level


class FramedSignalProcessor(Processor):
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/test_audio_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,19 @@ def test_write_method(self):
result = Signal(tmp_file)
self.assertTrue(np.allclose(orig, result))

def test_methods(self):
# mono signals
signal = Signal(sig_1d)
self.assertTrue(np.allclose(signal.energy(), 3))
self.assertTrue(np.allclose(signal.rms(), 0.57735026919))
self.assertTrue(np.allclose(signal.spl(), -4.7712125472))
# multi-channel signals
signal = Signal(sig_2d)
self.assertTrue(np.allclose(signal.energy(), 8))
self.assertTrue(np.allclose(signal.root_mean_square(), 2. / 3))
self.assertTrue(np.allclose(signal.sound_pressure_level(),
-3.52182518111))


class TestSignalProcessorClass(unittest.TestCase):

Expand Down Expand Up @@ -1458,6 +1471,25 @@ def test_values_file_fps(self):
self.assertTrue(result.frame_size == 2048)
self.assertTrue(result.hop_size == 882.)

def test_methods(self):
# mono signals
frames = FramedSignal(sig_1d, frame_size=4, hop_size=2)
self.assertTrue(np.allclose(frames.energy(), [0, 1, 2, 1, 1]))
self.assertTrue(np.allclose(frames.rms(),
[0, 0.5, 0.70710678, 0.5, 0.5]))
self.assertTrue(np.allclose(frames.spl(),
[-np.finfo(float).max, -6.0206, -3.0103,
-6.0206, -6.0206]))
# multi-channel signals
frames = FramedSignal(sig_2d, frame_size=4, hop_size=2)
self.assertTrue(np.allclose(frames.energy(), [1, 3, 4, 3, 3]))
self.assertTrue(np.allclose(frames.root_mean_square(),
[0.35355339, 0.61237244, 0.70710678,
0.61237244, 0.61237244]))
self.assertTrue(np.allclose(frames.sound_pressure_level(),
[-9.03089987, -4.25968732, -3.01029996,
-4.25968732, -4.25968732]))


class TestFramedSignalProcessorClass(unittest.TestCase):

Expand Down

0 comments on commit 3c3727b

Please sign in to comment.