wave_forms
A no_std Rust library for generating waveform data. This library provides standard-library-free implementations of waveforms.
Features
- No standard library (
#![no_std]) - suitable for embedded systems and constrained environments - Sine wave generation - using Taylor series approximation
- Square wave generation - alternating high/low values
- Triangle wave generation - linear ramp waveform
- Zero dependencies - pure Rust implementation
Generating a Sine Wave
use generate_sine_wave;
let mut buffer = ;
generate_sine_wave;
// buffer now contains 1000 samples of a 440Hz sine wave
Generating a Square Wave
use generate_square_wave;
let mut buffer = ;
generate_square_wave;
// buffer now contains 1000 samples of a square wave
Generating a Triangle Wave
use generate_triangle_wave;
let mut buffer = ;
generate_triangle_wave;
// buffer now contains 1000 samples of a triangle wave
Single Sample Calculation
For more control, you can calculate individual samples:
use sin_val;
let sample = sin_val;
// Calculate the first sample of a 440Hz sine wave
API Reference
Functions
generate_sine_wave(output: &mut [f64], frequency: f64, sample_rate: f64, amplitude: f64)
Generates a sine wave into the provided mutable slice.
output: Mutable slice to fill with waveform samplesfrequency: Frequency in Hzsample_rate: Sample rate in Hz (e.g., 44100.0)amplitude: Amplitude of the waveform
generate_square_wave(output: &mut [f64], amplitude: f64)
Generates a square wave into the provided mutable slice.
output: Mutable slice to fill with waveform samplesamplitude: Amplitude of the waveform
generate_triangle_wave(output: &mut [f64], amplitude: f64)
Generates a triangle wave into the provided mutable slice.
output: Mutable slice to fill with waveform samplesamplitude: Amplitude of the waveform
sin_val(frequency: f64, i: usize, sample_rate: f64, amplitude: f64) -> f64
Calculates a single sine wave sample value.
frequency: Frequency in Hzi: Sample indexsample_rate: Sample rate in Hzamplitude: Amplitude of the waveform- Returns: The sample value at index
i
Implementation Details
Sine Wave
The sine wave implementation uses a Taylor series approximation:
sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + x⁹/9! - x¹¹/11! + x¹³/13!
The input is normalized to the range [-π, π] for optimal convergence of the series.
License
MIT