Home > Mobile >  How to scale and crop a sine wave in python and numpy
How to scale and crop a sine wave in python and numpy

Time:07-26

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

enter image description here

CodePudding user response:

You need enter image description here

  • Related