rdmax 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rdmax/__init__.py +18 -0
- rdmax/_common.py +31 -0
- rdmax/compat.py +20 -0
- rdmax/license.py +36 -0
- rdmax/mfn.py +357 -0
- rdmax/modeling.py +1674 -0
- rdmax/post.py +479 -0
- rdmax/rplt.py +320 -0
- rdmax/start.py +83 -0
- rdmax/textfile.py +302 -0
- rdmax/utils.py +89 -0
- rdmax-0.1.0.dist-info/METADATA +76 -0
- rdmax-0.1.0.dist-info/RECORD +16 -0
- rdmax-0.1.0.dist-info/WHEEL +5 -0
- rdmax-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
- rdmax-0.1.0.dist-info/top_level.txt +1 -0
rdmax/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""rdmax: personal RecurDyn helper functions.
|
|
2
|
+
|
|
3
|
+
import recurdyn
|
|
4
|
+
from rdmax import *
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__version__ = "0.1.0"
|
|
8
|
+
import rdmax._common # first: clear error message when recurdyn is missing
|
|
9
|
+
from rdmax.start import start_recurdyn
|
|
10
|
+
from rdmax.utils import use_korean_font, use_wide_print, setup_logging, RTOD, DTOR, ColorSeries, store_data, restore_data, unique_geo_name, str_to_liststr, subsystem_list_full
|
|
11
|
+
from rdmax.modeling import RecurDynModeling
|
|
12
|
+
from rdmax.post import RecurDynPost, AnalysisResponse, MSG_LEGEND_L23, MSG_LEGEND_L21, MSG_LEGEND_EXT, MSG_LEGEND_STATIC, Post
|
|
13
|
+
from rdmax.textfile import TextFileRead, TextFileWrite, TextFileEdit
|
|
14
|
+
from rdmax.license import RecurDynLicense
|
|
15
|
+
from rdmax.rplt import RpltReader
|
|
16
|
+
from rdmax.mfn import fig_pos, CampbellDiagram, uniform, interp_sampling, FFT, FRF, PSD, FFT2PSD, PSD2FFT, FFT2SIG, LPF, HPF, BPF, Interp, step5, step3, step_sin, AmatZXZ, OrientZXZ
|
|
17
|
+
|
|
18
|
+
__all__ = ['start_recurdyn', 'use_korean_font', 'use_wide_print', 'setup_logging', 'RTOD', 'DTOR', 'ColorSeries', 'store_data', 'restore_data', 'unique_geo_name', 'str_to_liststr', 'subsystem_list_full', 'RecurDynModeling', 'RecurDynPost', 'AnalysisResponse', 'MSG_LEGEND_L23', 'MSG_LEGEND_L21', 'MSG_LEGEND_EXT', 'MSG_LEGEND_STATIC', 'Post', 'TextFileRead', 'TextFileWrite', 'TextFileEdit', 'RecurDynLicense', 'RpltReader', 'fig_pos', 'CampbellDiagram', 'uniform', 'interp_sampling', 'FFT', 'FRF', 'PSD', 'FFT2PSD', 'PSD2FFT', 'FFT2SIG', 'LPF', 'HPF', 'BPF', 'Interp', 'step5', 'step3', 'step_sin', 'AmatZXZ', 'OrientZXZ']
|
rdmax/_common.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Split from RDFunctions.py. Shared imports for all rdmax modules.
|
|
2
|
+
# The recurdyn package must be importable (pip-installed); no sys.path changes here.
|
|
3
|
+
import os, sys, struct, time, subprocess, pickle, logging
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
try:
|
|
6
|
+
import winreg
|
|
7
|
+
except ImportError: # non-Windows
|
|
8
|
+
winreg = None
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
|
|
13
|
+
from math import *
|
|
14
|
+
try:
|
|
15
|
+
import recurdyn as _recurdyn
|
|
16
|
+
except ImportError as _e:
|
|
17
|
+
raise ImportError(
|
|
18
|
+
"rdmax needs the 'recurdyn' package (RecurDyn Python API), which is not installed in this Python.\n"
|
|
19
|
+
" - pip install recurdyn (the version matching your RecurDyn), or\n"
|
|
20
|
+
" - use a Python where RecurDyn's recurdyn package can be imported."
|
|
21
|
+
) from _e
|
|
22
|
+
from recurdyn import *
|
|
23
|
+
from recurdyn.FFlex import *
|
|
24
|
+
from recurdyn.RFlex import *
|
|
25
|
+
from recurdyn.TrackHM import *
|
|
26
|
+
from recurdyn.utils.color import Color
|
|
27
|
+
from recurdyn import Flexible
|
|
28
|
+
|
|
29
|
+
# Default RecurDyn version for RecurDynModeling(ver=...), as in the old RDFunctions:
|
|
30
|
+
# RECURDYN_VERSION environment variable, else the pip-installed recurdyn release year, else "2027".
|
|
31
|
+
RECURDYN_VERSION = os.environ.get("RECURDYN_VERSION") or str(getattr(_recurdyn, "RECURDYN_YEAR", "2027"))
|
rdmax/compat.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Drop-in replacement for the old ``from RDFunctions import *``.
|
|
2
|
+
|
|
3
|
+
Old scripts:
|
|
4
|
+
from RDFunctions import * -> from rdmax.compat import *
|
|
5
|
+
import Mfn -> from rdmax import mfn as Mfn
|
|
6
|
+
|
|
7
|
+
Brings in everything the old RDFunctions module exposed:
|
|
8
|
+
rdmax functions/classes + recurdyn API + Korean matplotlib font + wide numpy print + numpy (np) + matplotlib (plt) + math + os, sys, ...
|
|
9
|
+
New code should prefer: import recurdyn / from rdmax import *
|
|
10
|
+
"""
|
|
11
|
+
from rdmax._common import * # noqa: F401,F403
|
|
12
|
+
from rdmax import * # noqa: F401,F403
|
|
13
|
+
from rdmax import mfn as Mfn # noqa: F401
|
|
14
|
+
# The old RDFunctions/Mfn set these on import; keep that behavior for old scripts.
|
|
15
|
+
use_korean_font()
|
|
16
|
+
use_wide_print()
|
|
17
|
+
try:
|
|
18
|
+
import winreg # noqa: F401 (old RDFunctions exposed it)
|
|
19
|
+
except ImportError: # non-Windows
|
|
20
|
+
pass
|
rdmax/license.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""RecurDyn license helper (split from RDFunctions.py)."""
|
|
2
|
+
|
|
3
|
+
from rdmax._common import * # noqa: F401,F403
|
|
4
|
+
|
|
5
|
+
__all__ = ['RecurDynLicense']
|
|
6
|
+
|
|
7
|
+
class RecurDynLicense:
|
|
8
|
+
def __init__(self, ver=RECURDYN_VERSION):
|
|
9
|
+
# RecurDyn install folder of the requested version (was an undefined name: recurdyn_root_path)
|
|
10
|
+
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, rf"SOFTWARE\FunctionBay, Inc.\RecurDyn {ver}\Path") as key:
|
|
11
|
+
recurdyn_root_path = winreg.QueryValueEx(key, "ROOT")[0]
|
|
12
|
+
dll_path = os.path.join(recurdyn_root_path, "Bin", "Addin", "License", "LicenseCheckFn.dll")
|
|
13
|
+
if os.path.isfile(dll_path):
|
|
14
|
+
import ctypes
|
|
15
|
+
self.rdlic = ctypes.WinDLL(dll_path)
|
|
16
|
+
# Define the function signature
|
|
17
|
+
self.__InitializeLicenseServer = self.rdlic.InitializeLicenseServer
|
|
18
|
+
self.__CheckOutLicense = self.rdlic.CheckOutLicense
|
|
19
|
+
self.__CheckOutLicense.restype = ctypes.c_int
|
|
20
|
+
self.__CheckOutLicense.argtypes = [ctypes.c_char_p]
|
|
21
|
+
self.__CheckInLicense = self.rdlic .CheckInLicense
|
|
22
|
+
self.__CheckInLicense.restype = ctypes.c_int
|
|
23
|
+
self.__CheckInLicense.argtypes = [ctypes.c_char_p]
|
|
24
|
+
self.__LicenseErrorString = self.rdlic .LicenseErrorString
|
|
25
|
+
self.__LicenseErrorString.restype = ctypes.c_char_p
|
|
26
|
+
self.__FinalizeLicense = self.rdlic .FinalizeLicense
|
|
27
|
+
self.__FinalizeLicense.restype = ctypes.c_char_p
|
|
28
|
+
self.__InitializeLicenseServer()
|
|
29
|
+
else:
|
|
30
|
+
raise FileNotFoundError(f"RecurDyn license DLL not found: {dll_path}")
|
|
31
|
+
def check(self, feature=None):
|
|
32
|
+
if feature is not None:
|
|
33
|
+
if self.__CheckOutLicense(bytes(feature, 'utf-8')) == 0:
|
|
34
|
+
self.__CheckInLicense(bytes(feature, 'utf-8'))
|
|
35
|
+
return True
|
|
36
|
+
return False
|
rdmax/mfn.py
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"""Signal processing / math helpers (was Mfn.py)."""
|
|
2
|
+
__all__ = ['fig_pos', 'CampbellDiagram', 'uniform', 'interp_sampling', 'FFT', 'FRF', 'PSD', 'FFT2PSD', 'PSD2FFT', 'FFT2SIG', 'LPF', 'HPF', 'BPF', 'Interp', 'step5', 'step3', 'step_sin', 'AmatZXZ', 'OrientZXZ']
|
|
3
|
+
|
|
4
|
+
from scipy.signal import butter, filtfilt, lfilter
|
|
5
|
+
from math import floor, sin, cos, pi
|
|
6
|
+
from scipy import interpolate
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
import matplotlib as mpl
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
# Korean font is no longer set on import. Call rdmax.use_korean_font() when needed.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def fig_pos(nfig, name, x, y, dx, dy):
|
|
15
|
+
backend = mpl.get_backend()
|
|
16
|
+
plt.figure(nfig)
|
|
17
|
+
x = int(x); y = int(y); dx = int(dx); dy = int(dy)
|
|
18
|
+
if backend in ("Qt5Agg", "Qt4Agg"):
|
|
19
|
+
plt.get_current_fig_manager().window.setGeometry(x, y, dx, dy)
|
|
20
|
+
plt.title(name)
|
|
21
|
+
elif backend in ("TkAgg",):
|
|
22
|
+
plt.get_current_fig_manager().window.SetPosition((x, y))
|
|
23
|
+
plt.get_current_fig_manager().window.wm_geometry("+%d+%d" % (dx, dy ))
|
|
24
|
+
plt.title(name)
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def CampbellDiagram(time, rpm, signal, fft_num=512, frame_num=300, ts=None, te=None, log=False, color_num=None, max_freq=None, order=None, fig_name=None):
|
|
29
|
+
interp = False
|
|
30
|
+
rn = len(time)
|
|
31
|
+
# check dt irregularity-----------------------------------------------------------------------------
|
|
32
|
+
dt_series = []
|
|
33
|
+
for i in range(1, rn):
|
|
34
|
+
dt = time[i] - time[i - 1]
|
|
35
|
+
dt_series.append(dt)
|
|
36
|
+
dt_series = np.array(dt_series)
|
|
37
|
+
print("dt max:" + str(dt_series.max()))
|
|
38
|
+
print("dt min:" + str(dt_series.min()))
|
|
39
|
+
print("dt mean:" + str(dt_series.mean()))
|
|
40
|
+
if dt_series.max() > 1e-12:
|
|
41
|
+
print("Irregular dt! so Interpolation is necessary!")
|
|
42
|
+
interp = True
|
|
43
|
+
pdt = dt_series.mean()
|
|
44
|
+
# interpolation--------------------------------------------------------------------------------------
|
|
45
|
+
data = []
|
|
46
|
+
if interp:
|
|
47
|
+
pdt = np.mean(dt_series)
|
|
48
|
+
rn = int((time[-1] - time[0]) / pdt) + 1
|
|
49
|
+
time1 = np.linspace(time[0], time[-1], rn)
|
|
50
|
+
f = interpolate.interp1d(time, rpm, kind="linear", fill_value="extrapolate")
|
|
51
|
+
rpm1 = f(time1)
|
|
52
|
+
f = interpolate.interp1d(time, signal, kind="linear", fill_value="extrapolate")
|
|
53
|
+
signal1 = f(time1)
|
|
54
|
+
else:
|
|
55
|
+
time1 = time
|
|
56
|
+
rpm1 = rpm
|
|
57
|
+
signal1 = signal
|
|
58
|
+
# find sn--------------------------------------------------------------------------------------------
|
|
59
|
+
sn = 0
|
|
60
|
+
if ts is not None:
|
|
61
|
+
for i, t in enumerate(time1):
|
|
62
|
+
if t >= ts:
|
|
63
|
+
sn = i
|
|
64
|
+
break
|
|
65
|
+
else:
|
|
66
|
+
ts = time1[0]
|
|
67
|
+
en = rn-1
|
|
68
|
+
if te is not None:
|
|
69
|
+
for i, t in enumerate(reversed(time1)):
|
|
70
|
+
if t <= te:
|
|
71
|
+
en = rn-i-1
|
|
72
|
+
break
|
|
73
|
+
else:
|
|
74
|
+
te = time1[-1]
|
|
75
|
+
print("St: " + str(ts) + " Et: " + str(te))
|
|
76
|
+
print("TIME: " + str(time1[sn]) + "[" + str(sn) + "]" + " ETIME: " + str(time1[en]) + "[" + str(en) + "]")
|
|
77
|
+
# Campbell data generate-----------------------------------------------------------------------------
|
|
78
|
+
dt = pdt
|
|
79
|
+
nf = frame_num
|
|
80
|
+
T = time1[en]-time1[sn]
|
|
81
|
+
ipt = floor(T/dt)
|
|
82
|
+
ipl = fft_num
|
|
83
|
+
ipd = floor((ipt-ipl)/(nf-1))
|
|
84
|
+
ipo = ipl-ipd
|
|
85
|
+
ipu = (nf-1)*ipd+ipl
|
|
86
|
+
ipr = ipt-ipu
|
|
87
|
+
print("Overlap: " + str(ipo))
|
|
88
|
+
print("Residual: " + str(ipr))
|
|
89
|
+
sig_freq = []
|
|
90
|
+
sig_amp = []
|
|
91
|
+
sig_rpm = []
|
|
92
|
+
# def FFT(t, x):
|
|
93
|
+
# ft = np.fft.rfft(x); rn = len(ft)
|
|
94
|
+
# freq = np.linspace(0.0, (rn-1)/(t[-1]-t[0]), rn)
|
|
95
|
+
# mag = abs(ft)/rn
|
|
96
|
+
# mag[0] = 1e-10
|
|
97
|
+
# return freq, mag
|
|
98
|
+
for i in range(nf):
|
|
99
|
+
ista = i*ipd + sn
|
|
100
|
+
iend = ista + ipl
|
|
101
|
+
t = time1[ista:iend]
|
|
102
|
+
s = signal1[ista:iend] - np.mean(signal1[ista:iend])
|
|
103
|
+
# FFT
|
|
104
|
+
ft = np.fft.rfft(s)
|
|
105
|
+
ft[0] = 1e-10
|
|
106
|
+
rn = len(ft)
|
|
107
|
+
sig_amp.append(abs(ft) / rn)
|
|
108
|
+
sig_rpm.append(np.mean(rpm1[ista:iend]))
|
|
109
|
+
if i == 0:
|
|
110
|
+
sig_freq = np.linspace(0.0, (rn-1)/(t[-1]-t[0]), rn)
|
|
111
|
+
# Campbell 2D-----------------------------------------------------------------------------
|
|
112
|
+
plt.figure(figsize=(8, 8*9/16))
|
|
113
|
+
rpms = np.array(sig_rpm).reshape(len(sig_rpm), 1)
|
|
114
|
+
freqs = sig_freq.reshape(1, len(sig_freq))
|
|
115
|
+
print("RPM min:" + str(np.min(rpms)))
|
|
116
|
+
print("RPM max:" + str(np.max(rpms)))
|
|
117
|
+
print("FREQ min:" + str(np.min(freqs)))
|
|
118
|
+
print("FREQ max:" + str(np.max(freqs)))
|
|
119
|
+
|
|
120
|
+
X = np.repeat(rpms, len(sig_freq), axis=1)
|
|
121
|
+
Y = np.repeat(freqs, len(sig_rpm), axis=0)
|
|
122
|
+
if log:
|
|
123
|
+
Z = np.log(sig_amp)
|
|
124
|
+
else:
|
|
125
|
+
Z = sig_amp
|
|
126
|
+
color = ["hsv_r", "twilight", "twilight_r", "tab20b_r", "brg", "gist_ncar", "terrain", "prism"]
|
|
127
|
+
if color_num is None:
|
|
128
|
+
color_num = 0
|
|
129
|
+
plt.pcolormesh(X, Y, Z, shading='nearest', cmap=color[color_num])
|
|
130
|
+
|
|
131
|
+
if order is not None:
|
|
132
|
+
rpm_max = np.max(rpms)
|
|
133
|
+
freq_max = np.max(freqs)
|
|
134
|
+
for odr in order:
|
|
135
|
+
x_odr = np.linspace(0.0, rpm_max, 2)
|
|
136
|
+
y_odr = np.linspace(0.0, odr*rpm_max/60, 2)
|
|
137
|
+
plt.plot(x_odr, y_odr, "-k", linewidth=0.2)
|
|
138
|
+
plt.annotate(str(odr), xy=(rpm_max, odr*rpm_max/60))
|
|
139
|
+
plt.xlim(0.0, np.max(rpms))
|
|
140
|
+
if max_freq:
|
|
141
|
+
plt.ylim(0, max_freq)
|
|
142
|
+
plt.ylabel("Frequency(Hz)")
|
|
143
|
+
plt.xlabel("RPM")
|
|
144
|
+
plt.tight_layout()
|
|
145
|
+
|
|
146
|
+
if fig_name is not None:
|
|
147
|
+
plt.title(fig_name)
|
|
148
|
+
print("ColorMap: " + color[color_num])
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def uniform(time):
|
|
152
|
+
tpre = time[0]
|
|
153
|
+
dt_pre = time[1] - time[0]
|
|
154
|
+
for t in time[1:-1]:
|
|
155
|
+
dt = t - tpre
|
|
156
|
+
if abs(dt - dt_pre) > 1e-6:
|
|
157
|
+
return False
|
|
158
|
+
tpre = t
|
|
159
|
+
dt_pre = dt
|
|
160
|
+
return True
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def interp_sampling(x, y):
|
|
164
|
+
xdiff = [x[n] - x[n - 1] for n in range(1, len(x))]
|
|
165
|
+
dx = np.average(xdiff)
|
|
166
|
+
delta_x = x[-1] - x[0]
|
|
167
|
+
q = np.linspace(0, x[-1], int(delta_x / dx))
|
|
168
|
+
f = interpolate.interp1d(x, y, fill_value="extrapolate")
|
|
169
|
+
return q, f(q)
|
|
170
|
+
|
|
171
|
+
def FFT(t, x, interp=False):
|
|
172
|
+
if uniform(t) is False:
|
|
173
|
+
if interp: t, x = interp_sampling(t, x)
|
|
174
|
+
else: print("Warning: Sampling time is not uniform")
|
|
175
|
+
dt = (t[-1] - t[0])/(len(t)-1) # dt from Original time data
|
|
176
|
+
if len(t) % 2 != 0:
|
|
177
|
+
t = np.delete(t, -1)
|
|
178
|
+
x = np.delete(x, -1)
|
|
179
|
+
N = len(t)
|
|
180
|
+
ft = np.fft.rfft(x)
|
|
181
|
+
rn = len(ft) # rfft의 n은 10000 --> 5001 즉 1이 추가됨 (즉, rn은 데이터 개수, rn-1 갭의 개수)
|
|
182
|
+
df = 1.0/(N*dt)
|
|
183
|
+
freq = np.linspace(0.0, df * (rn - 1), rn)
|
|
184
|
+
mag = abs(ft) / N
|
|
185
|
+
mag[1:-1] *= 2
|
|
186
|
+
pha = np.angle(ft)
|
|
187
|
+
mag[0] = 0.0
|
|
188
|
+
return [np.transpose(freq), mag, pha]
|
|
189
|
+
|
|
190
|
+
def FRF(t, x, y):
|
|
191
|
+
dt = (t[-1] - t[0]) / (len(t) - 1) # dt from Original time data
|
|
192
|
+
if len(t) % 2 != 0: t = np.delete(t, -1)
|
|
193
|
+
if len(x) % 2 != 0: x = np.delete(x, -1)
|
|
194
|
+
if len(y) % 2 != 0: y = np.delete(y, -1)
|
|
195
|
+
# FFT 계산
|
|
196
|
+
xf = np.fft.fft(x)
|
|
197
|
+
yf = np.fft.fft(y)
|
|
198
|
+
Hf = yf / xf
|
|
199
|
+
N = len(t)
|
|
200
|
+
rn = N // 2 + 1 # rfft의 n은 10000 --> 5001 즉 1이 추가됨 (즉, rn은 데이터 개수, rn-1 갭의 개수)
|
|
201
|
+
df = 1.0/(N*dt)
|
|
202
|
+
freq = np.linspace(0.0, df * (rn - 1), rn)
|
|
203
|
+
freq = freq[:rn-1]
|
|
204
|
+
mag = np.abs(Hf)[:rn-1]
|
|
205
|
+
pha = np.angle(Hf)[:rn-1]
|
|
206
|
+
# 전달함수 계산 (복소수)
|
|
207
|
+
return [np.transpose(freq), mag, pha, Hf]
|
|
208
|
+
|
|
209
|
+
def PSD(t, x, interp=False):
|
|
210
|
+
if uniform(t) is False:
|
|
211
|
+
if interp:
|
|
212
|
+
t, x = interp_sampling(t, x)
|
|
213
|
+
else:
|
|
214
|
+
print("Warning: Sampling time is not uniform")
|
|
215
|
+
t = np.array(t)
|
|
216
|
+
x = np.array(x)
|
|
217
|
+
if len(t) % 2 != 0:
|
|
218
|
+
t = np.delete(t, -1)
|
|
219
|
+
x = np.delete(x, -1)
|
|
220
|
+
ft = np.fft.rfft(x)
|
|
221
|
+
rn = len(ft)
|
|
222
|
+
df = float(1.0 / (t[-1] - t[1]))
|
|
223
|
+
freq = np.linspace(0.0, df * (rn - 1), rn)
|
|
224
|
+
mag = abs(ft) / rn
|
|
225
|
+
mag[0] = 0.0
|
|
226
|
+
psd = 2 * (mag / 2) ** 2 / df
|
|
227
|
+
pha = np.angle(ft)
|
|
228
|
+
return [freq, psd, pha]
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def FFT2PSD(freq, mag):
|
|
232
|
+
df = freq[2] - freq[1]
|
|
233
|
+
psd = 2 * (mag) ** 2 / df
|
|
234
|
+
return [freq, psd]
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def PSD2FFT(freq, psd):
|
|
238
|
+
df = freq[2] - freq[1]
|
|
239
|
+
rfft = 2.0 * np.sqrt(df * psd / 2.0)
|
|
240
|
+
return [freq, rfft]
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def FFT2SIG(freq, rfft):
|
|
244
|
+
nft = len(rfft)
|
|
245
|
+
phi = 2.0 * np.pi * np.random.rand(nft)
|
|
246
|
+
df = freq[2] - freq[1]
|
|
247
|
+
n = 2 * nft - 1
|
|
248
|
+
dt = 1 / (df * n)
|
|
249
|
+
t = np.zeros(n)
|
|
250
|
+
x = np.zeros(n)
|
|
251
|
+
for i in range(n):
|
|
252
|
+
t[i] = i * dt
|
|
253
|
+
x[i] = 0.0
|
|
254
|
+
for j in range(0, nft):
|
|
255
|
+
x[i] = x[i] + rfft[j] * np.sin(2 * np.pi * freq[j] * t[i] + phi[j])
|
|
256
|
+
return [t, x]
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def LPF(data, fc, fs, order=5, nodelay=False):
|
|
260
|
+
normal_cutoff = fc / (fs /2)
|
|
261
|
+
b, a = butter(order, normal_cutoff, btype='low', analog=False)
|
|
262
|
+
if nodelay:
|
|
263
|
+
y = filtfilt(b, a, data)
|
|
264
|
+
else:
|
|
265
|
+
y = lfilter(b, a, data)
|
|
266
|
+
return y
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def HPF(data, fc, fs, order=5, nodelay=False):
|
|
270
|
+
normal_cutoff = fc / (fs/2)
|
|
271
|
+
b, a = butter(order, normal_cutoff, btype='high', analog=False)
|
|
272
|
+
if nodelay:
|
|
273
|
+
y = filtfilt(b, a, data)
|
|
274
|
+
else:
|
|
275
|
+
y = lfilter(b, a, data)
|
|
276
|
+
return y
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def BPF(data, fc1, fc2, fs, order=5, nodelay=False):
|
|
280
|
+
normal_cutoff1 = fc1 / (fs/2)
|
|
281
|
+
normal_cutoff2 = fc2 / (fs/2)
|
|
282
|
+
b, a = butter(order, [normal_cutoff1, normal_cutoff2], btype='high', analog=False)
|
|
283
|
+
if nodelay:
|
|
284
|
+
y = filtfilt(b, a, data)
|
|
285
|
+
else:
|
|
286
|
+
y = lfilter(b, a, data)
|
|
287
|
+
return y
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def Interp(x, y, q, type='linear', fill_value=None):
|
|
291
|
+
if fill_value:
|
|
292
|
+
f = interpolate.interp1d(x, y, kind=type, bounds_error=False, fill_value=0.0)
|
|
293
|
+
else:
|
|
294
|
+
f = interpolate.interp1d(x, y, kind=type, fill_value="extrapolate")
|
|
295
|
+
return f(q)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def step5(x, x1, y1, x2, y2):
|
|
299
|
+
if x < x1:
|
|
300
|
+
y = y1
|
|
301
|
+
elif x > x2:
|
|
302
|
+
y = y2
|
|
303
|
+
else:
|
|
304
|
+
alpha = (x - x1) / (x2 - x1)
|
|
305
|
+
y = y1 + (y2 - y1) * alpha**3 * (10 - 15 * alpha + 6 * alpha**2)
|
|
306
|
+
return y
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def step3(x, x1, y1, x2, y2):
|
|
310
|
+
if x < x1:
|
|
311
|
+
y = y1
|
|
312
|
+
elif x > x2:
|
|
313
|
+
y = y2
|
|
314
|
+
else:
|
|
315
|
+
alpha = (x - x1) / (x2 - x1)
|
|
316
|
+
y = y1 + (y2 - y1) * alpha**2 * (3 - 2 * alpha)
|
|
317
|
+
return y
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def step_sin(x, x1, y1, x2, y2):
|
|
321
|
+
if x < x1:
|
|
322
|
+
y = y1
|
|
323
|
+
elif x > x2:
|
|
324
|
+
y = y2
|
|
325
|
+
else:
|
|
326
|
+
alpha = (x - x1) / (x2 - x1)
|
|
327
|
+
y = y1 + 0.5 * (1 - cos(alpha * pi)) * (y2 - y1)
|
|
328
|
+
return y
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def AmatZXZ(a, b, c):
|
|
332
|
+
s1, c1 = sin(a), cos(a)
|
|
333
|
+
s2, c2 = sin(b), cos(b)
|
|
334
|
+
s3, c3 = sin(c), cos(c)
|
|
335
|
+
|
|
336
|
+
A = np.array([
|
|
337
|
+
[c1 * c3 - s1 * c2 * s3, -c1 * s3 - s1 * c2 * c3, s1 * s2],
|
|
338
|
+
[s1 * c3 + c1 * c2 * s3, -s1 * s3 + c1 * c2 * c3, -c1 * s2],
|
|
339
|
+
[s2 * s3, s2 * c3, c2]
|
|
340
|
+
])
|
|
341
|
+
|
|
342
|
+
return A
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def OrientZXZ(A):
|
|
346
|
+
a1 = 0.0
|
|
347
|
+
a2 = np.arccos(A[2, 2])
|
|
348
|
+
a3 = 0.0
|
|
349
|
+
|
|
350
|
+
if a2 == 0.0: # c2=1
|
|
351
|
+
a1 = np.tan(-A[0, 2] / A[1, 2])
|
|
352
|
+
a3 = np.tan(A[2, 0] / A[2, 1])
|
|
353
|
+
else:
|
|
354
|
+
a1 = np.arcsin(A[0, 2] / np.sin(a2))
|
|
355
|
+
a3 = np.arcsin(A[2, 0] / np.sin(a2))
|
|
356
|
+
|
|
357
|
+
return [a1, a2, a3]
|