Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triangle wave shaped array in Python

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 mind:

enter image description here

like image 857
8765674 Avatar asked Sep 08 '12 16:09

8765674


People also ask

How to generate a triangular wave in python?

The simplest way to generate a triangle wave is by using signal. sawtooth. Notice that signal. sawtooth(phi, width) accepts two arguments.

How do you make a triangular waveform?

➢ Triangular waveform can also be generated by integrating square wave from an astable multivibrator. ➢ The cycle from the square wave to the next operational amplifier repeats and generates a triangular waveform. ➢ Triangular waveform can also be generated by integrating square wave from an astable multivibrator.

How do you make a sawtooth wave in Python?

Approach: Import required module. Create a sample rate. The NumPy linspace function is a tool in Python for creating numeric sequences that return evenly spaced numbers over a specified interval.


1 Answers

The simplest way to generate a triangle wave is by using signal.sawtooth. Notice that signal.sawtooth(phi, width) accepts two arguments. The first argument is the phase, the next argument specifies the symmetry. width = 1 gives a right-sided sawtooth, width = 0 gives a left-sided sawtooth and width = 0.5 gives a symmetric triangle. Enjoy!

from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500)
triangle = signal.sawtooth(2 * np.pi * 5 * t, 0.5)
plt.plot(t, triangle)
like image 156
py_man Avatar answered Sep 20 '22 06:09

py_man