Cosine interpolation option in ResamplingAudioSource?


Has anyone tried to use cosine interpolation in ResamplingAudioSource? It would be fairly easy to use. I wonder why no one else has tried it?


Nope, I use the HIIR code from Laurent de Soras. I only use it for factor of 2 oversampling, but IIRC it can do non-integer resampling also.


I tried this. It worked:


*destBuffers[channel]++ = srcBuffers[channel][bufferPos] +
  ((1-std::cos(alpha*3.14159265358979))/2) *
    (srcBuffers[channel][nextPos] - srcBuffers[channel][bufferPos]);

What are the advantages / disadvantages?


I’m not familiar with the characteristics of cosine interpolation, but the things to look for are how flat the pass band is and the amount of rejection of aliasing. See chapter 2 of The Quest For The Perfect Resampler for more pros/cons.

Actually, just checked HIIR again and it doesn’t support rational resampling. :oops:


I’m very skeptical about this interpolation for AudioSources, it may look cool for your eyes, but isn’t the right approach for audio.
The only right (when it should something with sin), and perfect interpolation method is sinc-interpolation, but this will introduce latency to the signal.


Well if you’re skeptical about cosine interpolation, I can only imagine how you feel about linear interpolation (the current implementation).


linear interpolation is far from perfect, of course it adds additional harmonics, its a compromise, but the fastest way between two values is a line and not a curve. The more important part of the resampler is the pre-filtering (to prevent aliasing) and post filtering (to remove additional harmonics).

Why should a cos-function between two sample values should be better, it can’t be better,…
Imagine a saw-tooth which will be heavily up sampled with this kind of pseudo-resampling, it will have lots of bumps,…


Why should a cos-function between two sample values should be better, it can’t be better,…[/quote]
It may be better in terms of aliasing. Naively speaking, the sharper a change in a signal, then the higher frequency the partials need to be to generate that signal. So by using a smoother interpolation, the generated harmonics may not extend as high as for linear.

On the other hand, it may well be worse than linear for introduced noise. See here for a comparison based on image processing.