PCM algorithm for upsampling

杀马特。学长 韩版系。学妹 提交于 2019-12-06 09:25:12

问题


I have 8k16bit pcm audio and I want to upsample it to 16k16bit. I have to do this manually.

Can someone tell me the algorithm for linear interpolation? Should I interpolate between each two bytes?

Also when I upsample i have to make changes for the wav header - what should I change?


回答1:


As others have mentioned, linear interpolation doesn't give the best sound quality, but it's simple and cheap.

For each new sample you create, just average it with the next one, e.g.

short[] source = ...;
short[] result = new short[source.length * 2];
for(int i = 0; i < source.length; ++i) {
  result[i * 2] = source[i];
  result[i * 2 + 1] = (source[i] + source[i + 1]) / 2;
}

You should definitely search for a library that helps you with working with WAV files. Even though it's a simple format, you shouldn't have to do that yourself if there's code available that will do what you need. By the way, why are you doing this in the first place? Perhaps you could just use sox or a similar tool to do this.




回答2:


Can someone tell me the algorithm for linear interpolation? Should I interpolate between each two bytes?

sure:

double interpolate_linear(double a, double b, double x) {
    assert(0.0 <= x);
    assert(1.0 >= x);

    if (0.0 >= x)
        return a;
    else if (1.0 <= x)
        return b;
    else
        return (1.0 - x) * a + x * b;
}

linear interpolation, while better than nothing, has a high amount of error. it's better to zero fill and window if you have the cpu time.

Also when I upsample i have to make changes for the wav header - what should I change?

not sure for java.




回答3:


Here's a good link to working with WAV files in java:

http://www.labbookpages.co.uk/audio/javaWavFiles.html




回答4:


Not sure about the header, but I would look into cubic spline interpolation. You could look at this website. It's got a very neat way of performing cubic interpolation. I'm not sure how to modify the header, but I'm pretty sure there have been answers to that on Stack Overflow that you could search for.



来源:https://stackoverflow.com/questions/5407452/pcm-algorithm-for-upsampling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!