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
c050576b
Commit
c050576b
authored
1 year ago
by
Oscar Gustafsson
Browse files
Options
Downloads
Patches
Plain Diff
Add Upsample and Downsample signal generators
parent
a0ca094e
No related branches found
Branches containing commit
No related tags found
1 merge request
!304
Add Upsample and Downsample signal generators
Pipeline
#95361
failed
1 year 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
+77
-1
77 additions, 1 deletion
b_asic/signal_generator.py
test/test_signal_generator.py
+60
-0
60 additions, 0 deletions
test/test_signal_generator.py
with
137 additions
and
1 deletion
b_asic/signal_generator.py
+
77
−
1
View file @
c050576b
...
...
@@ -13,7 +13,7 @@ if you want more information.
from
math
import
pi
,
sin
from
numbers
import
Number
from
typing
import
Optional
,
Sequence
from
typing
import
Optional
,
Sequence
,
Union
import
numpy
as
np
...
...
@@ -408,3 +408,79 @@ class _DivGenerator(SignalGenerator):
else
f
"
{
self
.
_b
}
"
)
return
f
"
{
a
}
/
{
b
}
"
class
Upsample
(
SignalGenerator
):
"""
Signal generator that upsamples the value of a signal generator or a sequence.
*factor* - 1 zeros are inserted between every value. If a sequence, it will
automatically be zero-padded.
Parameters
----------
data : SignalGenerator or 1-D array
The signal generator or array to upsample.
factor : int
Upsampling factor.
phase : int, default 0
The phase of the upsampling.
"""
def
__init__
(
self
,
data
:
Union
[
SignalGenerator
,
Sequence
[
complex
]],
factor
:
int
,
phase
:
int
=
0
,
)
->
None
:
if
not
isinstance
(
data
,
SignalGenerator
):
data
=
ZeroPad
(
data
)
self
.
_generator
=
data
self
.
_factor
=
factor
self
.
_phase
=
phase
def
__call__
(
self
,
time
:
int
)
->
complex
:
index
=
(
time
-
self
.
_phase
)
/
self
.
_factor
if
int
(
index
)
==
index
:
return
self
.
_generator
(
int
(
index
))
else
:
return
0.0
def
__repr__
(
self
)
->
str
:
return
f
"
Upsample(
{
self
.
_generator
!r}
,
{
self
.
_factor
}
,
{
self
.
_phase
}
)
"
class
Downsample
(
SignalGenerator
):
"""
Signal generator that downsamples the value of a signal generator or a sequence.
Return every *factor*:th value. If a sequence, it will automatically be zero-padded.
Parameters
----------
data : SignalGenerator or 1-D array
The signal generator or array to downsample.
factor : int
Downsampling factor.
phase : int, default 0
The phase of the downsampling.
"""
def
__init__
(
self
,
data
:
Union
[
SignalGenerator
,
Sequence
[
complex
]],
factor
:
int
,
phase
:
int
=
0
,
)
->
None
:
if
not
isinstance
(
data
,
SignalGenerator
):
data
=
ZeroPad
(
data
)
self
.
_generator
=
data
self
.
_factor
=
factor
self
.
_phase
=
phase
def
__call__
(
self
,
time
:
int
)
->
complex
:
index
=
time
*
self
.
_factor
+
self
.
_phase
return
self
.
_generator
(
index
)
def
__repr__
(
self
)
->
str
:
return
f
"
Downsample(
{
self
.
_generator
!r}
,
{
self
.
_factor
}
,
{
self
.
_phase
}
)
"
This diff is collapsed.
Click to expand it.
test/test_signal_generator.py
+
60
−
0
View file @
c050576b
...
...
@@ -5,12 +5,14 @@ import pytest
from
b_asic.signal_generator
import
(
Constant
,
Delay
,
Downsample
,
FromFile
,
Gaussian
,
Impulse
,
Sinusoid
,
Step
,
Uniform
,
Upsample
,
ZeroPad
,
_AddGenerator
,
_DivGenerator
,
...
...
@@ -284,3 +286,61 @@ def test_fromfile(datadir):
with
pytest
.
raises
(
ValueError
,
match
=
"
could not convert string
"
):
g
=
FromFile
(
datadir
.
join
(
'
bad.csv
'
))
def
test_upsample
():
g
=
Upsample
([
0.4
,
0.6
],
2
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.4
assert
g
(
1
)
==
0
assert
g
(
2
)
==
0.6
assert
g
(
3
)
==
0.0
assert
str
(
g
)
==
"
Upsample(ZeroPad([0.4, 0.6]), 2, 0)
"
g
=
Upsample
([
0.4
,
0.6
],
2
,
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.0
assert
g
(
1
)
==
0.4
assert
g
(
2
)
==
0.0
assert
g
(
3
)
==
0.6
assert
g
(
4
)
==
0.0
assert
str
(
g
)
==
"
Upsample(ZeroPad([0.4, 0.6]), 2, 1)
"
g
=
Upsample
([
0.4
,
0.6
],
3
)
assert
g
(
-
1
)
==
0.0
assert
g
(
0
)
==
0.4
assert
g
(
1
)
==
0.0
assert
g
(
2
)
==
0.0
assert
g
(
3
)
==
0.6
assert
g
(
4
)
==
0.0
assert
str
(
g
)
==
"
Upsample(ZeroPad([0.4, 0.6]), 3, 0)
"
g
=
Upsample
(
Sinusoid
(
0.5
,
0.25
),
3
)
assert
g
(
0
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
)
assert
g
(
1
)
==
0.0
assert
g
(
2
)
==
0.0
assert
g
(
3
)
==
pytest
.
approx
(
sqrt
(
2
)
/
2
)
assert
g
(
4
)
==
0.0
assert
g
(
5
)
==
0.0
assert
g
(
6
)
==
pytest
.
approx
(
-
sqrt
(
2
)
/
2
)
assert
str
(
g
)
==
"
Upsample(Sinusoid(0.5, 0.25), 3, 0)
"
def
test_downsample
():
g
=
Downsample
([
0.4
,
0.6
],
2
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.4
assert
g
(
1
)
==
0.0
assert
g
(
2
)
==
0.0
assert
str
(
g
)
==
"
Downsample(ZeroPad([0.4, 0.6]), 2, 0)
"
g
=
Downsample
([
0.4
,
0.6
],
2
,
1
)
assert
g
(
-
1
)
==
0
assert
g
(
0
)
==
0.6
assert
g
(
1
)
==
0.0
assert
g
(
2
)
==
0.0
assert
str
(
g
)
==
"
Downsample(ZeroPad([0.4, 0.6]), 2, 1)
"
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