2022-03-11 17:49:59 +01:00
|
|
|
import numpy as np
|
2022-03-24 18:01:20 +01:00
|
|
|
from functools import wraps, partial
|
2022-03-11 17:49:59 +01:00
|
|
|
|
|
|
|
from . import sampling as smp
|
|
|
|
from .sampler import Sampler
|
|
|
|
|
|
|
|
class Digitizer(Sampler):
|
|
|
|
"""
|
|
|
|
Digitizer that takes in a signal and resamples and quantises the signal.
|
|
|
|
"""
|
|
|
|
|
2022-03-24 17:14:47 +01:00
|
|
|
def __init__(self, resolution=0.1, bias=0, sampling_frequency=None):
|
2022-03-11 17:49:59 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
##########
|
|
|
|
resolution - float
|
|
|
|
Resolution of the digitizer
|
|
|
|
sampling_frequency - float
|
|
|
|
Frequency this digitizer will sample a signal
|
|
|
|
"""
|
|
|
|
super().__init__(sampling_frequency)
|
|
|
|
|
|
|
|
self.resolution = resolution
|
2022-03-24 17:14:47 +01:00
|
|
|
self.bias = bias
|
2022-03-11 17:49:59 +01:00
|
|
|
|
|
|
|
def digitise(self, signal, signal_sample_frequency=None):
|
|
|
|
"""
|
|
|
|
Digitize signal according to the specs of this digitizer.
|
|
|
|
|
|
|
|
Effectively resamples signal
|
|
|
|
"""
|
|
|
|
|
2022-03-24 17:14:47 +01:00
|
|
|
if callable(signal):
|
2022-03-24 18:01:20 +01:00
|
|
|
# if signal is already a partial,
|
|
|
|
# try to rebuild it after setting the wrapper
|
|
|
|
if isinstance(signal, partial):
|
|
|
|
rebuild_partial = True
|
|
|
|
p_args = signal.args
|
|
|
|
p_kwargs = signal.keywords
|
|
|
|
signal = signal.func
|
|
|
|
else:
|
|
|
|
rebuild_partial = False
|
|
|
|
|
2022-03-24 17:14:47 +01:00
|
|
|
@wraps(signal)
|
2022-03-24 18:01:20 +01:00
|
|
|
def wrapper(*args, **kwargs):
|
2022-03-24 17:14:47 +01:00
|
|
|
return smp.quantise(
|
|
|
|
self.sample(signal(*args, **kwargs), signal_sample_frequency),
|
|
|
|
self.resolution,
|
|
|
|
self.bias
|
|
|
|
)
|
|
|
|
|
2022-03-24 18:01:20 +01:00
|
|
|
# rebuild the partial if applicable
|
|
|
|
if rebuild_partial:
|
|
|
|
wrapper = partial(wrapper, *p_args, **p_kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
2022-03-24 17:14:47 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
signal = np.asarray(signal)
|
|
|
|
|
|
|
|
return smp.quantise(
|
|
|
|
self.sample(signal, signal_sample_frequency),
|
|
|
|
self.resolution,
|
|
|
|
self.bias
|
|
|
|
)
|