Question

I'm trying to code a digital ringing filter on an AVR microncontroller, and I am having some trouble with the implementation of the state diagram in fixed point arithmetic. Here's a picture of the signal flow I am attempting to write code for:enter image description here

Edit: (I believe the equation for T_c above should be e^[-1/(F_s*D)] )

Here's what I have so far. I have a routine called smultfix that does a fixed point signed multiply on two 8 bit signed integers and returns a 16 bit signed product. F_c and T_c are 8 bit signed binary fractions. "Output" and the intermediate step at the junction of T_c's input and the delay element, z1, are treated as 16 bit binary fractions. So I have:

(assume F_c and T_c are defined elsewhere)

int8_t generateSample()
{
    static int16_t z1 = 0x7FFF;  //initialize first delay element to max positive value
    static int16_t output;
    int8_t byteOutput = 0;
    int8_t bytez1 = 0;

    bytez1 = (z1 & 0xFF00)>>8; //make z1 into an eight bit signed binary fraction for    
                               //multiplication

    output = (smultfix(bytez1,F_c)<<1) + output; //calculate output, shift product
                                                 //left once to 
                                                 //remove double sign bit

    byteOutput = (output & 0xFF00)>>8;          //generate output byte

    z1 = (-(smultfix(byteOutput,F_c)<<1)) - \
            (smultfix(bytez1,T_c)<<1) //generate intermediate                
                                                             //product z1
    return byteOutput;  
}

Unfortunately, I seem to have just created a poor random number generator, as this code generates a lot of garbage filling up my output buffer! If someone could point out where I might be going wrong, or if they have an implementation idea that would be better, it would be much appreciated.

Was it helpful?

Solution

The code is actually correct - but the sign of the equation for T_c should indeed be positive rather than negative. It's shown as being negative in the first edition of the book, and it seems that this image was taken from the second edition of the book, in which the equation was corrected. If T_c is evaluated with a negative exponential, there will be increasing oscillations, but if it is positive the oscillations will decrease, which is what we want. Having T_c negative and flipping the sign of the subtraction on the second to last line also works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top