### Decimator

Type : Bit-reducer and sample&hold unit
References : Posted by tobyear[AT]web[DOT]de
Notes :
This is a simple bit and sample rate reduction code, maybe some of you can use it. The parameters are bits (1..32) and rate (0..1, 1 is the original samplerate).
Call the function like this:
y=decimate(x);

A VST plugin implementing this algorithm (with full Delphi source code included) can be downloaded from here:
http://tobybear.phreque.com/decimator.zip

Comments/suggestions/improvements are welcome, send them to: tobybear@web.de

Code :
// bits: 1..32
// rate: 0..1 (1 is original samplerate)

********** Pascal source **********
var m:longint;
y,cnt,rate:single;

// call this at least once before calling
// decimate() the first time
procedure setparams(bits:integer;shrate:single);
begin
m:=1 shl (bits-1);
cnt:=1;
rate:=shrate;
end;

function decimate(i:single):single;
begin
cnt:=cnt+rate;
if (cnt>1) then
begin
cnt:=cnt-1;
y:=round(i*m)/m;
end;
result:=y;
end;

********** C source **********
int bits=16;
float rate=0.5;

long int m=1<<(bits-1);
float y=0,cnt=0;

float decimate(float i)
{
cnt+=rate;
if (cnt>=1)
{
cnt-=1;
y=(long int)(i*m)/(float)m;
}
return y;
}

from : kaleja[AT]estarcion[DOT]com
comment : Nothing wrong with that, but you can also do fractional-bit-depth decimations, allowing smooth degradation from high bit depth to low and back: --------------------------------------- // something like this -- this is // completely off the top of my head // precalculate the quantization level float bits; // effective bit depth float quantum = powf( 2.0f, bits ); // per sample y = floorf( x * quantum ) / quantum; ---------------------------------------

from : dr[DOT]kef[AT]spray[DOT]se
comment : it looks to me like the c-line long int m=1<<(bits-1); doesnt give the correct number of quantisation levels if the number of levels is defined as 2^bits. if bits=2 for instance, the above code line returns a bit pattern of 10 (3) and not 11 (2^2) like one would expect. please, do correct me if im wrong. /heatrof

from : resofactor[AT]hotmail[DOT]com
comment : just getting into coding, i've mainly been working with synthedit...but would really like to move on into the bigger arena? any pointers for a DSP newbie-totally not hip on structured programming...yet! :O)

from : gary[at]dmt uk
comment : great algo, works fine.. many thanks