# Sample rate change using Lagrange interpolator

I'm trying to understand how to make Sample rate change by a non integer factor say $$K=1.0012$$ In real hardware I will be using cubic polynomial but for now I have chosen a linear interpolator. I have calculated the Lagrange coefficients:

Let's simplify my case and assume that $$K=\frac{4}{3}$$ So $$K_{inv} = \frac{1}{K} = 0.75$$

I cannot fugure out the condition when the new sample should be generated. As you can see in my picture the $$\mu$$ range is [-1:0]. So my guess is that as long as $$\mu$$ is in the range we use the input data valid, when mu is out of range - we must generate the new data valid?

$$\mu$$ must be computed like this: $$\mu(n) = \mu(n-1) + K_{inv} + ?$$

So my questions are

1)Is that correct assumption?

2)How to keep $$\mu$$ in range [-1:0]?

3)$$\mu(0) = 0$$ or $$K_{inv}$$?

Thanks

## 1 Answer

This is called an asynchronous sample rate converter. You need to keep track of time in the target clock domain using units of the source clock domain. Let's assume your target clock period is 1.7 times your input clock period.

The input times at which your have to output samples are. 0, 1.7, 3.4, 5.1, 6.8, etc. The integer part of the time stamp is your bulk delay and the fractional is your fractional delay. For the fourth output sample (n=3) the time stamp is 5.1 so you need to interpolate between samples 5 and 6 with a fractional delay of 0.1.

More formally

$$y[n] = x[n\cdot \frac{T_{out}}{T_{in}}]$$

You are sampling the input on a grid that's regular but not integer so you need to split this into an integer and a fractional part.

That means that if your new sample period is higher you will occasionally skip and input sample and if its lower you will occasionally use an input sample twice.

In practice you will discard the bulk delay equally from both input and output counters occasionally to prevent overflow.

Note that sample rate conversion is typically done with a polyphase filter which gives much better performance than a Lagrange interpolator. In most hardware implementations this will require some sort of control loop to adjust the ratio since all real word clocks are drifting.