Simu: add Digitizer

This commit is contained in:
Eric Teunis de Boone 2022-03-11 17:49:59 +01:00
parent c4062481d4
commit 80a79bb4c6
5 changed files with 124 additions and 0 deletions

View file

@ -1,5 +1,6 @@
from . import signals from . import signals
from . import location from . import location
from . import sampling
from .util import * from .util import *

View file

@ -0,0 +1,3 @@
from .sampling import *
from .sampler import *
from .digitizer import *

View file

@ -0,0 +1,36 @@
import numpy as np
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, 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
def digitise(self, signal, signal_sample_frequency=None):
"""
Digitize signal according to the specs of this digitizer.
Effectively resamples signal
"""
signal = np.asarray(signal)
return smp.quantise(
self.sample(signal, signal_sample_frequency),
self.resolution
)

View file

@ -0,0 +1,29 @@
import numpy as np
from . import sampling as smp
class Sampler():
"""
A mechanism to sample signals.
"""
def __init__(self, sampling_frequency=None):
"""
Parameters
##########
sampling_frequency - float
Frequency the signals will be sampled at
"""
self.sampling_frequency = sampling_frequency
def sample(self, signal, signal_fs=None):
"""
Sample signal
"""
# Null operation
if signal_fs is None or self.sampling_frequency is None:
return signal
return smp.resample(signal, signal_fs, self.sampling_frequency)

View file

@ -0,0 +1,55 @@
"""
Sampling related stuff
Such as a Sampler and Digitizer
"""
import numpy as np
def quantise(signal, resolution, bias=0):
"""
Quantise the signal with resolution
Parameters
##########
signal - arraylike
The signal to be quantised
resolution - float
Resolution for quantising the signal
bias - optional,float
Optional bias applied before quantising
"""
return np.round(signal / resolution - bias) * resolution
def resample(signal, signal_fs, sample_frequency = 1, periodic=True):
"""
Resample signal (sampled at signal_fs) to sample_frequency
Parameters
##########
signal - arraylike
The signal to be resampled
signal_fs - float
Sampling frequency of signal
sample_frequency - float
Wanted sampling frequency for the resampled signal
"""
scale = sample_frequency / signal_fs
return _resample(signal, scale)
def _resample(signal, scale):
"""
Quick resampling algorithm
From: https://github.com/nwhitehead/swmixer/blob/master/swmixer.py
"""
n = round( len(signal) * scale )
return np.interp(
np.linspace(0, 1, n, endpoint=False), # where to interpret
np.linspace(0, 1, len(signal), endpoint=False), # known positions
signal, # known data points
)