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

52 lines
1.4 KiB
Python

import numpy as np
from functools import wraps
from . import sampling as smp
from .sampler import Sampler
class Digitizer(Sampler):
"""
Digitizer that takes in a signal and resamples and quantises the signal.
"""
def __init__(self, resolution=0.1, bias=0, sampling_frequency=None):
"""
Parameters
##########
resolution - float
Resolution of the digitizer
sampling_frequency - float
Frequency this digitizer will sample a signal
"""
super().__init__(sampling_frequency)
self.resolution = resolution
self.bias = bias
def digitise(self, signal, signal_sample_frequency=None):
"""
Digitize signal according to the specs of this digitizer.
Effectively resamples signal
"""
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
)