-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transfer_test.py
181 lines (153 loc) · 4.16 KB
/
Transfer_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 26 14:32:59 2016
@author: Jee-Bee for jBae (c) 2016
"""
import sys
if sys.version_info.major <3:
from __future__ import division
import numpy as np
from src.signal import chirp
from src import Transform, Window, DefaultFigures
import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
# from matplotlib.ticker import LinearLocator, FormatStrFormatter
"""transfer function test
Signal out is signal for speaker.
Signal in1 is recorded by mic.
Signal in2 is recorded + filter
transfer function is:
in signal in1 in2
H = ---------- --> --- or ---
out signal out out
"""
# http://www.mathworks.com/matlabcentral/answers/33500-2nd-order-digital-butterworth-filter
# Filtering acc:
# [B, A] = sig.butter(2, ff[0] / fs, 'bandstop')
# in3 = np.transpose(sig.filtfilt(B, A, np.transpose(in3)))
# [B, A] = sig.butter(4, (ff[1] * 2)/fs, 'bandstop')
# in3 = np.transpose(sig.filtfilt(B, A, np.transpose(in3)))
# [B, A] = sig.butter(2, 19000 * 2 / fs, 'lowpass')
# in3 = np.transpose(sig.filtfilt(B, A, np.transpose(in3)))
#
#N = 64
#t = np.arange(0, 1, 1 / N)
#x = chirp(t, 1, 1, 8, phi=90)
#y = 2 * np.sin(2 * np.pi * 4 * t) + 0.707 * np.sin(2 * np.pi * 6 * t)
#
#X = fft(x)
#Y = fft(y)
#F = np.arange(-N/2+1, N/2+1)
#
#Xr = np.real(X)
#Xi = np.imag(X)
#Yr = np.real(Y)
#Yi = np.imag(Y)
#
## From here test\
## for now just real and imag or complex.
## next step also Magnitude and phase
#
## Most Needed Function is not finished...
#
## X_in = x, X_out = complex
#H0_1_val = Transform.Transfer(x, Y, N)
#
## X_in = tuple, X_out = complex
#H0_2_val = Transform.Transfer((Xr, Xi), Y, N)
#
## X_in = complex, X_out = tuple
#H0_3_val = Transform.Transfer(X, (Yr, Yi), N)
## Fix Odd symmetry check
#
#plt.plot(F,abs(X))
#
#plt.figure()
#plt.plot(t,x,t,y)
#plt.figure()
#plt.plot(F,abs(X))
#plt.plot(F,np.real(X),F,np.imag(X))
#plt.figure()
#plt.plot(F, H0_1_val, F, H0_2_val, F, H0_3_val)
#
#
#
# ----- Part 2 of test
#
# signal generation
# allready exist
N = 2 ** 16
fs = 2 ** 12
overlap = 50 # [%]
t = np.arange(0, N / fs, 1 / fs)
x = chirp(t, 20, N / fs - 1 / fs, 2000, phi=90)
x = np.append(x, np.zeros(fs)) # silece for tail of real live sound
y = sd.playrec(x, samplerate=fs, channels=1)
sd.wait()
y = np.reshape(y, len(y))
# averaging multiple signals
# pass # for later on
# window + multi fft
Nw = fs
hannw = Window.Window(Nw)
(dummy, hann) = hannw.hanwind()
S_1 = np.sum(hann)
S_2 = np.sum(hann ** 2)
NENBW = Nw * S_2 / (S_1 ** 2)
ENBW = fs * S_2 / (S_1 ** 2) # = NENBW * fs/N
print(NENBW, ENBW)
overlap = overlap/100 *Nw
idxl = 0
idxu = Nw
Y = []
while idxu <= len(y):
(F, Yd) = Transform.FFT(y[idxl:idxu] * hann, fs)
#np.concatenate([a[None,:],b[None,:]])
# http://stackoverflow.com/questions/21887754/numpy-concatenate-two-arrays-vertically?rq=1
if Y == []:
Y = np.append(Y, Yd)
else:
Y = np.vstack((Y, Yd))
#(F, Y) = np.append(Y, Transform.FFT(y[idxl:idxu] * hann,fs), axis=1)
idxl += (Nw - overlap)
idxu += (Nw - overlap)
Y /= S_1
#Y = np.reshape(Y,(Nw,len(Y)/Nw))
twind = np.arange(0,16.5,0.5)
twind, F = np.meshgrid(twind, F)
if np.shape(F) != np.shape(Y):
Y = Y.T
## Later on fix
## now not volt_rms so PS, PSD, LS and LSD are just V and not V_rms
## PS_rms = 2 * np.abs(y_m)/S1 equation 23
#PS_rms = 2 * np.abs(WIND_nf)/S_1
#
## PSD_rms = PS_rms/ENBW equation 24
#PSD_rms = 2 * np.abs(WIND_nf)**2/(fs*S_2)
# spectogram
#DefaultFigures.Default3D(Y, F, twind)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(twind, F, abs(Y), rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
#ax.set_zlim(-1.01, 1.01)
#ax.zaxis.set_major_locator(LinearLocator(10))
#ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
#spectfig = DefaultFigures.default3D(twind, F, Y)
#spectfig.Spect()
#
# ---TEST-----------TEST------------TEST----------TEST----
#
#H4 = OUT / fftshift(IN1)
#plt.figure()
#plt.plot(t, ifft(H4)) # hold all;
#
#SIH3 = fftshift(ifft(H3))
#ISH3 = ifft(fftshift(H3))
#
#plt.figure()
## plt.plot(t, SIH3)# hold all;
#plt.plot(t, ISH3)