123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
-
- class Sonar:
-
- def __init__(self):
- pass
-
-
- def BuildWelch(self, p, root):
- costas = [0] * (p-1)
- for i in range(p-1):
- costas[i] = pow(root, i+1) % p
- return costas
-
- def isCostas(self, array):
- a_size = len(array)
- for c in range(1, a_size):
- hash = [0] * (2 * a_size)
- for i in range(a_size - c):
- dif = array[i+c] - array[i]
- #print dif,
- if hash[dif] == 1:
- return 0
- else:
- hash[dif] = 1
- #print
- return 1
-
- def CostasExtendedCrossCorrelation(self, costa1, costa2, n):
- cross = 0
- for h in range(n):
- hash = [0] * (n - 1)
- for i in range(n):
- if costa1[(i+h) % n] == "*" or costa2[i] == "*":
- continue
- dif = (costa1[(i + h) % n] - costa2[i]) % (n- 1)
- hash[dif]+=1
- if cross < hash[dif]:
- cross = hash[dif]
- return cross
-
-
- def isExtendedCostas(self, costas, n):
- for h in range(n):
- if h == 0:
- continue
- hash = [0] * (n-1)
- for i in range(n):
- if costas[(i + h) % n] == "*" or costas[i] == "*":
- continue
- dif = (costas[(i+h) % n] - costas[i]) % (n-1)
- if hash[dif] == 1:
- return 0
- else:
- hash[dif] = 1
- return 1
-
- def CostasColumnSymetries(self, a, p):
- costaList = []
- for i in range(2, p):
- costas = [0] * p
- for x in range(p):
- costas[(x*i) % p] = a[x]
- costaList.append(costas)
- return costaList
-
-
-
- def is3DCostas(self, costas, n):
- for h1 in range(n):
- for h2 in range(n):
- if h1 == 0 and h2 == 0:
- continue
- hash = [0] * ((n*n) - 1)
- for i in range(n):
- for j in range(n):
- #if i == 0 and j == 0:
- # continue
- #if (i+h1) % n == 0 and (j+h2) % n == 0:
- # Fix for * anyware on the grid.
- if costas[(i+h1) % n][(j+h2) % n] == "*" or costas[i][j] == "*":
- continue
-
- dif = (costas[((i + h1) % n)][((j+h2) % n)]
- - costas[i][j]) % (pow(n,2) - 1)
- #print dif,
- if hash[dif] == 1:
- return 0
- else:
- hash[dif] =1
- #print
- return 1
-
- def Costas3DCrossCorrelation(self, costa1, costa2, n):
- cross = 0
- for h1 in range(n):
- for h2 in range(n):
- #if h1 == 0 and h2 == 0:
- # continue
- hash = [0] * ((n*n) - 1)
- for i in range(n):
- for j in range(n):
- #if i == 0 and j == 0:
- # continue
- #if (i+h1) % n == 0 and (j+h2) % n == 0:
- # Fix for * anyware on the grid.
- if costa1[(i+h1) % n][(j+h2) % n] == "*" or costa2[i][j] == "*":
- continue
-
- dif = (costa1[((i + h1) % n)][((j+h2) % n)]
- - costa2[i][j]) % (pow(n,2) - 1)
- #print dif,
- hash[dif]+=1
- if cross < hash[dif]:
- cross = hash[dif]
- #print
- #print hash
- return cross
-
-
-
- def is3DSonar1D(self, costas, p, n):
- for h1 in range(p):
- for h2 in range(p):
- if h1 == 0 and h2 == 0:
- continue
- hash = [0] * ((p*p))
- for i in range(p):
- if p*i > n-1:
- break
- for j in range(p):
- if i == 0 and j == 0:
- continue
- if (i+h1) % p == 0 and (j+h2) % p == 0:
- continue
-
- if p*i+j > n-1:
- break
-
- if p *((i + h1) % p) + ((j+h2) % p) > n - 1:
- continue
-
- dif = (costas[ p *((i + h1) % p) + ((j+h2) % p)] - costas[p*i+j]) % (pow(p,2))
- if hash[dif] == 1:
- return 0
- else:
- hash[dif] =1
- #print
- return 1
-
- def is3DSonar1DFull(self, costas, p):
- for h1 in range(p):
- for h2 in range(p):
- if h1 == 0 and h2 == 0:
- continue
- hash = [0] * (p*p)
- for i in range(p):
- for j in range(p):
- if i == 0 and j == 0:
- continue
- if (i+h1) % p == 0 and (j+h2) % p == 0:
- continue
- dif = (costas[ p *((i + h1) % p) + ((j+h2) % p)] - costas[p*i+j]) % (pow(p,2))
- #print dif,
- if hash[dif] == 1:
- return 0
- else:
- hash[dif] =1
- return 1
-
- def Costas3DRowSymetries(self, a, p):
- # Given a Welch 3dCostas return all its column symetries
- costaList = []
- for i in range(2,p):
- costas2d = []
- for j in range(p):
- costas2d.append([0] * p)
- for x in range(p):
- for y in range(p):
- if x == 0 and y == 0:
- costas2d[x][y] = "*"
- else:
- costas2d[(x*i) % p][y] = a[x][y]
-
- costaList.append(costas2d)
- return costaList
-
- def Costas3DRowShiftPermutation(self, a, p):
- # Given a Welch 3dCostas return all its row power permutations
- costaList = []
- for i in range(1,p):
- costas2d = []
- costas2d.append(a[0][:])
- for j in range(1, p):
- costas2d.append(a[j][-(((i*j)%p)) :] + a[j][:-((i*j)%p)])
- costaList.append(costas2d)
- return costaList
-
-
- def Costas3DColumnSymetries(self, a, p):
- costaList = []
- for i in range(2, p):
- costas2d = []
- for j in range(p):
- costas2d.append([0] * p)
- for y in range(p):
- for x in range(p):
- if x == 0 and y == 0:
- costas2d[x][y] = "*"
- else:
- costas2d[x][(y*i) % p] = a[x][y]
- costaList.append(costas2d)
- return costaList
-
- def Costas3DColumnShiftPermutation(self, a, p):
- costaList = []
- for i in range(1, p):
- costas2d = []
- for j in range(p):
- costas2d.append([0] * p)
- for y in range(p):
- for x in range(p):
- if x == 0 and y == 0:
- costas2d[x][y] = "*"
- else:
- costas2d[(x+(y*i)) % p][y] = a[x][y]
- costaList.append(costas2d)
- return costaList
-
- def CostasAdditionSymetries(self, a, p):
- #Given a Welch 3dCostas return all its Addition symetries excluding a
- costaList = []
- for i in range(1, p):
- costas2d = []
- for j in range(p):
- costas2d.append([0] * p)
- for x in range(p):
- for y in range(p):
- if x == 0 and y == 0:
- costas2d[x][y] = "*"
- else:
- costas2d[x][y] = (a[x][y] + i) % (pow(p,2)-1)
- costaList.append(costas2d)
-
- return costaList
-
- def CostasMultiplicationSymetries(self, a, p1, primes):
- costaList = []
- for p in primes:
- costas2d = []
- for j in range(p1):
- costas2d.append([0] * p1)
-
- for x in range(p1):
- for y in range(p1):
- if x == 0 and y == 0:
- costas2d[0][0] = "*"
- else:
- costas2d[x][y] = (a[x][y] * p) % (pow(p1,2)-1)
- costaList.append(costas2d)
- return costaList
-
- def Costas3D90DegreeSymetry(self, a, p):
- # Given a Welch 3DCostas return its 90Degree Symetry
- costas = [] * p
- for i in range(p):
- costas.append([0] * p)
- for i in range(p):
- for j in range(p):
- costas[i][j] = a[j][i]
-
- return costas
-
- def Costas3DDihedralSymetry(self, a, p):
- costas = [] * p
- for i in range(p):
- costas.append([0] * p)
- for i in range(p):
- costas[i] = a[i][:]
- costas[i].reverse()
- return costas
-
- def Costas2Legendre(self, a, p):
- for i in range(p):
- for j in range(p):
- if a[i][j] == "*":
- continue
- if(a[i][j] % 2 == 0):
- a[i][j]=1
- else:
- a[i][j]=-1
-
- def GetLegendre(self, a, p):
- # Given a Welch 3DCostas return its Legendre
- costas = [] * p
- for i in range(p):
- costas.append([0] * p)
-
- for i in range(p):
- for j in range(p):
- if a[i][j] == "*":
- costas[i][j] = "*"
- continue
-
- if(a[i][j] % 2 == 0):
- costas[i][j]=1
- else:
- costas[i][j]=-1
- return costas
-
- def IsCalabro(self, a, p):
- # Check if Legendre array is Calabro
- similar = (p-1)/2
- counter = [0] * p
- for i in range(1, p-1):
- for j in range(i+1, p):
- if a[i] == a[j]:
- if counter[i] == 0:
- counter[i]+=2
- else:
- counter[i]+=1
- if counter[j] == 0:
- counter[j]+=2
- else:
- counter[j]+=1
- if similar in counter:
- return 1, counter
- else:
- return 0, counter
-
- def LegendreCorrelation(self, legendre, p):
- cor = 0
- cor_pos = 0
- cor_neg = 0
- for h1 in range(p):
- for h2 in range(p):
- if h1 == 0 and h2 == 0:
- continue
- tmp_cor = 0
- for i in range(p):
- for j in range(p):
- tmp_cor += legendre[(i + h1) % p][(j + h2) % p] * legendre[i][j]
-
- if tmp_cor < 0:
- if tmp_cor < cor_neg:
- cor_neg = tmp_cor
- else:
- if tmp_cor > cor_pos:
- cor_pos = tmp_cor
- if abs(tmp_cor) > cor:
- cor = abs(tmp_cor)
- return cor, cor_neg, cor_pos
-
- def LegendreCrossCorrelation(self, legendre1, legendre2, p):
- cor = 0
- cor_pos = 0
- cor_neg = 0
- for h1 in range(p):
- for h2 in range(p):
- #if h1 == 0 and h2 == 0:
- # continue
- tmp_cor = 0
- for i in range(p):
- for j in range(p):
- if legendre1[(i + h1) % p][(j + h2) % p] == "*" or legendre2[i][j] == "*":
- continue
- tmp_cor += legendre1[(i + h1) % p][(j + h2) % p] * legendre2[i][j]
-
- if tmp_cor < 0:
- if tmp_cor < cor_neg:
- cor_neg = tmp_cor
- else:
- if tmp_cor > cor_pos:
- cor_pos = tmp_cor
- if abs(tmp_cor) > cor:
- cor = abs(tmp_cor)
- return cor, cor_neg, cor_pos
-
-
- def LegendreCrossCorrelationCoincidences(self, legendre1, legendre2, p):
- cor = 0
- for h1 in range(p):
- for h2 in range(p):
- #if h1 == 0 and h2 == 0:
- # continue
- tmp_cor = 0
- for i in range(p):
- for j in range(p):
- if legendre1[(i + h1) % p][(j + h2) % p] == legendre2[i][j]:
- tmp_cor+=1
- if tmp_cor > cor:
- cor = tmp_cor
- return cor
-
- #c = Costas()
- #print c.isCostas([4,3,2,1])
|