-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSD_omega.py
More file actions
178 lines (131 loc) · 5.29 KB
/
SD_omega.py
File metadata and controls
178 lines (131 loc) · 5.29 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- 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 *
# Constants
c = 343 # speed of sound [m/s]
fs = 8000 # sampling frequency [Hz]
# Parameters
N = 150 # length of the impulse response
Q = [0.628, 1.375, 6.28] #12
K = 90 # desired number of impulse responses
Lf = 13 # length of the fractional delay filter
# Source position
xs = [0, 2]
D = np.zeros((3, 3, K))
Avg_D = np.zeros((3, 3, K))
# 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])**2 + (R*np.sin(Phi)-xs[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)):
Omega = 2 * np.pi / Q[ii] # 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)
impulse_response = np.zeros((N, K))
#####################################Interpolation method is linear#####################################################
interp_method = 'linear'
#for each subsignal
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
#print(k)
#print(Phi[k])
y[i] = spatial_interpolation(s_i_linear, phi_i_linear, Phi[k], interp_method) #interpolation
#calculating of impulse_response
impulse_response[:,k] = cxcorr(y, p)
#formula
for psi in range(K):
nummer = numerator(impulse_response[:, psi], h[:, psi])#numerator of formula
denom = denominator(h[:, psi])
D[ii,0,psi] = 10*np.log10(nummer/denom)
#######################################################################################################
#####################################Interpolation method is nearestNeighbour#####################################################
interp_method = 'nearestNeighbour'
#for each subsignal
for k in range(K):
y = np.zeros(N)
for i in range(N):
s_i_nearest = s[i::N]
phi_i_nearest = phi[i::N] #Decompose the captured signal into N sub-signals
#print(k)
#print(Phi[k])
y[i] = spatial_interpolation(s_i_nearest, phi_i_nearest, Phi[k], interp_method) #interpolation
#calculating of impulse_response
impulse_response[:,k] = cxcorr(y, p)
#formula
for psi in range(K):
nummer = numerator(impulse_response[:,psi],h[:,psi])#numerator of formula
denom = denominator(h[:,psi])
D[ii, 1,psi] = 10*np.log10(nummer/denom)
#######################################################################################################
#####################################Interpolation method is sinc#####################################################
interp_method = 'sinc'
#for each subsignal
for k in range(K):
y = np.zeros(N)
for i in range(N):
s_i_sinc = s[i::N]
phi_i_sinc = phi[i::N] #Decompose the captured signal into N sub-signals
#print(k)
#print(Phi[k])
y[i] = spatial_interpolation(s_i_sinc, phi_i_sinc, Phi[k], interp_method) #interpolation
#calculating of impulse_response
impulse_response[:,k] = cxcorr(y, p)
#formula
for psi in range(K):
nummer = numerator(impulse_response[:,psi],h[:,psi])#numerator of formula
denom = denominator(h[:,psi])
D[ii, 2,psi] = 10*np.log10(nummer/denom)
#######################################################################################################
Phi=np.deg2rad(Phi)
for ii in range(3):
# Plot
plt.figure()
plt.plot(Phi, D[ii, 0,:],label = "is linear")
plt.plot(Phi, D[ii, 1,:],label = "nearestNeighbour")
plt.plot(Phi, D[ii, 2,:],label = "sinc")
plt.legend()
plt.grid()
plt.ylim(-70,10)
#plt.xlim(0, 360)
plt.xlabel(r'$\varphi$ / deg')
plt.ylabel(r'$\System$ $distance / dB')
if ii == 0:
title = (r'$\Omega$ $10rad$/s')
elif ii==1:
title = (r'$\Omega$ $4.57rad$/s')
else:
title = (r'$\Omega$ $1rad$/s')
plt.title(title)
plt.show()