How to plot sine wave in Python with sudden amplitude change?

后端 未结 3 1250
余生分开走
余生分开走 2021-01-27 20:32

Posted: 7/4/2020

I was wondering if anyone knows how to plot a sine wave with let\'s say amplitude of 0.1 as a start and then continuing on as usual. Until at one point,

3条回答
  •  难免孤独
    2021-01-27 20:49

    You could plot a piece-wise sin function where the second part defines the surge happening and you can change the amplitude there.

    For instance:

    import numpy as np
    import matplotlib.pyplot as plt
    import math
    
    surge_point = 50
    amplitudeAfterSurge = 4
    T = 50
    x_normal = np.linspace(0, surge_point, 1000)
    x_surge = np.linspace(surge_point, 150, 1000)
    
    y_normal = [math.sin(2*math.pi*i/T) for i in x_normal] # first part of the function
    
    # second part ,note `amplitudeAfterSurge` multiplying the function
    y_surge = [amplitudeAfterSurge * math.sin(2*math.pi*i/T) for i in x_surge] 
    
    plt.plot(x_normal, y_normal , 'r')
    plt.plot(x_surge, y_surge , 'r')
    
    plt.show()
    

    And you will get:

提交回复
热议问题