diff --git a/libraries/base/common/python/try_round.py b/libraries/base/common/python/try_round.py index 0959b31a6677248904744e3273e4a6cae7ade637..b0ce97d471a901cb58404ba5e9bf6f93fe741f07 100755 --- a/libraries/base/common/python/try_round.py +++ b/libraries/base/common/python/try_round.py @@ -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 diff --git a/libraries/base/common/python/try_round_weight.py b/libraries/base/common/python/try_round_weight.py old mode 100644 new mode 100755 index 24715832be19db892977b4153dea8baeb68359cd..08c79bfb2b54c17b678a0553ed8bc44881dae448 --- a/libraries/base/common/python/try_round_weight.py +++ b/libraries/base/common/python/try_round_weight.py @@ -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() diff --git a/libraries/base/common/python/try_wrap.py b/libraries/base/common/python/try_wrap.py old mode 100644 new mode 100755 index a2cfcb4b50af0eb6bbcffbb1851fa95820d9824d..64bba67e7a4513566fb98576ab4a35c89c20ac71 --- a/libraries/base/common/python/try_wrap.py +++ b/libraries/base/common/python/try_wrap.py @@ -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) -