def handle_data2(): import time import re from utils import smooth, interpl, chunk_decode len_e = 3000 register = 0x0B last_ts = None plt.ion() ax = plt.gca() result_ts = time.time() while True: response, timestamp = q2.get() if not response: return # Fix raw value val = int.from_bytes(response, 'big') val_fixed = val raw_frames_m.push(np.array([ [timestamp, val_fixed], ])) if last_ts is None: last_ts = raw_frames_m.window[0][0] if raw_frames_m.window[-1][0] - last_ts < 0.6: continue M = raw_frames_m.window M = M[np.logical_and(M[:, 0] > last_ts - 0.5, M[:, 0] < last_ts + 0.5)] Mtime = M[:, 0] value = M[:, 1] last_ts = Mtime[-1] sample_time = np.arange(Mtime[0], Mtime[-1], 1 / 2400) sample_time = sample_time[sample_time < Mtime[-1]] sample_value = interpl(Mtime, value, sample_time, 'nearest') sample_value_smooth = smooth(sample_value, 41) sample_value_DCremove = smooth(sample_value - sample_value_smooth, 5) value = np.zeros((10, 1)) for i in range(10): temp_sample = sample_value_DCremove[i:len_e:10] value[i] = max(temp_sample) + min(temp_sample) std_min = max(value) shift_index = np.where(value == std_min)[0][0] sample_wave = sample_value_DCremove[shift_index:len_e:10] temp_sample = sample_value_DCremove[shift_index:len_e:10] # bit_stream = sample_wave <= (max(temp_sample) + min(temp_sample)) / 2 bit_stream = sample_wave <= np.mean(temp_sample) bit_stream = bit_stream.astype(int) result = chunk_decode(bit_stream) if result is None: result = chunk_decode(bit_stream, flip=True) if result is not None: result_ts = time.time() for i in result: print(i)
def handle_data(pos_data): from utils import smooth, interpl, chunk_decode from numpy import genfromtxt correct_pck_num = 0 diff_num = [] first_detected = False # plt.ion() # f = plt.figure() # ax = f.add_subplot(311) # ax2 = f.add_subplot(312) # ax3 = f.add_subplot(313) # pos_data = genfromtxt(FILE_NAME, delimiter=',') real_time = pos_data[:,1] real_val = pos_data[:,0] even_time = np.arange(real_time[0], real_time[-1], 1/2400) even_time = even_time[even_time < real_time[-1]] even_val = interpl(real_time, real_val, even_time, 'nearest') even_val_smooth = smooth(even_val, 21) even_val_DCremove = smooth(even_val - even_val_smooth, 5) # np.savetxt('even_val_DCremove.csv', even_val_DCremove, delimiter=',') total_points = len(even_time) # maintain slide window within 0.3s raw_frames_m = deque(maxlen=int(2400*0.3)) for i in range(total_points): val = even_val_DCremove[i] ts = even_time[i] raw_frames_m.append([ts, val]) if len(raw_frames_m) != raw_frames_m.maxlen: continue sample_value_DCremove = np.array(raw_frames_m)[:,1] value = np.zeros((10, 1)) for i in range(10): temp_sample = sample_value_DCremove[i:raw_frames_m.maxlen:10] value[i] = max(temp_sample) - min(temp_sample) std_min = max(value) shift_index = np.where(value==std_min)[0][0] sample_wave = sample_value_DCremove[shift_index:raw_frames_m.maxlen:10] temp_sample = sample_value_DCremove[shift_index:raw_frames_m.maxlen:10] # bit_stream = sample_wave <= (max(temp_sample) + min(temp_sample)) / 2 bit_stream = sample_wave <= np.mean(temp_sample) bit_stream = bit_stream.astype(int) result, diff_count = chunk_decode(bit_stream) if result is None: result, diff_count = chunk_decode(bit_stream, flip=True) if result is not None: if not first_detected: first_detected = True for _ in range(int(raw_frames_m.maxlen/2)): raw_frames_m.popleft() # print(result) start_ts = np.array(raw_frames_m)[0,0] end_ts = np.array(raw_frames_m)[-1,0] to_plot = pos_data[np.logical_and(pos_data[:,1] > start_ts, pos_data[:,1] < end_ts)][:,0] to_plot_FFT = fft(to_plot[:250]) l = 30 to_plot_FFT[1+l:to_plot_FFT.size-l] = 0 # ax.plot(to_plot[:250]) # ax2.plot(ifft(to_plot_FFT)) # ax3.plot(to_plot_FFT[1:]) # plt.pause(10) # plt.show() correct_pck_num += 1 diff_num.append(diff_count[0]) elif first_detected and diff_count != []: diff_num.append(diff_count[0]) print('correct_pck_num:', correct_pck_num) # print('diff_num:', diff_num) diff_num = sorted(diff_num) diff_num = diff_num[:100] # print(diff_num) total_error_num = sum(diff_num) + (100 - len(diff_num)) * 62
def resolve(self, val_tuple, fix=True): timestamp, val = val_tuple val_fixed = val if fix: # print(val_fixed) if val_fixed < 128: val_fixed += 128 if val_fixed > 240: return self.frames.append((timestamp, val_fixed)) frames = self.frames if self.last_ts is None: self.last_ts = frames[0][0] last_ts = self.last_ts # print(timestamp, val_fixed) else: self.frames_accum.append((timestamp, val_fixed)) frames = self.frames_accum if self.last_ts_accum is None: self.last_ts_accum = frames[0][0] last_ts = self.last_ts_accum if frames[-1][0] - last_ts < 0.6: return M = np.array(frames) M = M[np.logical_and(M[:,0] > last_ts - 0.5, M[:,0] < last_ts + 0.5)] if M.size == 0: M = np.array(frames) M = M[M[:,0] > last_ts - 0.5] Mtime = M[:,0] value = M[:,1] if fix: self.last_ts = Mtime[-1] else: self.last_ts_accum = Mtime[-1] if Mtime[-1] - Mtime[0] < 0.3: return sample_time = np.arange(Mtime[0], Mtime[-1], 1 / 2400) sample_value = interpl(Mtime, value, sample_time[:-1], 'nearest') sample_time = sample_time[:-1] sample_value_smooth = smooth(sample_value, 41) sample_value_DCremove = smooth(sample_value - sample_value_smooth, 5) value = np.zeros((10, 1)) for i in range(10): temp_sample = sample_value_DCremove[i:self.len_e:10] value[i] = max(temp_sample) - min(temp_sample) std_min = max(value) shift_index = np.where(value==std_min)[0][0] sample_wave = sample_value_DCremove[shift_index:self.len_e:10] temp_sample = sample_value_DCremove[shift_index:self.len_e:10] bit_stream = sample_wave <= np.mean(temp_sample) bit_stream = bit_stream.astype(int) result = chunk_decode(bit_stream) if result is None: result = chunk_decode(bit_stream, flip=True) if result is not None: # print(result) self.reset() return result
def handle_data(): global succ_count, crc_count import time import re from utils import smooth, interpl, chunk_decode len_e = 3000 register = 0x0B last_ts = None plt.ion() ax = plt.gca() result_ts = time.time() while True: response, timestamp = q.get() if not response: return # Fix raw value val = int.from_bytes(response, 'big') val_fixed = val if register == 0x0D: if val_fixed < 128: val_fixed += 128 if val_fixed > 240: continue raw_frames_m.push(np.array([ [timestamp, val_fixed], ])) # raw_frames_m.update_line_data() # ax.relim() # renew the data limits # ax.autoscale_view(True, True, True) # rescale plot view # plt.draw() # plot new figure # plt.pause(1e-17) # continue if last_ts is None: last_ts = raw_frames_m.window[0][0] if raw_frames_m.window[-1][0] - last_ts < 0.6: continue M = raw_frames_m.window M = M[np.logical_and(M[:, 0] > last_ts - 0.5, M[:, 0] < last_ts + 0.5)] Mtime = M[:, 0] value = M[:, 1] last_ts = Mtime[-1] sample_time = np.arange(Mtime[0], Mtime[-1], 1 / 2400) sample_time = sample_time[sample_time < Mtime[-1]] sample_value = interpl(Mtime, value, sample_time, 'nearest') sample_value_smooth = smooth(sample_value, 41) sample_value_DCremove = smooth(sample_value - sample_value_smooth, 5) value = np.zeros((10, 1)) for i in range(10): # temp_sample = sample_value_DCremove[i:len_e:5] temp_sample = sample_value_DCremove[i:len_e:10] # temp_sample2 = sample_value_DCremove[i+5:len_e:10*4] # temp_sample3 = sample_value_DCremove[i+10:len_e:10*4] # temp_sample4 = sample_value_DCremove[i+15:len_e:10*4] # print(temp_sample) # ts1 = np.hstack((temp_sample, temp_sample3)) # print(ts1) # ts2 = np.hstack((temp_sample2, temp_sample4)) value[i] = max(temp_sample) + min(temp_sample) # value[i] += np.std(ts2[ts2 > (max(ts2) + min(ts2)) / 2]) # value[i] std_min = max(value) shift_index = np.where(value == std_min)[0][0] sample_wave = sample_value_DCremove[shift_index:len_e:10] temp_sample = sample_value_DCremove[shift_index:len_e:10] ###################### draw ######################## # plt.cla() # plt.subplot(2, 1, 1) # plt.cla() # plt.plot(np.arange(len(sample_value_DCremove[shift_index:len_e])), sample_value_DCremove[shift_index:len_e]) # plt.subplot(2, 1, 2) # plt.plot(np.arange(sample_wave.size), sample_value_DCremove[shift_index:len_e:5], marker='x') # # plt.scatter(np.arange(sample_wave.size), sample_value_DCremove[shift_index:len_e:5]) # threshold = np.array([(max(temp_sample) + min(temp_sample)) / 2] * sample_wave.size) # # threshold = np.array([(max(sample_value_DCremove) + min(sample_value_DCremove)) / 2] * sample_wave.size) # plt.plot(np.arange(sample_wave.size), threshold) # # plt.scatter(sample_wave) # # for i in range(2, len_e, 10): # # plt.plot(sample_value_DCremove[i:i + 10], '.-') # raw_frames_m.update_line_data() # ax.relim() # renew the data limits # ax.autoscale_view(True, True, True) # rescale plot view # plt.draw() # plot new figure # plt.pause(1) ###################### draw ######################## # thresholding # bit_stream = sample_wave <= (max(temp_sample) + min(temp_sample)) / 2 bit_stream = sample_wave <= np.mean(temp_sample) bit_stream = bit_stream.astype(int) result, crc_fail = chunk_decode(bit_stream) if result is None: result, crc_fail = chunk_decode(bit_stream, flip=True) if crc_fail != []: crc_count.append(np.mean(crc_fail)) else: if crc_fail != []: crc_count.append(np.mean(crc_fail)) if result is not None: result_ts = time.time() succ_count += len(result) print('succ_count', succ_count) print('crc_fail', np.mean(crc_count)) crc_fail_count = len(crc_count) miss_count = 100 + 48.65 - succ_count - crc_fail_count # bit_error_rate = (succ_count * 14 * 4 + (100 + 48.65 - succ_count) * (14*4 - np.mean(crc_count))) / ((100 + 48.65) * 14*4) bit_error_rate = (succ_count * 14 * 4 + crc_fail_count * (14 * 4 - np.mean(crc_count))) / ( (100 + 48.65) * 14 * 4) print('bit_error_rate', 1 - bit_error_rate) for i in result: print(i)