This module contains APIs to program and use the CALCRATIO module.
Files | |
| file | calcratio.h |
| CALCRATIO API/interface file. | |
Data Structures | |
| struct | CalcRatio_t |
| CALCRATIO Parameters. More... | |
Functions | |
| int32_t | calcRatio_init (CalcRatio_t *calcRatioInstance, float sampleRateIn, float sampleRateOut) |
| Initializes the ratio calculation module for ASRC (Asynchronous Sample Rate Converter) with input and output sample rates. More... | |
| int32_t | calcRatio_exec (CalcRatio_t *calcRatioInstance, double *Tx_Counter, double *Rx_Counter) |
| Executes the CALCRATIO algorithm to derive and update the smoothed actual sample rate ratio. More... | |
| double | calcRatio_getActualratio (CalcRatio_t *calcRatioInstance) |
| Safely retrieve the derived actual sample rate ratio. More... | |
Macros | |
| #define | CALCRATIO_MA1_LENGTH (1024U) |
| Length of the first-stage moving average filter used in ratio smoothing. More... | |
| #define | CALCRATIO_MA2_LENGTH (128U) |
| Length of the second-stage moving average filter for refined smoothing. More... | |
| #define CALCRATIO_MA1_LENGTH (1024U) |
Length of the first-stage moving average filter used in ratio smoothing.
CALCRATIO_MA1_LENGTH: Defines the window size (1024 samples) for the coarse moving average filter. This large window effectively suppresses high-frequency jitter in the measured TX/RX sample ratio before finer filtering.
| #define CALCRATIO_MA2_LENGTH (128U) |
Length of the second-stage moving average filter for refined smoothing.
CALCRATIO_MA2_LENGTH: Defines the window size (128 samples) for the secondary moving average filter. Applied after the first stage to further reduce residual noise and provide a stable, smooth ratio value for resampling control.
| int32_t calcRatio_init | ( | CalcRatio_t * | calcRatioInstance, |
| float | sampleRateIn, | ||
| float | sampleRateOut | ||
| ) |
Initializes the ratio calculation module for ASRC (Asynchronous Sample Rate Converter) with input and output sample rates.
This function configures the CALCRATIO module by setting the input and output sample rates, computing the target ratio (fso / fsi), and resetting all internal filter states (moving average buffers, pointers, sums, and counters) to prepare for accurate ratio tracking.
| calcRatioInstance | Pointer to the CalcRatio_t instance to initialize. | |
| [in] | sampleRateIn | Input sampling frequency in Hz. |
| [in] | sampleRateOut | Output sampling frequency in Hz. |
| int32_t calcRatio_exec | ( | CalcRatio_t * | calcRatioInstance, |
| double * | Tx_Counter, | ||
| double * | Rx_Counter | ||
| ) |
Executes the CALCRATIO algorithm to derive and update the smoothed actual sample rate ratio.
This function must not be called from an ISR context because it uses semaphores for synchronization.
It performs one iteration of the two-stage moving average filter
one_shot flag.instantaneousRatio into the first-stage circular buffer (capture_history).movingAvg1Sum).capture2_history).movingAvg2Sum) using a graduated counter.actualRatio.The result converges to target_ratio as more samples are processed.
| calcRatioInstance | Pointer to a valid, initialized CalcRatio_t instance. | |
| [in] | Tx_Counter | Pointer to the latest sampled TX DMA transfer count (double). |
| [in] | Rx_Counter | Pointer to the latest sampled RX DMA transfer count (double). |
| double calcRatio_getActualratio | ( | CalcRatio_t * | calcRatioInstance | ) |
Safely retrieve the derived actual sample rate ratio.
This function retrieves the current value of actual_ratio from the specified CalcRatio_t instance in a thread-safe manner. It uses a global binary semaphore (gBinarySem_obj) to guarantee exclusive access and avoid race conditions when calcRatio_exec() updates the ratio concurrently (for example, from a timer callback or another task). It must not be invoked from an ISR context because semaphore-based synchronization is used internally.
| calcRatioInstance | Pointer to the CalcRatio instance. |
actual_ratio.