Búsqueda de Sonares 3D

Sonar.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. class Sonar:
  2. def __init__(self):
  3. pass
  4. def BuildWelch(self, p, root):
  5. costas = [0] * (p-1)
  6. for i in range(p-1):
  7. costas[i] = pow(root, i+1) % p
  8. return costas
  9. def isCostas(self, array):
  10. a_size = len(array)
  11. for c in range(1, a_size):
  12. hash = [0] * (2 * a_size)
  13. for i in range(a_size - c):
  14. dif = array[i+c] - array[i]
  15. #print dif,
  16. if hash[dif] == 1:
  17. return 0
  18. else:
  19. hash[dif] = 1
  20. #print
  21. return 1
  22. def CostasExtendedCrossCorrelation(self, costa1, costa2, n):
  23. cross = 0
  24. for h in range(n):
  25. hash = [0] * (n - 1)
  26. for i in range(n):
  27. if costa1[(i+h) % n] == "*" or costa2[i] == "*":
  28. continue
  29. dif = (costa1[(i + h) % n] - costa2[i]) % (n- 1)
  30. hash[dif]+=1
  31. if cross < hash[dif]:
  32. cross = hash[dif]
  33. return cross
  34. def isExtendedCostas(self, costas, n):
  35. for h in range(n):
  36. if h == 0:
  37. continue
  38. hash = [0] * (n-1)
  39. for i in range(n):
  40. if costas[(i + h) % n] == "*" or costas[i] == "*":
  41. continue
  42. dif = (costas[(i+h) % n] - costas[i]) % (n-1)
  43. if hash[dif] == 1:
  44. return 0
  45. else:
  46. hash[dif] = 1
  47. return 1
  48. def CostasColumnSymetries(self, a, p):
  49. costaList = []
  50. for i in range(2, p):
  51. costas = [0] * p
  52. for x in range(p):
  53. costas[(x*i) % p] = a[x]
  54. costaList.append(costas)
  55. return costaList
  56. def is3DCostas(self, costas, n):
  57. for h1 in range(n):
  58. for h2 in range(n):
  59. if h1 == 0 and h2 == 0:
  60. continue
  61. hash = [0] * ((n*n) - 1)
  62. for i in range(n):
  63. for j in range(n):
  64. #if i == 0 and j == 0:
  65. # continue
  66. #if (i+h1) % n == 0 and (j+h2) % n == 0:
  67. # Fix for * anyware on the grid.
  68. if costas[(i+h1) % n][(j+h2) % n] == "*" or costas[i][j] == "*":
  69. continue
  70. dif = (costas[((i + h1) % n)][((j+h2) % n)]
  71. - costas[i][j]) % (pow(n,2) - 1)
  72. #print dif,
  73. if hash[dif] == 1:
  74. return 0
  75. else:
  76. hash[dif] =1
  77. #print
  78. return 1
  79. def Costas3DCrossCorrelation(self, costa1, costa2, n):
  80. cross = 0
  81. for h1 in range(n):
  82. for h2 in range(n):
  83. #if h1 == 0 and h2 == 0:
  84. # continue
  85. hash = [0] * ((n*n) - 1)
  86. for i in range(n):
  87. for j in range(n):
  88. #if i == 0 and j == 0:
  89. # continue
  90. #if (i+h1) % n == 0 and (j+h2) % n == 0:
  91. # Fix for * anyware on the grid.
  92. if costa1[(i+h1) % n][(j+h2) % n] == "*" or costa2[i][j] == "*":
  93. continue
  94. dif = (costa1[((i + h1) % n)][((j+h2) % n)]
  95. - costa2[i][j]) % (pow(n,2) - 1)
  96. #print dif,
  97. hash[dif]+=1
  98. if cross < hash[dif]:
  99. cross = hash[dif]
  100. #print
  101. #print hash
  102. return cross
  103. def is3DSonar1D(self, costas, p, n):
  104. for h1 in range(p):
  105. for h2 in range(p):
  106. if h1 == 0 and h2 == 0:
  107. continue
  108. hash = [0] * ((p*p))
  109. for i in range(p):
  110. if p*i > n-1:
  111. break
  112. for j in range(p):
  113. if i == 0 and j == 0:
  114. continue
  115. if (i+h1) % p == 0 and (j+h2) % p == 0:
  116. continue
  117. if p*i+j > n-1:
  118. break
  119. if p *((i + h1) % p) + ((j+h2) % p) > n - 1:
  120. continue
  121. dif = (costas[ p *((i + h1) % p) + ((j+h2) % p)] - costas[p*i+j]) % (pow(p,2))
  122. if hash[dif] == 1:
  123. return 0
  124. else:
  125. hash[dif] =1
  126. #print
  127. return 1
  128. def is3DSonar1DFull(self, costas, p):
  129. for h1 in range(p):
  130. for h2 in range(p):
  131. if h1 == 0 and h2 == 0:
  132. continue
  133. hash = [0] * (p*p)
  134. for i in range(p):
  135. for j in range(p):
  136. if i == 0 and j == 0:
  137. continue
  138. if (i+h1) % p == 0 and (j+h2) % p == 0:
  139. continue
  140. dif = (costas[ p *((i + h1) % p) + ((j+h2) % p)] - costas[p*i+j]) % (pow(p,2))
  141. #print dif,
  142. if hash[dif] == 1:
  143. return 0
  144. else:
  145. hash[dif] =1
  146. return 1
  147. def Costas3DRowSymetries(self, a, p):
  148. # Given a Welch 3dCostas return all its column symetries
  149. costaList = []
  150. for i in range(2,p):
  151. costas2d = []
  152. for j in range(p):
  153. costas2d.append([0] * p)
  154. for x in range(p):
  155. for y in range(p):
  156. if x == 0 and y == 0:
  157. costas2d[x][y] = "*"
  158. else:
  159. costas2d[(x*i) % p][y] = a[x][y]
  160. costaList.append(costas2d)
  161. return costaList
  162. def Costas3DRowShiftPermutation(self, a, p):
  163. # Given a Welch 3dCostas return all its row power permutations
  164. costaList = []
  165. for i in range(1,p):
  166. costas2d = []
  167. costas2d.append(a[0][:])
  168. for j in range(1, p):
  169. costas2d.append(a[j][-(((i*j)%p)) :] + a[j][:-((i*j)%p)])
  170. costaList.append(costas2d)
  171. return costaList
  172. def Costas3DColumnSymetries(self, a, p):
  173. costaList = []
  174. for i in range(2, p):
  175. costas2d = []
  176. for j in range(p):
  177. costas2d.append([0] * p)
  178. for y in range(p):
  179. for x in range(p):
  180. if x == 0 and y == 0:
  181. costas2d[x][y] = "*"
  182. else:
  183. costas2d[x][(y*i) % p] = a[x][y]
  184. costaList.append(costas2d)
  185. return costaList
  186. def Costas3DColumnShiftPermutation(self, a, p):
  187. costaList = []
  188. for i in range(1, p):
  189. costas2d = []
  190. for j in range(p):
  191. costas2d.append([0] * p)
  192. for y in range(p):
  193. for x in range(p):
  194. if x == 0 and y == 0:
  195. costas2d[x][y] = "*"
  196. else:
  197. costas2d[(x+(y*i)) % p][y] = a[x][y]
  198. costaList.append(costas2d)
  199. return costaList
  200. def CostasAdditionSymetries(self, a, p):
  201. #Given a Welch 3dCostas return all its Addition symetries excluding a
  202. costaList = []
  203. for i in range(1, p):
  204. costas2d = []
  205. for j in range(p):
  206. costas2d.append([0] * p)
  207. for x in range(p):
  208. for y in range(p):
  209. if x == 0 and y == 0:
  210. costas2d[x][y] = "*"
  211. else:
  212. costas2d[x][y] = (a[x][y] + i) % (pow(p,2)-1)
  213. costaList.append(costas2d)
  214. return costaList
  215. def CostasMultiplicationSymetries(self, a, p1, primes):
  216. costaList = []
  217. for p in primes:
  218. costas2d = []
  219. for j in range(p1):
  220. costas2d.append([0] * p1)
  221. for x in range(p1):
  222. for y in range(p1):
  223. if x == 0 and y == 0:
  224. costas2d[0][0] = "*"
  225. else:
  226. costas2d[x][y] = (a[x][y] * p) % (pow(p1,2)-1)
  227. costaList.append(costas2d)
  228. return costaList
  229. def Costas3D90DegreeSymetry(self, a, p):
  230. # Given a Welch 3DCostas return its 90Degree Symetry
  231. costas = [] * p
  232. for i in range(p):
  233. costas.append([0] * p)
  234. for i in range(p):
  235. for j in range(p):
  236. costas[i][j] = a[j][i]
  237. return costas
  238. def Costas3DDihedralSymetry(self, a, p):
  239. costas = [] * p
  240. for i in range(p):
  241. costas.append([0] * p)
  242. for i in range(p):
  243. costas[i] = a[i][:]
  244. costas[i].reverse()
  245. return costas
  246. def Costas2Legendre(self, a, p):
  247. for i in range(p):
  248. for j in range(p):
  249. if a[i][j] == "*":
  250. continue
  251. if(a[i][j] % 2 == 0):
  252. a[i][j]=1
  253. else:
  254. a[i][j]=-1
  255. def GetLegendre(self, a, p):
  256. # Given a Welch 3DCostas return its Legendre
  257. costas = [] * p
  258. for i in range(p):
  259. costas.append([0] * p)
  260. for i in range(p):
  261. for j in range(p):
  262. if a[i][j] == "*":
  263. costas[i][j] = "*"
  264. continue
  265. if(a[i][j] % 2 == 0):
  266. costas[i][j]=1
  267. else:
  268. costas[i][j]=-1
  269. return costas
  270. def IsCalabro(self, a, p):
  271. # Check if Legendre array is Calabro
  272. similar = (p-1)/2
  273. counter = [0] * p
  274. for i in range(1, p-1):
  275. for j in range(i+1, p):
  276. if a[i] == a[j]:
  277. if counter[i] == 0:
  278. counter[i]+=2
  279. else:
  280. counter[i]+=1
  281. if counter[j] == 0:
  282. counter[j]+=2
  283. else:
  284. counter[j]+=1
  285. if similar in counter:
  286. return 1, counter
  287. else:
  288. return 0, counter
  289. def LegendreCorrelation(self, legendre, p):
  290. cor = 0
  291. cor_pos = 0
  292. cor_neg = 0
  293. for h1 in range(p):
  294. for h2 in range(p):
  295. if h1 == 0 and h2 == 0:
  296. continue
  297. tmp_cor = 0
  298. for i in range(p):
  299. for j in range(p):
  300. tmp_cor += legendre[(i + h1) % p][(j + h2) % p] * legendre[i][j]
  301. if tmp_cor < 0:
  302. if tmp_cor < cor_neg:
  303. cor_neg = tmp_cor
  304. else:
  305. if tmp_cor > cor_pos:
  306. cor_pos = tmp_cor
  307. if abs(tmp_cor) > cor:
  308. cor = abs(tmp_cor)
  309. return cor, cor_neg, cor_pos
  310. def LegendreCrossCorrelation(self, legendre1, legendre2, p):
  311. cor = 0
  312. cor_pos = 0
  313. cor_neg = 0
  314. for h1 in range(p):
  315. for h2 in range(p):
  316. #if h1 == 0 and h2 == 0:
  317. # continue
  318. tmp_cor = 0
  319. for i in range(p):
  320. for j in range(p):
  321. if legendre1[(i + h1) % p][(j + h2) % p] == "*" or legendre2[i][j] == "*":
  322. continue
  323. tmp_cor += legendre1[(i + h1) % p][(j + h2) % p] * legendre2[i][j]
  324. if tmp_cor < 0:
  325. if tmp_cor < cor_neg:
  326. cor_neg = tmp_cor
  327. else:
  328. if tmp_cor > cor_pos:
  329. cor_pos = tmp_cor
  330. if abs(tmp_cor) > cor:
  331. cor = abs(tmp_cor)
  332. return cor, cor_neg, cor_pos
  333. def LegendreCrossCorrelationCoincidences(self, legendre1, legendre2, p):
  334. cor = 0
  335. for h1 in range(p):
  336. for h2 in range(p):
  337. #if h1 == 0 and h2 == 0:
  338. # continue
  339. tmp_cor = 0
  340. for i in range(p):
  341. for j in range(p):
  342. if legendre1[(i + h1) % p][(j + h2) % p] == legendre2[i][j]:
  343. tmp_cor+=1
  344. if tmp_cor > cor:
  345. cor = tmp_cor
  346. return cor
  347. #c = Costas()
  348. #print c.isCostas([4,3,2,1])