m-thesis-introduction/simulations/lib/sampling/digitizer.py

53 lines
1.4 KiB
Python
Raw Normal View History

2022-03-11 17:49:59 +01:00
import numpy as np
2022-03-24 17:14:47 +01:00
from functools import wraps
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):
@wraps(signal)
def func(*args, **kwargs):
return smp.quantise(
self.sample(signal(*args, **kwargs), signal_sample_frequency),
self.resolution,
self.bias
)
return func
else:
signal = np.asarray(signal)
return smp.quantise(
self.sample(signal, signal_sample_frequency),
self.resolution,
self.bias
)