aboutsummaryrefslogtreecommitdiff
path: root/src/fsk_put_test_bits.c
blob: 7b2da3435eacca81b8bf60b9df80fcee7ff64622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*---------------------------------------------------------------------------*\

  FILE........: fsk_get_test_bits.c
  AUTHOR......: Brady O'Brien
  DATE CREATED: January 2016

  Generates a pseudorandom sequence of bits for testing of fsk_mod and
  fsk_demod.

\*---------------------------------------------------------------------------*/


/*
  Copyright (C) 2016 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "fsk.h"

#define TEST_FRAME_SIZE 100  /* must match fsk_get_test_bits.c */

#define VALID_PACKET_BER_THRESH 0.1

int main(int argc,char *argv[]){
    int bitcnt,biterr,i,errs,packetcnt;
    int framesize = TEST_FRAME_SIZE;
    float valid_packet_ber_thresh = VALID_PACKET_BER_THRESH;
    int packet_pass_thresh = 0;
    float ber_pass_thresh = 0;
    FILE *fin;
    uint8_t *bitbuf_tx, *bitbuf_rx, abyte, abit;
    int verbose = 1;
    int packed_in = 0;
    
    char usage[] = "usage: %s [-f frameSizeBits] [-t VaildFrameBERThreshold] [-b BERPass] [-p numPacketsPass] [-k] InputOneBitPerByte\n"
                   "  [-k] packet byte input\n";

    int opt;
    while ((opt = getopt(argc, argv, "f:b:p:hqt:k")) != -1) {
        switch (opt) {
        case 't':
            valid_packet_ber_thresh = atof(optarg);
            break;
          case 'b':
            ber_pass_thresh = atof(optarg);
            break;
        case 'p':
            packet_pass_thresh = atoi(optarg);
            break;
        case 'f':
            framesize = atoi(optarg);
            break;
        case 'q':
            verbose = 0;
            break;
        case 'k':
            packed_in = 1;
            break;
        case 'h':
        default:
            fprintf(stderr, usage, argv[0]);
            exit(1);
        }
    }
    if (argc == 1) {
        fprintf(stderr, usage, argv[0]);
        exit(1);
    }

    int bits_per_byte = 1;
    if (packed_in) {
        if (framesize % 8) {
            fprintf(stderr, "framesize (-f) must be a multiple of 8 for packed byte input (-k)\n");
            exit(1);
        }
        bits_per_byte = 8;
    }

    char *fname = argv[optind++];
    if ((strcmp(fname,"-")==0) || (argc<2)){
        fin = stdin;
    } else {
        fin = fopen(fname,"r");
    }
    
    if(fin==NULL){
        fprintf(stderr,"Couldn't open input file: %s\n", argv[1]);
        exit(1);
    }

    /* allocate buffers for processing */
    bitbuf_tx = (uint8_t*)malloc(sizeof(uint8_t)*framesize);
    bitbuf_rx = (uint8_t*)malloc(sizeof(uint8_t)*framesize);
    
    /* Generate known tx frame from known seed */
    srand(158324);
    for(i=0; i<framesize; i++){
	bitbuf_tx[i] = rand()&0x1;
	bitbuf_rx[i] = 0;
    }
    
    bitcnt = 0; biterr = 0; packetcnt = 0;
    float ber = 0.5;
    
    while(fread(&abyte,sizeof(uint8_t),1,fin)>0){

        for (int b=0; b<bits_per_byte; b++) {
            abit = (abyte >> ((bits_per_byte-1)-b)) & 0x1;
            
            /* update sliding window of input bits */

            for(i=0; i<framesize-1; i++) {
                bitbuf_rx[i] = bitbuf_rx[i+1];
            }
            bitbuf_rx[framesize-1] = abit;

            /* compare to know tx frame.  If they are time aligned, there
               will be a fairly low bit error rate */

            errs = 0;
            for(i=0; i<framesize; i++) {
                if (bitbuf_rx[i] != bitbuf_tx[i]) {
                    errs++;
                }
            }
            if (errs < valid_packet_ber_thresh * framesize) {
                /* OK, we have a valid packet, so lets count errors */
                packetcnt++;
                bitcnt += framesize;
                biterr += errs;
                ber =  (float)biterr/(float)bitcnt;
                if (verbose) {
                    fprintf(stderr,"[%04d] BER %5.3f, bits tested %6d, bit errors %6d errs: %4d \n",
                            packetcnt, ber, bitcnt, biterr, errs);
                }
            }
        }
    }
 
    free(bitbuf_rx);
    free(bitbuf_tx);

    fclose(fin);

    fprintf(stderr,"[%04d] BER %5.3f, bits tested %6d, bit errors %6d\n", packetcnt, ber, bitcnt, biterr);
    if ((packetcnt >= packet_pass_thresh) && (ber <= ber_pass_thresh)) {
        fprintf(stderr,"PASS\n");
        return 0;
    }
    else {
        fprintf(stderr,"FAIL\n");
        return 1;
    }
}