Re: Ruby resampling help
Posted: Wed Sep 13, 2017 7:06 am
The following might sound strange, but I just want to make sure, we're on the same path.
Interpolation is finding values between two given values, based on certain rules. Linear is simple: If we're given 5 and 10 and want to find the value that is exactly in the center between them (50%), we calculate 10 - 5 = 5 * 0.5 = 2.5 + 5 = 7.5
In other words, we need the first and last value (a, b) and a percentage that describes the "position" between them (t): lerp(a, b, t)
Cubic Interpolation works a bit different. It uses first and last value and a percentage (a, d, t), but additionally two control points (b, c).
As you can see, instead of magically calculating the curve, instead 6 calculations are done per t-value: linear interpolation between a and b (P01), b and c (P11), c and d (P21), then linear calculation of P01 and P11 (P02), P11 and P21 (P12), and finally linear interpolation between P02 and P12.
That means, you define two end points, two control points and a t-value to get the resulting value. a and d are easy: that's last sample and current sample (or current sample and next sample, depending on your approach). b, c and t however have to be defined in a way that suits your needs. Since I don't know which values are preferred for standard cubic interpolation of samples, you could go with another standard, called the one-third-rule. Make b 1/3 of the way from a to d, c 1/3 of the way from d to a. For t again you have to experiment, but I would think the one-third-rule would be quite nice as well.
If you copied the methods from the spline class, just call qubic(a, b, c, d, t) with qubic(lastsample, lastsample + (currentsample-lastsample) * 0.333, currentsample - (currentsample-lastsample) * 0.333, currentsample, 0.333)
Interpolation is finding values between two given values, based on certain rules. Linear is simple: If we're given 5 and 10 and want to find the value that is exactly in the center between them (50%), we calculate 10 - 5 = 5 * 0.5 = 2.5 + 5 = 7.5
In other words, we need the first and last value (a, b) and a percentage that describes the "position" between them (t): lerp(a, b, t)
Cubic Interpolation works a bit different. It uses first and last value and a percentage (a, d, t), but additionally two control points (b, c).
As you can see, instead of magically calculating the curve, instead 6 calculations are done per t-value: linear interpolation between a and b (P01), b and c (P11), c and d (P21), then linear calculation of P01 and P11 (P02), P11 and P21 (P12), and finally linear interpolation between P02 and P12.
That means, you define two end points, two control points and a t-value to get the resulting value. a and d are easy: that's last sample and current sample (or current sample and next sample, depending on your approach). b, c and t however have to be defined in a way that suits your needs. Since I don't know which values are preferred for standard cubic interpolation of samples, you could go with another standard, called the one-third-rule. Make b 1/3 of the way from a to d, c 1/3 of the way from d to a. For t again you have to experiment, but I would think the one-third-rule would be quite nice as well.
If you copied the methods from the spline class, just call qubic(a, b, c, d, t) with qubic(lastsample, lastsample + (currentsample-lastsample) * 0.333, currentsample - (currentsample-lastsample) * 0.333, currentsample, 0.333)