123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import numpy as np
- import os
-
-
- class Localization:
- def __init__(self):
- self.scale_factor = 0.003
- self.k_nearest = 15
- self.table = np.load('fingerprinting_tables/Julian_BThesis_table2.npy')
- self.data = np.load('recorded_data/current_recording.npy')
-
- def localize(self, fv):
- """ Input: - fv [type = numpy array (mb list) [15, 1]]:
- A feature vector contains the antenna voltages of one sample in following order:
- frame1, frame2, ... frame 8, main1, main2, ... main8
- - scale_factor [type = float]:
- This is multiplied with the fv to adjust the difference in constants between the real world
- and the measurements. Things like resistance, coil windings, amplification factors
- - k_nearest [type = int]:
- This tells the localization method k-nearest how many neighbours it should take into account
- to average the position in the end
-
- Output: - position [type = np.array[3]]:
- The estimated position of the object in this sample x,y,z
- """
- feature_vector = fv * self.scale_factor
- feature_vector[8] = -feature_vector[8]
-
- repeated_feature_vector = np.square(self.table[:, 9:] - feature_vector)
- euclidean_distances = np.sum(repeated_feature_vector, 1)
- order = np.argsort(euclidean_distances)
-
- minDist = np.sqrt(euclidean_distances[order[0]])
- maxDist = np.sqrt(euclidean_distances[order[self.k_nearest - 1]])
- dsum = 0.0
- position = np.array([0.0, 0.0, 0.0])
- for idx in order[:self.k_nearest]:
- d = (maxDist - np.sqrt(euclidean_distances[idx])) / (maxDist - minDist)
- dsum += d
-
- position += self.table[idx][:3] * d
- position /= dsum
-
- return position
-
- def localize_all_samples(self, input_path, output_path):
- # Load data
- data = np.load('recorded_data/'+input_path+".npy")
- # Just User Feedback
- print("Start calculating positions from: recorded_data/" + input_path + ".npy")
- print("With: scale_factor=", self.scale_factor, ", k_nearest=", self.k_nearest, ", every 10th sample")
-
- data = data[::10, :] # taking only every 10th sample
-
- # Test if splicing worked
- #print("Shape data", np.shape(data))
- # print("data[0, :]= ", data[0, :])
- # print("data[5, :]= ", data[5, :])
- # print("SPLICING")
- # print("data[0, :]= ", data[0, :])
- # print("data[1, :]= ", data[50, :])
-
- # Localize
- positions = np.empty((np.shape(data)[0], 3), dtype=np.float)
- for i in range(np.shape(data)[0]):
- fv = data[i, 3:]
- positions[i, :] = self.localize(fv)
- print("loc progress=", i)
-
-
-
-
- # Save result
- np.save('calculated_positions/'+output_path, positions)
- print("Saved result in: calculated_positions/"+output_path+".npy")
|