Commit 39309a72 authored by Sean Leavey's avatar Sean Leavey
Browse files

Move db/mag conversions to functions

parent 944cfca4
......@@ -2,9 +2,9 @@
import numpy as np
from zero.data import Series, MultiNoiseDensity
from zero.misc import mag_to_db
from ..data import ZeroDataTestCase
class SeriesTestCase(ZeroDataTestCase):
"""Data series tests"""
def setUp(self):
......@@ -19,7 +19,7 @@ class SeriesTestCase(ZeroDataTestCase):
self.data_im = test_data[:, 1]
# Magnitude and phase equivalent.
self.data_mag_abs = np.abs(self.data_cplx)
self.data_mag_db = 20 * np.log10(self.data_mag_abs)
self.data_mag_db = mag_to_db(self.data_mag_abs)
self.data_phase_rad = np.angle(self.data_cplx)
self.data_phase_deg = np.degrees(self.data_phase_rad)
......
......@@ -6,11 +6,12 @@ from numbers import Number
import numpy as np
from .config import ZeroConfig
from .misc import db
from .misc import mag_to_db, db_to_mag
LOGGER = logging.getLogger(__name__)
CONF = ZeroConfig()
def frequencies_match(vector_a, vector_b):
return np.all(vector_a == vector_b)
......@@ -83,7 +84,7 @@ class Series:
phase_scale = "deg"
if mag_scale.lower() == "db":
magnitude = 10 ** (magnitude / 20)
magnitude = db_to_mag(magnitude)
elif mag_scale.lower() == "abs":
# don't need to scale
pass
......@@ -347,7 +348,7 @@ class Response(SingleSourceFunction, SingleSinkFunction):
The response is power scaled such that the response is :math:`20 \log_{10} \left| x \right|`
where :math:`x` is the complex response provided by :attr:`.complex_magnitude`.
"""
return db(self.magnitude)
return mag_to_db(self.magnitude)
@property
def phase(self):
......
......@@ -144,16 +144,6 @@ class Downloadable:
return filename, request
def db(magnitude):
"""Calculate (power) magnitude in decibels
:param magnitude: magnitude
:type magnitude: Numeric or :class:`np.array`
:return: dB magnitude
:rtype: Numeric or :class:`np.array`
"""
return 20 * np.log10(magnitude)
def lighten_colours(colour_cycle, factor):
"""Lightens the given color by multiplying (1 - luminosity) by the given factor.
......@@ -177,3 +167,9 @@ def lighten_colours(colour_cycle, factor):
cycle.append(hexcode)
return cycle
def db_to_mag(quantity):
return 10 ** (quantity / 20)
def mag_to_db(quantity):
return 20 * np.log10(quantity)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment