UDP Function Generator
Generates and transmits multiple real-time waveforms (sine, triangle, sawtooth, square) over UDP for testing and visualizing signal behavior.
What you'll learn: a UDP source and Quick Plot, using a program that sends sine, triangle, sawtooth and square waveforms as CSV.
Prerequisites: the UDP function generator program included in the folder. Serial Studio's free features are enough.
Overview
This project shows how to use the UDP function generator program to generate and transmit multiple real-time waveforms (sine, triangle, sawtooth, and square) over UDP. The program feeds CSV-formatted data to Serial Studio, so you can visualize the waveforms in real time.
With Serial Studio, you can use Quick Plot to visualize data coming in over UDP. It's a handy way to test and analyze waveform generation.
Function generator background
A function generator produces electrical waveforms for testing circuits, analyzing systems, and generating real-time signals. This program simulates one but transmits its output over a UDP socket instead of generating physical signals.
The waveforms are useful for:
- Testing UDP-based communication.
- Stress-testing Serial Studio to find bugs.
- Visualizing signal behavior in applications.
- Learning and experimenting with waveform generation and signal processing.
Program features
- Waveform types. Sine, triangular, sawtooth, and square.
- Customizable settings:
- Number of waveforms to generate.
- Frequency, phase, and type of each waveform.
- Adjustable transmission interval.
- Verbose output. Print real-time data to the console (optional).
- Aliasing protection. Warns if the frequency is too high for smooth reconstruction.
- Source code. See
udp_function_generator.cfor the full list of customizable options.
Getting started
Requirements
- GCC or any compatible C compiler.
- Linux, macOS, or Windows (the program uses Winsock natively, so MinGW works without WSL).
- Serial Studio for real-time visualization.
1. Compile the program
On UNIX systems:
gcc -o udp_function_generator udp_function_generator.c -lm
On Windows:
gcc -o udp_function_generator.exe udp_function_generator.c -lws2_32 -lm
2. Run the program
./udp_function_generator [-p port] [-i interval] [-n num_functions] [-v]
Command-line options:
-p <port>: UDP port (default9000).-i <interval>: transmission interval in milliseconds (default1.0ms).-n <num_functions>: number of waveforms to generate (default1).-v: enable verbose output (prints generated data to the console).
Example:
./udp_function_generator -p 9000 -i 5 -n 3 -v
3. Visualize data in Serial Studio
- Download and install Serial Studio. Visit the official site to download and install.
- Configure Serial Studio.
- Set Connection to Network Socket.
- Pick UDP as the socket type.
- Set Remote Address to
localhost. - Set both the local and remote ports to match the program's
-poption (default9000).
- Select Quick Plot. In the Setup pane, select the Quick Plot option. It plots numeric values transmitted via UDP in real time.
- Run the program. Waveforms will show up in Serial Studio's real-time plot.
Step by step
Waveform configuration
When you run the program, it prompts you to configure the waveforms:
- Enter the waveform type (
sine,triangle,saw, orsquare). - Enter the frequency in Hertz.
- Enter the phase in radians.
The program validates your input and warns about aliasing or distortion if the frequency is too high relative to the sampling rate.
Data transmission
The program formats the waveform data as a comma-separated string and transmits it over UDP at the interval you specified. You can view this data in Serial Studio or analyze it with any UDP client.
Troubleshooting
- No waveforms in Serial Studio. Make sure the UDP port matches between the program and Serial Studio, and that the host is set to
localhost. - Distorted waveforms. Lower the frequency if it's approaching the Nyquist limit. The program issues warnings for frequencies near this threshold. If the frequencies look fine, please open a bug report.
- No data output. Make sure the program is running and the network configuration is correct.
Examples
Example 1: single sine wave
Command:
./udp_function_generator -p 9000 -i 1 -n 1 -v
Configuration:
- Waveform type:
sine. - Frequency:
10 Hz. - Phase:
0 radians.
Example 2: multiple waveforms
Command:
./udp_function_generator -p 8000 -i 5 -n 3 -v
Configuration:
- Waveform 1:
triangle,5 Hz,0 radians. - Waveform 2:
saw,20 Hz,1.5 radians. - Waveform 3:
square,50 Hz,0 radians.
Visualization: Serial Studio shows all three waveforms in real time, with a 5 ms sampling interval.
Example 3: high-frequency warning
If the frequency exceeds 80% of the Nyquist rate, the program prints a warning:
Warning: Frequency 450.00 Hz approaches the Nyquist rate (500.00 Hz). Consider reducing it below 400.00 Hz to ensure smooth waveform reconstruction.