forked from johnkimdinh/dtmf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtmf-decoder.py
123 lines (123 loc) · 4.21 KB
/
dtmf-decoder.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'''
A python implementation of the Goertzel algorithm to decode DTMF tones.
The wave file is split into bins and each bin is analyzed
for all the DTMF frequencies. The method run() will return a numeric
representation of the DTMF tone.
'''
import wave
import struct
import math
class pygoertzel_dtmf:
def __init__(self, samplerate):
self.samplerate = samplerate
self.goertzel_freq = [1209.0,1336.0,1477.0,1633.0,697.0,770.0,852.0,941.0]
self.s_prev = {}
self.s_prev2 = {}
self.totalpower = {}
self.N = {}
self.coeff = {}
# create goertzel parameters for each frequency so that
# all the frequencies are analyzed in parallel
for k in self.goertzel_freq:
self.s_prev[k] = 0.0
self.s_prev2[k] = 0.0
self.totalpower[k] = 0.0
self.N[k] = 0.0
normalizedfreq = k / self.samplerate
self.coeff[k] = 2.0*math.cos(2.0 * math.pi * normalizedfreq)
def __get_number(self, freqs):
hi = [1209.0,1336.0,1477.0,1633.0]
lo = [697.0,770.0,852.0,941.0]
# get hi freq
hifreq = 0.0
hifreq_v = 0.0
for f in hi:
if freqs[f]>hifreq_v:
hifreq_v = freqs[f]
hifreq = f
# get lo freq
lofreq = 0.0
lofreq_v = 0.0
for f in lo:
if freqs[f]>lofreq_v:
lofreq_v = freqs[f]
lofreq = f
if lofreq==697.0:
if hifreq==1209.0:
return "1"
elif hifreq==1336.0:
return "2"
elif hifreq==1477.0:
return "3"
elif hifreq==1633.0:
return "A"
elif lofreq==770.0:
if hifreq==1209.0:
return "4"
elif hifreq==1336.0:
return "5"
elif hifreq==1477.0:
return "6"
elif hifreq==1633.0:
return "B"
elif lofreq==852.0:
if hifreq==1209.0:
return "7"
elif hifreq==1336.0:
return "8"
elif hifreq==1477.0:
return "9"
elif hifreq==1633.0:
return "C"
elif lofreq==941.0:
if hifreq==1209.0:
return "*"
elif hifreq==1336.0:
return "0"
elif hifreq==1477.0:
return "#"
elif hifreq==1633.0:
return "D"
def run(self, sample):
freqs = {}
for freq in self.goertzel_freq:
s = sample + (self.coeff[freq] * self.s_prev[freq]) - self.s_prev2[freq]
self.s_prev2[freq] = self.s_prev[freq]
self.s_prev[freq] = s
self.N[freq]+=1
power = (self.s_prev2[freq]*self.s_prev2[freq]) + (self.s_prev[freq]*self.s_prev[freq]) - (self.coeff[freq]*self.s_prev[freq]*self.s_prev2[freq])
self.totalpower[freq]+=sample*sample
if (self.totalpower[freq] == 0):
self.totalpower[freq] = 1
freqs[freq] = power / self.totalpower[freq] / self.N[freq]
return self.__get_number(freqs)
if __name__ == '__main__':
# load wav file
wav = wave.open('/home/michael/Downloads/dtmf.wav', 'r')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = wav.getparams()
frames = wav.readframes(nframes * nchannels)
# convert wave file to array of integers
frames = struct.unpack_from("%dH" % nframes * nchannels, frames)
# if stereo get left/right
if nchannels == 2:
left = [frames[i] for i in range(0,len(frames),2)]
right = [frames[i] for i in range(1,len(frames),2)]
else:
left = frames
right = left
binsize = 400
# Split the bin in 4 to average out errors due to noise
binsize_split = 4
prevvalue = ""
prevcounter = 0
for i in range(0,len(left)-binsize,binsize/binsize_split):
goertzel = pygoertzel_dtmf(framerate)
for j in left[i:i+binsize]:
value = goertzel.run(j)
if value==prevvalue:
prevcounter+=1
if prevcounter==10:
print value
else:
prevcounter=0
prevvalue=value