-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMISO.py
More file actions
132 lines (96 loc) · 3.53 KB
/
MISO.py
File metadata and controls
132 lines (96 loc) · 3.53 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
import pylab as pl
from scipy import interpolate
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline
import numpy as np
import scipy.signal as sig
import copy
from utils import *
def nearestneighbour(ni_i, phi_i):
"""
"""
h = interpolate.NearestNDInterpolator(ni_i, phi_i)
return h.x
def spatial_interpolation(s_i, phi_i, phi_target, interp_method):
if phi_i[-1] < phi_target:
phi_i[-1] = phi_target
elif phi_i[0] > phi_target:
phi_i[0] = phi_target
s_i = np.array(s_i)
phi_i = np.array(phi_i)
# phi_target[k] = np.array(phi_target[k])
if interp_method == 'linear':
f = interpolate.interp1d(phi_i, s_i, bounds_error=False)
h = f(phi_target)
elif interp_method == 'spline':
tck = interpolate.interp1d(phi_i, s_i, kind = 'cubic',bounds_error=False)
h = tck(phi_target)
elif interp_method == 'fitpack2 method':
ius = InterpolatedUnivariateSpline(phi_i, s_i)
h = ius(phi_target)
else:
print("Please select correct interpolation method")
return
return h
def cross_correlation(x, y):
return cxcorr(x, y)
# Define a microphone movement (radius, angular speed, etc)
# The angular position of the microphone phi (length of L)
# Gerenate a perfect sequence p (length N)
# Compute the captured signal s, which has the same length L
# Constants
c = 343
# Parameters
N = 150 # length of the impulse reponses
Nh = int(N / 2)
Q = 8
Omega = 2 * np.pi / Q # angular speed of the microphone [rad/s]
K = 90 # desired number of impulse responses
Lf = 13 # length of the fractional delay filter
fs = 8000
L = 2 * np.pi / Omega * fs
time = np.arange(np.ceil(L)) / fs
# Source: You can change the number of sources
mic_number = 2
xs = [[0, 2],[0,-2]] # Point source
L = int(2 * np.pi / Omega * fs)
t = (1 / fs) * np.arange(L)
#phi = Omega * t
R = 0.5 # Radius
#setting target phases
#phi_target = np.linspace(0.5*np.pi, 1.5*np.pi, num=K,endpoint= False)
phi_target = np.linspace(0, 2*np.pi, num=K,endpoint= False)
#h, _, _ = construct_ir_matrix(waveform*weight[:, np.newaxis], shift, N)
# Excitation by perfet sequences.
#p = perfect_sequence_randomphase(N)
p = perfect_sweep(N)
type = 'lagrange' # FD filters
# initializing of spatial interplation values
y_i = np.zeros(N)
#################Select interpolation method####################################
interp_method = 'spline'
#interp_method = 'linear'
#interp_method = 'fitpack2 method'
#initializing of impulse_response
impulse_response = np.zeros((N,K))
for mic_num in range(mic_number):
#consideration of microphone's initial phase.
phi = Omega * t + mic_num*np.pi*2/mic_number
# Define mic position
distance = np.sqrt((R * np.cos(phi) - xs[0][0]) ** 2 + (R * np.sin(phi) - xs[1][1]) ** 2)
delay = distance / c
weight = 1 / distance
type = 'lagrange' # FD filters
waveform, shift, offset = fractional_delay(delay, Lf, fs=fs, type=type) # getting impulse_respones
# getting captured signal for each microphone
s = captured_signal(waveform, shift, p)
#for each subsignal
for k in range(K):
y = np.zeros(N)
for i in range(N):
s_i = s[i::N]
phi_i = phi[i::N] # Decompose the captured signal into N sub-signals
y[i] = spatial_interpolation(s_i, phi_i, phi_target[k], interp_method) # interpolation
# calculating of impulse_response
impulse_response[:, k] = cxcorr(y, p)
##################################################################