Beispiel #1
0
def identify_duplicates(detections):
    """
    Returns a mask for filtering duplicate detections for the same transmitter.

    The block prior to or after the full detection may contain a portion of
    positioning signal and also trigger a detection. It is thus necessary
    to remove those "duplicate" detections.
    It is assumed that all detections were captured by the same receiver.

    The mask will exclude unidentified detections.
    """
    array = toads_data.toads_array(detections, with_ids=True)

    # Sort by receiver ID, then transmitter ID, then block ID, then timestamp
    idx = np.argsort(array[['rxid', 'txid', 'block', 'timestamp']])

    cur = array[idx]
    prev = np.roll(cur, 1)
    next_ = np.roll(cur, -1)

    # TODO: only filter if SOA is within code_len
    mask_unidentified = (cur['txid'] == -1)  # FIXME: magic number
    mask_prev = ((cur['block'] == prev['block'] + 1) &
                 (cur['energy'] < prev['energy']))
    mask_next = ((cur['block'] == next_['block'] - 1) &
                 (cur['energy'] < next_['energy']))
    mask = ~(mask_prev | mask_next | mask_unidentified)

    reverse_idx = np.argsort(idx)

    return mask[reverse_idx]
Beispiel #2
0
def _main():
    import argparse

    formatter = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=formatter)

    parser.add_argument('toads', nargs='?',
                        type=argparse.FileType('rb'), default='data.toads',
                        help="toads data (\"-\" streams from stdin)")
    parser.add_argument('matches', nargs='?',
                        type=argparse.FileType('rb'), default='data.match',
                        help="toads data (\"-\" streams from stdin)")
    parser.add_argument('--beacon', type=int, default=0,
                        help="transmitter ID of beacon")
    parser.add_argument('--rx0', type=int, default=0,
                        help="receiver ID of first receiver")
    parser.add_argument('--rx1', type=int, default=1,
                        help="receiver ID of second receiver")
    parser.add_argument('--range', type=parse_range, default=None,
                        help="limit to a range of detection IDs")
    parser.add_argument('--deg', type=int, default=2,
                        help="degree of polynomial to fit through the SOAs")
    parser.add_argument('--export', type=str, nargs='?',
                        const=True,
                        help="export plot to a .PDF file")

    args = parser.parse_args()

    toads = toads_data.load_toads(args.toads)
    detections = toads_data.toads_array(toads, with_ids=True)

    all_matches = matchmaker.load_matches(args.matches)

    matches = matchmaker.extract_match_matrix(toads, all_matches,
                                              [args.rx0, args.rx1],
                                              [args.beacon])
    if args.range is not None:
        start, stop = args.range
        matches = [m for m in matches
                   if (m[0] >= start and m[0] <= stop and
                       m[1] >= start and m[1] <= stop)]
    matches = np.array(matches)

    analyze(detections, matches, deg=args.deg)

    if args.export:
        if args.export is True:
            filename = 'beacon_analysis_beacon{}_rx{}_rx{}_deg{}'.format(
                       args.beacon, args.rx0, args.rx1, args.deg)
            if args.range is not None:
                filename += '_{}-{}'.format(args.range[0], args.range[1])
            filename += '.pdf'
        else:
            filename = args.export + '.pdf'

        plt.savefig(filename, format='pdf')

    plt.show()
Beispiel #3
0
def _main():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument('--toad',
                        dest='toad',
                        action='store_true',
                        help="input data is .toad data instead of .toads")
    parser.add_argument('-i',
                        '--input',
                        type=argparse.FileType('rb'),
                        default='data.toads',
                        help=".toads data (\'-\' streams from stdin)")
    parser.add_argument('-m',
                        '--match',
                        type=argparse.FileType('rb'),
                        default=None,
                        help="exclude unmatched detections")
    # TODO: --filter, e.g. txid == 2
    # TODO: take plot/print commands as arguments, e.g. cmd1 [cmd2,...]
    # TODO: tabbed Qt interface (like detect_analysis)
    args = parser.parse_args()

    if args.toad:
        toads = toads_data.load_toad(args.input)
        detections = toads_data.toads_array(toads, with_ids=False)
    else:
        toads = toads_data.load_toads(args.input)
        detections = toads_data.toads_array(toads, with_ids=True)

    if args.match:
        matches = matchmaker.load_matches(args.match)
        matched_ids = np.sort(np.concatenate(matches))
        detections = detections[matched_ids]

    time0 = np.min(detections['timestamp'])
    print('Timestamps relative to {:.6f}'.format(time0))
    detections['timestamp'] -= time0

    splits = split_rxtx(detections)

    print_rxtx_stats(splits)
    plot_all(detections, splits)
    plt.show()
Beispiel #4
0
def calculate_tdoas(toads, matches, rx1, rx2, beacon, tx):
    if beacon == tx:
        return None

    extract = matchmaker.extract_match_matrix(toads, matches, [rx1, rx2],
                                              [beacon, tx])
    data = toads_data.toads_array(toads)
    # print(data['txid'][extract])
    # extract = [x for x in extract if x[0] is not None and x[1] is not None]
    num_beacon = np.sum([toads[m[0]].txid == beacon for m in extract])
    num_tx = np.sum([toads[m[0]].txid == tx for m in extract])
    # print(len(extract), num_beacon, num_tx)

    if len(extract) == 0 or num_beacon < 3 or num_tx < 3:
        return None
    # if beacon == 2:
    #     print(extract)

    # print("rx1, rx2, beacon, tx:", rx1, rx2, beacon, tx)
    groups, failures = tdoa_est.estimate_tdoas(detections=toads,
                                               matches=extract,
                                               window_size=WINDOW_SIZE,
                                               beacon_pos={beacon: 0.0},
                                               rx_pos={
                                                   rx1: 0.0,
                                                   rx2: 0.0
                                               },
                                               sample_rate=2.4e6)
    matrix = tdoa_est.groups_to_matrix(groups)

    # tdoas = np.array([g.tdoas['tdoa'] for g in groups])
    # print("Number of TDOAs:", len(groups))
    # print("Number of failures:", len(failures))
    if len(groups) > 1:
        outliers = stat_tools.is_outlier(matrix['tdoa'])
        matrix = matrix[~outliers]
        # print("Number of outliers:", np.sum(outliers))
        # print num outliers / return num outliers

    # import matplotlib.pyplot as plt
    # plt.plot(np.array(tdoas) * SPEED_OF_LIGHT, '.-')
    # plt.show()

    # TODO: return timestamp
    return matrix
Beispiel #5
0
def print_count_table(matched_toads):
    data = toads_data.toads_array(matched_toads)
    rxids = np.sort(np.unique(data['rxid']))
    txids = np.sort(np.unique(data['txid']))

    rxnum = {rxids[i]: i for i in range(len(rxids))}
    txnum = {txids[i]: i for i in range(len(txids))}

    counts = [[0] * len(rxids) for _ in range(len(txids))]
    for row in data:
        rxid, txid = row['rxid'], row['txid']
        rxidx, txidx = rxnum[rxid], txnum[txid]
        counts[txidx][rxidx] += 1

    headers = ['v TX / RX >'] + list(rxids)
    table = [[txids[i]] + counts[i] for i in range(len(txids))]
    print('# Detection count table:')
    print(tabulate(table, headers=headers))
    # TODO: total row
    print()
Beispiel #6
0
def _main():
    import argparse

    formatter = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=formatter)

    parser.add_argument('toads',
                        nargs='?',
                        type=argparse.FileType('rb'),
                        default='rx.toads',
                        help="toads data (\"-\" streams from stdin)")
    parser.add_argument('matches',
                        nargs='?',
                        type=argparse.FileType('rb'),
                        default='rx.match',
                        help="toads data (\"-\" streams from stdin)")
    args = parser.parse_args()

    toads = toads_data.load_toads(args.toads)
    detections = toads_data.toads_array(toads, with_ids=True)
    matches = matchmaker.load_matches(args.matches)
    reldist(detections, matches, 1, 0)
Beispiel #7
0
def print_snr_table(matched_toads):
    # TODO: fix copy-paste from print_count_table
    data = toads_data.toads_array(matched_toads)
    rxids = np.sort(np.unique(data['rxid']))
    txids = np.sort(np.unique(data['txid']))

    rxnum = {rxids[i]: i for i in range(len(rxids))}
    txnum = {txids[i]: i for i in range(len(txids))}

    corr_peaks = [[[] for _ in range(len(rxids))] for _ in range(len(txids))]
    for row in data:
        rxid, txid = row['rxid'], row['txid']
        rxidx, txidx = rxnum[rxid], txnum[txid]
        corr_peaks[txidx][rxidx].append(row['energy'])
    corr_means = [[
        int(np.mean(corr_peaks[tx][rx])) if len(corr_peaks[tx][rx]) > 0 else 0
        for rx in range(len(rxids))
    ] for tx in range(len(txids))]

    headers = ['v TX / RX >'] + list(rxids)
    table = [[txids[i]] + corr_means[i] for i in range(len(txids))]
    print('# Mean corr peak amplitude table:')
    print(tabulate(table, headers=headers))
    print()