IN PYTHON: Build a DTMF decoder. Your code should accept a tonal sequence and ou
ID: 3596883 • Letter: I
Question
IN PYTHON:
Build a DTMF decoder. Your code should accept a tonal sequence and output a string of key presses.
Rules:
1. Use a sampling rate of 8000 samples per second.
2. Use a set of bandpass filters, not a FFT.
3. For full credit, use FIR filters of length 31 or less or second order (two pole) IIR filters. You get partial credit for working implementations that use larger filters.
4. You may assume the silence and tonal periods are both multiples of 400 samples. (This is a simplifying assumption.)
5. You may not assume the tones or the silence periods are the same length.
# GIVEN CODE
import numpy as np
import matplotlib.pyplot as plt
import numpy.random as rnd
from IPython.display import Audio
%matplotlib inline
# Code below encodes the dialed keypresses
Fs = 8000
#define the keypad and its frequencies
validkeys = '*#0123456789ABCD'
rowfreqs = [697, 770, 852, 941]
colfreqs = [1209, 1336, 1477, 1633]
buttons = {'1':(0,0), '2':(0,1), '3':(0,2), 'A':(0,3),
'4':(1,0), '5':(1,1), '6':(1,2), 'B':(1,3),
'7':(2,0), '8':(2,1), '9':(2,2), 'C':(2,3),
'*':(3,0), '0':(3,1), '#':(3,2), 'D':(3,3)}
def dtmf_encoder(phonenumber, dur = 0.5, silencedur=0.1, Fs=8000):
"""return the DTMF tones for a phone number"""
t = np.linspace(0,dur,int(dur*Fs),endpoint=False)
silence = np.zeros(int(silencedur*Fs))
sounds = []
for key in phonenumber:
if key.upper() in validkeys:
r,c = buttons[key]
fr, fc = rowfreqs[r], colfreqs[c]
#print key, fr, fc
sounds.append(np.sin(2*np.pi*fr*t)+np.sin(2*np.pi*fc*t))
sounds.append(silence)
return np.concatenate(sounds[:-1]) #drop last silence period
# TESTING CODE FOR ENCODER
test = '123A456B789C*0#D'
Audio(dtmf_encoder(test), rate=Fs)
# INSERT YOUR CODE FOR DECODING BELOW
def DTMF_decode(tones):
"PUT CODE HERE"
pass
# TESTING CODE FOR DECODER (SHOULD OUTPUT TRUE)
def test_dtmfdecoder(dtmfdecoder, dur=0.5, silencedur=0.1, Fs=8000):
works = False
sigma = 0.0
while sigma < 3.1:
number = ''.join([c for c in rnd.permutation(list(validkeys))])
#print number
tones = dtmf_encoder(number, dur=dur, silencedur=silencedur)
for i in range(5):
noisy_tones = tones + sigma*rnd.randn(len(tones))
decoded = dtmfdecoder(noisy_tones)
if decoded == number:
works = True
#print sigma, i
else:
return works, sigma
sigma += 0.1
return works, sigma
test_dtmfdecoder(DTMF_decode) #my solution
Explanation / Answer
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