3

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
gop
  • 2,150
  • 5
  • 26
  • 54

4 Answers4

8

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.

Martin Vilcans
  • 5,428
  • 5
  • 22
  • 17
3

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.

justin
  • 104,054
  • 14
  • 179
  • 226
2

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

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

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
0

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.

Phonon
  • 12,549
  • 13
  • 64
  • 114
  • Ok, I am using linear interpolation and the calculations seem correct but all i get is some static like noise. I am interpolating between every two bytes adding a new byte between them...but i think this is my mistake. Since the pcm is 16bit it means that there are 16 bits per sample so maybe i should be interpolating between every two samples(chunks of 16 bits). Is this correct or i got the whole idea wrong? – gop Mar 23 '11 at 16:01
  • If you show your code, I can take a look at it and see if anything is wrong (just the interpolation part). – Phonon Mar 23 '11 at 16:48
  • 4
    @gosho: Yes, of course, you should be interpolating between pairs of *samples*, not pairs of *bytes*. The individual bytes have no meaning. – Oliver Charlesworth Mar 23 '11 at 17:11
  • @Oli Charlesworth thanks. This makes sense...i don't know why i started interpolation between bytes. So how am i supposed to interpolate whole samples? Lets say I have samples A and B (each 16 bits). How is the resulting sample C formed? Is it formed like C[i]=(A[i]+B[i])/2 for each i? Currently my algorithm basically uses the algorithm suggested by @Justin with x=1/2. Now I am not at the pc where the code is but tomorrow i can show some code. – gop Mar 23 '11 at 17:45
  • 1
    Yes. When you perform linear interpolation, each new sample is the average of the two samples it sits between. – Phonon Mar 23 '11 at 18:04
  • Thanks again for the help I will write tomorrow with the results - hopefully successful :) – gop Mar 23 '11 at 18:38
  • Thanks guys i didn't have time to write back the past few days. It worked out gread. I still have questions about the wav header but I'll create another question for that. It will be nice to use some apis for wav manipulation but i am working on the blackberry java mobile edition and i don't know such libraries. Thanks again! – gop Mar 28 '11 at 13:32
  • Cubic interpolation is better than linear interpolation, but still not that great. http://stackoverflow.com/questions/1851384/resampling-interpolating-matrix/7493607#7493607 – endolith Sep 23 '11 at 14:23