diff --git a/libraries/base/common/python/try_round_weight.py b/libraries/base/common/python/try_round_weight.py index e2a6986247d7e14a2a23ec22b1c187f66f17ec45..35ef0b31f2e49ada3036626b3d8d9bfff3880c39 100644 --- a/libraries/base/common/python/try_round_weight.py +++ b/libraries/base/common/python/try_round_weight.py @@ -48,47 +48,82 @@ import common as cm # Parse arguments to derive user parameters _parser = argparse.ArgumentParser('try_round_weight') +_parser.add_argument('-S', default=0, type=int, help='Random number seed') _parser.add_argument('-N', default=1000, type=int, help='Number of input samples') -_parser.add_argument('--weight_lo', default=0.3, type=float, help='Lowest weight') -_parser.add_argument('--weight_hi', default=2.0, type=float, help='Highest weight') -_parser.add_argument('--weight_step', default=0.1, type=float, help='Step weight') +_parser.add_argument('--s_lo', default=0.1, type=float, help='Lowest sigma') +_parser.add_argument('--s_hi', default=25.0, type=float, help='Highest sigma') +_parser.add_argument('--s_step', default=0.1, type=float, help='Step sigma') +_parser.add_argument('--w_lo', default=0.3, type=float, help='Lowest weight') +_parser.add_argument('--w_hi', default=2.0, type=float, help='Highest weight') +_parser.add_argument('--w_step', default=0.1, type=float, help='Step weight') args = _parser.parse_args() -N_samples = args.N -weight_lo = args.weight_lo -weight_hi = args.weight_hi -weight_step = args.weight_step +np.random.seed(args.S) -# Prepare signals +# Prepare noise signal +N_samples = args.N +noise = np.random.randn(N_samples) noise = np.random.randn(N_samples) noise /= np.std(noise) -# Noise level range, 1 unit = 1 LSbit -sigma_lo = 0.1 -sigma_hi = 25 -sigmas = np.arange(sigma_lo, sigma_hi, 0.1) +# Noise levels range, 1 unit = 1 LSbit +sigma_lo = args.s_lo +sigma_hi = args.s_hi +sigma_step = args.s_step +sigmas = np.arange(sigma_lo, sigma_hi, sigma_step) N_sigmas = len(sigmas) -# Weight range, unit weight = 1 +# Weights range, unit weight = 1 +weight_lo = args.w_lo +weight_hi = args.w_hi +weight_step = args.w_step weights = np.arange(weight_lo, weight_hi, weight_step) N_weights = len(weights) # Determine weighted rounded noise sigma / weighted noise sigma for range of weights and input noise sigmas sigmas_ratio = np.nan * np.zeros((N_weights, N_sigmas)) # w rows, s cols +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 noise_q = np.round(noise_s) 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 - sigma_qq = np.std(noise_q_weighted_q) - sigma_sq = np.std(noise_s_weighted_q) - if sigma_sq != 0: - sigmas_ratio[w][s] = sigma_qq / sigma_sq # weighted rounded noise sigma / weighted noise sigma + 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[w][s] = s_qq / s_sq # weighted rounded noise sigma / weighted noise sigma +# Transpose [w][s] to have index ranges [s][w] +sigmas_ratio_T = sigmas_ratio.transpose() +sigmas_qq_T = sigmas_qq.transpose() +sigmas_sq_T = sigmas_sq.transpose() # Plot results figNr = 0 +figNr += 1 +plt.figure(figNr) +for s, sigma in enumerate(sigmas): + plt.plot(weights, sigmas_qq_T[s], label='s = %4.2f' % sigma) +plt.title("Sigma of weighted quantized noise") +plt.xlabel("Weight") +plt.ylabel("Sigma") +plt.legend(loc='upper right') +plt.grid() + +figNr += 1 +plt.figure(figNr) +for s, sigma in enumerate(sigmas): + plt.plot(weights, sigmas_ratio_T[s], label='s = %4.2f' % sigma) +plt.title("Relative sigma difference of weighting after / before quantisation") +plt.xlabel("Weight") +plt.ylabel("Relative sigma difference") +plt.legend(loc='upper right') +plt.grid() + figNr += 1 plt.figure(figNr) for w, weight in enumerate(weights):