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
ec515e35
Commit
ec515e35
authored
2 years ago
by
Oscar Gustafsson
Browse files
Options
Downloads
Patches
Plain Diff
Add str-support for generators
parent
dd7aaafa
No related branches found
No related tags found
1 merge request
!166
Add str-support for generators
Pipeline
#88949
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
+54
-0
54 additions, 0 deletions
b_asic/signal_generator.py
test/test_signal_generator.py
+50
-8
50 additions, 8 deletions
test/test_signal_generator.py
with
104 additions
and
8 deletions
b_asic/signal_generator.py
+
54
−
0
View file @
ec515e35
...
...
@@ -76,6 +76,9 @@ class Impulse(SignalGenerator):
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
1
if
time
==
self
.
_delay
else
0
def
__repr__
(
self
):
return
f
"
Impulse(
{
self
.
_delay
}
)
"
if
self
.
_delay
else
"
Impulse()
"
class
Step
(
SignalGenerator
):
"""
...
...
@@ -93,6 +96,9 @@ class Step(SignalGenerator):
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
1
if
time
>=
self
.
_delay
else
0
def
__repr__
(
self
):
return
f
"
Step(
{
self
.
_delay
}
)
"
if
self
.
_delay
else
"
Step()
"
class
Constant
(
SignalGenerator
):
"""
...
...
@@ -110,6 +116,9 @@ class Constant(SignalGenerator):
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
self
.
_constant
def
__str__
(
self
):
return
f
"
{
self
.
_constant
}
"
class
ZeroPad
(
SignalGenerator
):
"""
...
...
@@ -130,6 +139,9 @@ class ZeroPad(SignalGenerator):
return
self
.
_data
[
time
]
return
0.0
def
__repr__
(
self
):
return
f
"
ZeroPad(
{
self
.
_data
}
)
"
class
Sinusoid
(
SignalGenerator
):
"""
...
...
@@ -153,6 +165,13 @@ class Sinusoid(SignalGenerator):
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
sin
(
pi
*
(
self
.
_frequency
*
time
+
self
.
_phase
))
def
__repr__
(
self
):
return
(
f
"
Sinusoid(
{
self
.
_frequency
}
,
{
self
.
_phase
}
)
"
if
self
.
_phase
else
f
"
Sinusoid(
{
self
.
_frequency
}
)
"
)
class
AddGenerator
:
"""
...
...
@@ -168,6 +187,9 @@ class AddGenerator:
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
self
.
_a
(
time
)
+
self
.
_b
(
time
)
def
__str__
(
self
):
return
f
"
{
self
.
_a
}
+
{
self
.
_b
}
"
class
SubGenerator
:
"""
...
...
@@ -183,6 +205,9 @@ class SubGenerator:
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
self
.
_a
(
time
)
-
self
.
_b
(
time
)
def
__str__
(
self
):
return
f
"
{
self
.
_a
}
-
{
self
.
_b
}
"
class
MultGenerator
:
"""
...
...
@@ -198,6 +223,19 @@ class MultGenerator:
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
self
.
_a
(
time
)
*
self
.
_b
(
time
)
def
__str__
(
self
):
a
=
(
f
"
(
{
self
.
_a
}
)
"
if
isinstance
(
self
.
_a
,
(
AddGenerator
,
SubGenerator
))
else
f
"
{
self
.
_a
}
"
)
b
=
(
f
"
(
{
self
.
_b
}
)
"
if
isinstance
(
self
.
_b
,
(
AddGenerator
,
SubGenerator
))
else
f
"
{
self
.
_b
}
"
)
return
f
"
{
a
}
*
{
b
}
"
class
DivGenerator
:
"""
...
...
@@ -212,3 +250,19 @@ class DivGenerator:
def
__call__
(
self
,
time
:
int
)
->
complex
:
return
self
.
_a
(
time
)
/
self
.
_b
(
time
)
def
__str__
(
self
):
a
=
(
f
"
(
{
self
.
_a
}
)
"
if
isinstance
(
self
.
_a
,
(
AddGenerator
,
SubGenerator
))
else
f
"
{
self
.
_a
}
"
)
b
=
(
f
"
(
{
self
.
_b
}
)
"
if
isinstance
(
self
.
_b
,
(
AddGenerator
,
SubGenerator
,
MultGenerator
,
DivGenerator
),
)
else
f
"
{
self
.
_b
}
"
)
return
f
"
{
a
}
/
{
b
}
"
This diff is collapsed.
Click to expand it.
test/test_signal_generator.py
+
50
−
8
View file @
ec515e35
...
...
@@ -12,12 +12,16 @@ def test_impulse():
assert
g
(
1
)
==
0
assert
g
(
2
)
==
0
assert
str
(
g
)
==
"
Impulse()
"
g
=
Impulse
(
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0
assert
g
(
1
)
==
1
assert
g
(
2
)
==
0
assert
str
(
g
)
==
"
Impulse(1)
"
def
test_step
():
g
=
Step
()
...
...
@@ -26,12 +30,16 @@ def test_step():
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
assert
str
(
g
)
==
"
Step()
"
g
=
Step
(
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
assert
str
(
g
)
==
"
Step(1)
"
def
test_constant
():
g
=
Constant
()
...
...
@@ -40,12 +48,16 @@ def test_constant():
assert
g
(
1
)
==
1
assert
g
(
2
)
==
1
assert
str
(
g
)
==
"
1.0
"
g
=
Constant
(
0.5
)
assert
g
(
-
1
)
==
0.5
assert
g
(
0
)
==
0.5
assert
g
(
1
)
==
0.5
assert
g
(
2
)
==
0.5
assert
str
(
g
)
==
"
0.5
"
def
test_zeropad
():
g
=
ZeroPad
([
0.4
,
0.6
])
...
...
@@ -54,6 +66,8 @@ def test_zeropad():
assert
g
(
1
)
==
0.6
assert
g
(
2
)
==
0
assert
str
(
g
)
==
"
ZeroPad([0.4, 0.6])
"
def
test_sinusoid
():
g
=
Sinusoid
(
0.5
)
...
...
@@ -62,12 +76,16 @@ def test_sinusoid():
assert
g
(
2
)
==
pytest
.
approx
(
0
)
assert
g
(
3
)
==
-
1
assert
str
(
g
)
==
"
Sinusoid(0.5)
"
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
)
assert
str
(
g
)
==
"
Sinusoid(0.5, 0.25)
"
def
test_addition
():
g
=
Impulse
()
+
Impulse
(
2
)
...
...
@@ -77,20 +95,26 @@ def test_addition():
assert
g
(
2
)
==
1
assert
g
(
3
)
==
0
g
=
1
+
Impulse
(
2
)
assert
str
(
g
)
==
"
Impulse() + Impulse(2)
"
g
=
1.0
+
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
str
(
g
)
==
"
Impulse(2) + 1.0
"
g
=
Impulse
(
1
)
+
1.0
assert
g
(
-
1
)
==
1
assert
g
(
0
)
==
1
assert
g
(
1
)
==
2
assert
g
(
2
)
==
1
assert
g
(
3
)
==
1
assert
str
(
g
)
==
"
Impulse(1) + 1.0
"
def
test_subtraction
():
g
=
Impulse
()
-
Impulse
(
2
)
...
...
@@ -100,20 +124,26 @@ def test_subtraction():
assert
g
(
2
)
==
-
1
assert
g
(
3
)
==
0
g
=
1
-
Impulse
(
2
)
assert
str
(
g
)
==
"
Impulse() - Impulse(2)
"
g
=
1.0
-
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
str
(
g
)
==
"
1.0 - Impulse(2)
"
g
=
Impulse
(
2
)
-
1.0
assert
g
(
-
1
)
==
-
1
assert
g
(
0
)
==
-
1
assert
g
(
1
)
==
-
1
assert
g
(
2
)
==
0
assert
g
(
3
)
==
-
1
assert
str
(
g
)
==
"
Impulse(2) - 1.0
"
def
test_multiplication
():
g
=
Impulse
()
*
0.5
...
...
@@ -122,17 +152,23 @@ def test_multiplication():
assert
g
(
1
)
==
0
assert
g
(
2
)
==
0
assert
str
(
g
)
==
"
Impulse() * 0.5
"
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
))
g
=
Step
(
1
)
*
Sinusoid
(
0.5
,
0.25
)
assert
str
(
g
)
==
"
Sinusoid(0.5, 0.25) * 2
"
g
=
Step
(
1
)
*
(
Sinusoid
(
0.5
,
0.25
)
+
1.0
)
assert
g
(
0
)
==
0
assert
g
(
1
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
)
assert
g
(
2
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
)
assert
g
(
3
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
)
assert
g
(
1
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
+
1
)
assert
g
(
2
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
+
1
)
assert
g
(
3
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
+
1
)
assert
str
(
g
)
==
"
Step(1) * (Sinusoid(0.5, 0.25) + 1.0)
"
def
test_division
():
...
...
@@ -142,13 +178,19 @@ def test_division():
assert
g
(
1
)
==
0.5
assert
g
(
2
)
==
0.5
assert
str
(
g
)
==
"
Step() / 2
"
g
=
0.5
/
Step
()
assert
g
(
0
)
==
0.5
assert
g
(
1
)
==
0.5
assert
g
(
2
)
==
0.5
assert
str
(
g
)
==
"
0.5 / Step()
"
g
=
Sinusoid
(
0.5
,
0.25
)
/
(
0.5
*
Step
())
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
))
assert
str
(
g
)
==
"
Sinusoid(0.5, 0.25) / (Step() * 0.5)
"
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