-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdtmf-generator.py
244 lines (215 loc) · 7.31 KB
/
dtmf-generator.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import sys
import argparse
import numpy as np
import scipy.io.wavfile as wav
import matplotlib.pyplot as plt
class DtmfGenerator:
DTMF_TABLE = {
"1": np.array([1209, 697]),
"2": np.array([1336, 697]),
"3": np.array([1477, 697]),
"A": np.array([1633, 697]),
"4": np.array([1209, 770]),
"5": np.array([1336, 770]),
"6": np.array([1477, 770]),
"B": np.array([1633, 770]),
"7": np.array([1209, 852]),
"8": np.array([1336, 852]),
"9": np.array([1477, 852]),
"C": np.array([1633, 852]),
"*": np.array([1209, 941]),
"0": np.array([1336, 941]),
"#": np.array([1477, 941]),
"D": np.array([1633, 941]),
}
def __init__(
self,
phone_number: str,
Fs: float,
time: float,
delay: float,
amp: float,
):
self.signal = self.compose(phone_number, Fs, time, delay, amp)
def __dtmf_function(
self, number: str, Fs: float, time: float, delay: float, amp: float
) -> np.array:
"""
Function which generate DTMF tone (samples) to one specific character
and its delay
:number: Represents the character to be converted to DTMF tone
:Fs: Sample frequency used to generate the signal in Hz
:time: Duration of each tone in seconds
:delay: Duration of delay between each tone in seconds
:return: Array with samples to the DTMF tone and delay
"""
time_tone = np.arange(0, time, (1 / Fs))
time_delay = np.arange(0, delay, (1 / Fs))
tone_samples = amp * (
np.sin(2 * np.pi * self.DTMF_TABLE[number][0] * time_tone)
+ np.sin(2 * np.pi * self.DTMF_TABLE[number][1] * time_tone)
)
delay_samples = np.sin(2 * np.pi * 0 * time_delay)
return np.append(tone_samples, delay_samples)
def compose(
self,
phone_number: str,
Fs: float,
time: float,
delay: float,
amp: float,
) -> np.array:
"""
Function which generate DTMF tones (samples) to compose a signal
representing the phone number
:number: Represents the number to be converted to DTMF tone
:Fs: Sample frequency used to generate the signal in Hz
:time: Duration of each tone in seconds
:delay: Duration of delay between each tone in seconds
:return: Array with samples to the DTMF tone and delay
"""
signal = np.array([])
for number in phone_number:
tone_delay_signal = self.__dtmf_function(number, Fs, time, delay, amp)
signal = np.append(signal, tone_delay_signal)
return signal
def test_signal(
self,
filename: str,
phone_number: str,
Fs: float,
time: float,
delay: float,
):
"""
Function which debug DTMF tones generated in the WAV file plotting their frequencies
:filename: WAV filename to debug
:phone_number: Phone number to verify
:Fs: Sample frequency used to generate the signal in Hz
:time: Duration of each tone in seconds
:delay: Duration of delay between each tone in seconds
:return: A graph with tones and their frequencies
"""
rate, signal = wav.read(filename)
n_columns_axis = np.ceil(np.sqrt(len(phone_number))).astype(int)
n_rows_axis = np.ceil(len(phone_number) / n_columns_axis).astype(int)
fig, axis = plt.subplots(n_rows_axis, n_columns_axis)
fig.suptitle("DTMF tones frequencies")
for i in np.arange(0, 2 * len(phone_number), 2):
self.__test_tone(
signal[i * int(Fs * time) : (i + 1) * int(Fs * time)],
Fs,
time,
delay,
axis,
n_rows_axis,
n_columns_axis,
(i / 2) + 1,
)
plt.tight_layout()
plt.show()
def __test_tone(
self,
signal: np.array,
Fs: float,
time: float,
delay: float,
axis,
n_rows_axis: int,
n_columns_axis: int,
index: int,
):
"""
Function which debug one DTMF tone generated in the WAV file plotting its frequency
:signal: Array with the tone to be plotted
:Fs: Sample frequency used to generate the signal in Hz
:time: Duration of each tone in seconds
:delay: Duration of delay between each tone in seconds
:axix: Axis of matplotlib to plot the tone
:n_rows_axis: Number of rows into subplot of matplotlib
:n_columns_axis: Number of columns into subplot of matplotlib
:index: Index of the tone
:return: A graph with one tone and its frequencies
"""
tone_fft = np.fft.fft(signal, Fs)
time_tone = np.arange(0, time + (1 / Fs), (1 / Fs))
freq = np.fft.fftfreq(len(tone_fft), time_tone[1] - time_tone[0])
row = int(np.ceil(index / n_columns_axis))
column = int(index - ((row - 1) * n_columns_axis))
axis[row - 1, column - 1].plot(freq, np.abs(tone_fft))
axis[row - 1, column - 1].set_title("Tone {}".format(int(index)))
axis[row - 1, column - 1].set_xlabel("Frequency (Hz)")
axis[row - 1, column - 1].set_ylabel("Amplitude")
axis[row - 1, column - 1].set_xlim([600, 2000])
def main():
try:
parser = argparse.ArgumentParser(description="DTMF generator to phone numbers.")
parser.add_argument(
"-p",
"--phonenumber",
required=True,
type=str,
help="Phone number to encoder (Only numbers)",
)
parser.add_argument(
"-f",
"--samplefrequency",
required=True,
type=int,
help="Sample Frequency (Hz)",
)
parser.add_argument(
"-t",
"--toneduration",
required=True,
type=float,
help="Tones duration (s)",
)
parser.add_argument(
"-s",
"--silence",
required=True,
type=float,
help="Silence duration between tones duration (s)",
)
parser.add_argument(
"-a",
"--amplitude",
required=True,
type=float,
help="Amplitude of the sine waves",
)
parser.add_argument(
"-o",
"--output",
required=True,
type=str,
help="Filename output for WAV file",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Enable FFT graph of each tone (character) to debug",
)
args = parser.parse_args()
dtmf = DtmfGenerator(
args.phonenumber,
args.samplefrequency,
args.toneduration,
args.silence,
args.amplitude,
)
wav.write(args.output, args.samplefrequency, dtmf.signal)
if args.debug:
dtmf.test_signal(
args.output,
args.phonenumber,
args.samplefrequency,
args.toneduration,
args.silence,
)
except SystemExit:
pass
if __name__ == "__main__":
main()