Real-time Enough
by Robert Krten

Listing One

// clear out intermediate sums
factors [0] = factors [1] = factors [2] = factors [3] = 0;

// get to the beginning of the samples
j = handle -> ringstart;

// do the multiply-accumulates
for (i = 0; i < handle -> corrsize; i++) {
    if (j >= handle -> corrsize) {
        j = 0;
    }
    val = handle -> buffer [j];
    factors [0] += handle -> correlates [0][i] * val;
    factors [1] += handle -> correlates [1][i] * val;
    factors [2] += handle -> correlates [2][i] * val;
    factors [3] += handle -> correlates [3][i] * val;
    j++;
}


Listing Two

// store previous bit in preparation
handle -> previous_bit = handle -> current_bit;

// compute current bit (as above)
handle -> current_bit = (factors [0] * factors [0] + factors [1] * 
                         factors [1] > factors [2] * factors [2] + 
                         factors [3] * factors [3]);
// if there's a bit reversal
if (handle -> prevbit != handle -> current_bit) {
    // adjust cell position to be in the middle
    handle -> cellpos = 0.5;
}

// walk the cell along
handle -> cellpos += handle -> celladj;

// gone past the end of the bitcell?
if (handle -> cellpos > 1.0) {

    // compensate
    handle -> cellpos -= 1.0;

    // tell the higher level application that we have a new bit value
    bit_outcall (handle -> current_bit);
}


Listing Three

// if waiting for a start bit (0)
if (!handle -> have_start) {
    if (bit) {
        // ignore it, it's not a start bit
        return;
    }
    // got a start bit, reset
    handle -> have_start = 1;
    handle -> data = 0;
    handle -> nbits = 0;
    return;
}

// here, we have a start bit

// accumulate data
handle -> data >>= 1;
handle -> data |= 0x80 * !!bit;

handle -> nbits++;

// if we have enough...
if (handle -> nbits == 8) {
    // ship it to the higher level
    byte_outcall (handle -> data);

    // and reset for next time
    handle -> have_start = 0;
}



2


