简体   繁体   中英

How to scale and crop a sine wave in python and bumpy

I'm trying to generate a sine wave that matches my requirements for amplitude, number of phases and start and end values.

I can accurately control amplitude and number of phases, but I don't know how to set the start and end values accurately. I believe there is a mathematical approach to this, but I don't know where to start.

I also tried actually "cropping" the wave by cutting elements of y before start and after end, but beside being not very elegant, I encountered issues with accuracy.

Thanks for any help

    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

在此处输入图像描述

You need arcsin to get the t for a given y :

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]])

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM