Doing audio processing (though it could just as well be image processing) I have a one-dimensional array of numbers. (They happen to be 16-bit signed integers representing audio samples, this question could apply to floats or integers of different sizes equally.)
In order to match audio with different frequencies (e.g. blend a 44.1kHz sample with a 22kHz sample), I need to either stretch or squash the array of values to meet a specific length.
Halving the array is simple: drop every other sample.
[231, 8143, 16341, 2000, -9352, ...] => [231, 16341, -9352, ...]
Doubling the array width is slightly less simple: double each entry in place (or optionally perform some interpolation between neighboring 'real' samples).
[231, 8143, 16341, 2000, -9352, ...] => [231, 4187, 8143, 12242, 16341, ...]
What I want is an efficient, simple algorithm that handles any scaling factor, and (ideally) optionally supports performing interpolation of one kind or another in the process.
My use case happens to be using Ruby arrays, but I'll happily take answers in most any language or pseudo-code.
The array/matrix math features you're looking for are typically found in "Scientific Computing" libraries. NArray may be a good place to start for Ruby.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With