def minimize_time_delay(dt): """ Get experiment data """ ap = pd.read_csv(fname, index_col=0, names=['MAC', 'Time', 'RSSIs', 'channel']) ap = ap.loc[ap.index == cfg_exp.mac] ap['Time'] += dt # ap = ap.loc[ap['Time'] >= track.track_time[i][0]] # ap = ap.loc[ap['Time'] <= track.track_time[i][-1]] ap_rssis = list(ap['RSSIs']) for k in range(len(ap_rssis)): ap_rssis[k] = parse_rssi(ap_rssis[k]) del ap['RSSIs'] if ap_rssis: ap_rssis = pd.DataFrame(ap_rssis, columns=['V0', 'V1', 'V2', 'V3', 'H0', 'H1', 'H2', 'H3'], index=ap.index) ap = pd.concat([ap, ap_rssis], axis=1) ap_rssis = np.array(ap_rssis) ap_arranged = fn.arrange_data(ap_rssis) # Arranging model data ap_max = np.apply_along_axis(np.max, 1, ap_rssis) # conditions not_sat_power = ap_max <= config.max_rssi not_low_power = ap_max >= config.min_rssi not_erroneous = ap_arranged[:, 8] > -10 ap_arranged = pd.DataFrame(ap_arranged, columns=['V0', 'V1', 'V2', 'V3', 'H0', 'H1', 'H2', 'H3', 'VmH'], index=ap.index) ap_arranged_filtered = ap_arranged.loc[not_sat_power & not_low_power & not_erroneous] ap_arranged_filtered = np.array(ap_arranged_filtered) ap_arranged_filtered = fn.noise_filter(ap_arranged_filtered) # Filtered ap_pred_times[i][j] = np.array(ap['Time'].loc[not_sat_power & not_low_power & not_erroneous]) # Predicting basic model result if ap_arranged_filtered.shape[0]: ap_pred[i][j] = cal.rfc[cur_ap_i].predict(ap_arranged_filtered) ap_pred[i][j] = ap_pred[i][j].reshape((ap_pred[i][j].shape[0], 1)) # # plot predictions # plt.plot(ap_pred_times[i][j], ap_pred[i][j], 'go') # plt.show() """ amalgamating predictions for each time frame """ # not valid prediction are saved as 100 ap_timed_pred, ap_timed_sd = timed_predictions(ap_pred[i][j], ap_pred_times[i][j], track.track_time_int[i]) # remove NaNs ap_timed_pred = fn.remove_nan(ap_timed_pred) # plt.plot(track.track_time_int[i], track.doa_true[0][:, j], 'r', # track.track_time_int[i], ap_timed_pred, 'go') # plt.show() rsme = np.sqrt(np.sum((track.doa_true[0][:, j].reshape(ap_timed_pred.shape) - ap_timed_pred) ** 2) / ap_timed_pred.shape[0]) # print 'AP: ', j, 'RSME is: ', rsme return rsme
# # plot predictions # plt.plot(ap_pred_times[i][j], ap_pred[i][j], 'go') # plt.show() """ amalgamating predictions for each time frame """ # Building time frames time_frames.append(range(int(track.track_time[i][0]), int(track.track_time[i][-1] + cfg_exp.time_step), cfg_exp.time_step)) # not valid prediction are saved as 100 ap_timed_pred[i], ap_timed_sd[i] = fn_exp.timed_predictions(ap_pred[i], ap_pred_times[i], time_frames[i]) # remove NaNs ap_timed_pred[i] = fn.remove_nan(ap_timed_pred[i]) # Holt's filtering algorithm holt = np.zeros(ap_timed_pred[i].shape) holt[0, :] = ap_timed_pred[i][0, :] holt_trend = np.zeros(ap_timed_pred[i].shape) for j in range(1, ap_timed_pred[i].shape[0]): holt[j, :] = (1 - cfg_exp.alpha) * (holt[j-1, :] + holt_trend[j-1, :]) + cfg_exp.alpha * ap_timed_pred[i][j, :] holt_trend[j, :] = cfg_exp.trend * (holt[j, :] - holt[j-1, :]) + (1 - cfg_exp.trend) * holt_trend[j-1, :] ap_timed_holt[i] = holt # Kalman's filtering algorithm kalman_x_p = np.zeros(ap_timed_pred[i].shape) kalman_x_m = np.zeros(ap_timed_pred[i].shape)
# Calculate total SD sdmax, sdmin = fn_exp.add_sd(angle0, angle1, dist0, dist1) weights.append(fn.find_weights(sdmax)) # Calculate position_error(crossing) in order to find optimal weights # [x, y] prelim_pos = np.array(prelim_pos) weights = np.array(weights) prelim_pos_x = prelim_pos[:, 0] prelim_pos_y = prelim_pos[:, 1] pos[j] = fn_exp.estimate_xy(prelim_pos_x, prelim_pos_y, weights) # Change NaN to last known position pos = fn.remove_nan(pos) # Remove points from outside # pos = fn.remove_outside(pos) estimate_pos.append(pos) # Holt's filtering algorithm holt = np.zeros(pos.shape) holt[0, :] = pos[0, :] holt_trend = np.zeros(pos.shape) for j in range(1, pos.shape[0]): holt[j, :] = (1 - cfg_exp.alpha) * (holt[j-1, :] + holt_trend[j-1, :]) + cfg_exp.alpha * pos[j, :] holt_trend[j, :] = cfg_exp.trend * (holt[j, :] - holt[j-1, :]) + (1 - cfg_exp.trend) * holt_trend[j-1, :] # # 1D plot
ap_pred[i][j] = cal.rfc[cur_ap_i].predict(ap_arranged_filtered) ap_pred[i][j] = ap_pred[i][j].reshape((ap_pred[i][j].shape[0], 1)) # # plot predictions # plt.plot(ap_pred_times[i][j], ap_pred[i][j], 'go') # plt.show() """ amalgamating predictions for each time frame """ # not valid prediction are saved as 100 ap_timed_pred[i], ap_timed_sd[i] = fn_exp.timed_predictions(ap_pred[i], ap_pred_times[i], track.track_time_int[i]) # remove NaNs ap_timed_pred[i] = fn.remove_nan(ap_timed_pred[i]) # Kalman's filtering algorithm kalman_x_p = np.zeros(ap_timed_pred[i].shape) kalman_x_p[0, :] = ap_timed_pred[i][0, :] kalman_p_m = np.zeros(ap_timed_pred[i].shape) kalman_p_p = np.zeros(ap_timed_pred[i].shape) kalman_p_p[0, :] = np.ones((1, 4)) * cfg_exp.kalman_q for j in range(1, ap_timed_pred[i].shape[0]): kalman_p_m[j, :] = kalman_p_p[j-1, :] + cfg_exp.kalman_q kalman_k_j = kalman_p_m[j, :] / (kalman_p_m[j, :] + cfg_exp.kalman_r) kalman_p_p[j, :] = (1 - kalman_k_j) * kalman_p_m[j, :] kalman_x_p[j, :] = kalman_x_p[j-1, :] + kalman_k_j * (ap_timed_pred[i][j, :] - kalman_x_p[j-1, :]) ap_timed_kaplan[i] = kalman_x_p