Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithms to efficiently "scale" or "resize" of an array of numbers (audio resampling)

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.

like image 606
Phrogz Avatar asked Dec 10 '10 20:12

Phrogz


1 Answers

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.

like image 180
Brad Mace Avatar answered Sep 26 '22 23:09

Brad Mace