TR123e - EMCT Computing Final Project Version 1.0
Research Project Translation from gen~ to embedded Moog synth
Loading...
Searching...
No Matches
MoogLadderFilterFixedPoint Class Reference

High-performance fixed-point implementation of Moog ladder filter. More...

#include <MoogLadderFilterFixedPoint.h>

Public Member Functions

 MoogLadderFilterFixedPoint (int sampleRate)
 Construct fixed-point Moog filter with sample rate configuration.
void setCutoff (int frequency)
 Configure filter cutoff frequency using integer parameter.
void setResonance (int resonance)
 Configure filter resonance using integer parameter.
void reset ()
 Reset all filter state to zero for clean initialization.
int process (int in)
 Process single audio sample through fixed-point ladder filter.
 MoogLadderFilterFixedPoint (int sampleRate)
 Construct fixed-point filter with integer sample rate.
void setCutoff (int frequency)
 Set cutoff frequency using integer parameter.
void setResonance (int resonance)
 Set resonance using 8-bit integer parameter.
void reset ()
 Reset all filter state to prevent artifacts.
int process (int in)
 Process integer audio sample through fixed-point filter.

Private Member Functions

int tanh_approx (int x)
 Fast tanh approximation for nonlinear processing.
int tanh_approx (int x)
 Fast tanh approximation for fixed-point nonlinear processing.

Private Attributes

int sampleRate
 Audio system sample rate for frequency calculations.
int alpha = 0
 Filter coefficient alpha in Q16 fixed-point format.
int feedbackAmount = 0
 Feedback amount coefficient in Q16 fixed-point format.
int fc = 1000
 Current cutoff frequency in Hz.
int rc = 0
 Current resonance coefficient in Q8 fixed-point format.
int prevIn = 0
 Previous input sample for delay line implementation.
int s [4] = {0}
 Filter stage state variables [4 elements].

Detailed Description

High-performance fixed-point implementation of Moog ladder filter.

Professional fixed-point Moog ladder filter for embedded systems.

This class provides a complete Moog ladder filter implementation using fixed-point arithmetic optimized for embedded systems and applications requiring deterministic execution. The implementation maintains the essential characteristics of the analog Moog filter while providing significant computational advantages over floating-point alternatives.

@design_principles

  • Computational Efficiency: Optimized for integer arithmetic units
  • Deterministic Behavior: Bit-exact results across platforms and runs
  • Resource Conservation: Minimal memory and power consumption
  • Precision Management: Careful scaling to maximize useful dynamic range
  • Overflow Protection: Comprehensive bounds checking and saturation

@performance_characteristics

  • Computational complexity: O(1) per sample (fixed integer operations)
  • Memory footprint: ~32 bytes (8 integers × 4 bytes each)
  • Processing overhead: ~15-20 integer operations per sample
  • Dynamic range: ~96dB (16-bit integer range)
  • Frequency resolution: ~0.0015% (Q16 fractional precision)

@applications

  • Embedded audio systems (Arduino, Raspberry Pi, microcontrollers)
  • Mobile audio applications requiring battery optimization
  • Real-time systems with strict timing requirements
  • Legacy hardware without floating-point units
  • Research applications requiring reproducible results

@usage_example

filter.setCutoff(1000); // 1kHz cutoff
filter.setResonance(180); // ~70% resonance (0-255 range)
// Process 16-bit audio samples
for (int i = 0; i < bufferSize; ++i) {
int sample = (int)inputBuffer[i]; // Convert to integer
int filtered = filter.process(sample);
outputBuffer[i] = (short)filtered; // Convert back to 16-bit
}
MoogLadderFilterFixedPoint(int sampleRate)
Construct fixed-point Moog filter with sample rate configuration.
Definition MoogLadderFilterFixedPoint.cpp:11
float * inputBuffer
Definition render_with_MOOGFILTER.cpp:114
int bufferSize
Definition render_with_MOOGFILTER.cpp:116
float * outputBuffer
Definition render_with_MOOGFILTER.cpp:115

This class implements a complete Moog ladder filter using fixed-point arithmetic optimized for embedded systems and applications requiring deterministic execution. The implementation maintains the essential characteristics of the analog Moog filter while providing significant computational and power advantages over floating-point alternatives.

@design_principles

  • Deterministic Execution: Completely predictable timing for real-time systems
  • Resource Efficiency: Minimal memory and computational requirements
  • Numerical Stability: Careful scaling to prevent overflow and maintain precision
  • Hardware Optimization: Designed for integer arithmetic units
  • Bit-Exact Reproducibility: Identical results across platforms and runs

@fixed_point_implementation_strategy

  • Q16.16 Format: 16-bit integer and 16-bit fractional components
  • Overflow Protection: Comprehensive bounds checking and saturation
  • Precision Management: Careful scaling to maximize useful dynamic range
  • Coefficient Optimization: Pre-scaled constants for efficient arithmetic
  • State Management: Integer state variables with denormal protection

@performance_optimization

  • Integer-Only Arithmetic: No floating-point operations required
  • Efficient Saturation: Fast tanh approximation using polynomial methods
  • Memory Efficiency: Compact data structures optimized for cache performance
  • Branch Optimization: Minimal conditional operations for predictable execution

@usage_example

filter.setCutoff(1000); // 1kHz cutoff
filter.setResonance(180); // ~70% resonance (0-255 range)
// Process 16-bit audio samples
for (int i = 0; i < bufferSize; ++i) {
int sample = (int)inputBuffer[i]; // Convert to integer
int filtered = filter.process(sample);
outputBuffer[i] = (short)filtered; // Convert back to 16-bit
}

Constructor & Destructor Documentation

◆ MoogLadderFilterFixedPoint() [1/2]

MoogLadderFilterFixedPoint::MoogLadderFilterFixedPoint ( int sampleRate)

Construct fixed-point Moog filter with sample rate configuration.

Initialize fixed-point Moog filter with comprehensive setup.

Parameters
sampleRateAudio system sample rate in Hz

Initializes the filter with integer-based parameters and clears all internal state variables. The constructor sets up the fixed-point arithmetic framework and prepares the filter for immediate use.

@complexity O(1) - Simple initialization @memory 32 bytes object size (8 × 4-byte integers) @precision Fixed-point Q16.16 format throughout

◆ MoogLadderFilterFixedPoint() [2/2]

MoogLadderFilterFixedPoint::MoogLadderFilterFixedPoint ( int sampleRate)

Construct fixed-point filter with integer sample rate.

Parameters
sampleRateAudio system sample rate as integer value

Initializes the filter with integer-based parameters and establishes the fixed-point arithmetic framework for all subsequent operations.

Member Function Documentation

◆ process() [1/2]

int MoogLadderFilterFixedPoint::process ( int in)

Process single audio sample through fixed-point ladder filter.

Process audio sample through fixed-point ladder algorithm.

Parameters
inInput audio sample (integer format)
Returns
Filtered audio sample (integer format)

Core processing method implementing the complete ladder filter algorithm using fixed-point arithmetic. Includes anti-denormalization, nonlinear feedback, and four-stage ladder processing with overflow protection.

@complexity O(1) - Fixed number of integer operations @precision Q16.16 fixed-point throughout processing chain @overflow_protection Comprehensive bounds checking and saturation

@algorithm_implementation

  1. Anti-denormalization: Add small offset to prevent zero-signal stasis
  2. Nonlinear Feedback: Apply tanh approximation to feedback signal
  3. Ladder Processing: Four cascaded lowpass stages with state updates
  4. Overflow Protection: Saturation arithmetic prevents wraparound errors

Implements the complete ladder filter using fixed-point arithmetic with careful attention to overflow prevention and precision management.

Apply anti-denormalization offset to prevent filter stasis Small offset ensures filter continues processing during silence

Calculate nonlinear feedback using tanh approximation Provides the resonance effect through controlled feedback

Process through four ladder stages with fixed-point arithmetic Each stage implements: output = (input * alpha + state * feedback) >> 16 The >> 16 operation maintains Q16 format after multiplication

Update filter state variables for next sample These maintain the temporal memory of each filter pole

◆ process() [2/2]

int MoogLadderFilterFixedPoint::process ( int in)

Process integer audio sample through fixed-point filter.

Parameters
inInput audio sample as integer (typically 16-bit range)
Returns
Filtered audio sample as integer with same range

Core processing method implementing complete fixed-point ladder algorithm with overflow protection, saturation limiting, and precision management while maintaining real-time performance guarantees.

◆ reset() [1/2]

void MoogLadderFilterFixedPoint::reset ( )

Reset all filter state to zero for clean initialization.

Clear all filter state for clean initialization.

Clears all internal state variables to eliminate residual signal content. Essential for filter initialization and clean transitions between different audio materials.

@complexity O(1) - Fixed loop clearing 4 state variables @memory_safety Accesses only local array elements

Essential for preventing artifacts from previous audio content and ensuring predictable filter behavior from first sample.

◆ reset() [2/2]

void MoogLadderFilterFixedPoint::reset ( )

Reset all filter state to prevent artifacts.

Clears all integer state variables to eliminate residual signal content and ensure clean startup behavior without floating-point dependencies.

◆ setCutoff() [1/2]

void MoogLadderFilterFixedPoint::setCutoff ( int frequency)

Configure filter cutoff frequency using integer parameter.

Configure cutoff frequency with fixed-point coefficient calculation.

Parameters
frequencyCutoff frequency in Hz (integer)

Sets the filter cutoff frequency with automatic range validation and fixed-point coefficient calculation. The method performs all necessary mathematical operations using integer arithmetic while maintaining adequate precision for musical applications.

@complexity O(1) - Fixed number of integer operations @precision Frequency accuracy limited by Q16 fractional precision @range [20Hz, sampleRate/2] automatically enforced

@algorithm_details

  1. Clamp frequency to valid range [20, sampleRate/2]
  2. Convert to normalized frequency using Q16 fixed-point
  3. Calculate filter coefficient 'g' using fixed-point multiplication
  4. Compute alpha and feedback coefficients for ladder implementation

This method implements frequency-to-coefficient conversion using fixed-point arithmetic while maintaining adequate precision for musical applications. The algorithm carefully manages bit precision to prevent overflow while maximizing useful dynamic range.

Validate and clamp frequency to safe operating range

Convert to normalized frequency using Q16 fixed-point arithmetic Formula: normFreq = (fc << 16) / sampleRate This shifts fc left by 16 bits then divides, effectively creating Q16 format

Calculate filter coefficient 'g' using fixed-point multiplication Formula: g = normFreq² (in Q16 format) Q16 * Q16 >> 16 maintains Q16 format while preventing overflow

Calculate alpha coefficient for ladder implementation Formula: alpha = g / (1 + g) This provides the fundamental filter coefficient for each pole

Calculate feedback amount for resonance implementation Formula: feedbackAmount = alpha² Provides the scaling factor for resonance feedback around ladder

◆ setCutoff() [2/2]

void MoogLadderFilterFixedPoint::setCutoff ( int frequency)

Set cutoff frequency using integer parameter.

Parameters
frequencyCutoff frequency in Hz as integer value

Configures filter cutoff with automatic range validation and fixed-point coefficient calculation while maintaining adequate precision for musical applications through careful scaling and bit manipulation.

◆ setResonance() [1/2]

void MoogLadderFilterFixedPoint::setResonance ( int resonance)

Configure filter resonance using integer parameter.

Configure resonance with 8-bit precision scaling.

Parameters
resonanceResonance intensity [0-255] integer range

Sets the filter resonance with 8-bit precision, providing 256 discrete resonance levels from no resonance to self-oscillation. The integer parameter is scaled internally for optimal fixed-point processing.

@complexity O(1) - Simple bit shifting and clamping @precision 8-bit resonance control (256 levels) @range [0-255] automatically clamped and scaled to Q8 format

Converts 8-bit resonance control to internal Q8 fixed-point format optimized for feedback calculations in the ladder structure.

Clamp resonance to 8-bit range and scale to Q8 format

2 operation converts from 8-bit to 6-bit range for stability

◆ setResonance() [2/2]

void MoogLadderFilterFixedPoint::setResonance ( int resonance)

Set resonance using 8-bit integer parameter.

Parameters
resonanceResonance intensity [0-255] for 8-bit precision

Controls filter resonance using 8-bit precision providing 256 discrete levels from no resonance to self-oscillation, with automatic scaling to internal fixed-point format for optimal processing efficiency.

◆ tanh_approx() [1/2]

int MoogLadderFilterFixedPoint::tanh_approx ( int x)
private

Fast tanh approximation for nonlinear processing.

Fast tanh approximation using quadratic polynomial.

Parameters
xInput value in fixed-point format
Returns
Approximated tanh(x) in fixed-point format

Provides computationally efficient tanh approximation using quadratic polynomial. Trades some accuracy for significant performance improvement over transcendental function evaluation.

@complexity O(1) - Single multiply and subtract operation @accuracy Within ~5% of true tanh for audio range @range Input clamped to [-32768, 32767] for stability

Provides efficient nonlinear processing suitable for fixed-point systems where transcendental function evaluation would be prohibitively expensive.

Clamp input to prevent overflow in subsequent operations

Quadratic approximation: tanh(x) ≈ x - x²/65536 The >> 16 operation provides the division by 65536 in fixed-point

◆ tanh_approx() [2/2]

int MoogLadderFilterFixedPoint::tanh_approx ( int x)
private

Fast tanh approximation for fixed-point nonlinear processing.

Parameters
xInput value in fixed-point format
Returns
Tanh approximation in fixed-point format

Implements efficient polynomial approximation of tanh function using only integer arithmetic, providing nonlinear saturation characteristics essential for authentic analog filter modeling.

Member Data Documentation

◆ alpha

int MoogLadderFilterFixedPoint::alpha = 0
private

Filter coefficient alpha in Q16 fixed-point format.

Filter coefficient in Q16.16 format.

Primary filter coefficient determining cutoff frequency behavior. Calculated from normalized frequency using fixed-point arithmetic.

◆ fc

int MoogLadderFilterFixedPoint::fc = 1000
private

Current cutoff frequency in Hz.

◆ feedbackAmount

int MoogLadderFilterFixedPoint::feedbackAmount = 0
private

Feedback amount coefficient in Q16 fixed-point format.

Feedback gain in Q16.16 format.

Controls the amount of resonance feedback around the ladder structure. Derived from alpha coefficient using fixed-point multiplication.

◆ prevIn

int MoogLadderFilterFixedPoint::prevIn = 0
private

Previous input sample for delay line implementation.

Filter state variables in fixed-point format.

Previous input sample for delay implementation

◆ rc

int MoogLadderFilterFixedPoint::rc = 0
private

Current resonance coefficient in Q8 fixed-point format.

Current resonance coefficient in Q8 format.

Resonance control parameter scaled to Q8 format for optimal precision in feedback calculations.

◆ s

int MoogLadderFilterFixedPoint::s = {0}
private

Filter stage state variables [4 elements].

Filter stage states in Q16.16 format.

Internal state of each ladder stage stored as fixed-point integers. These represent the "memory" of each filter pole.

◆ sampleRate

int MoogLadderFilterFixedPoint::sampleRate
private

Audio system sample rate for frequency calculations.

System configuration parameters.

Audio sample rate for coefficient calculation


The documentation for this class was generated from the following files: