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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_1cm_precision_corrected_antenas.npy') # corrected table made by Ihm (No ferrite)
  8. #self.table = np.load('fingerprinting_tables/SmallManualFingerprint_Ferrite.npy') # manual fingerprinting table (Ferrite) [A bit off]
  9. #self.table = np.load('fingerprinting_tables/Julian_BThesis_table2_switchedAnt5&6 and 7&8.npy') # Switched Ant5<->6 and 7<->8 in Excel (No Ferrite) [Does not work!]
  10. self.table = np.load('fingerprinting_tables/Julian_THIS_ONE_IS_IT.npy') # 2cm precision this definetly worked for (No ferrite)
  11. self.data = np.load('recorded_data/current_recording.npy')
  12. <<<<<<< HEAD
  13. def localize(self, fv, exclude_frames=False):
  14. =======
  15. def localize(self, fv):
  16. >>>>>>> parent of f2b8eb4 (Script that can analyze (Horizontal+Diagonal, Center, Off_center) quickly)
  17. """ Input: - fv [type = numpy array (mb list) [15, 1]]:
  18. A feature vector contains the antenna voltages of one sample in following order:
  19. frame1, frame2, ... frame 8, main1, main2, ... main8
  20. - scale_factor [type = float]:
  21. This is multiplied with the fv to adjust the difference in constants between the real world
  22. and the measurements. Things like resistance, coil windings, amplification factors
  23. - k_nearest [type = int]:
  24. This tells the localization method k-nearest how many neighbours it should take into account
  25. to average the position in the end
  26. Output: - position [type = np.array[3]]:
  27. The estimated position of the object in this sample x,y,z
  28. """
  29. feature_vector = fv * self.scale_factor
  30. # table_copy = self.table # make a copy of the fingerprinting table
  31. # table_copy[:, 17:25] = 0 # set all mains in the copy to 0
  32. #
  33. # max_idx = np.argmax(abs(fv[:8])) # find highest Frame
  34. #
  35. # table_copy[:, max_idx+9] = 0 # set the row of highest Frame in fingerprinting to 0
  36. #
  37. # print("fv =", fv)
  38. # print("max_idx=", max_idx)
  39. # print("tablecopy= ", table_copy[0, :])
  40. repeated_feature_vector = np.square(self.table[:, 9:] - feature_vector) # Before changing anything on this function
  41. #repeated_feature_vector = np.square(table_copy[:, 9:] - feature_vector)
  42. euclidean_distances = np.sum(repeated_feature_vector, 1)
  43. order = np.argsort(euclidean_distances)
  44. minDist = np.sqrt(euclidean_distances[order[0]])
  45. maxDist = np.sqrt(euclidean_distances[order[self.k_nearest - 1]])
  46. dsum = 0.0
  47. position = np.array([0.0, 0.0, 0.0])
  48. for idx in order[:self.k_nearest]:
  49. d = (maxDist - np.sqrt(euclidean_distances[idx])) / (maxDist - minDist)
  50. dsum += d
  51. position += self.table[idx][:3] * d
  52. position /= dsum
  53. #print(position)
  54. return position*1000 # conversion from metre to mm
  55. def localize_all_samples(self, input_path, output_path):
  56. # Load data
  57. data = np.load('recorded_data/' + input_path + ".npy")
  58. # Just User Feedback
  59. print("Start calculating positions from: recorded_data/" + input_path + ".npy")
  60. print("With: scale_factor=", self.scale_factor, ", k_nearest=", self.k_nearest, ", every 10th sample")
  61. data = data[::10, :] # taking only every 10th sample
  62. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  63. #Normal Localization
  64. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  65. for i in range(np.shape(data)[0]):
  66. fv = data[i, 3:]
  67. positions[i, :] = self.localize(fv)
  68. #print("loc progress=", i)
  69. # Save result
  70. np.save('calculated_positions/' + output_path, positions)
  71. print("Saved result in: calculated_positions/" + output_path + ".npy")
  72. def localize_all_samples_direct(self, data, output_path):
  73. """ same as localize all samples, but takes input_data directly and doesnt load it"""
  74. # Just User Feedback
  75. # print("Start calculating positions from data with shape:", np.shape(data))
  76. # print("With: scale_factor=", self.scale_factor, ", k_nearest=", self.k_nearest, ", every 10th sample")
  77. #data = data[::10, :] # taking only every 10th sample
  78. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  79. #Normal Localization
  80. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  81. for i in range(np.shape(data)[0]):
  82. fv = data[i, 3:]
  83. #print("fv=", fv)
  84. positions[i, :] = self.localize(fv)
  85. #print("loc progress=", i)
  86. # Save result
  87. np.save('calculated_positions/' + output_path, positions)
  88. #print("Saved result in: calculated_positions/" + output_path + ".npy")
  89. return positions
  90. def localize_averaged_samples(self, input_path, output_path):
  91. # Load data
  92. data = np.load('recorded_data/' + input_path + ".npy")
  93. # Just User Feedback
  94. print("Start calculating positions from: recorded_data/" + input_path + ".npy")
  95. print("With: scale_factor=", self.scale_factor, ", k_nearest=", self.k_nearest)
  96. # Average the recorded samples before localization
  97. positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
  98. mean_data = np.zeros(np.shape(data))
  99. mean_data[:, :] = np.mean(data, axis=0) # average all recorded samples
  100. fv = mean_data[0, 3:] # as now all of mean_data is the same in every entry, just take any entry (at pos 0)
  101. positions = self.localize(fv) # we get a single position out
  102. print("Averaged position: x=", positions[0], ", y=", positions[1], ", z=", positions[2])
  103. # Save result
  104. np.save('calculated_positions/'+output_path, positions)
  105. print("Saved result in: calculated_positions/"+output_path+".npy")