-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_processing.py
295 lines (239 loc) · 9.03 KB
/
signal_processing.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
from Redbox_v2 import file_manager as fm
import pandas as pd
from scipy.fftpack import rfft, rfftfreq
import matplotlib.pyplot as plt
import os
import math
def rms_time_dom(signal):
N = len(signal)
return math.sqrt(np.sum(np.power(signal,2))/N)
def rms_freq_dom(amplitude):
return math.sqrt(2*np.sum(np.power(amplitude, 2)))/2
def n_minutes_max(signal, dt, n=5):
""""
:param signal (np.array or list)
:param n: n-minutes range to obtain maximum (int)
:param dt: sample space or time between samples
this functions does not return the real time of the occuring
return: numpy array with
"""
maximums = np.zeros(1)
samples = int((n*60)/dt)
start = 0
end = samples
while start < len(signal):
selection = signal[start:end]
maximums = np.append(maximums, [np.amax(selection),np.amin(selection)])
start = end
end = min(len(signal), start + samples)
maximums = np.delete(maximums,0)
return maximums
def n_seconds_min_max(data, dt, n):
"""
:param data = 2D array with t and velocity in one direction
:param dt = sample space or time between samples
:param n = in seconds for which interval maximum value is determined
collect minimum and maximum values of the data over an interval
"""
samples = int(1/dt * n)
start = 0
end = samples
min_max_array = np.zeros([1, 2]) # col 1 = time [s], col 2 = min and max of direction
while start < data.shape[0]:
index_max = start + np.argmax(data[start:end, 1])
index_min = start + np.argmin(data[start:end, 1])
x = np.array([[data[index_max,0], data[index_max,1]], [data[index_min,0], data[index_min,1]]])
min_max_array = np.concatenate((min_max_array, x), axis=0)
start = end
end += samples
min_max_array = np.delete(min_max_array,0,0)
min_max_array = min_max_array[min_max_array[:,0].argsort()]
return min_max_array
def FFT(signal,dT):
"""
:param signal: [array]
:param dT: sample space [float]
"""
ampl = np.abs(rfft(signal)) * 2.0 / len(signal)
freq = rfftfreq(len(ampl),d=dT)
return ampl, freq
def FFT_amplitude(signal):
"""
:param signal: [array]
"""
ampl = np.abs(rfft(signal)) * 2.0 / len(signal)
return ampl
def OneThird_octave(low, high):
""""
:param low: lowest required frequency band
:param high: highest required frequency band
this function starts at the highest band and
"""
one_third_octave = 2**(1/3)
last_band = high
first_band = last_band
N = 0
while first_band > low:
first_band = first_band/one_third_octave
N += 1
first_band = first_band * one_third_octave
return first_band * np.logspace(0, N, endpoint=False, num=N, base=one_third_octave)
def FFT_to_OneThird_Octave(amplitude, df, low, high):
"""
:param amplitude: amplitudes of the FFT [array]
:param frequency: frequencies of the FFT [array]
"""
one_third_octave = 2 ** (1 / 3)
spectrum = OneThird_octave(low, high)
rms_amplitude = np.empty(len(spectrum))
#check if the maximum available frequency exceeds the upper bound
if (df*len(amplitude))*one_third_octave**0.5 > high:
lower_bound = spectrum[0] / one_third_octave ** 0.5
upper_bound = spectrum[0] * one_third_octave ** 0.5
for n in range(rms_amplitude.size):
rms_amplitude[n] = rms_freq_dom(amplitude[int(lower_bound // df)*2:int(upper_bound // df)*2])
lower_bound = lower_bound * one_third_octave
upper_bound = upper_bound * one_third_octave
return rms_amplitude, spectrum
else:
print("ERROR frequency range is not large enough")
return
def FFT_to_OneThird_Octave2(amplitude, df, spectrum):
"""
:param amplitude: amplitudes of the FFT [array]
:param frequency: frequencies of the FFT [array]
"""
one_third_octave = 2 ** (1 / 3)
spectrum = spectrum
rms_amplitude = np.empty(len(spectrum))
high = spectrum[-1]
#check if the maximum available frequency exceeds the upper bound
if (df*len(amplitude))*one_third_octave**0.5 > high:
lower_bound = spectrum[0] / one_third_octave ** 0.5
upper_bound = spectrum[0] * one_third_octave ** 0.5
for n in range(rms_amplitude.size):
rms_amplitude[n] = rms_freq_dom(amplitude[int(lower_bound // df)*2:int(upper_bound // df)*2])
lower_bound = lower_bound * one_third_octave
upper_bound = upper_bound * one_third_octave
return rms_amplitude
else:
print("ERROR frequency range is not large enough")
return
"""
integration and differentiation
"""
def integ_to_disp(vel,dt):
"""
:param vel: velocity obtained from data (np.array)
:return: (np.array) (displacement)
"""
disp = np.zeros(len(vel))
disp = disp[:-1]
for i in range(1, len(disp)):
disp[i] = disp[i - 1] + (vel[i + 1] - vel[i]) * dt
return disp
def diff_to_acc(vel,dt):
"""
:param vel: velocity obtained from data(lst)
:return: (tpl) (acceleration)
"""
acc = np.zeros(len(vel))
acc = acc[:-1]
for i in range(0, len(acc)):
acc[i] = (vel[i + 1] - vel[i]) / dt
return acc
def select_part(start, stop, to_select):
"""
TODO nagaan of deze functie werkelijk nuttig is
:param start: start of selection(flt/int)
:param stop: end time of selection (flt/int)
:return: (tpl) (displacement u, velocity v)
"""
i = int(start / dt)
j = int(stop / dt)
lst = []
for k in range(i,j):
lst.append(to_select[k])
lst_t = np.linspace(start, dt, stop)
return lst_t, lst
""" SBR methods"""
def compute_veff_sbr(v,T,Ts=0.125, a=8):
"""
:param =df = vels (mm/s)
:param = T = sample space (s)
:param a = each a'th sample is used
"""
l = int(np.log2(v.size)+1) #nth-power
N_org = v.size
N = 2**l
t = np.linspace(0,N*T,N,endpoint=False)
v = np.pad(v,(0,N-v.size),'constant')
vibrations_fft = np.fft.fft(v)
f = np.linspace(0, 1 / T, N, endpoint=False)
f_mod=f
f_mod[f<1.0]=0.1
weight = 1 / np.sqrt(1 + (5.6 / f_mod) ** 2)
vibrations_fft_w = weight * vibrations_fft
vibrations_w = np.fft.ifft(vibrations_fft_w).real
t_sel = t[:N_org:a]
vibrations_w = vibrations_w[:N_org:a]
v_sqrd_w = vibrations_w ** 2
v_eff = np.zeros(t_sel.size)
dt = t_sel[1] - t_sel[0]
print('compute v_eff')
for i in range(t_sel.size - 1):
g_xi = np.exp(-t_sel[:i + 1][::-1] / Ts)
v_eff[i] = np.sqrt(1 / Ts * np.trapz(g_xi * v_sqrd_w[:i + 1], dx=dt))
fm.progress(i,t_sel.size-1,"processing %s of %s" % (i + 1, t_sel.size))
idx = np.argmax(v_eff)
return v_eff[idx], t_sel, vibrations_w, v_eff
def plot_SBR_B(save_to_path,vibrations, vibrations_w,v_eff,t_sel):
"""
vibrations, vibrations_w,v_eff are optional arguments
"""
plt.figure(figsize=(10, 6))
if vibrations:
plt.plot(t_sel, vibrations, label="signal")
if vibrations_w:
plt.plot(t_sel, vibrations_w, label="weighted_signal")
if v_eff:
plt.plot(t_sel, v_eff, label="v_eff")
plt.text(t[idx], v_eff[idx], "max v_eff: {}".format(round(v_eff[idx], 3)), color="r")
plt.xlabel("t [s]")
plt.ylabel("v [mm/s]")
plt.title("velocity")
plt.legend()
plt.savefig(save_to_path.format("png"))
plt.show()
def plot_SBR_B_xyz(save_to_path,vibrations, vibrations_w,v_eff,t_sel):
"""
TODO check use of pandas plotting wrapper
vibrations, vibrations_w,v_eff are optional arguments (tpl)
"""
fig = plt.figure(figsize=(10, 18))
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2)
ax3 = fig.add_subplot(3,1,3)
if vibrations:
ax1.plot(t_sel, vibrations[0], label="signal")
ax2.plot(t_sel, vibrations[1], label="signal")
ax3.plot(t_sel, vibrations[2], label="signal")
if vibrations_w:
ax1.plot(t_sel, vibrations_w[0], label="weighted_signal")
ax2.plot(t_sel, vibrations_w[1], label="weighted_signal")
ax3.plot(t_sel, vibrations_w[2], label="weighted_signal")
if v_eff:
idx = [np.argmax(v_eff[x]) for x in range(len(v_eff))]
ax1.plot(t_sel, v_eff, label="v_eff")
ax1.text(t[idx[0]], v_eff[0][idx[0]], "max v_eff: {}".format(round(v_eff[idx], 3)), color="r")
ax2.plot(t_sel, v_eff, label="v_eff")
ax2.text(t[idx[1]], v_eff[1][idx[1]], "max v_eff: {}".format(round(v_eff[idx], 3)), color="r")
ax3.plot(t_sel, v_eff, label="v_eff")
ax3.text(t[idx[1]], v_eff[2][idx[2]], "max v_eff: {}".format(round(v_eff[idx], 3)), color="r")
plt.xlabel("t [s]")
plt.ylabel("v [mm/s]")
plt.title("velocity")
plt.legend()
plt.savefig(save_to_path.format("png"))
plt.show()