examples/UDP Function Generator/README.md
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.
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:
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
./udp_function_generator [-p port] [-i interval] [-n num_functions] [-v]
Command-line options:
-p <port>: UDP port (default 9000).-i <interval>: transmission interval in milliseconds (default 1.0 ms).-n <num_functions>: number of waveforms to generate (default 1).-v: enable verbose output (prints generated data to the console).Example:
./udp_function_generator -p 9000 -i 5 -n 3 -v
localhost.-p option (default 9000).When you run the program, it prompts you to configure the waveforms:
sine, triangle, saw, or square).The program validates your input and warns about aliasing or distortion if the frequency is too high relative to the sampling rate.
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.
localhost.Command:
./udp_function_generator -p 9000 -i 1 -n 1 -v
Configuration:
sine.10 Hz.0 radians.Command:
./udp_function_generator -p 8000 -i 5 -n 3 -v
Configuration:
triangle, 5 Hz, 0 radians.saw, 20 Hz, 1.5 radians.square, 50 Hz, 0 radians.Visualization: Serial Studio shows all three waveforms in real time, with a 5 ms sampling interval.
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.
That keeps waveform visualization smooth.
For more advanced use cases, check the source code and explore the customizable options. Pull requests with improvements are welcome.