-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_symetric_func.py
61 lines (52 loc) · 1.51 KB
/
rec_symetric_func.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
import sounddevice as sd
import numpy as np
import time
# Parameters of filter moving average
M = 15
x = np.zeros((M, 1))
def myFiltrar (data): # Method symetric
y = 0
x[0] = data
for k in range(0, M/2):
y = y + (x[k]+x[(M-1)-k])*1
y = y * (1 / float(M))
for k in range(M - 1, -1, -1):
x[k] = x[k - 1]
# Output filter
return np.transpose(y)
# Parameters for getting audio
fs = 44100
duration = 5 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=1, dtype='float32')
print "Recording Audio"
sd.wait()
print "Audio recording complete, play audio"
sd.play(myrecording, fs)
sd.wait()
print "Played audio complete"
np.savetxt('data.csv', (myrecording), delimiter=',')
# noisy Gaussian
mu, sigma = 0, 0.1 # mean and standard deviation
s = np.random.normal(mu, sigma, len(myrecording))
s = np.array(([s,]))
s = s.transpose()
# apply Noisy Gaussian
myrecording = myrecording + s
print "Play audio with noise"
sd.play(myrecording, fs)
sd.wait()
print "Played with noise audio complete"
np.savetxt('data2.csv', (myrecording), delimiter=',')
#Applying Filter moving average
Y = np.zeros(np.size(myrecording), dtype='float32')
start = time.time() #Measure time of computing
for j in range(0, np.size(myrecording, 0)):
Y[j] = myFiltrar(myrecording[j])
end = time.time()
elapsed = end - start
print(elapsed)
#Output filter
print "Play Audio without Noise"
sd.play(Y, fs)
sd.wait()
print "Play without Noise Audio Complete"