-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlsb-autocorr-b.py
executable file
·81 lines (55 loc) · 2.13 KB
/
lsb-autocorr-b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
# -*- coding: latin1 -*-
"""
SYNOPSIS
Calculate the autocorrelation of a bitstream (presumably extracted from an image)
DESCRIPTION
EXAMPLES
EXIT STATUS
AUTHOR
luca.mella@studio.unibo.it
LICENSE
Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
VERSION
0.3
"""
import sys
from bitstring import bits
from bitstring import Distance
from collections import deque
from optparse import OptionParser
def calcAutocorrs(infile,dst,num,shft):
""" Calculate autocorrelations """
distance=Distance()
d=deque()
autocorr=list()
char = infile.read(1)
while char != '':
[ d.append(x) for x in bits(ord(char)) ]
char = infile.read(1)
original = ''.join( x for x in d )
for i in range(1,num+1):
d.rotate(shft)
shifted = ''.join(x for x in d)
corr = getattr(distance, dst )( original , shifted )
autocorr.append((i*shft , corr))
return autocorr
#-------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------
if __name__ == "__main__":
dists = [ y for y in dir(Distance)if callable(getattr(Distance,y))]
parser = OptionParser("usage: %prog [OPTIONS] ARGS \nBitstring will be picked from STDIN")
parser.add_option("-n", "--num",dest="num", action="store", type="int",
default=8,help="number of autocorrelation to compute", metavar="NITERATIONS")
parser.add_option("-s", "--shift",dest="shift", action="store", type="int",
default=1,help="bit shift for each iteration", metavar="BITSHIFT")
parser.add_option("-d", "--distance",dest="distance", action="store", type="string",
default=dists[0],help="distance to use for calculation. Options: "+str(dists), metavar="DISTANCE")
(options, args) = parser.parse_args()
dst = options.distance
num = abs(options.num)
shft = abs(options.shift)
autocorr=calcAutocorrs(sys.stdin,dst,num,shft)
for (shift,corr) in autocorr:
print " Shift %d Corr %f" % ( shift , corr )