def get_filter_coefficients(delay_samples=None): if not delay_samples is None: (delay_samples, delay_dir) = set_delay_parameters(delay_samples) print("Wait {} seconds to allow AEC to converge".format( CONVERGE_TIME_SEC)) time.sleep(CONVERGE_TIME_SEC) try: os.remove("aec_coefficients.py") except OSError: pass print("Running GET_FILTER_COEFFICIENTS_AEC...") vfctrl.do_command("GET_FILTER_COEFFICIENTS_AEC") # H_hat values are stored in coefficients.py, reload them with exec() # Defines the following values: # frame_advance # y_channel_count # x_channel_count # max_phase_count # f_bin_count # H_hat namespace = {} with open('aec_coefficients.py') as f: exec(f.read(), globals(), namespace) if not delay_samples is None: output_filename = OUTPUT_DIR + "coefficients_w_delay_samples_{}_dir_{}.py"\ .format(delay_samples, delay_dir) shutil.copy('aec_coefficients.py', output_filename) return namespace['H_hat']
def estimate_delay(max_values, x_ch_num, y_ch_num, set_delay=False, print_delay_info=True): """Analyze the max values and print the recommended delay values Args: max_values: list of max values and respective samples indexes Returns: None """ recommended_delay_samples = 0 for y_ch in range(y_ch_num): for x_ch in range(x_ch_num): recommended_delay_samples += max_values[y_ch][x_ch][0] recommended_delay_samples /= (y_ch_num * x_ch_num) for y_ch in range(y_ch_num): for x_ch in range(x_ch_num): m_x = max_values[y_ch][x_ch][0] if max_values[y_ch][x_ch][1] == 0: print( "Error: max value is 0, check that the reference is correctly played into the device" ) print_delay_info = False if abs(m_x - recommended_delay_samples ) > 0.1 * abs(recommended_delay_samples): print("Warning: delay for y_ch{}, x_ch{} differs from the average: {} vs {} relative to {} samples". \ format(y_ch, x_ch, m_x, recommended_delay_samples, -MAX_DELAY_SAMPLES)) recommended_delay_samples -= MAX_DELAY_SAMPLES + 40 if recommended_delay_samples < -MAX_DELAY_SAMPLES: recommended_delay_samples = MAX_DELAY_SAMPLES recommended_delay_dir = 0 if recommended_delay_samples < 0: recommended_delay_samples = -recommended_delay_samples recommended_delay_dir = 1 if set_delay: print("Setting optimal delay values on the device:") vfctrl.do_command('SET_DELAY_DIRECTION', recommended_delay_dir) vfctrl.do_command('SET_DELAY_SAMPLES', recommended_delay_samples) elif print_delay_info: print("Recommended delay settings are:") print("\tSET_DELAY_DIRECTION {}".format(recommended_delay_dir)) print("\tSET_DELAY_SAMPLES {:.0f}".format(recommended_delay_samples))
def set_delay_parameters(delay_samples): """Configure the delay parameters to test Args: delay_samples: number of delay samples to test Returns: number of delay samples and the delay direction """ delay_dir = 0 if delay_samples < 0: delay_dir = 1 if delay_samples > MAX_DELAY_SAMPLES: overlapping_samples = delay_samples - MAX_DELAY_SAMPLES delay_samples = MAX_DELAY_SAMPLES delay_samples = abs(delay_samples) print("Getting coefficients for delay samples {} and direction {}". \ format(delay_samples, delay_dir)) vfctrl.do_command("SET_DELAY_DIRECTION", delay_dir) vfctrl.do_command("SET_DELAY_SAMPLES", delay_samples) vfctrl.do_command("RESET_FILTER_AEC", 0) return (delay_samples, delay_dir)
def main(args): if not os.path.isdir(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) print("Run script on {} interface".format(args.interface[0])) vfctrl.init(args.interface[0], build=args.build_host) print(DIVIDER_STRING) print("Get AEC parameters") start_delay = -MAX_DELAY_SAMPLES try: frame_advance = int(vfctrl.do_command("GET_FRAME_ADVANCE_AEC")) except Exception as e: print(str(e)) print( "Error: check that the device is connected and correctly flashed") exit(4) x_ch_num = int(vfctrl.do_command("GET_X_CHANNELS_AEC")) y_ch_num = int(vfctrl.do_command("GET_Y_CHANNELS_AEC")) phases_num = int( vfctrl.do_command("GET_X_CHANNEL_PHASES_AEC").decode().lstrip().split( " ")[0]) filter_len_samples = frame_advance * phases_num h_hat_ir = [[[np.zeros(0)] for x in range(x_ch_num)] for y in range(y_ch_num)] print(DIVIDER_STRING) if args.full: original_delay_samples = int(vfctrl.do_command('GET_DELAY_SAMPLES')) original_delay_direction = int( vfctrl.do_command('GET_DELAY_DIRECTION')) for delay_samples in range(-MAX_DELAY_SAMPLES, MAX_DELAY_SAMPLES + filter_len_samples, filter_len_samples): H_hat = get_filter_coefficients(delay_samples) h_hat_ir = compute_all_h_hat(H_hat, h_hat_ir, overlapping_samples=0) print(DIVIDER_STRING) else: H_hat = get_filter_coefficients(None) h_hat_ir = compute_all_h_hat(H_hat, h_hat_ir, overlapping_samples=0) max_values = [[[] for x in range(x_ch_num)] for y in range(y_ch_num)] for y_ch in range(y_ch_num): for x_ch in range(x_ch_num): h_hat_ir_abs = np.absolute(h_hat_ir[y_ch][x_ch]) max_values[y_ch][x_ch] = (h_hat_ir_abs.argmax(), h_hat_ir_abs.max()) if args.full: print(DIVIDER_STRING) estimate_delay(max_values, x_ch_num, y_ch_num, set_delay=args.set_delay) if args.full and not args.set_delay: # Reset delay to original values and reset filter vfctrl.do_command('SET_DELAY_SAMPLES', original_delay_samples, quiet=True) vfctrl.do_command('SET_DELAY_DIRECTION', original_delay_direction, quiet=True) vfctrl.do_command("RESET_FILTER_AEC", 0) print("Plot AEC filter coefficients") delay = 0 if args.full: delay = -MAX_DELAY_SAMPLES plot_h_hat(h_hat_ir, x_ch_num, y_ch_num, delay)
if args.h_hat_filename: filename = args.h_hat_filename if os.path.splitext(filename)[1] == '.py': # load the filter coefficients with open(filename, 'r') as f: exec('\n'.join(f.readlines())) else: H_hat = np.load(filename) make_plot(H_hat, args.half, args.show_range, show_button=False) elif args.interface: vfctrl.init(args.interface[0]) while True: try: os.remove("ic_coefficients.py") except OSError: pass print("Getting IC filter coefficients...") vfctrl.do_command("GET_FILTER_COEFFICIENTS_IC") # reload the filter coefficients with open("ic_coefficients.py", 'r') as f: exec('\n'.join(f.readlines())) update = make_plot(H_hat, args.half) if not update: break else: parse_arguments(print_help=True) print("\nError: Specify an interface or a coefficients file") sys.exit(1)
def main(set_delay): delay_time = (float(delay_samples) / 16000) * 1000 print("Set delay to -{:.1f}ms".format(delay_time)) do_command('SET_DELAY_DIRECTION', 1) do_command('SET_DELAY_SAMPLES', delay_samples) print("Enable delay estimator") do_command('SET_DELAY_ESTIMATOR_ENABLED', 1) time.sleep(0.5) do_command('RESET_FILTER_AEC', 1) print("Wait 100ms...") time.sleep(0.1) delays = [] for i in range(15): delay = int(do_command('GET_DELAY_ESTIMATE')) delays.append(delay) time.sleep(0.2) estimated_delay = (delays[-1] - delay_samples) - delay_margin print("Delays: {}".format(delays)) print("Mode(Delay): {}".format(mode(delays))) set_delay_samples = abs(estimated_delay) if set_delay_samples > delay_samples: set_delay_samples = delay_samples set_delay_direction = 0 if estimated_delay < 0: set_delay_direction = 1 if set_delay: print("Setting delay to {}".format(estimated_delay)) do_command('SET_DELAY_DIRECTION', set_delay_direction) do_command('SET_DELAY_SAMPLES', set_delay_samples) else: print( "To set delay on the device, use the following control commands:") print('SET_DELAY_DIRECTION {}'.format(int(set_delay_direction))) print('SET_DELAY_SAMPLES {}'.format(int(set_delay_samples))) print("Or run this script again with the --set option\n") print("Switch back to AEC") do_command('SET_DELAY_ESTIMATOR_ENABLED', 0)
set_delay_samples = abs(estimated_delay) if set_delay_samples > delay_samples: set_delay_samples = delay_samples set_delay_direction = 0 if estimated_delay < 0: set_delay_direction = 1 if set_delay: print("Setting delay to {}".format(estimated_delay)) do_command('SET_DELAY_DIRECTION', set_delay_direction) do_command('SET_DELAY_SAMPLES', set_delay_samples) else: print( "To set delay on the device, use the following control commands:") print('SET_DELAY_DIRECTION {}'.format(int(set_delay_direction))) print('SET_DELAY_SAMPLES {}'.format(int(set_delay_samples))) print("Or run this script again with the --set option\n") print("Switch back to AEC") do_command('SET_DELAY_ESTIMATOR_ENABLED', 0) if __name__ == "__main__": args = parse_arguments() vfctrl.init(args.interface[0]) success = do_command('GET_VERSION') if not success: sys.exit(1) main(args.set)