Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HDL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RTSD
HDL
Commits
7731e040
Commit
7731e040
authored
2 years ago
by
Eric Kooistra
Browse files
Options
Downloads
Patches
Plain Diff
Try wrapping in a summator.
parent
28023929
No related branches found
No related tags found
1 merge request
!305
Resolve L2SDP-846
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
libraries/base/common/python/try_wrap.py
+89
-0
89 additions, 0 deletions
libraries/base/common/python/try_wrap.py
with
89 additions
and
0 deletions
libraries/base/common/python/try_wrap.py
0 → 100644
+
89
−
0
View file @
7731e040
#! /usr/bin/env python3
###############################################################################
#
# Copyright 2022
# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
# Author: Eric Kooistra
# Date: jan 2023
# Purpose:
# Try wrapping in a summator
# Description:
# Usage:
# > python3 try_wrap.py -N 10
#
import
argparse
import
textwrap
import
numpy
as
np
import
matplotlib
matplotlib
.
use
(
'
tkagg
'
)
# to make X11 forwarding work
import
matplotlib.pyplot
as
plt
import
random
import
common
as
cm
# Parse arguments to derive user parameters
_parser
=
argparse
.
ArgumentParser
(
description
=
""
.
join
(
textwrap
.
dedent
(
"""
\
The int_wrap function keeps w LSbits and remove MSbits from an integer,
so that the result is in range range -2**(w-1) to + 2**(w-1)-1. This
try_wrap.py show that wrap(sum) = wrap(sum(wrap)), so intermediate
wrapping in a summator does not change the final sum.
The wrap function is distributive, similar to modulo [1]:
(a + b) mod n = [(a mod n) + (b mod n)] mod n
ab mod n = [(a mod n)(b mod n)] mod n
> python try_wrap.py -N 10 --w_inp 10 --w_sum 4
References:
[1] https://en.wikipedia.org/wiki/Modulo_operation
\n
"""
)),
formatter_class
=
argparse
.
RawTextHelpFormatter
)
_parser
.
add_argument
(
'
-N
'
,
default
=
1000
,
type
=
int
,
help
=
'
Number of input samples
'
)
_parser
.
add_argument
(
'
--w_inp
'
,
default
=
10
,
type
=
int
,
help
=
'
Number bits of input samples
'
)
_parser
.
add_argument
(
'
--w_sum
'
,
default
=
4
,
type
=
int
,
help
=
'
Number bits of output sum
'
)
args
=
_parser
.
parse_args
()
N_samples
=
args
.
N
W_input
=
args
.
w_inp
W_sum
=
args
.
w_sum
lo
=
-
1
*
2
**
(
W_input
-
1
)
hi
=
2
**
(
W_input
-
1
)
-
1
x
=
[
random
.
randint
(
lo
,
hi
)
for
i
in
range
(
N_samples
)]
x_wrap
=
[
cm
.
int_wrap
(
i
,
W_sum
)
for
i
in
x
]
x_wrap_sum_wrap
=
cm
.
int_wrap
(
cm
.
add_list_elements
(
x_wrap
),
W_sum
)
x_wrap_sum
=
cm
.
int_wrap
(
cm
.
add_list_elements
(
x
),
W_sum
)
print
(
x
)
print
()
print
(
x_wrap
)
print
()
if
x_wrap_sum_wrap
==
x_wrap_sum
:
print
(
'
OK
'
)
else
:
print
(
'
Error:
'
)
print
(
x_wrap_sum_wrap
)
print
(
x_wrap_sum
)
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