def get_phase_and_diff(i): fc = F0 + i * STEP data_filter = butter_bandpass_filter(gesture_frames, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) # denoise, 10可能太大了,但目前训练使用的都是10 decompositionQ = seasonal_decompose(Q.T, period=period, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=period, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) trendQ = trendQ[:, period:] trendI = trendI[:, period:] unwrapped_phase = get_phase(trendI, trendQ) # 这里的展开目前没什么效果 # plt.plot(unwrapped_phase[0]) # plt.show() assert unwrapped_phase.shape[1] > 1 # 用diff,和两次diff unwrapped_phase_list[i] = np.diff(unwrapped_phase)[:, :-1]
def extract_phasedata_from_audio_special_for_onemic(audio_file, phasedata_save_file, audio_type='wav', mic_array=True): origin_data, fs = load_audio_data(audio_file, audio_type) fs = fs # 采样率 data = origin_data.reshape((-1, 8)) data = data.T # shape = (num_of_channels, all_frames) data = data[:, int(fs * DELAY_TIME):] mic_num = 0 # 只用一个mic data = data[mic_num, :] data = data.reshape((1, -1)) # 开始处理数据 t = 0 magnti_list = [] for i in range(NUM_OF_FREQ): fc = F0 + i * STEP data_filter = butter_bandpass_filter(data, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) # denoise decompositionQ = seasonal_decompose(Q.T, period=10, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=10, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) trendQ = trendQ[:, 10:] trendI = trendI[:, 10:] magnti = get_phase(trendI, trendQ) # 这里的展开目前没什么效果 # plt.plot(magnti[0]) # plt.show() assert magnti.shape[1] > 1 # 用diff,和两次diff magnti_list.append(np.diff(magnti)[:, :-1]) # plt.plot(np.diff(magnti).reshape(-1)) # plt.show() magnti_list.append(np.diff(np.diff(magnti))) merged_u_p = np.array(magnti_list).reshape((NUM_OF_FREQ * 1 * 2, -1)) print(merged_u_p.shape) # 压缩便于保存 flattened_m_u_p = merged_u_p.flatten() # 由于长短不一,不能放在一起 # np.savetxt(dataset_save_file, flattened_m_u_p.reshape(1, -1)) np.savez_compressed(phasedata_save_file, phasedata=flattened_m_u_p) return 1
def beamform_on_raw_audio_data(filename): data, fs = load_audio_data(filename, 'wav') data = data.T data = data[:7, int(fs * DELAY_TIME):] a_angel = 240 e_angel = 0 two_d_angel = [[np.deg2rad(a_angel), np.deg2rad(e_angel)]] c = 343 spacing = 0.043 mic_array_pos = cons_uca(spacing) sd = steering_plane_wave(mic_array_pos, c, two_d_angel) beamformed_data = beamform_real(data, sd).reshape(1, -1) beamformed_data_2 = ump_8_beamform(data, 48000, two_d_angel).reshape(1, -1) phase_list = [] for i in range(NUM_OF_FREQ): fc = F0 + i * STEP data_filter = butter_bandpass_filter(data, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) decompositionQ = seasonal_decompose(Q.T, period=10, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=10, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T # trendQ = Q # trendI = I assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) # trendQ = trendQ[:, 10:] # trendI = trendI[:, 10:] trendQ = Q trendI = I # draw_circle(trendI[0], trendQ[0]) raw_phase = get_phase(trendI, trendQ) ''' 对相位beamform ''' noise = np.mean(raw_phase[:, 20:60], axis=1).reshape(-1, 1) print(noise.shape) raw_phase_denoised = raw_phase - noise bphase_denoised = beamform_real(raw_phase_denoised, sd).reshape(1, -1) def normalize_max_min(x): max = np.max(x) min = np.min(x) return (x - min) / (max - min) ''' 对beamformed signal求相位 ''' beamformed_data_filter = butter_bandpass_filter( beamformed_data, fc - 150, fc + 150) bI_raw, bQ_raw = get_cos_IQ_raw(beamformed_data_filter, fc, fs) bI = move_average_overlap_filter(bI_raw) bQ = move_average_overlap_filter(bQ_raw) bphase = get_phase(bI, bQ) beamformed_data_filter = butter_bandpass_filter( beamformed_data_2, fc - 150, fc + 150) bI_raw, bQ_raw = get_cos_IQ_raw(beamformed_data_filter, fc, fs) bI = move_average_overlap_filter(bI_raw) bQ = move_average_overlap_filter(bQ_raw) bphase_2 = get_phase(bI, bQ) plt.figure() plt.subplot(2, 1, 1) plt.plot(raw_phase[0]) plt.subplot(2, 1, 2) plt.plot(bphase_denoised[0]) plt.show()
def beamform_after_IQ(filename, start, dur): data, fs = load_audio_data(filename, 'wav') data = data.T data = data[:7, int(fs * DELAY_TIME):] # data = data[:7, start:start+dur] phase_list = [] for i in range(NUM_OF_FREQ): fc = F0 + i * STEP data_filter = butter_bandpass_filter(data, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) # I = butter_lowpass_filter(I_raw, 150) # Q = butter_lowpass_filter(Q_raw, 150) # I = I[:, 5:-5] # Q = Q[:, 5:-5] # plt.plot(I[0][5:-5]) # plt.plot(Q[0][5:-5]) # plt.show() # denoise decompositionQ = seasonal_decompose(Q.T, period=10, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=10, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T # trendQ = Q # trendI = I assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) # trendQ = trendQ[:, 10:] # trendI = trendI[:, 10:] trendQ = Q trendI = I # draw_circle(trendI[0], trendQ[0]) raw_phase = get_phase(trendI, trendQ) exp_phase = trendI + 1j * trendQ ''' 去除噪声,减去平均值(尝试) ''' mean_noise = np.mean(raw_phase[:1000], axis=1) # beamform a_angel = 120 e_angel = 0 two_d_angel = [[np.deg2rad(a_angel), np.deg2rad(e_angel)]] c = 343 spacing = 0.043 mic_array_pos = cons_uca(spacing) # sp = music(data, mic_array_pos, fc, c, np.arange(0, 360), np.arange(0, 30), 1) # # plt.plot(sp.reshape(-1)) # plt.pcolormesh(sp) # plt.show() azumi = (0, 360) eleva = (0, 90) # beamscan_spectrum = np.zeros((azumi[1] - azumi[0], eleva[1] - eleva[0])) # # 估计 # for angel_1 in range(azumi[0], azumi[1]): # for angel_2 in range(eleva[0], eleva[1]): # two_angel = [[np.deg2rad(angel_1), np.deg2rad(angel_2)]] # sd = steering_plane_wave(mic_array_pos, c, two_angel) # adjust = np.exp(-1j * 2 * np.pi * fc * sd) # syn_signals = exp_phase * adjust.T # # syn_signals = np.real(syn_signals) # beamformed_signal = np.sum(syn_signals, axis=0) # beamscan_spectrum[angel_1][angel_2] = np.sum(abs(beamformed_signal)) # # return beamscan_spectrum # # plt.pcolormesh(beamscan_spectrum) # # plt.show() # plt.plot(beamscan_spectrum[:, 0]) # plt.grid() # plt.show() sd = steering_plane_wave(mic_array_pos, c, two_d_angel) adjust = np.exp(-1j * 2 * np.pi * fc * sd).T assert exp_phase.shape[0] == adjust.shape[0] syn_signals = exp_phase * adjust beamformed_signal = np.sum(syn_signals, axis=0).reshape(1, -1) phase = get_phase(np.real(beamformed_signal), np.imag(beamformed_signal)) plt.show() plt.figure() plt.subplot(2, 1, 1) plt.plot(raw_phase[0]) plt.subplot(2, 1, 2) plt.plot(phase[0]) plt.show() phase_list.append(phase)
def extract_magndata_from_beamformed_audio(audio_file, phasedata_save_file, audio_type='pcm', mic_array=False): origin_data, fs = load_audio_data(audio_file, audio_type) fs = fs # 采样率 # 已经reshape过了为什么还要reshape if mic_array: data = origin_data.reshape((-1, N_CHANNELS + 1)) else: data = origin_data.reshape((-1, N_CHANNELS)) data = data.T # shape = (num_of_channels, all_frames) data = data[:, int(fs * DELAY_TIME):] if mic_array: # 第八个声道不要 data = data[:7, :] # beamform,角度? data = ump_8_beamform(data, fs, angel=[[np.pi * 4 / 3, 0]]) assert data.shape[0] == 1 # 开始处理数据 t = 0 magnti_list = [] for i in range(NUM_OF_FREQ): fc = F0 + i * STEP data_filter = butter_bandpass_filter(data, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) # denoise decompositionQ = seasonal_decompose(Q.T, period=10, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=10, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) trendQ = trendQ[:, 10:] trendI = trendI[:, 10:] magnti = get_magnitude(trendI, trendQ) # 这里的展开目前没什么效果 # plt.plot(np.diff(magnti[0].reshape(-1))) # plt.show() assert magnti.shape[1] > 1 # 用diff,和两次diff magnti_list.append(np.diff(magnti)[:, :-1]) # plt.plot(np.diff(magnti).reshape(-1)) # plt.show() magnti_list.append(np.diff(np.diff(magnti))) merged_u_p = np.array(magnti_list).reshape((NUM_OF_FREQ * 1 * 2, -1)) print(merged_u_p.shape) # 压缩便于保存 flattened_m_u_p = merged_u_p.flatten() # 由于长短不一,不能放在一起 # np.savetxt(dataset_save_file, flattened_m_u_p.reshape(1, -1)) np.savez_compressed(phasedata_save_file, phasedata=flattened_m_u_p) return 1
def extract_phasedata_from_audio(audio_file, phasedata_save_file, audio_type='pcm', mic_array=False): ''' 目前使用的是对相位取一次差分 :param audio_file: :param phasedata_save_file: :param audio_type: :param mic_array: :return: ''' origin_data, fs = load_audio_data(audio_file, audio_type) fs = fs # 采样率 # data = origin_data[int(fs * DELAY_TIME):] # data = data.reshape((-1, N_CHANNELS)) # data = data.T # shape = (num_of_channels, all_frames) if mic_array: data = origin_data.reshape((-1, N_CHANNELS + 1)) else: data = origin_data.reshape((-1, N_CHANNELS)) data = data.T # shape = (num_of_channels, all_frames) data = data[:, int(fs * DELAY_TIME):] if mic_array: # 第八个声道不要 data = data[:7, :] # 开始处理数据 t = 0 unwrapped_phase_list = [] for i in range(NUM_OF_FREQ): fc = F0 + i * STEP data_filter = butter_bandpass_filter(data, fc - 150, fc + 150) I_raw, Q_raw = get_cos_IQ_raw(data_filter, fc, fs) # 滤波+下采样 I = move_average_overlap_filter(I_raw) Q = move_average_overlap_filter(Q_raw) # denoise decompositionQ = seasonal_decompose(Q.T, period=10, two_sided=False) trendQ = decompositionQ.trend decompositionI = seasonal_decompose(I.T, period=10, two_sided=False) trendI = decompositionI.trend trendQ = trendQ.T trendI = trendI.T assert trendI.shape == trendQ.shape if len(trendI.shape) == 1: trendI = trendI.reshape((1, -1)) trendQ = trendQ.reshape((1, -1)) trendQ = trendQ[:, 10:] trendI = trendI[:, 10:] unwrapped_phase = get_phase(trendI, trendQ) # 这里的展开目前没什么效果 # plt.plot(unwrapped_phase[0]) # plt.show() assert unwrapped_phase.shape[1] > 1 # 用diff,和两次diff unwrapped_phase_list.append(np.diff(unwrapped_phase)[:, :-1]) # plt.plot(np.diff(unwrapped_phase).reshape(-1)) # plt.show() # unwrapped_phase_list.append(np.diff(np.diff(unwrapped_phase))) merged_u_p = np.array(unwrapped_phase_list).reshape( (NUM_OF_FREQ * N_CHANNELS * 1, -1)) print(merged_u_p.shape) # 压缩便于保存 flattened_m_u_p = merged_u_p.flatten() # 由于长短不一,不能放在一起 # np.savetxt(dataset_save_file, flattened_m_u_p.reshape(1, -1)) np.savez_compressed(phasedata_save_file, phasedata=flattened_m_u_p) return N_CHANNELS