async def do_pcm_test_fft_correction(dut, pdm_fname, test_num): '''Correct for fft innacuracies.''' print('Starting pcm test with fft correction.') x = np.load(pdm_fname, allow_pickle=True) x = parse_mic_data.pad_pdm(x) # pad half-second signal to 1 second x = pdm.pdm_to_pcm(x, 2) y = aco.aco(x) cocotb.fork(write_pcm_input(dut, x)) # change on falling edge of pdm clk fft_out = await check_fft(dut, y[2], test_num) wake_bad = await read_wake_no_assert(dut) # non-deterministic wake print('Running through with known fft value.') for i in range(8000): await FallingEdge(dut.wb_clk_i) y = aco.aco(x, fft_override=fft_out) cocotb.fork(write_pcm_input(dut, x)) # change on falling edge of pdm clk # fft_out = await check_fft(dut, y[2], test_num) cocotb.fork(check_final(dut, y[8], test_num)) aco_out = y[-1] aco_out = aco_out.reshape((int(aco_out.size / 13), 13)) wrd_out = na.get_numpy_pred(aco_out)[0] wake_expected = (wrd_out[0] > wrd_out[1]) await read_wake(dut, wake_expected) print('Finished test.') return wake_expected # assert ensured expected is observed
def get_cosine_test_vector(): '''Get a max amplitude cosine test vector to try saturate the pipeline with.''' t = np.linspace(0, 1, 16000) f = 20 # Hz A = 2**15 - 1 # max 16b signed amplitude x = A * np.cos(2 * np.pi * f * t) x = aco.pdm_model(x, 'fast') sigs = aco.aco(x) return x, sigs
def test_on_set(foo, params): global best_time, best_set, best_q s, d, t, q = params[0], params[1], params[2], params[3] st = time.clock() res = aco(m, s[0], d, t, s[1], s[2], s[3], q) en = time.clock() - st if res in good_answers: if best_time is None or en < best_time: best_time = en best_set = s best_q = q
def get_multi_cosine_test_vector(): '''Get a max amplitude cosine test vector to try saturate the pipeline with.''' t = np.linspace(0, 1, 16000) n_freqs = 100 freqs = np.logspace(0, np.log10(16000), 10) A = (2**15 - 1) / n_freqs # max 16b signed amplitude x = np.zeros(16000) for f in freqs: x += A * np.cos(2 * np.pi * f * t) x = aco.pdm_model(x, 'fast') sigs = aco.aco(x) return x, sigs
def get_random_sample_test_vector(test_num): top_dir = '../../../py/' categories = ['yes/', 'noise/', 'no/', 'unknown/'] category = categories[test_num % 4] sample_dir = top_dir + category fnames = os.listdir(sample_dir) idx = np.random.randint(len(fnames)) fname = sample_dir + fnames[idx] print('Running test with', fname) x = pdm.read_sample_file(fname) x = aco.pdm_model(x, 'fast') sigs = aco.aco(x) return x, sigs
def process_pdm_wake(source='mic', method='cic2'): if source[-4:] == '.wav': # 16b wavfile saved on computer fs, x = wavfile.read(source) x = x[:16000] / 2**8 dfe_out = x elif source == 'mic': # pdm microphone x = get_pdm(serial_port=TEENSY_PORT) x = pad_pdm(x) elif source[-4:] == '.npy': # saved pdm sample x = np.load(source, allow_pickle=True) x = pad_pdm(x) else: raise Exception('unkown source ' + source) if source[-4:] != '.wav': # process pdm to pcm if not a wav file if method == 'cic1': dfe_out = pdm.pdm_to_pcm(x, 1).astype(np.int16) dfe_out *= 8 dfe_out = np.clip(dfe_out, -128, 127).astype(np.int8) elif method == 'cic2': dfe_out = pdm.pdm_to_pcm(x, 2) # dfe_out = dfe_out / 2**5 # scaling now in pdm.py # dfe_out = np.clip(dfe_out, -128, 127).astype(np.int8) elif method == 'ideal': # ideal decimation dfe_out = signal.decimate(x, 10) dfe_out = signal.decimate(dfe_out, 5) dfe_out = signal.decimate(dfe_out, 5) else: raise Exception('unkown method ' + method) detect_max(dfe_out, 'dfe_out') if False: # rescaling dfe_out = dfe_out.astype(np.float32) dfe_out[:5] = dfe_out.mean() dfe_out -= dfe_out.min() dfe_out *= 2**8 / dfe_out.max() dfe_out -= 2**7 dfe_out = dfe_out.astype(np.int8) aco_out = aco.aco(dfe_out)[-1] aco_out = aco_out.reshape((int(aco_out.size / 13), 13)) wrd_out = na.get_numpy_pred(aco_out)[0] wake = (wrd_out[0] > wrd_out[1]) if wake: print('WAKE!') else: print('sleep.') return x, dfe_out, wake
def test_fix_p(): D = np.array([[0, 6, 3, 7, 1], [6, 0, 8, 10, 6], [3, 8, 0, 6, 7], [7, 10, 6, 0, 5], [1, 6, 7, 5, 0]]) good_answers = ([0, 4, 1, 3, 2], 26), ([3, 4, 0, 1, 2], 26), \ ([2, 0, 4, 1, 3], 26), ([2, 3, 4, 1, 0], 26), \ ([1, 4, 0, 2, 3], 26), ([0, 4, 3, 2, 1], 26) m = 5 # amount of ants and cities Q = MAX_DIS * m // 2 # coefficient of the alleged best way P = (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ) # coefficient of evaporation a = 2 # coefficient of strengthen the sense of smell b = 1 # coefficient of strengthen of desire e = 2 # amount of elite ants gen = 200 timing = [None for i in range(len(P))] l = [] for j in (0, 1, 2): for t in range(TIMES): for p in P: st = time.clock() res = aco(m, e, D, gen, a, b, p, Q) en = time.clock() - st if res in good_answers: if timing[P.index(p)] is None or en < timing[P.index(p)]: timing[P.index(p)] = en print(timing) l.append(timing) timing = [None for i in range(len(P))] plt.plot(P, l[0], P, l[1], P, l[2]) plt.xlabel(r'$p$') plt.ylabel(r'$Time(sec)$') plt.title(r'Вариация параметра p') plt.grid(True) plt.show()
def test_fix_Q(): D = np.array([[0, 6, 3, 7, 1], [6, 0, 8, 10, 6], [3, 8, 0, 6, 7], [7, 10, 6, 0, 5], [1, 6, 7, 5, 0]]) good_answers = ([0, 4, 1, 3, 2], 26), ([3, 4, 0, 1, 2], 26), \ ([2, 0, 4, 1, 3], 26), ([2, 3, 4, 1, 0], 26), \ ([1, 4, 0, 2, 3], 26), ([0, 4, 3, 2, 1], 26) m = 5 # amount of ants and cities Q = range(MIN_DIS, MAX_DIS * m) # coefficient of the alleged best way timing = [None for i in range(len(Q))] l = [] p = 0.2 # coefficient of evaporation a = 1 # coefficient of strengthen the sense of smell b = 1 # coefficient of strengthen of desire e = 1 # amount of elite ants gen = 200 for j in (0, 1, 2): for t in range(TIMES): for q in Q: st = time.clock() res = aco(m, e, D, gen, a, b, p, q) en = time.clock() - st if res in good_answers: if timing[Q.index(q)] is None or en < timing[Q.index(q)]: timing[Q.index(q)] = en print(timing) l.append(timing) timing = [None for i in range(len(Q))] plt.plot(Q, l[0], Q, l[1], Q, l[2]) plt.xlabel(r'$Q$') plt.ylabel(r'$Time(sec)$') plt.title(r'Вариация параметра Q') plt.grid(True) plt.show()
async def do_test(dut, test_num): print('Beginning test #{}.'.format(test_num)) await reset(dut) # reset to clear out previous values in pipeline dut.en_i <= 1 if test_num == 0: await do_test_no_en(dut) return elif test_num == 1: x, y = get_sample_test_vector() elif test_num == 2: x, y = get_cosine_test_vector() elif test_num == 3: x, y = get_multi_cosine_test_vector() elif test_num == 4: x, y = get_random_test_vector() else: x, y = get_random_sample_test_vector(test_num) cocotb.fork(write_input(dut, x)) cocotb.fork(check_preemphasis(dut, y[0])) cocotb.fork(check_framing(dut, y[1])) fft_out = await check_fft(dut, y[2], test_num) # Take RTL FFT output and feed it to the ACO model to obtain the rest of the # expected signals. This is because we don't have a bit-accurate model of # the FFT. y = aco.aco(x, fft_override=fft_out) await reset(dut) # reset to clear out previous values in pipeline dut.en_i <= 1 cocotb.fork(write_input(dut, x)) # rewrite the input cocotb.fork(check_preemphasis(dut, y[0])) cocotb.fork(check_framing(dut, y[1])) cocotb.fork(check_fft(dut, y[2], test_num)) cocotb.fork(check_power_spectrum(dut, y[3], test_num)) cocotb.fork(check_filterbank(dut, y[4], test_num)) cocotb.fork(check_log(dut, y[5], test_num)) cocotb.fork(check_dct(dut, y[6], test_num)) cocotb.fork(check_quant(dut, y[7], test_num)) await check_final(dut, y[8], test_num) print()
def get_random_test_vector(): x = np.random.randint(-2**15, 2**15 - 1, size=16000) x = aco.pdm_model(x, 'fast') sigs = aco.aco(x) return x, sigs
def get_sample_test_vector(): '''Get a real audio sample for input and calculate the expected outputs.''' x = pdm.read_sample_file(pdm.SAMPLE_FNAME) x_distorted = aco.pdm_model(x, 'fast') sigs = aco.aco(x_distorted) return x_distorted, sigs
contador = 1 nombre_instancia = "Instancia1.txt" resultados = [] ejemploInstancias = InstanciaTXT(5, 10) max_valor = 0 cont_max_valor = 0 sumatorio_tiempos = 0 # while contador <= numero_de_iteraciones: tiempo_inicial = time() datosInstancia = ejemploInstancias.leeInstancia(nombre_instancia) solucion = aco(datosInstancia, 100, 20, 5.0) datos = solucion.solucion() tiempo_final = time() peso = 0 valor = 0 array = str(datos) for i in datos: peso = peso + i[1] valor = valor + i[2] if max_valor == valor: cont_max_valor = cont_max_valor + 1 if max_valor < valor: max_valor = valor