Foldback and Wavefolding — User Guide

Nonlinear distortion through signal folding: creates harmonic complexity by mirroring audio signals beyond threshold boundaries.

Author: Based on Praat AudioTools by Shai Cohen Version: 2025 Process: Vectorized signal folding for efficient processing
Contents:

What this does

This script implements foldback and wavefolding distortion — a sophisticated nonlinear processing technique that creates harmonic complexity by mirroring audio signals when they exceed defined thresholds. The process: (1) Input gain: Amplify signal to drive into nonlinear region, (2) Threshold detection: Identify signal peaks beyond fold boundaries, (3) Signal folding: Mirror excess signal back toward zero, (4) Iterative processing: Apply multiple folds for complex harmonics, (5) Output conditioning: Apply smoothing, gain staging, and DC removal. Result: rich harmonic distortion that adds complexity and character without traditional clipping artifacts.

Key Features:

What is foldback distortion? Traditional distortion: clipping (hard/soft) creates harmonically rich but often harsh results. Foldback distortion: when signal exceeds threshold, it's "folded" back toward zero rather than clipped. Advantages: (1) Musical harmonics: Creates complex, often more musical distortion spectra, (2) No hard clipping: Avoids harsh digital artifacts, (3) Controllable complexity: Multiple folds create increasingly complex harmonics, (4) Analog modeling: Mimics behavior of certain analog circuits and tape saturation, (5) Creative possibilities: Can create everything from subtle warmth to extreme sonic destruction. Use cases: Music production (harmonic enhancement), sound design (complex textures), synthesizer design (wave shaping), guitar processing (amp simulation), experimental music (sonic manipulation).

Technical Implementation: (1) Input conditioning: Apply gain and calculate asymmetric thresholds. (2) Foldback algorithm: Vectorized Formula operations for efficient processing. (3) Bipolar folding: Separate positive/negative threshold handling. (4) Iterative processing: Multiple passes through fold algorithm. (5) Output conditioning: Smoothing, gain compensation, DC removal, peak protection. Key insight: Signal mirroring at thresholds creates harmonically rich distortion different from traditional clipping.

Quick start

  1. In Praat, select exactly one Sound object.
  2. Run script…foldback_wavefolding.praat.
  3. Choose preset: Soft Fold for subtle effects, Hard Fold for aggressive distortion.
  4. Adjust input gain to control distortion amount (higher = more folding).
  5. Modify threshold to set foldback point (lower = earlier folding).
  6. Click OK — processes audio, creates "originalname_fold" output.
Quick tip: Start with Soft Fold preset for subtle harmonic enhancement. Use Hard Fold for aggressive distortion. Try Multi-Fold for complex harmonic spectra. For vocals, use lower input gain (3-6dB) and higher threshold (0.6-0.8). For drums, use higher input gain (8-15dB) and moderate threshold (0.4-0.6). Processing is very fast due to vectorized implementation.
Important: GAIN STAGING CRITICAL — high input gain drives folding, output gain compensates level. Extreme settings can create very distorted results. Multiple iterations create complex but potentially chaotic spectra. Asymmetry can create even/odd harmonic bias. Always monitor output levels — folding can create unexpected peaks. DC offset removal recommended for clean results. Vectorized processing handles all samples efficiently but may create different character than sample-by-sample processing.

Foldback Distortion Theory

Basic Foldback Mathematics

Core Folding Algorithm

Foldback function definition:

Basic foldback (bipolar): if x > threshold: y = threshold - (x - threshold) × depth if x < -threshold: y = -threshold + (|x| - threshold) × depth else: y = x Where: x = input sample threshold = fold boundary (0-1) depth = fold intensity (0-1) Visual representation: Input: ────┬──── (exceeds threshold) Output: ──┐ │ ┌── (folded back inward) Creates "mirror" effect at boundaries

Harmonic Generation

Why folding creates harmonics:

Sine wave input: sin(2πft) After single fold at threshold T: Contains harmonics at 3f, 5f, 7f... (odd harmonics) After multiple folds: Adds even harmonics (2f, 4f, 6f...) Creates complex harmonic structure Compared to clipping: Clipping: abrupt harmonic generation Folding: smoother harmonic roll-off More musical, less harsh Mathematical basis: Folding creates piecewise-linear transfer function Each linear segment contributes different harmonics

Bipolar vs Unipolar Folding

Symmetric vs Asymmetric Processing

Harmonic characteristics:

Bipolar folding (symmetric): Positive and negative treated equally Harmonic spectrum: Odd harmonics dominant Character: Balanced, tube-like Transfer: ──┐ │ ┌── (symmetric) Unipolar folding (asymmetric): Positive and negative can differ Harmonic spectrum: Even + odd harmonics Character: Transformer-like, rich Transfer: ──┐ │ ╰── (asymmetric) Even vs Odd harmonics: Odd harmonics (3f, 5f, 7f): "Hollow" character Even harmonics (2f, 4f, 6f): "Warm" character Complex mixes: "Rich" or "Aggressive"

Iterative Folding

Multiple Fold Stages

Cascaded folding effects:

Single iteration (fold_iterations = 1): - Basic fold at threshold - Simple harmonic structure - Clean, predictable results Double iteration (fold_iterations = 2): - First fold creates harmonics - Second fold folds already-folded signal - More complex harmonics - Richer, denser sound Triple+ iteration (fold_iterations ≥ 3): - Multiple folding stages - Very complex harmonic structure - Can become chaotic/noisy - Experimental textures Progressive complexity: Iteration 1: Fundamental + few harmonics Iteration 2: Denser harmonic spectrum Iteration 3: Near-noise texture

Complete Processing Pipeline

INPUT: Sound object STEP 1: PARAMETER CONVERSION - Convert dB gains to linear: 10^(dB/20) - Calculate asymmetric thresholds: if asymmetry ≥ 0: threshold_pos = threshold×(1-asymmetry) else: threshold_neg = threshold×(1+asymmetry) STEP 2: INPUT CONDITIONING - Copy original sound - Apply input gain (vectorized multiplication) STEP 3: FOLDBACK PROCESSING LOOP FOR iteration from 1 to fold_iterations: IF bipolar_folding: # Positive side folding Formula: "if self > threshold_pos then threshold_pos - (self - threshold_pos)×fold_depth else self fi" # Negative side folding Formula: "if self < -threshold_neg then -threshold_neg + (abs(self) - threshold_neg)×fold_depth else self fi" ELSE (unipolar): Formula: "if abs(self) > threshold then (if self > 0 then threshold - (self - threshold)×fold_depth else -threshold + (abs(self) - threshold)×fold_depth fi) else self fi" STEP 4: OUTPUT CONDITIONING - Apply smoothing if smoothing > 0 (soft clipping) - Apply output gain compensation - Remove DC offset if enabled - Check for clipping, normalize if needed OUTPUT: Processed sound with foldback distortion

Preset Configurations

Preset 1: Custom

⚙️ Full Parameter Control

Threshold: User specified (default: 0.5)

Input Gain: User specified (default: 0.0 dB)

Fold Depth: User specified (default: 1.0)

Character: Fully customizable foldback distortion

Best for: Experimental work, specific harmonic targeting

Preset 2: Soft Fold (Subtle)

🎵 Gentle Harmonic Enhancement

Threshold: 0.7 (high, gentle folding)

Input Gain: 3.0 dB (light drive)

Fold Depth: 0.6 (moderate folding)

Smoothing: 0.3 (smooth character)

Character: Warm, subtle, musical enhancement

Best for: Vocals, acoustic instruments, subtle saturation

Preset 3: Hard Fold (Aggressive)

🔥 Intense Harmonic Distortion

Threshold: 0.3 (low, aggressive folding)

Input Gain: 12.0 dB (heavy drive)

Fold Depth: 1.0 (full folding)

Smoothing: 0.0 (raw, edgy)

Character: Aggressive, bright, intense distortion

Best for: Drums, synths, electric guitars, special FX

Preset 4: Bipolar Fold

⚖️ Symmetric Harmonic Generation

Threshold: 0.5 (balanced)

Input Gain: 6.0 dB (moderate drive)

Fold Iterations: 2 (complex harmonics)

Bipolar: Enabled (symmetric)

Character: Rich, complex, odd-harmonic dominant

Best for: Synthetic sounds, complex textures

Preset 5: Asymmetric Fold

🎛️ Even + Odd Harmonic Mix

Threshold: 0.6 (moderate-high)

Input Gain: 8.0 dB (medium drive)

Asymmetry: 0.6 (strong positive bias)

Bipolar: Disabled (asymmetric processing)

Character: Warm, transformer-like, even harmonics

Best for: Analog simulation, warm distortion

Preset 6: Multi-Fold (Harmonics)

🎶 Complex Harmonic Structures

Threshold: 0.4 (moderate-low)

Input Gain: 10.0 dB (strong drive)

Fold Iterations: 3 (multiple stages)

Fold Depth: 1.0 (full folding)

Character: Very complex, dense harmonic texture

Best for: Experimental sounds, maximal distortion

Preset 7: Tape Saturation Style

📼 Analog Tape Simulation

Threshold: 0.65 (gentle folding)

Input Gain: 4.0 dB (light drive)

Fold Depth: 0.5 (soft folding)

Smoothing: 0.5 (very smooth)

Character: Warm, smooth, vintage tape-like

Best for: Mix bus, vocals, overall warming

Preset 8: Digital Crush

💥 Extreme Digital Distortion

Threshold: 0.25 (very aggressive)

Input Gain: 15.0 dB (extreme drive)

Fold Iterations: 2 (complex)

Bipolar: Disabled (asymmetric)

Character: Harsh, digital, bit-crush style

Best for: Special effects, aggressive textures

Preset 9: Oscillating Fold

🔄 Dynamic Harmonic Movement

Threshold: 0.55 (balanced)

Input Gain: 7.0 dB (medium drive)

Asymmetry: -0.3 (negative bias)

Fold Iterations: 2 (complex)

Character: Dynamic, moving, complex harmonics

Best for: Evolving textures, synthetic sounds

Preset Transfer Function Visualization

Input/Output Relationships:

Soft Fold: ────╮···╭─── (gentle curves)
Hard Fold: ──┐ │ ┌── (sharp folds)
Bipolar Fold: ──┐ │ ┌── (symmetric)
Asymmetric Fold: ──┐ │ ╰── (uneven sides)
Multi-Fold: ─╮│╭│╮│╭─ (multiple folds)
Tape Style: ───╮···╭─── (smooth saturation)
Digital Crush: ─┐ ┌┐ ┌┐ ┌─ (aggressive folds)
Oscillating: ─╮│╰│╮│╭─ (dynamic asymmetry)

Vertical lines represent fold thresholds

Folding Algorithms

Bipolar Folding Algorithm

⚖️ Symmetric Signal Processing

Principle: Equal treatment of positive and negative signal halves

Implementation: Separate positive and negative threshold checks

Harmonic Result: Odd harmonics dominant, balanced spectrum

Character: Clean, tube-amp like, musical

Bipolar Algorithm Details

Positive side folding: IF sample > threshold_pos THEN output = threshold_pos - (sample - threshold_pos) × fold_depth Negative side folding: IF sample < -threshold_neg THEN output = -threshold_neg + (|sample| - threshold_neg) × fold_depth Otherwise: output = sample (no folding) Key features: - Separate positive/negative thresholds allow asymmetry - fold_depth controls how far back signal is folded - 1.0 = fold back to opposite threshold - <1.0 = partial folding (softer effect)

Unipolar Folding Algorithm

🎛️ Asymmetric Signal Processing

Principle: Combined positive/negative processing

Implementation: Absolute value threshold checking

Harmonic Result: Mixed even and odd harmonics

Character: Warmer, transformer-like, rich

Unipolar Algorithm Details

Absolute value check: IF |sample| > threshold THEN IF sample > 0 THEN output = threshold - (sample - threshold) × fold_depth ELSE output = -threshold + (|sample| - threshold) × fold_depth ENDIF ELSE output = sample Key features: - Single threshold for both polarities - Creates different harmonic structure - Often produces warmer, more complex results - Can be combined with asymmetry parameter

Smoothing Algorithm

🔄 Soft Clipping Integration

Principle: Apply soft clipping to fold transitions

Implementation: Conditional soft saturation

Result: Smoother transitions, reduced harshness

Character: More analog-like, less digital

Smoothing Implementation

Soft clipping application: smooth_amount = smoothing × 2 IF |sample| > (1 - smooth_amount) THEN output = sample / (1 + |sample| × smooth_amount) ELSE output = sample Effect: - Reduces harshness at fold transitions - Creates more musical, less abrasive distortion - Higher smoothing = more gentle saturation - Particularly useful with high fold_iterations

Vectorized Processing Advantage

⚡ Efficient Sample Processing

Principle: Process all samples simultaneously using Praat's Formula

Implementation: Vectorized conditional operations

Benefit: Much faster than sample-by-sample loop

Trade-off: Different computational characteristics

Vectorized vs Sequential Processing

Traditional sample-by-sample: FOR each sample i: IF sample[i] > threshold THEN sample[i] = threshold - (sample[i] - threshold)×depth ENDIF ENDFOR Vectorized processing: Formula: "if self > threshold then threshold - (self - threshold)×depth else self fi" Advantages of vectorization: - Much faster execution (10-100× speedup) - Efficient use of Praat's optimized math - Clean, readable code Considerations: - All samples processed with same formula - May have slightly different numeric behavior - Still produces musically excellent results

Parameters & Settings

Core Folding Parameters

ParameterTypeDefaultDescription
PresetoptionmenuCustomPredefined folding configurations
Threshold_(0-1)real0.5Signal level where folding begins
Input_gain_(dB)real0.0Pre-folding amplification
Fold_depth_(0-1)real1.0Intensity of folding effect
Asymmetry_(-1_to_1)real0.0Positive/negative threshold difference

Advanced Processing Parameters

ParameterTypeDefaultDescription
Fold_iterationsinteger1Number of folding passes
Bipolar_foldingboolean1Separate positive/negative processing
Smoothing_(0-1)real0.0Soft clipping amount
Output_gain_(dB)real0.0Post-processing level adjustment
DC_offset_removalboolean1Remove DC component from output

Parameter Interactions and Guidelines

Threshold (0-1): 0.1-0.3: Aggressive, early folding 0.4-0.6: Balanced, musical range 0.7-0.9: Subtle, gentle folding Lower = more distortion, higher = cleaner Input gain (dB): 0-6 dB: Subtle drive 6-12 dB: Moderate distortion 12-18 dB: Heavy distortion 18+ dB: Extreme destruction Fold depth (0-1): 0.0-0.3: Very soft folding 0.4-0.7: Musical folding 0.8-1.0: Hard folding 1.0 = fold completely to opposite threshold Asymmetry (-1 to 1): -1.0: Only negative side folds 0.0: Symmetric folding +1.0: Only positive side folds Creates even/odd harmonic bias Fold iterations (1-5+): 1: Simple harmonics 2: Complex harmonics 3: Very complex, dense 4+: Experimental, noisy

Gain Staging Formulas

dB to linear conversion: linear_gain = 10^(dB_value / 20) Asymmetric threshold calculation: IF asymmetry ≥ 0: threshold_pos = threshold × (1 - asymmetry) threshold_neg = threshold ELSE: threshold_pos = threshold threshold_neg = threshold × (1 + asymmetry) Smoothing amount: smooth_amount = smoothing × 2 Peak detection: peak = max(|maximum|, |minimum|) IF peak > 0.99: Apply normalization: × (0.99 / peak)

Applications

Music Production

Use case: Adding harmonic complexity to sounds

Technique: Apply Soft Fold or Tape Style to individual tracks

Example: Vocal harmonic enhancement with Soft Fold preset

Sound Design

Use case: Creating complex textures from simple sources

Technique: Use Multi-Fold or Hard Fold with high iterations

Example: Turning sine waves into rich harmonic stacks

Synthesizer Design

Use case: Wave shaping for complex oscillator spectra

Technique: Apply folding to basic waveforms

Example: Creating complex synth sounds from simple oscillators

Guitar Processing

Use case: Amp simulation and distortion effects

Technique: Use Bipolar Fold for tube-amp character

Example: Guitar distortion with musical harmonic content

Practical Workflow Examples

🎤 Vocal Harmonic Enhancement

Goal: Add warmth and presence to vocal track

Settings:

  • Preset: Soft Fold or Tape Style
  • Input gain: 3-6 dB
  • Threshold: 0.6-0.8
  • Smoothing: 0.3-0.5
  • Fold iterations: 1

Result: Warmer, more present vocal with subtle harmonics

🥁 Drum Destruction

Goal: Create aggressive, distorted drum sounds

Settings:

  • Preset: Hard Fold or Digital Crush
  • Input gain: 12-18 dB
  • Threshold: 0.2-0.4
  • Fold iterations: 2-3
  • Smoothing: 0.0-0.2

Result: Powerful, distorted drums with complex harmonics

🎹 Synth Wave Shaping

Goal: Create complex synth textures from simple waves

Settings:

  • Preset: Multi-Fold or Bipolar Fold
  • Input gain: 8-12 dB
  • Threshold: 0.4-0.6
  • Fold iterations: 2-3
  • Asymmetry: Experiment with values

Result: Rich, complex synth sounds with evolving harmonics

Advanced Techniques

Creative parameter combinations:
  • Progressive folding: Automate threshold over time for evolving effects
  • Frequency-specific folding: Apply to filtered frequency bands only
  • Parallel processing: Mix heavily folded and dry signals
  • Serial processing: Apply multiple fold stages with different settings
  • Modulation: Use asymmetry modulation for dynamic harmonic movement
Genre-specific applications:
  • Electronic music: Use Multi-Fold for complex synth textures
  • Rock music: Use Bipolar Fold for guitar amp simulation
  • Hip-hop: Use Asymmetric Fold for warm drum processing
  • Ambient: Use Soft Fold with smoothing for atmospheric textures
  • Experimental: Use extreme settings with high iterations

Troubleshooting Common Issues

Problem: Output too distorted/unrecognizable
Cause: Too high input gain or too low threshold
Solution: Reduce input gain, increase threshold, use fewer iterations
Problem: Harsh, digital-sounding results
Cause: Lack of smoothing with aggressive settings
Solution: Increase smoothing, reduce fold depth, use bipolar folding
Problem: Weak or no effect
Cause: Insufficient input gain or too high threshold
Solution: Increase input gain, decrease threshold, check fold depth
Problem: DC offset or unstable baseline
Cause: Asymmetric processing without DC removal
Solution: Enable DC offset removal, check asymmetry settings
Problem: Unexpected volume changes
Cause: Extreme folding creating unusual peaks
Solution: Adjust output gain, enable peak protection, use smoothing