-
Notifications
You must be signed in to change notification settings - Fork 4
/
correlogram2.py
70 lines (56 loc) · 1.52 KB
/
correlogram2.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
#!/usr/bin/env python
import math
import numpy
import cmath
import random
# Compute Correlogram with the Wiener-Khninchin theorem
# Fr(f) = FFT[X(t)]
# S(f) = Fr(f)Fr*(f) // times its complex conugate
# R(tau) = IFFT[S(f)]
def wiener_khninchin(bits):
n = len(bits)
if (n % 2) == 1: # Make it an even number
raise ValueError("The number of data samples must be even")
ts = list() # Convert to +1,-1
for bit in bits:
if bit == 0:
ts.append(-1.0)
else:
ts.append(1.0)
ts_np = numpy.array(ts)
fs = numpy.fft.fft(ts_np) # Compute DFT
# Muliply each element by its complex conjugate
fs_out = list()
for x in xrange(len(fs)):
theconjugate = fs[x].conjugate()
newvalue = fs[x]*theconjugate
fs_out.append(newvalue)
np_fs_out = numpy.array(fs_out)
# Take the inverse FFT
crg = numpy.fft.ifft(np_fs_out)
# Turn it into a list of reals
corellogram = list()
for x in crg:
corellogram.append(x.real/n)
return corellogram
# Make some serially correlated bits
r = random.SystemRandom()
bits = list()
previous = 0
for i in xrange(256):
if previous == 0:
level = 0.2
else:
level = 0.8
ref = r.random()
if (ref > level):
newbit = 1
else:
newbit = 0
previous = newbit
bits.append(newbit)
c = wiener_khninchin(bits) # Compute the correllogram
print "bits:",bits
print
for i in xrange(32):
print "%02d %06f" % (i,c[i])