Skip to content
Snippets Groups Projects
Commit 498a6751 authored by Eric Kooistra's avatar Eric Kooistra
Browse files

Correct for flake8.

parent bf85f9eb
No related branches found
No related tags found
No related merge requests found
Pipeline #43421 passed
......@@ -22,23 +22,14 @@
# Author: Eric Kooistra
# Date: may 2021
# Purpose:
# Simulate linearity of rounding
# Description:
# Usage:
# > python3 try_round.py -N 1024 -ampl 12 --useplot
import argparse
import numpy as np
import matplotlib
matplotlib.use('tkagg') # to make X11 forwarding work
import matplotlib.pylab as plt
"""Try rounding
Simulate linearity of rounding.
Usage:
> python try_round.py -h
> python3 try_round.py -N 1024 -w 8 -f 3 --useplot
Note:
. For values exactly halfway between rounded decimal values, NumPy rounds to
......@@ -47,8 +38,11 @@ Note:
"""
import argparse
import numpy as np
import matplotlib.pylab as plt
import matplotlib
matplotlib.use('tkagg') # to make X11 forwarding work
import matplotlib.pyplot as plt
import common as cm
figNr = 1
......@@ -57,7 +51,7 @@ figNr = 1
_parser = argparse.ArgumentParser('try_round')
_parser.add_argument('-N', default=1024, type=int, help='Number of points of FFT')
_parser.add_argument('-w', default=8, type=int, help='Total number of bits')
_parser.add_argument('-f', default=0, type=int, help='Number of bits of fraction that gets rounded')
_parser.add_argument('-f', default=1, type=int, help='Number of bits of fraction that gets rounded')
_parser.add_argument('--useplot', action='store_true', dest='useplot', default=False, help='Default without plotting, else with plotting')
args = _parser.parse_args()
......@@ -71,8 +65,8 @@ useplot = args.useplot
# Sinus
t = 2 * np.pi * np.arange(0, N) / N
a_adc = np.round(ampl*np.sin(t))
a_away = [cm.int_round(inp = x, r = fraction, direction="HALF_AWAY") for x in a_adc]
a_even = [cm.int_round(inp = x, r = fraction, direction="HALF_EVEN") for x in a_adc]
a_away = [cm.int_round(inp=x, r=fraction, direction="HALF_AWAY") for x in a_adc]
a_even = [cm.int_round(inp=x, r=fraction, direction="HALF_EVEN") for x in a_adc]
a_adc = a_adc / scale
# Spectrum
......
......@@ -62,8 +62,6 @@ import matplotlib
matplotlib.use('tkagg') # to make X11 forwarding work
import matplotlib.pyplot as plt
import common as cm
# Parse arguments to derive user parameters
_parser = argparse.ArgumentParser(
description="".join(textwrap.dedent("""\
......@@ -145,29 +143,29 @@ sigmas_ratio_sq_input = np.nan * np.zeros((N_weights, N_sigmas)) # w rows, s co
sigmas_qq = np.zeros((N_weights, N_sigmas))
sigmas_sq = np.zeros((N_weights, N_sigmas))
for s, sigma in enumerate(sigmas):
noise_s = noise * sigma
if resolution == 0:
if fraction == 0:
# Default use full double resolution of fraction of noise_s
noise_q = np.round(noise_s)
else:
# First use round() to get noise_f with a fraction of fraction number of LSbits
noise_f = np.round(noise_s * fraction_factor) / fraction_factor
# Then use round to round the fraction of fraction number of LSbits in noise_f
noise_q = np.round(noise_f)
else:
noise_q = np.round(noise_s * resolution_factor) / resolution_factor
noise_s = noise * sigma
if resolution == 0:
if fraction == 0:
# Default use full double resolution of fraction of noise_s
noise_q = np.round(noise_s)
else:
# First use round() to get noise_f with a fraction of fraction number of LSbits
noise_f = np.round(noise_s * fraction_factor) / fraction_factor
# Then use round to round the fraction of fraction number of LSbits in noise_f
noise_q = np.round(noise_f)
else:
noise_q = np.round(noise_s * resolution_factor) / resolution_factor
for w, weight in enumerate(weights):
noise_q_weighted_q = np.round(noise_q * weight) # apply weight to rounded noise
noise_s_weighted_q = np.round(noise_s * weight) # apply weight to original noise
s_qq = np.std(noise_q_weighted_q)
s_sq = np.std(noise_s_weighted_q)
sigmas_qq[w][s] = s_qq
sigmas_sq[w][s] = s_sq
if s_sq != 0:
sigmas_ratio_qq_sq[w][s] = s_qq / s_sq # weighted rounded noise sigma / weighted noise sigma
sigmas_ratio_sq_input[w][s] = s_sq / sigma # weighted noise sigma / input noise sigma
for w, weight in enumerate(weights):
noise_q_weighted_q = np.round(noise_q * weight) # apply weight to rounded noise
noise_s_weighted_q = np.round(noise_s * weight) # apply weight to original noise
s_qq = np.std(noise_q_weighted_q)
s_sq = np.std(noise_s_weighted_q)
sigmas_qq[w][s] = s_qq
sigmas_sq[w][s] = s_sq
if s_sq != 0:
sigmas_ratio_qq_sq[w][s] = s_qq / s_sq # weighted rounded noise sigma / weighted noise sigma
sigmas_ratio_sq_input[w][s] = s_sq / sigma # weighted noise sigma / input noise sigma
# Transpose [w][s] to have index ranges [s][w]
sigmas_ratio_qq_sq_T = sigmas_ratio_qq_sq.transpose()
sigmas_ratio_sq_input_T = sigmas_ratio_sq_input.transpose()
......
......@@ -31,11 +31,6 @@
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
......@@ -86,4 +81,3 @@ else:
print('Error:')
print(x_wrap_sum_wrap)
print(x_wrap_sum)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment