-
Notifications
You must be signed in to change notification settings - Fork 0
/
demodfm.py
65 lines (54 loc) · 1.18 KB
/
demodfm.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
import sys
import math
ll = []
# reading ASCII file containing IQ data
filename=sys.argv[1]
print("reading data from file "+filename)
fd = open(filename, 'r')
ll = fd.readlines()
fd.close()
llen = len(ll)
llen2 = int(llen/2)
print("len "+str(llen))
twopi = 2. * math.pi
def regul(dtheta):
# function correction phase corresponding to
# undetermination of atan function
if dtheta>math.pi:
dtheta = dtheta - twopi
elif dtheta<-math.pi:
dtheta = dtheta + twopi
return(dtheta)
freq=96000
dt=1./freq
# real demodulation
lv = []
coeff = 0.05
theta0=0.
mx=0
mn=0
for c in range(llen2):
i = ll[2*c]
q = ll[2*c+1]
# get atan(i/q)
theta1 = math.atan2(float(q), float(i))
# calculates derivative
dtheta = theta1 - theta0
dtheta = regul(dtheta)
v = coeff * dtheta / dt
# also calculates min and max
if v<mn: mn=v
if v>mx: mx=v
lv.append(v)
theta0 = theta1
print("mx "+str(mx))
print("mn "+str(mn))
# ascii dump of sound samples
fd = open("demodfm.dat", 'w')
for v in lv: fd.write(str(int(v))+"\n")
fd.close()
# scipy wav dump
#import scipy.io.wavfile
#import numpy
#lv = map(lambda x: 0.5*x/mx, lv)
#scipy.io.wavfile.write("sample.wav", freq, numpy.array(lv))