def data_average(Dist, source_loc, sensor_loc, Ptx): # ,alpha,Pl0,Ptx,expect,std_err,Nm PPrx = zeros((1, Dist.shape[0])) for ll in range(0, myglobals.Nm): dij = Dist N = dij.shape[0] sigmaij = myglobals.std_err**2 * exp(-abs(dij) / myglobals.dcor) # # free space model di = zeros((1, N)) Mu = zeros((1, N)) for i in range(0, N): # free space model di[0, i] = get_distance(source_loc, sensor_loc[i, :]) if di[0, i] <= myglobals.d0: Mu[0, i] = Ptx + 20 * log10( (myglobals.c) / (4 * pi * di[0, i] * myglobals.f)) elif di[0, i] > myglobals.d0: Mu[0, i] = Ptx + 20 * log10( (myglobals.c) / (4 * pi * myglobals.f)) - 10 * myglobals.alpha * log10( di[0, i]) # correlated shadowing !!!!! Prx = random.multivariate_normal(Mu[0], sigmaij, 1) PPrx = PPrx + Prx PPrx = PPrx / myglobals.Nm return PPrx
def generate_matrices(p, Dist, Pos_vec, coord): N = Dist.shape[0] pad_col = np.ones((N + 1, 1)) pad_col[-1] = 0 pad_row = np.ones((1, N)) B = pad_col B[-1] = 1 d1 = Dist.reshape(1, N * N) #SEMI2 = [Spherical_modul(i, ps1, ps2, ps3) for i in dd] semi = Exponent_modul(d1[0], p[0], p[1], p[2]) # #===to check fit curves # plt.figure(30) # plt.scatter(d1[0], semi) # plt.xlabel('distance[m]') # plt.ylabel('Semivariance') # plt.title('Sample Semivariogram') # #=== semi = semi.reshape(N, N) #semi = semi + np.eye(semi.shape[1], dtype=int) * 0.000000000000001 semi1 = np.vstack((semi, pad_row)) A = np.hstack((semi1, pad_col)) for tx in range(0, N): d2 = get_distance(coord, Pos_vec[tx, :]) B[tx, 0] = Exponent_modul(d2, p[0], p[1], p[2]) return A, B
def data_for_true_cor(dist, source_loc, points_loc, Ptx): # correlated shadowing dij = dist N = dij.shape[0] sigmaij = myglobals.std_err**2 * exp(-abs(dij) / myglobals.dcor) # free space model di = zeros((1, N)) Mu = zeros((1, N)) for i in range(0, N): di[0, i] = get_distance(source_loc, points_loc[i, :]) if di[0, i] == 0: Mu[0, i] = myglobals.Ptx #num=i elif di[0, i] <= myglobals.d0: Mu[0, i] = Ptx + 20 * log10( (myglobals.c) / (4 * pi * di[0, i] * myglobals.f)) elif di[0, i] > myglobals.d0: Mu[0, i] = Ptx + 20 * log10( (myglobals.c) / (4 * pi * myglobals.f)) - 10 * myglobals.alpha * log10(di[0, i]) # correlated shadowing !!!!! Prx = random.multivariate_normal(Mu[0], sigmaij, 1) return Prx
def find_pairwise_distance(coord): # locations of sensors N=coord.shape[0] Dist = -1 * np.ones((N, N)) Mean=0 for tx in range(0,N): for ty in range(0,N): if Dist[ty,tx]<0: Dist[tx,ty]=get_distance(coord[tx,:],coord[ty,:]) else: Dist[tx,ty]=Dist[ty,tx] Mean=Mean+Dist[tx,ty] Mean=Mean/(N**2) return Dist,Mean
def fit_variogram(self): # calculate semivariogram using bin_bound ## VarResolution = 1 range = self.Clust.Range MaxDist = get_distance(np.array([range[0], range[2]]), np.array([range[1], range[3]])) bins=np.arange(0,MaxDist+VarResolution,VarResolution)+VarResolution Pow=self.SensorPowers self.update_pairwise_distance() Variogram, Bin_bound = find_variogram(self.PairwiseDist, Pow, bins) self.Variogram=Variogram pg1, pg2, pg3 = \ opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[ 0] self.p=[pg1,pg2,pg3]
def estimate_map_nearst_method(self, N): # for each point, use nearst N sensors self.update_sensor_powers() Pos = self.get_sensor_locations() Pow = self.SensorPowers rang = self.Clust.PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] flag=0 for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) sen_dis.append(dis) sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] pg1, pg2, pg3 = \ opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) self.Img = img
def map_LS(PixelRange,esSource_loc, esPtx): rang = PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for ix in range(0, img.shape[0]): txx = rang[0] + ix for jy in range(0, img.shape[1]): tyy = rang[2] + jy pxcoord = np.array([txx, tyy]) coord = pixel2coord(pxcoord) dd=get_distance(coord,esSource_loc) if dd==0: esPrx=esPtx else: esPrx = esPtx - myglobals.Pl0 - 10 * myglobals.alpha * np.log10(dd) img[ix,jy]=esPrx return img
def estimate_map_nearst_method1(self, N): # for each point, use nearst N sensors self.update_sensor_powers() Pos = self.get_sensor_locations() Pow = self.SensorPowers rang = self.Clust.PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] flag=0 for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) sen_dis.append(dis) sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] data = np.hstack((pos, pow.T)) gridx = [coord[0]] gridy = [coord[1]] #print(gridx) OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='spherical', verbose=False, enable_plotting=False) z, ss = OK.execute('points', gridx, gridy) img[i,j]=z[0] self.Img = img
def estimate_map_nearst_method(self, N): # for each point, use nearst N sensors self.update_sensor_powers() Pos = self.get_sensor_locations() Pow = self.SensorPowers rang = self.Clust.PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] flag=0 for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) # if dis==0: # flag=1 # img[i,j]=Pow[0,ii] # break #if dis==0: # dis=0.00000000001 sen_dis.append(dis) # if flag==1: # continue # sen_dis = np.array(sen_dis) # sen_dis1 = np.sort(sen_dis) # pos = np.zeros((N, 2)) # pow = np.zeros((1, N)) # # for jj in range(0, N): # lo = np.argwhere(sen_dis == sen_dis1[jj])[0][0] # #print(pos) # pos[jj,:] = Pos[lo,:] # # pow[0,jj] = Pow[0,lo] # tdist, tmean = find_pairwise_distance(pos) # dij = tdist sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = dd # Variogram = semi # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] pg1, pg2, pg3 = \ opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) self.Img = img
def estimate_map(Prx, Dist, Locations, method,vari): Pow = Prx if vari=='pbp': rij = np.zeros((Pow.shape[1], Pow.shape[1])) for i in range(0, Pow.shape[1]): rij[i, :] = 0.5 * (Pow[0, i] - Pow) * (Pow[0, i] - Pow) dij = Dist dd = [] semi = [] for n in range(0, dij.shape[0]): for m in range(0, dij.shape[1]): if n == m: continue else: lo = np.argwhere(dij == dij[n, m]) dd.append(dij[n, m]) se = [] for l in range(0, lo.shape[0]): se.append(rij[lo[l, 0], lo[l, 1]]) semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = np.sort(dd) # Variogram = np.sort(semi) Bin_bound = dd Variogram = semi elif vari=='bin': VarResolution = 1 rang = Range MaxDist = get_distance(np.array([rang[0], rang[2]]), np.array([rang[1], rang[3]])) bins = np.arange(0, MaxDist + VarResolution, VarResolution) + VarResolution Pow = Pow Variogram, Bin_bound = find_variogram(Dist, Pow, bins) # plt.figure(30) # plt.plot(Bin_bound, Variogram, 'b.-') pg1, pg2, pg3 = opt.curve_fit(Gaussian_modul, Bin_bound, Variogram)[0] # pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] rang = PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) if method == 'kriging': # Kriging_method # Pow = Prx Pos = Locations A, B = generate_matrices(p, Dist, Pos, coord) W = solve(A, B) W = W[0:Pow.shape[1], 0].T Pow_est = sum(sum(W * Pow)) img[i, j] = Pow_est elif method == 'shepard': Pos = Locations PowerFactor = 2 flag = 1 n = Pow.shape[1] W = np.zeros((1, n)) for nn in range(0, n): td = get_distance(Pos[nn, :], coord) if td == 0: flag = 0 Pow_est = Pow[0, nn] break W[0, nn] = 1 / (td ** PowerFactor) if flag == 1: Pow_est = sum(sum(W * Pow)) / sum(sum(W)) img[i, j] = Pow_est elif method == 'neighbour': Pos = Locations Pow = Prx grid_z0 = griddata(Pos, Pow.T, coord, method='nearest') # grid_z0 = naturalneighbor.griddata(Pos, Pow.T, coord) img[i, j] = grid_z0 Img = img return Img
def estimate_map(Prx, Dist, Locations, method, vari): Pow = Prx if vari == 'pbp': rij = np.zeros((Pow.shape[1], Pow.shape[1])) for i in range(0, Pow.shape[1]): rij[i, :] = 0.5 * (Pow[0, i] - Pow) * (Pow[0, i] - Pow) dij = Dist # # # calculate mean-value of variogram that under same distance # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound=dd # Variogram=semi # # calculate variogram directly dd = dij.reshape(1, Pow.shape[1] * Pow.shape[1]) semi = rij.reshape(1, Pow.shape[1] * Pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] plt.figure(30) plt.plot(Bin_bound, Variogram, 'b.-') plt.xlabel('distance/m') plt.ylabel('semivariogram') plt.title('equal space method') plt.show() elif vari == 'bin': VarResolution = 1 rang = Range MaxDist = get_distance(np.array([rang[0], rang[2]]), np.array([rang[1], rang[3]])) bins = np.arange(0, MaxDist + VarResolution, VarResolution) + VarResolution Pow = Pow Variogram, Bin_bound = find_variogram(Dist, Pow, bins) plt.figure(30) plt.plot(Bin_bound, Variogram, 'b.-') plt.xlabel('distance/m') plt.ylabel('semivariogram') plt.title('equal space method') plt.show() # sometimes the last value of Variogram is smaller than the first one. # Then it can't fit'RuntimeError: Optimal parameters not found: # The maximum number of function evaluations is exceeded.' # if Variogram[len(Variogram)-1]<Variogram[0]: # print(Variogram) # print(Bin_bound) # Variogram=Variogram[0:len(Variogram)-1] # Bin_bound=Bin_bound[0:len(Bin_bound)-1] pg1, pg2, pg3 = opt.curve_fit( Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[ 0] #bounds=([-np.inf,0,-np.inf,], [np.inf,np.inf,np.inf]) #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] #pg1, pg2, pg3 = opt.curve_fit(Exponent_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] #print(p) rang = PixelRange img = np.zeros( (int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) if method == 'kriging': # Kriging_method # Pow = Prx Pos = Locations A, B = generate_matrices(p, Dist, Pos, coord) W = np.linalg.solve(A, B) W = W[0:Pow.shape[1], 0].T Pow_est = sum(sum(W * Pow)) img[i, j] = Pow_est elif method == 'shepard': Pos = Locations PowerFactor = 2 flag = 1 n = Pow.shape[1] W = np.zeros((1, n)) for nn in range(0, n): td = get_distance(Pos[nn, :], coord) if td == 0: flag = 0 Pow_est = Pow[0, nn] break W[0, nn] = 1 / (td**PowerFactor) if flag == 1: Pow_est = sum(sum(W * Pow)) / sum(sum(W)) img[i, j] = Pow_est elif method == 'neighbour': Pos = Locations Pow = Prx grid_z0 = griddata(Pos, Pow.T, coord, method='nearest') # grid_z0 = naturalneighbor.griddata(Pos, Pow.T, coord) img[i, j] = grid_z0 Img = img return Img
def estimate_map_nearst_method( N, Locations, Prx, PixelRange): # for each point, use nearst N sensors Pos = Locations flag = 0 Pow = Prx rang = PixelRange img = np.zeros( (int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) # if dis==0: # flag=1 # img[i,j]=Pow[0,ii] # break # if dis==0: # dis=0.00000000001 sen_dis.append(dis) # if flag==1: # continue sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # # calculate mean-value of variogram that under same distance # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = dd # Variogram = semi # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] ########### # if Variogram[len(Variogram) - 1] < Variogram[0]: # Variogram = Variogram[0:len(Variogram) - 1] # Bin_bound = Bin_bound[0:len(Bin_bound) - 1] ########### pg1, pg2, pg3 = opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] # pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) return img