Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B-ASIC - Better ASIC Toolbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Computer Engineering
B-ASIC - Better ASIC Toolbox
Commits
1da96dd5
Commit
1da96dd5
authored
2 years ago
by
Oscar Gustafsson
Browse files
Options
Downloads
Patches
Plain Diff
Add ZeroPad and Sinusoid signal generators
parent
c2a9e42b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!154
Add ZeroPad and Sinusoid signal generators
Pipeline
#88752
passed
2 years ago
Stage: test
Stage: deploy
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
b_asic/signal_generator.py
+45
-1
45 additions, 1 deletion
b_asic/signal_generator.py
test/test_signal_generator.py
+46
-1
46 additions, 1 deletion
test/test_signal_generator.py
with
91 additions
and
2 deletions
b_asic/signal_generator.py
+
45
−
1
View file @
1da96dd5
...
...
@@ -4,8 +4,9 @@ B-ASIC signal generators
These can be used as input to Simulation to algorithmically provide signal values.
"""
from
math
import
pi
,
sin
from
numbers
import
Number
from
typing
import
Callable
from
typing
import
Callable
,
Sequence
class
SignalGenerator
:
...
...
@@ -94,6 +95,49 @@ class Constant(SignalGenerator):
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
:
"""
Signal generator that adds two signals.
...
...
This diff is collapsed.
Click to expand it.
test/test_signal_generator.py
+
46
−
1
View file @
1da96dd5
from
b_asic.signal_generator
import
Constant
,
Impulse
,
Step
from
math
import
sqrt
import
pytest
from
b_asic.signal_generator
import
Constant
,
Impulse
,
Sinusoid
,
Step
,
ZeroPad
def
test_impulse
():
g
=
Impulse
()
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
1
assert
g
(
1
)
==
0
assert
g
(
2
)
==
0
g
=
Impulse
(
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0
assert
g
(
1
)
==
1
assert
g
(
2
)
==
0
...
...
@@ -15,11 +21,13 @@ def test_impulse():
def
test_step
():
g
=
Step
()
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
1
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
g
=
Step
(
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
...
...
@@ -27,30 +35,57 @@ def test_step():
def
test_constant
():
g
=
Constant
()
assert
g
(
-
1
)
==
1
assert
g
(
0
)
==
1
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
g
=
Constant
(
0.5
)
assert
g
(
-
1
)
==
0.5
assert
g
(
0
)
==
0.5
assert
g
(
1
)
==
0.5
assert
g
(
2
)
==
0.5
def
test_zeropad
():
g
=
ZeroPad
([
0.4
,
0.6
])
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.4
assert
g
(
1
)
==
0.6
assert
g
(
2
)
==
0
def
test_sinusoid
():
g
=
Sinusoid
(
0.5
)
assert
g
(
0
)
==
0
assert
g
(
1
)
==
1
assert
g
(
2
)
==
pytest
.
approx
(
0
)
assert
g
(
3
)
==
-
1
g
=
Sinusoid
(
0.5
,
0.25
)
assert
g
(
0
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
)
assert
g
(
1
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
)
assert
g
(
2
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
)
assert
g
(
3
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
)
def
test_addition
():
g
=
Impulse
()
+
Impulse
(
2
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
1
assert
g
(
1
)
==
0
assert
g
(
2
)
==
1
assert
g
(
3
)
==
0
g
=
1
+
Impulse
(
2
)
assert
g
(
-
1
)
==
1
assert
g
(
0
)
==
1
assert
g
(
1
)
==
1
assert
g
(
2
)
==
2
assert
g
(
3
)
==
1
g
=
Impulse
(
1
)
+
1
assert
g
(
-
1
)
==
1
assert
g
(
0
)
==
1
assert
g
(
1
)
==
2
assert
g
(
2
)
==
1
...
...
@@ -59,18 +94,21 @@ def test_addition():
def
test_subtraction
():
g
=
Impulse
()
-
Impulse
(
2
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
1
assert
g
(
1
)
==
0
assert
g
(
2
)
==
-
1
assert
g
(
3
)
==
0
g
=
1
-
Impulse
(
2
)
assert
g
(
-
1
)
==
1
assert
g
(
0
)
==
1
assert
g
(
1
)
==
1
assert
g
(
2
)
==
0
assert
g
(
3
)
==
1
g
=
Impulse
(
2
)
-
1
assert
g
(
-
1
)
==
-
1
assert
g
(
0
)
==
-
1
assert
g
(
1
)
==
-
1
assert
g
(
2
)
==
0
...
...
@@ -79,6 +117,13 @@ def test_subtraction():
def
test_multiplication
():
g
=
Impulse
()
*
0.5
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.5
assert
g
(
1
)
==
0
assert
g
(
2
)
==
0
g
=
2
*
Sinusoid
(
0.5
,
0.25
)
assert
g
(
0
)
==
pytest
.
approx
(
sqrt
(
2
))
assert
g
(
1
)
==
pytest
.
approx
(
sqrt
(
2
))
assert
g
(
2
)
==
pytest
.
approx
(
-
sqrt
(
2
))
assert
g
(
3
)
==
pytest
.
approx
(
-
sqrt
(
2
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment