-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVG_MISO.py
158 lines (119 loc) · 5.01 KB
/
AVG_MISO.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 13:55:01 2017
@author: davidkumar
"""
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline
from scipy import interpolate
from utility import *
def initialize(Q, fs, Lf, xs, p):
Omega = 2 * np.pi / Q # angular speed of the microphone [rad/s]
L = int(2 * np.pi / Omega * fs)
t = (1 / fs) * np.arange(L)
phi = Omega * t
distance = np.sqrt((R*np.cos(phi)-xs[0])**2 + (R*np.sin(phi)-xs[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
waveform = waveform * weight[:, np.newaxis]
#h, _, _ = construct_ir_matrix(waveform*weight[:, np.newaxis], shift, N)
# getting captured signal for each microphone
s = captured_signal(waveform, shift, p)
return s, phi
def calc_impulse_response(K, N, s, phi, Phi, interp_method, h, p):
# calculating of impulse_response
impulse_response = np.zeros((N, K))
for k in range(K):
y = np.zeros(N)
for i in range(N):
s_i_linear = s[i::N]
phi_i_linear = phi[i::N] # Decompose the captured signal into N sub-signals
y[i] = spatial_interpolation(s_i_linear, phi_i_linear, Phi[k], interp_method) # interpolation
impulse_response[:, k] = cxcorr(y, p)
DD = np.zeros(K)
for psi in range(K):
nummer = numerator(impulse_response[:, psi], h[:, psi])# numerator of formula
denom = denominator(h[:, psi])
DD[psi] = nummer/denom
return DD, impulse_response
##########################################################################################
# Constants
c = 343 # speed of sound [m/s]
fs = 8000 # sampling frequency [Hz]
# Parameters
N = 150 # length of the impulse response
K = 90 # desired number of impulse responses
Lf = 13 # length of the fractional delay filter
inter_method = 4
# Q = [0.628, 1.375, 6.28] #12
m_omega = 100
Q = np.linspace(6.28, 0.628, num=m_omega, endpoint=False) #12
# Source position
num_source = 2
#xs = [0, 2]
xs = [[0, 2], [0, -2]]
D = np.zeros((m_omega, inter_method, K))
Avg_D = np.zeros((inter_method, m_omega))
# Receiver positions on a circle
R = 0.5 # radius
Phi = np.linspace(0, 2*np.pi, num=K, endpoint=False)
distance = np.sqrt((R*np.cos(Phi)-xs[0][0])**2 + (R*np.sin(Phi)-xs[0][1])**2)
delay = distance / c
weight = 1 / distance
#######################Static impulse respones########################
waveform, shift, _ = fractional_delay(delay, Lf, fs=fs, type='lagrange')
h, _, _ = construct_ir_matrix(waveform*weight[:, np.newaxis], shift, N)
h = h.T
#denom = denominator(h, Phi)# denominator of formula
#######################End of Static response######################
# Excitation by perfet sequences.
# p = perfect_sequence_randomphase(N)
p = perfect_sweep(N)
for ii in range(len(Q)):
s_0, phi = initialize(Q[ii], fs, Lf, xs[0], p)
s_1, _ = initialize(Q[ii], fs, Lf, xs[1], np.roll(p,2))
s = s_0 + s_1
impulse_response = np.zeros((N, K))
#####################################Interpolation method is linear#####################################################
interp_method = 'linear'
D[ii, 0, :], _ = calc_impulse_response(K, N, s, phi, Phi, interp_method, h, p)
Avg_D[0, ii] = db(np.mean(D[ii, 0, :]))
#####################################Interpolation method is nearestNeighbour#####################################################
interp_method = 'nearestNeighbour'
D[ii, 1, :], _ = calc_impulse_response(K, N, s, phi, Phi, interp_method, h, p)
Avg_D[1, ii] = db(np.mean(D[ii, 1, :]))
#####################################Interpolation method is sinc#####################################################
interp_method = 'sinc'
D[ii, 2, :], _ = calc_impulse_response(K, N, s, phi, Phi, interp_method, h, p)
Avg_D[2, ii] = db(np.mean(D[ii, 2, :]))
#####################################Interpolation method is spline#####################################################
interp_method = 'spline'
D[ii, 3, :], _ = calc_impulse_response(K, N, s, phi, Phi, interp_method, h, p)
Avg_D[3, ii] = db(np.mean(D[ii, 3, :]))
Omega = 2 * np.pi / Q
#Omega = np.rad2deg(2 * np.pi / Q)
min_o = np.amin(Avg_D)
max_o = np.amax(Avg_D)
#Descret line for Omege_C
y_val = np.linspace(min_o, max_o, num=50)
Qmega_o = 2 * np.pi / 1.375
Omega_seq = np.ones((1, 50)) * Qmega_o
# Plot
plt.figure()
plt.plot(Omega, Avg_D[0, :], label="Interp_Method is linear")
plt.plot(Omega, Avg_D[1, :], label="Interp_Method is nearestNeighbour")
plt.plot(Omega, Avg_D[2, :], label="Interp_Method is sinc")
plt.plot(Omega, Avg_D[3, :], label="Interp_Method is spline")
plt.plot(Omega_seq[0, :], y_val, label="Omega_C(Q=1.375):{}".format(Qmega_o)+"rad/s")
plt.legend()
plt.grid()
#plt.xlim(0, 360)
#plt.ylim(max_o)
plt.xlabel('Omega : rad/s')
plt.ylabel(r'$Average$ $System$ $distance$ / dB')
plt.title('Average System distance')
plt.show()