AM275 FreeRTOS SDK  11.02.00
APIs for CALCRATIO

Introduction

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...
 

Macro Definition Documentation

◆ CALCRATIO_MA1_LENGTH

#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.

◆ CALCRATIO_MA2_LENGTH

#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.

Function Documentation

◆ calcRatio_init()

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.

Parameters
calcRatioInstancePointer to the CalcRatio_t instance to initialize.
[in]sampleRateInInput sampling frequency in Hz.
[in]sampleRateOutOutput sampling frequency in Hz.
Returns
The computed target ratio (same value written to *targetRatio).

◆ calcRatio_exec()

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

  1. If enabled, it discards the first (noisy) measurement using the one_shot flag.
  2. Stores the incoming instantaneousRatio into the first-stage circular buffer (capture_history).
  3. Updates the first-stage moving average sum (movingAvg1Sum).
  4. Feeds the first-stage average into the second-stage buffer (capture2_history).
  5. Updates the second-stage moving average sum (movingAvg2Sum) using a graduated counter.
  6. Computes and returns the smoothed actual ratio via actualRatio.

The result converges to target_ratio as more samples are processed.

Parameters
calcRatioInstancePointer to a valid, initialized CalcRatio_t instance.
[in]Tx_CounterPointer to the latest sampled TX DMA transfer count (double).
[in]Rx_CounterPointer to the latest sampled RX DMA transfer count (double).
Returns
Success/Failure for configuration.

◆ calcRatio_getActualratio()

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.

Parameters
calcRatioInstancePointer to the CalcRatio instance.
Returns
The current value of actual_ratio.