def test8():
    exp = [2077,2074,2071,2068,2065,2062,2059,2056,2053,2050,2047,2044,2041,
           2038,2035,2032,2033,2034,2035,2036,2037,2038,2039,2040,2041,2042,
           2043,2044,2045,2046,2047,2048,2047,2046,2045,2044,2043,2042,2041,
           2040,2039,2038,2037,2036,2035,2034,2033,2032,2035,2038,2041,2044,
           2047,2050,2053,2056,2059,2062,2065,2068,2071,2074,2077,2080,]

    setup[2] = SCALE | COPY | WRAP
    setup[3] = 1  # No decimation
    setup[4] = 2048  # Offset
    bufin = array('H', (2048 for i in range(2*SIGLEN)))
    op = array('f', (0 for _ in range(2*SIGLEN)))
    op[0] = 0.001  # Scaling
    coeffs = array('f', (-1 if x < SIGLEN/2 else 1 for x in range(SIGLEN)))
    idx = SIGLEN
    for coeff in coeffs:
        bufin[idx] = int(2048 + 1000 * coeff)
        idx += 1
    n_results = dcf(bufin, op, coeffs, setup)
    ok = True
    for idx, x in enumerate(bufin):
        if x != exp[idx]:
            print('FAIL')
            ok = False
            break
    if n_results != 2 * SIGLEN:
        print('Siglen fail')
        ok = False
    return ok
def test7():
    exp = [2048,2049,2050,2051,2052,2053,2054,2055,2056,2057,2058,2059,2060,2061,
           2062,2063,2064,2061,2058,2055,2052,2049,2046,2043,2040,2037,2034,2031,
           2028,2025,2022,2019,2016,2048,2048,2048,2048,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048]

    setup[2] = SCALE | COPY | REVERSE
    setup[3] = 1  # No decimation
    setup[4] = 2048  # Offset
    bufin = array('H', (2048 for i in range(2*SIGLEN)))
    op = array('f', (0 for _ in range(2*SIGLEN)))
    op[0] = 0.001  # Scaling
    coeffs = array('f', (-1 if x < SIGLEN/2 else 1 for x in range(SIGLEN)))
    idx = SIGLEN
    for coeff in coeffs:
        bufin[idx] = int(2048 + 1000 * coeff)
        idx += 1
    n_results = dcf(bufin, op, coeffs, setup)
    ok = True
    for idx, x in enumerate(bufin):
        if x != exp[idx]:
            print('FAIL')
            ok = False
            break
    if n_results != SIGLEN + 1:
        print('Siglen fail')
        ok = False
    return ok
def test9():
    exp = [2044,2040,2036,2032,2044,2056,2068,2080,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,
           2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,]

    setup[2] = SCALE | COPY
    setup[3] = 4  # Decimation
    setup[4] = -1  # Offset
    bufin = array('H', (2048 for i in range(2*SIGLEN)))
    op = array('f', (0 for _ in range(2*SIGLEN)))
    op[0] = 0.001  # Scaling
    coeffs = array('f', (-1 if x < SIGLEN/2 else 1 for x in range(SIGLEN)))
    idx = SIGLEN
    for coeff in coeffs:
        bufin[idx] = int(2048 + 1000 * coeff)
        idx += 1
    n_results = dcf(bufin, op, coeffs, setup)
    ok = True
    #print(n_results)
    #print('[', end='')
    #for x in op:
        #print('{:4.1f},'.format(x), end='')
    #print(']')
    #print('[', end='')
    #for x in bufin:
        #print('{:d},'.format(x), end='')
    #print(']')
    for idx, x in enumerate(bufin):
        if x != exp[idx]:
            print('FAIL')
            ok = False
            break
    if n_results != SIGLEN // 4:
        print('Siglen fail')
        ok = False
    return ok
Example #4
0
# Coeffs hold the expected signal in normal time order (oldest first).
coeffs = array('f', (1 if signal[i] == 1 else -1
                     for i in range(siglen)))  # range +-1

op = array('f', (0 for _ in range(RBUFLEN)))
setup = array('i', [0] * 5)
setup[0] = len(bufin)
setup[1] = len(coeffs)
setup[
    2] = SCALE  # No wrap, normal time order. No copy back: I/P sample set unchanged
setup[3] = 1  # No decimation.
setup[4] = 2048  # Offset
op[0] = 0.001  # Scale
t = utime.ticks_us()
n_results = dcf(bufin, op, coeffs, setup)
t = utime.ticks_diff(utime.ticks_us(), t)

ns = 0
maxop = 0
for x in range(n_results):
    res = op[x]
    print('{:3d}  {:8.1f}'.format(x, res))
    if res > maxop:
        maxop = res
        ns = x  # save sample no.
nextop = 0
for x in op:
    if x < maxop and x > nextop:
        nextop = x
s = 'Max correlation {:5.1f} at sample {:d} Next largest {:5.1f} Detection ratio {:5.1f}.'
cycles = RBUFLEN / NCYCLES

bufin = array('H', (2048 + int(1500 * sin(2 * cycles * pi * i / RBUFLEN))
                    for i in range(RBUFLEN)))
setup = array('i', (0 for _ in range(5)))
op = array('f', (0 for _ in range(RBUFLEN)))

# This filter extracts a DC level in presence of noise
# Wrap because signal sought (DC) is constant.
setup[0] = len(bufin)
setup[1] = len(coeffs_0)
setup[2] = WRAP | SCALE
setup[3] = 1  # Decimate by 1
setup[4] = 0  # zero offset
op[0] = 0.967  # Scale
dcf(bufin, op, coeffs_0, setup)
print('DC', sum(op) / len(op))

# This is a bandpass filter centred on a frequency such that the
# input sample array contains 8 full cycles
setup[0] = len(bufin)
setup[1] = len(coeffs_8a)
setup[2] = SCALE | COPY
setup[3] = 1  # Decimate
setup[4] = -1  # offset == -1: calculate mean
op[0] = 1.037201

t = utime.ticks_us()
n_results = dcf(bufin, op, coeffs_8a, setup)
t = utime.ticks_diff(utime.ticks_us(), t)
print('No. of results =', n_results)