-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeVolder.py
More file actions
151 lines (97 loc) · 3.28 KB
/
DeVolder.py
File metadata and controls
151 lines (97 loc) · 3.28 KB
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
import numpy as np
from scipy import signal
import audiofile
from collections import deque
from scipy.optimize import root
from glob import glob
import scipy.signal as sc
from glob import glob
import scipy.io.wavfile as wf
import audiofile
import re
def create_sine_wave(f, A, fs, N):
t=np.arange(0,N/fs,1/fs)
return A*np.sin(2*np.pi*f*t)
def read_wavefile(path):
signal_audio, sampling_rate = audiofile.read(path)
return signal_audio,sampling_rate
def create_ringbuffer(maxlen):
# your code here #
out = deque(maxlen=maxlen)
return out
def normalise(s):
max_val=np.max(np.abs(s))
if max_val == 0:
return s
out = s/max_val
return out
def create_filter_cheby(wp, ws, gpass, gstop, fs):
# your code here #
N,Wn = signal.cheb1ord(wp, ws, gpass,gstop,fs=fs)
B, A = signal.cheby1(N,gpass,wp,'low',fs=fs)
return B, A
def create_filter_cauer(wp, ws, gpass, gstop, fs):
# your code here #
N,Wn = signal.ellipord(wp,ws,gpass,gstop,fs=fs)
B, A = signal.ellip(N,gpass,gstop,wp,'lowpass',fs=fs)
return B, A
def downsampling(sig, B, A, M):
signal_filtered = signal.lfilter(B,A,sig)
signal_decimated = signal_filtered[::M]
return signal_decimated
def fftxcorr(in1, in2):
# your code here #
n = len(in1) + len(in2) - 1
FFT1 = np.fft.fft(in1,n=n)
FFT2 = np.fft.fft(in2,n=n)
corr = np.fft.ifft(FFT1 * np.conj(FFT2))
corr = np.fft.fftshift(corr.real)
out=corr
return out
def TDOA(xcorr,fs=44100):
max_index = np.argmax(np.abs(xcorr))
sample_offset = max_index - (len(xcorr) / 2)
time_delay=sample_offset/fs
return time_delay
# mic coordinates in meters
MICS = [{'x': 0, 'y': 0.0487}, {'x': 0.0425, 'y': -0.025}, {'x': -0.0425, 'y': -0.025}]
def equations(p, deltas):
v = 343
x, y = p
alpha = np.arctan2((MICS[1]['y'] - MICS[0]['y']), (MICS[1]['x'] - MICS[0]['x']))
beta = np.arctan2((MICS[2]['y'] - MICS[0]['y']), (MICS[2]['x'] - MICS[0]['x']))
eq1 = v*deltas[0] - (np.sqrt((MICS[1]['x'] - MICS[0]['x'])**2 + (MICS[1]['y'] - MICS[0]['y'])**2) * np.sqrt((x)**2 + (y)**2) * np.cos(alpha-np.arctan2(y, x)))
eq2 = v*deltas[1] - (np.sqrt((MICS[2]['x'] - MICS[0]['x'])**2 + (MICS[2]['y'] - MICS[0]['y'])**2) * np.sqrt((x)**2 + (y)**2) * np.cos(beta-np.arctan2(y, x)))
return (eq1, eq2)
def localize_sound(deltas):
sol = root(equations, [0, 0], (deltas), tol=10)
return sol.x
def source_angle(coordinates):
# your code here
x = coordinates[0]
y = coordinates[1]
out = np.arctan(y/x) #vérifier formule, pourquoi x et pas y
out = np.degrees(out)
if(x > 0 and y> 0 ):
return out
if(x < 0 and y> 0 ):
return out + 180
if(x < 0 and y < 0 ):
return out + 180
if(x > 0 and y < 0 ):
return out + 360
def accuracy(pred_angle, gt_angle, threshold):
if np.abs(pred_angle-gt_angle)<threshold:
return True
else:
return False
## 1.6.3
from time import time_ns, sleep
def func_example(a, b):
return a*b
def time_delay(func, args):
start_time = time_ns()
out = func(*args)
end_time = time_ns()
print(f"{func.__name__} in {end_time - start_time} ns")
return out