Summary
This Python script generates and plays a waveform (sine or square) of a specified frequency, duration, and amplitude using the
pyaudioandnumpylibraries.
I’m excited to share a new Python tool I’ve developed, SignalGenerator. This script is specifically designed with embedded systems in mind, providing a simple yet powerful way to generate and test audio waveforms. While it works on any system with PyAudio, its real value shines when used with hardware like the Raspberry Pi 4’s onboard digital-to-analog converter (DAC) for quick and easy signal testing.
Why SignalGenerator for Embedded Systems?
When working with embedded systems, you often need to generate test signals to verify hardware functionality, troubleshoot audio circuits, or calibrate sensors. Manually setting up a signal generator can be cumbersome. SignalGenerator simplifies this process by allowing you to create and output precise sine, square, or frequency sweep signals directly from the command line, turning your Raspberry Pi into a versatile signal source. This is particularly useful for engineers and hobbyists prototyping audio applications or testing the performance of their DAC and related circuitry.
Features and Usage
SignalGenerator is built on Python and uses NumPy for waveform generation and PyAudio for output. It’s lightweight and easy to install.
To get started, simply install the required libraries:
pip install numpy pyaudio argparse
The script’s command-line interface makes it easy to specify the exact signal you need.
Key Features:
- Waveform Types: Generate sine waves, square waves, or a frequency sweep.
- Customizable Parameters: Easily set the duration, frequency, and amplitude of your signal.
- Frequency Sweep Mode: Test a range of frequencies to analyze the frequency response of your hardware. This mode is a game-changer for evaluating the performance of filters, amplifiers, or the DAC itself across different frequencies.
Example Usage on a Raspberry Pi:
- Test your DAC with a 440 Hz sine wave:
python siggen.py sine -f 440
- Check a high-frequency filter’s performance with a 10-second sweep:
python siggen.py sweep -f 1000 -e 20000 -s 500
- Send a 1 kHz square wave to a connected circuit:
python siggen.py square -f 1000
A Note on the Raspberry Pi DAC
The Raspberry Pi 4 has a built-in DAC that provides audio output through its 3.5mm jack. This DAC can be used as the output “sink” for SignalGenerator. By connecting a simple audio breakout cable to the Pi’s audio jack, you can easily probe the generated signal with an oscilloscope or feed it into another circuit. This direct, no-hassle approach makes SignalGenerator an invaluable tool for on-the-fly testing and development.
I encourage you to try out SignalGenerator on your next embedded systems project. You can find the full details and source code on the GitHub repository. I hope this tool helps streamline your workflow and makes testing your hardware a breeze!
Samples

