[英]How to scale and crop a sine wave in python and bumpy
我正在嘗試生成一個符合我對幅度、相數以及開始和結束值的要求的正弦波。
我可以准確地控制幅度和相位數,但我不知道如何准確地設置開始值和結束值。 我相信有一種數學方法可以解決這個問題,但我不知道從哪里開始。
我還嘗試通過在開始之前和結束之后切割y
的元素來實際“裁剪”波浪,但除了不是很優雅之外,我還遇到了准確性問題。
謝謝你的幫助
import numpy as np
import math
import matplotlib.pylab as plt
min=0
max=180
f = 2
start = 45
stop = 60
t = np.linspace(0, 2*np.pi, 1000)
y = ((max+min)/2) + (((max-min)/2) * np.sin(f*t + 100)) # "100" set manually by experimenting
actual_min, actual_max = np.min(y), np.max(y)
actual_start, actual_stop = y[0], y[-1]
print(f"min: {actual_min}, max: {actual_max}")
print(f"start: {actual_start}, stop: {actual_stop}")
plt.plot(t, y)
min: 0.00019246157938823671, max: 179.99994783571566
start: 44.42709230012171, stop: 44.42709230012167
您需要arcsin
來獲取給定y
的t
:
import numpy as np
import matplotlib.pylab as plt
min = 0
max = 180
f = 2
start = 45
stop = 60
a = (max + min) / 2
b = (max - min) / 2
t0 = np.arcsin((start - a) / b) / f
t1 = np.arcsin((stop - a) / b) / f + 2 * np.pi
t = np.linspace(t0, t1, 1000)
y = a + b * np.sin(f * t)
assert np.allclose([start, stop], [y[0], y[-1]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.