def GetSNRs(h5_module_data_dir): # ! Process each wifi module module_data_num = dict() named_SNRs = {} for wifi_module_file in glob.glob(os.path.join(h5_module_data_dir, '*.h5')): module_name = os.path.split(wifi_module_file)[1].split('.')[0] sh_logger.info("Currently Processing Wifi Module {}".format(module_name)) # ! Get Raw Data with h5py.File(wifi_module_file) as hf: I_data = hf['I'][...] Q_data = hf['Q'][...] # ! Get SNR I_energies = I_data ** 2 Q_energies = Q_data ** 2 head_tail_len = int(I_data.shape[1] * kHeadTailPercentage) # Get unbalance samples indexes_unbalance = [] sh_logger.debug("Detect Unbalanced") process_bar = ProcessBar(I_energies.shape[0] - 1) for i in range(I_energies.shape[0]): I_energy = I_energies[i, :] Q_energy = Q_energies[i, :] if np.mean(I_energy[-head_tail_len:]) < kTailInHeadThreshold * np.mean(I_energy[0:head_tail_len])\ and \ np.mean(Q_energy[-head_tail_len:]) < kTailInHeadThreshold * np.mean(Q_energy[0:head_tail_len]): indexes_unbalance.append(i) process_bar.UpdateBar(i) # Get low power samples I_mean_energy = np.mean(I_energies) Q_mean_energy = np.mean(Q_energies) indexes_low_power = [] sh_logger.debug("Detect Low Power") process_bar = ProcessBar(I_energies.shape[0] - 1) for i in range(I_energies.shape[0]): I_energy = I_energies[i, :] Q_energy = Q_energies[i, :] if np.mean(I_energy) < kSampleInBatchMeanThreshold * I_mean_energy\ or \ np.mean(Q_energy) < kSampleInBatchMeanThreshold * Q_mean_energy : indexes_low_power.append(i) process_bar.UpdateBar(i) half_tail_indexes = list(filter(lambda index: index not in indexes_low_power, indexes_unbalance)) if kIsDebug: print("Plot half tail indexes") PlotSamples(I_data[half_tail_indexes, :]) PlotSamples(Q_data[half_tail_indexes, :]) head_half_tail_I = I_data[half_tail_indexes, :1000] tail_half_tail_I = I_data[half_tail_indexes, -1000:] head_half_tail_Q = Q_data[half_tail_indexes, :1000] tail_half_tail_Q = Q_data[half_tail_indexes, -1000:] head_mean_power = np.mean(head_half_tail_I ** 2 + head_half_tail_Q ** 2) tail_mean_power = np.mean(tail_half_tail_I ** 2 + tail_half_tail_Q ** 2) named_SNRs[module_name] = 10 * np.log10(head_mean_power / tail_mean_power) print(f'Current SNR is {10 * np.log10(head_mean_power / tail_mean_power)}') return named_SNRs
def TrunkQuiteTo10k(data: np.ndarray) -> Union[np.ndarray, int]: normalized_data = data / np.max(abs(data)) if kTrunkNaive: # Most Naive Way: trunked_data = normalized_data[-kSampleLen - 1:-1] return trunked_data else: st_energy = [] i1 = 0 i2 = i1 + kNStep while i2 <= len(normalized_data): # Sadly use abs won't make this program quicker # st_energy.append(np.sum(np.abs(normalized_data[i1:i2]))) st_energy.append(np.sum(normalized_data[i1:i2]**2)) i1 = i2 i2 += kNStep threshold = np.mean(st_energy) for i in range(len(st_energy)): if st_energy[i] > threshold: break start_point = i * kNStep + kNStep sh_logger.info("Start Point is at {}".format(start_point)) sh_logger.info("Threshold = {}".format(threshold)) if kIsDebug: plt.plot(np.array(st_energy)) plt.show() print("Press Any Key to Continue:") input() if start_point + kSampleLen <= len(normalized_data): return normalized_data[start_point:start_point + kSampleLen] else: return start_point
def DataProcess(): # Process Each Dir for wifi_module_path in glob.glob(os.path.join(kDataPath, '*')): if os.path.isdir(wifi_module_path): print('-------------------------------------') print('Processing Data In', wifi_module_path) wifi_module_name = os.path.split(wifi_module_path)[1] if os.path.isdir(os.path.join( wifi_module_path, 'output.finished')): # If finished, Skip sh_logger.warning( "Current Wifi Module has been Processed. Skip") continue if not os.path.isdir(os.path.join(wifi_module_path, 'output')): # Make output dir os.mkdir(os.path.join(wifi_module_path, 'output')) if kIsTiming: # Time for Processing one folder start_folder_time = time.time() # Process Each CSV for csv_path in glob.glob(os.path.join(wifi_module_path, '*.csv')): csv_name = (os.path.split(csv_path)[1] )[:-4] # Get csv name without '.csv' sh_logger.info("------------------------------") sh_logger.info('Processing CSV ' + csv_name + ' from wifi module ' + wifi_module_name) if kIsTiming: start_csv_time = time.time( ) # Time for processing one csv file # Prepare file path. If I/Q files exit, skip I_path = os.path.join(wifi_module_path, 'output', csv_name + '_I' + '.txt') Q_path = os.path.join(wifi_module_path, 'output', csv_name + '_Q' + '.txt') if os.path.isfile(I_path) and os.path.isfile(Q_path): sh_logger.warning('Trunked data of ' + csv_name + ' already exits. Skip') else: # Get Trunked and normalized IQ, save as txt I, Q = Get10kIQFromCSV(csv_path) write_start_time = time.time() SaveIQ(I_path, I, Q_path, Q) sh_logger.debug( "Writing txt cost {}s".format(time.time() - write_start_time)) # Timing if kIsTiming: sh_logger.debug("Processing Current CSV cost " + str(time.time() - start_csv_time) + 's') if kIsTiming: sh_logger.debug("Processing Current module cost " + str(time.time() - start_folder_time) + 's') # Label Current Wifi module as Finished os.rename(os.path.join(wifi_module_path, 'output'), os.path.join(wifi_module_path, 'output.finished'))
def show_measure_result(self, rslt_save_path: Optional[str]=None): self._update_attr_need_measured() if not self._is_measured: sh_logger.warning("Using Unmeasured Attribution of MultiClassificationTester") sh_logger.info("Now Print Out Test Result") for key, value in self.attr_need_measured.items(): print("**************************************") print(key) print(value) with open(rslt_save_path, 'w') as txt_f: for key, value in self.attr_need_measured.items(): txt_f.write("**************************************\n") txt_f.write(key + "\n") txt_f.write(str(value) + "\n")
def __init__(self, snapshot_file_str): self.snapshot_file_str = snapshot_file_str snapshot_dir = os.path.split(snapshot_file_str)[0] if not os.path.isdir(snapshot_dir): os.makedirs(snapshot_dir) sh_logger.info(f"Make snapshot dir at {snapshot_dir}")