Login / Register  0 items | $0.00 NewWhat is KVR? Submit News Advertise

Correct way to oversample?

zckumling
KVRer
 
5 posts since 7 Feb, 2010

Postby zckumling; Tue Jun 29, 2010 10:22 pm Correct way to oversample?

I'm not sure if I'm doing the polyphase oversampling right..
I use the polyphase filters from the musicdsp.org archive..

here is the code for 4x oversampling as it is atm:


Code: Select all
   inline void process4x(double& sample)
   {
       a = halfband[0]->process( sample );
         a = halfband[2]->process( a );
         a = effect.process ( a * 4.0);
         a = halfband[3]->process ( a );
         a = halfband[2]->process ( null );
         a = effect.process ( a * 4.0);
         a = halfband[3]->process ( a );
      a = halfband[1]->process( a );
      sample = a; //Decimate
      a = halfband[0]->process( null );
         a = halfband[2]->process( a );
         a = effect.process ( a * 4.0);
         a = halfband[3]->process ( a );
         a = halfband[2]->process ( null );
         a = effect.process ( a * 4.0);
         a = halfband[3]->process ( a );
      a = halfband[1]->process( a );
   }


The 4.0 multiplier is for normalizing the gain for the effects input..
Is this a correct way to do it?
nollock
KVRian
 
1153 posts since 9 Dec, 2003

Postby nollock; Wed Jun 30, 2010 12:22 pm

To upsample 2x you zero stuff the source sample, place a zero after every sample (so it's twice as long and every other sample is zero) and feed that through a half band filter.

To downsample by 1/2 you filter by the half band filter and throw away every other sample.

So to upsample 2x you alternately feed the half band filter a sample, then a zero, a sample, a zero, and so on...

To up sample 4x you do that twice. You zero stuff the sample, filter it, then you zero stuff it again, and filter it again.

eg...

a = halfband1(input);
b = halfband1(0);

output0 = halfband2(a);
output1 = halfband2(0);
output2 = halfband2(b);
output3 = halfband2(0);

(4 output samples from 1 input)

To down sample 4 times you half band filter it, throw away every other sample, half band filter it again, and throw away every other sample again.

eg...

a = halfband1(input0);
halfband1(input1);
b = halfband1(input2);
halfband1(input3);

output = halfband2(a);
halfband2(b);

(1 output from 4 input samples)

HTH.
User avatar
jupiter8
KVRAF
 
9367 posts since 17 Sep, 2002, from Gothenburg Sweden

Postby jupiter8; Wed Jun 30, 2010 12:51 pm

I have an idea i might try in the next few days that involves oversampling.
It's an offline process so performance doesn't matter much. I was thinking of using a much higher quality interpolator when upsampling to avoid the filtering. Is this false DSP? Am i just fooling myself ? Intuitively it feels right but there's a nagging voice in the back of my head that tells me:"That's not how it's done".
zckumling
KVRer
 
5 posts since 7 Feb, 2010

Postby zckumling; Wed Jun 30, 2010 2:40 pm

nollock:

Thanks, I think I understand it now...

(Does that hold for a recursive filter? Since I've heard of routines for memorlyless filters and recursive ones... )

So, if I understand this correct, this is for a recursive filter:

Code: Select all
//effect.setSamplerate(4.0 * samplerate);

a = halfband1(input);
b = halfband1(0);

output0 = halfband2(a);
output1 = halfband2(0);
output2 = halfband2(b);
output3 = halfband2(0);

output0 = effect.process(output0);
output1 = effect.process(output1);
output2 = effect.process(output2);
output4 = effect.process(output3);

a = halfband1(output0);
halfband1(output1);
b = halfband1(output2);
halfband1(output3);

output = halfband2(a);
halfband2(b);


Have I understood it?
And should I upsample and downsample with the same halfband filter?
I don't know where I got the idea that I had to use one halfband filter for each time I upsampled by 2.. and one filter for each decimation..
nollock
KVRian
 
1153 posts since 9 Dec, 2003

Postby nollock; Wed Jun 30, 2010 2:53 pm

jupiter8 wrote:I have an idea i might try in the next few days that involves oversampling.
It's an offline process so performance doesn't matter much. I was thinking of using a much higher quality interpolator when upsampling to avoid the filtering. Is this false DSP? Am i just fooling myself ? Intuitively it feels right but there's a nagging voice in the back of my head that tells me:"That's not how it's done".


It's the same thing. An interpolator is a lowpass filter.

If for example you take a hermite interpolator. you can rearange the equation so it's in the form...

y = f1(x)*in[-1] + f2(x)*in[0] + f3(x)*in[1] + f4(x)*in[2]

Where x is subsample position and each of those f(x) parts is just a polynomial in x. So when you specify 'x' in the equation each of the f(x) parts becomes a constant, and you have a plane old FIR.

So if you're upsampling 4x then X is only ever [0, 0.25, 0.5, 0.75], so you have a set of 4 FIRS. Which is just a polyphase FIR.

Basicaly a polynomial interpolator and FIR are the same thing. The polynomial interpolator is just stored in a continuous form and allows random access.
Last edited by nollock on Wed Jun 30, 2010 3:00 pm, edited 2 times in total.
nollock
KVRian
 
1153 posts since 9 Dec, 2003

Postby nollock; Wed Jun 30, 2010 2:58 pm

Code: Select all
//effect.setSamplerate(4.0 * samplerate);

a = halfband1(input);
b = halfband1(0);

output0 = halfband2(a);
output1 = halfband2(0);
output2 = halfband2(b);
output3 = halfband2(0);

output0 = effect.process(output0);
output1 = effect.process(output1);
output2 = effect.process(output2);
output4 = effect.process(output3);

a = halfband3(output0);
halfband3(output1);
b = halfband3(output2);
halfband3(output3);

output = halfband4(a);
halfband4(b);


You need to use seeperate filters for each step either up or down.

Otherwise you should be good to go.
zckumling
KVRer
 
5 posts since 7 Feb, 2010

Postby zckumling; Wed Jun 30, 2010 3:08 pm

Thank you nollock, you have been very helpful! :)

Moderator: Moderators (Main)

Return to DSP and Plug-in Development