Skip to content
Snippets Groups Projects

Add ZeroPad and Sinusoid signal generators

Merged Oscar Gustafsson requested to merge moresignalgenerators into master
2 files
+ 91
2
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 45
1
@@ -4,8 +4,9 @@ B-ASIC signal generators
@@ -4,8 +4,9 @@ B-ASIC signal generators
These can be used as input to Simulation to algorithmically provide signal values.
These can be used as input to Simulation to algorithmically provide signal values.
"""
"""
 
from math import pi, sin
from numbers import Number
from numbers import Number
from typing import Callable
from typing import Callable, Sequence
class SignalGenerator:
class SignalGenerator:
@@ -94,6 +95,49 @@ class Constant(SignalGenerator):
@@ -94,6 +95,49 @@ class Constant(SignalGenerator):
return self._constant
return self._constant
 
class ZeroPad(SignalGenerator):
 
"""
 
Signal generator that pads a sequence with zeros.
 
 
Parameters
 
----------
 
data : 1-D array
 
The data that should be padded.
 
"""
 
 
def __init__(self, data: Sequence[complex]) -> Callable[[int], complex]:
 
self._data = data
 
self._len = len(data)
 
 
def __call__(self, time: int) -> complex:
 
if 0 <= time < self._len:
 
return self._data[time]
 
return 0.0
 
 
 
class Sinusoid(SignalGenerator):
 
"""
 
Signal generator that generates a sinusoid.
 
 
Parameters
 
----------
 
frequency : float
 
The normalized frequency of the sinusoid. Should normally be in the
 
interval [0, 1], where 1 corresponds to half the sample rate.
 
phase : float, default: 0
 
The normalized phase offset.
 
"""
 
 
def __init__(
 
self, frequency: float, phase: float = 0.0
 
) -> Callable[[int], complex]:
 
self._frequency = frequency
 
self._phase = phase
 
 
def __call__(self, time: int) -> complex:
 
return sin(pi * (self._frequency * time + self._phase))
 
 
class AddGenerator:
class AddGenerator:
"""
"""
Signal generator that adds two signals.
Signal generator that adds two signals.
Loading