-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavgen
executable file
·157 lines (126 loc) · 4.79 KB
/
wavgen
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
#!/usr/bin/python3
"""wavgen
A tool to generate signals with frequency components and write them to .wav files.
Usage:
wavgen [--channels=CH] FILE
wavgen (-v | --version)
wavgen (-h | --help)
Options:
-h --help Show this screen.
-v --version Show version.
--channels=CH Specifies the number of channels of the .wav file. [default: 1]
Channels are populated with the same data.
Example:
wavgen signal.wav
wavgen --channels 2 signal.wav
"""
import struct
import random
from wavhelp import *
try:
from docopt import docopt
except ImportError:
exit("This software refuses to run until docopt is installed:\n$ pip install docopt")
try:
import numpy as np
except ImportError:
exit("This software refuses to run until numpy is installed:\n$ pip install numpy")
try:
import matplotlib.pyplot as plt
except ImportError:
exit("This software refuses to run until matplotlib is installed:\n$ pip install matplotlib")
def normalize(wave):
M = max(wave)
m = -M #min(wave)
return [-32768 + 65535 * (x - m) / (M - m) for x in wave]
class sine:
def __init__(self, amplitude=16384, frequency=50., phase=0., duration=1., rate=44100):
self.a = abs(amplitude)
self.f = frequency # hertz
self.p = phase * np.pi / 180.0 # radians
assert duration > 0
self.d = duration # seconds
assert rate > 0
self.r = rate # samples per second
self.t = np.arange(self.r * self.d) / self.r # time table based on duration and sampling rate
self.__components = []
self.__transients = []
self.__noise = 0
def fundamental(self):
return self.a * np.sin(2.0 * np.pi * self.f * self.t + self.p)
def component(self, amplitude, frequency, phase):
self.__components.append({'amplitude': amplitude, 'frequency': frequency, 'phase': phase * np.pi / 180.0})
def noise(self, relative_amplitude):
self.__noise = relative_amplitude
if self.__noise > 1.0 or self.__noise < 0.0:
self.__noise = 0.0
def transient(self, ra, rt, t1, t2):
self.__transients.append({'ra': ra, 'rt': rt, 't1': t1, 't2': t2})
def gentransient(self, t, ra, rt, t1, t2):
A = ra * 32768
C = 1
Tth = rt * t[-1] # threshold time
T1 = Tth + t1 # end of rise marker
T2 = Tth + t1 + t2 # end of fall marker
Tth_T1 = np.linspace(0,5, int((T1 - Tth) * self.r + 1)).tolist() # linear range between 0...1 used for exponential computation
T1_T2 = np.linspace(0,5, int((T2 - T1) * self.r)).tolist()
transient = np.zeros(len(t))
try:
for n, T in enumerate(t):
if T <= Tth:
pass
elif T <= T1:
transient[n] = A * (1 - np.exp(-Tth_T1.pop(0) / C)) # pop from the beginning
elif T <= T2:
transient[n] = A * (1 - np.exp(-T1_T2.pop() / C)) # pop from the end (mirror exponential around y axis)
else:
pass
except IndexError as E:
# print(str(E))
pass
return transient
def components(self):
return [c['amplitude'] * np.sin(2.0 * np.pi * c['frequency'] * self.t + c['phase']) for c in self.__components]
def descriptors(self):
return self.__components
def time(self):
return self.t
def wave(self):
# fundamental
wave = self.fundamental()
# sinusoid components
for c in self.__components:
component = c['amplitude'] * np.sin(2.0 * np.pi * c['frequency'] * self.t + c['phase'])
wave = np.add(wave, component)
# transients
for t in self.__transients:
transient = self.gentransient(self.t, t['ra'], t['rt'], t['t1'], t['t2'])
wave = np.add(wave, transient)
# noise
if self.__noise > 0:
limit = self.__noise * 32768
noise = np.random.randint(-limit, limit, len(wave))
wave = np.add(wave, noise)
return wave
def rate(self):
return self.r
def main(args):
signal = sine(amplitude=1000,
frequency=50,
phase=0,
duration=0.2,
rate=31250)
for f in range(9000, 9100, 100):
ph = 90 * random.randrange(0, 3, 2)
a = random.randrange(500, 800, 2)
signal.component(1000, f, ph)
#signal.noise(0.02)
#signal.transient(-0.5, 0.5, 0.0004, 0.0004)
w = normalize(signal.wave())
wavcontent = wav_write_float(signal.wave(), signal.rate(), 4, 'f')
wavf=open(args['FILE'], 'wb')
wavf.write(wavcontent)
wavf.close()
if __name__ == "__main__":
args = docopt(__doc__, version='0.1')
main(args)