Custom implementation of reflex3s (fft, working, with plotting)

This commit is contained in:
Edgar P. Burkhart 2022-04-01 15:50:18 +02:00
parent 0cb87c5032
commit 4f7780c6ff
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 99 additions and 33 deletions

View file

@ -5,43 +5,41 @@ from scipy import optimize as opti
def reflex3s(eta, x, h, f):
#_welch = [sgl.welch(z, f) for z in eta]
#eta_psd = np.stack([_w[1] for _w in _welch])
#f_psd = _welch[0][0]
eta_psd = np.stack([fft.rfft(z) for z in eta])
f_psd = fft.rfftfreq(eta.shape[1])
f_psd = fft.rfftfreq(eta.shape[1], 1 / f)
eta_amp = np.abs(eta_psd) / eta_psd.shape[1]
eta_amp = np.abs(eta_psd) / (f_psd.size - 1)
eta_phase = np.angle(eta_psd)
g = 9.81
k = np.asarray([opti.root_scalar(
f=lambda k: k * np.tanh(k) - (2 * np.pi * f) ** 2 / g * h,
fprime=lambda k: np.tanh(k) + k * (1 - np.tanh(k)) ** 2,
x0=0.2,
).root for f in f_psd])
dx = np.roll(x, 1) - x
dphi = np.roll(eta_phase, 1, axis=0) - eta_phase
ai = np.sqrt(
eta_amp**2
+ np.roll(eta_amp, 1, axis=0)**2
- 2
* eta_amp
* np.roll(eta_amp, 1, axis=0)
* np.cos(dphi - k * dx[:, None])
/ (2 * np.abs(np.sin(k * dx[:, None])))
)
ar = np.sqrt(
eta_amp**2
+ np.roll(eta_amp, 1, axis=0)**2
- 2
* eta_amp
* np.roll(eta_amp, 1, axis=0)
* np.cos(dphi + k * dx[:, None])
/ (2 * np.abs(np.sin(k * dx[:, None])))
k = np.asarray(
[
opti.root_scalar(
f=lambda k: k * np.tanh(k) - (2 * np.pi * f) ** 2 / g * h,
fprime=lambda k: np.tanh(k) + k * (1 - np.tanh(k) ** 2),
x0=0.5,
).root
/ h
for f in f_psd
]
)
return f_psd, ar / ai
s1 = 1 + np.exp(2j * k * (x[1] - x[0])) + np.exp(2j * k * (x[2] - x[0]))
s2 = 1 + np.exp(-2j * k * (x[1] - x[0])) + np.exp(-2j * k * (x[2] - x[0]))
s12 = 3
s3 = (
eta_amp[0] * np.exp(-1j * (eta_phase[0]))
+ eta_amp[1] * np.exp(-1j * (eta_phase[1] - k * (x[1] - x[0])))
+ eta_amp[2] * np.exp(-1j * (eta_phase[2] - k * (x[2] - x[0])))
)
s4 = (
eta_amp[0] * np.exp(-1j * (eta_phase[0]))
+ eta_amp[1] * np.exp(-1j * (eta_phase[1] + k * (x[1] - x[0])))
+ eta_amp[2] * np.exp(-1j * (eta_phase[2] + k * (x[2] - x[0])))
)
s5 = s1 * s2 - s12**2
ai = np.abs((s2 * s3 - s12 * s4) / s5)
ar = np.abs((s1 * s4 - s12 * s3) / s5)
return f_psd, ai, ar