Introduction
The CalcRatio algorithm is a real-time sample rate ratio estimator designed to track clock mismatch between two asynchronous audio domains. It computes the ratio of output (Tx) to input (Rx) sample counters and applies a Two-stage moving average to deliver a smooth, stable actual ratio for Asynchronous Sample Rate Conversion (ASRC).
Key Features
- DMA-Counter-Based Ratio Calculation
- Two-Stage Moving Average
- One-Shot Noise Rejection
- High Accuracy Ratio Tracking
Algorithm Overview
The CalcRatio algorithm works as follows:
- Initialization (calcRatio_init()) :
- Initializes the CALC_RATIO module with input and output sample rates.
- Computes the target ratio (sampleRateOut / sampleRateIn).
- Resets all internal states:
- Moving-average buffers
- Window pointers
- Accumulated sums
- Counters and the one-shot flag
- Prepares the module for accurate ratio estimation.
- First Stage Moving Average (MA1) :
- Applies a 1024-point moving average to the instantaneous ratio.
- Removes high-frequency noise and sudden spikes.
- Produces a smoothed ratio that still responds to short-term changes.
- Second Stage Filtering (MA2) :
- Applies a 128-point moving average to the output of MA1.
- Reduces long-term drift and stabilizes the ratio.
- Generates a clean, low-noise ratio used by the resampling stage.
- Ratio Calculation (calcRatio_exec()) :
- Reads instantaneous ratio from the TX/RX DMA counters.
- Uses a one-shot skip to ignore the first noisy measurement.
- Updates MA1 and MA2 buffers and their accumulated sums.
- Computes the actual ratio using both filtering stages.
- Output (calcRatio_getActualratio())
- Provides two key ratio values:
- Target Ratio: Calculated at initialization.
- Actual Ratio: Filtered, stable ratio for SW_ASRC operation.
- Thread-safe access using a global semaphore to avoid race conditions.
API Sequence
To use CALC_RATIO, the application should call the following APIs:
API
APIs for CALCRATIO