Triangle wave shaped array in Python

前端 未结 6 1269
生来不讨喜
生来不讨喜 2021-02-08 04:22

What is the most efficient way to produce an array of 100 numbers that form the shape of the triangle wave below, with a max/min amplitude of 0.5?

Triangle waveform in m

6条回答
  •  耶瑟儿~
    2021-02-08 04:33

    To use numpy:

    def triangle2(length, amplitude):
        section = length // 4
        x = np.linspace(0, amplitude, section+1)
        mx = -x
        return np.r_[x, x[-2::-1], mx[1:], mx[-2:0:-1]]
    

提交回复
热议问题