-
Notifications
You must be signed in to change notification settings - Fork 102
/
clear_noise.py
57 lines (50 loc) · 1.2 KB
/
clear_noise.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
import wave, os, array, struct
import os.path
IN_FOLDER = "wavNexusGyroY"
def sample_out_of_range(min, max, x, i, direction):
ret = False
if x[i] > max or x[i] < min:
for j in range(1,10):
if x[i+direction*j] > max or x[i+direction*j] < min:
ret = True
break
return ret
files = os.listdir(IN_FOLDER)
wavfiles = list()
for f in files:
if f.endswith('.wav'):
wavfiles.append(f)
for f in wavfiles:
print f
w = wave.open(IN_FOLDER + '\\' + f, 'r')
frame = w.readframes(w.getnframes())
params = w.getparams()
x = array.array('h', frame)
w.close()
max = -65000
min = 65000
for i in range(len(x)):
if i <= 60:
if x[i] < min:
min = x[i]
if x[i] > max:
max = x[i]
if not sample_out_of_range(min, max, x, i, 1):
x[i] = 0
else:
break
low_index = i
for i in reversed(range(len(x))):
if not sample_out_of_range(min, max, x, i, -1):
x[i] = 0
else:
break
high_index = i
w = wave.open(os.path.join(IN_FOLDER, 'cleaned', f), 'wb')
w.setparams(params)
for i in range(len(x)):
if i < low_index or i > high_index:
w.writeframes(struct.pack('h', 0))
else:
w.writeframes(struct.pack('h', x[i]))
w.close()