Python Forum
How can one generate triangular and sawtooth waves in python?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can one generate triangular and sawtooth waves in python?
#5
You could use the module scipy.signal

Sawtooth:
Output:
Examples -------- A 5 Hz waveform sampled at 500 Hz for 1 second: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> t = np.linspace(0, 1, 500) >>> plt.plot(t, signal.sawtooth(2 * np.pi * 5 * t))
Square:
Output:
Examples -------- A 5 Hz waveform sampled at 500 Hz for 1 second: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> t = np.linspace(0, 1, 500, endpoint=False) >>> plt.plot(t, signal.square(2 * np.pi * 5 * t)) >>> plt.ylim(-2, 2) A pulse-width modulated sine wave: >>> plt.figure() >>> sig = np.sin(2 * np.pi * t) >>> pwm = signal.square(2 * np.pi * 30 * t, duty=(sig + 1)/2) >>> plt.subplot(2, 1, 1) >>> plt.plot(t, sig) >>> plt.subplot(2, 1, 2) >>> plt.plot(t, pwm) >>> plt.ylim(-1.5, 1.5)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How can one generate triangular and sawtooth waves in python? - by DeaD_EyE - Sep-25-2019, 05:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 827 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  Generate Python variables dynamically Pedroski55 5 2,962 Mar-04-2022, 10:13 PM
Last Post: deanhystad
  Generate RPM package from a Python + Kivy application for an "offline" installation pruvosim 2 2,270 Jun-04-2020, 12:16 PM
Last Post: pruvosim
  generate UML design from python code Phaze90 2 2,535 Apr-13-2020, 11:36 AM
Last Post: Phaze90
  Generate a report in Python qureshi 2 3,680 Aug-24-2019, 04:50 AM
Last Post: ndc85430
  I need help using Python to generate usernames and passwords with excel documents Jannejannesson 3 4,071 May-08-2019, 02:30 PM
Last Post: Jannejannesson
  How to only extract upper or lower triangular matrix into tabular form SriRajesh 3 25,867 Jan-04-2019, 01:21 AM
Last Post: scidam
  How to generate calendar with 2 formats in python luizcrf 1 2,719 Nov-01-2018, 06:46 AM
Last Post: Larz60+
  How to generate more MP3 files at the same time in Amazon Polly using Python code? makiwara 2 3,813 Jul-02-2018, 08:43 PM
Last Post: makiwara
  how to generate sha256 hash for each line of my txt file | using python version 3.6.4 rajtekken5 2 9,148 Feb-11-2018, 01:41 PM
Last Post: rajtekken5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020