You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localization.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import numpy as np
  2. import os
  3. class Localization:
  4. def __init__(self):
  5. self.scale_factor = 0.003
  6. self.k_nearest = 15
  7. self.table = np.load('fingerprinting_tables/Julian_BThesis_table2.npy')
  8. self.data = np.load('recorded_data/current_recording.npy')
  9. def localize(self, fv):
  10. """ Input: - fv [type = numpy array (mb list) [15, 1]]:
  11. A feature vector contains the antenna voltages of one sample in following order:
  12. frame1, frame2, ... frame 8, main1, main2, ... main8
  13. - scale_factor [type = float]:
  14. This is multiplied with the fv to adjust the difference in constants between the real world
  15. and the measurements. Things like resistance, coil windings, amplification factors
  16. - k_nearest [type = int]:
  17. This tells the localization method k-nearest how many neighbours it should take into account
  18. to average the position in the end
  19. Output: - position [type = np.array[3]]:
  20. The estimated position of the object in this sample x,y,z
  21. """
  22. feature_vector = fv * self.scale_factor
  23. feature_vector[8] = -feature_vector[8]
  24. repeated_feature_vector = np.square(self.table[:, 9:] - feature_vector)
  25. euclidean_distances = np.sum(repeated_feature_vector, 1)
  26. order = np.argsort(euclidean_distances)
  27. minDist = np.sqrt(euclidean_distances[order[0]])
  28. maxDist = np.sqrt(euclidean_distances[order[self.k_nearest - 1]])
  29. dsum = 0.0
  30. position = np.array([0.0, 0.0, 0.0])
  31. for idx in order[:self.k_nearest]:
  32. d = (maxDist - np.sqrt(euclidean_distances[idx])) / (maxDist - minDist)
  33. dsum += d
  34. position += self.table[idx][:3] * d
  35. position /= dsum
  36. return position
  37. def localize_all_samples(self, input_path, output_path):
  38. # Load data
  39. data = np.load('recorded_data/'+input_path+".npy")
  40. # Just User Feedback
  41. print("Start calculating positions from: recorded_data/" + input_path + ".npy")
  42. print("With: scale_factor=", self.scale_factor, ", k_nearest=", self.k_nearest, ", every 10th sample")
  43. data = data[::10, :] # taking only every 10th sample
  44. # Test if splicing worked
  45. #print("Shape data", np.shape(data))
  46. # print("data[0, :]= ", data[0, :])
  47. # print("data[5, :]= ", data[5, :])
  48. # print("SPLICING")
  49. # print("data[0, :]= ", data[0, :])
  50. # print("data[1, :]= ", data[50, :])
  51. # Localize
  52. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  53. for i in range(np.shape(data)[0]):
  54. fv = data[i, 3:]
  55. positions[i, :] = self.localize(fv)
  56. print("loc progress=", i)
  57. # Save result
  58. np.save('calculated_positions/'+output_path, positions)
  59. print("Saved result in: calculated_positions/"+output_path+".npy")