File: tdeframer.c

package info (click to toggle)
codec2 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,056 kB
  • sloc: ansic: 414,118; sh: 2,612; objc: 2,574; python: 2,105; cpp: 2,091; asm: 683; makefile: 598
file content (148 lines) | stat: -rw-r--r-- 4,448 bytes parent folder | download | duplicates (3)
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
/*---------------------------------------------------------------------------*\

  FILE........: tdeframer.c
  AUTHOR......: Brady O'Brien
  DATE CREATED: 8 April 2016

  C unit test for the VHF framer/deframer used by modes 2400A and 2400B.
  The deframer should sync up within one frame at a BER of 10e-3
\*---------------------------------------------------------------------------*/

/*
  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/>.
*/

/* BER of test */
#define TESTBER 0.01

/* Frame count */
#define FRCNT 1500

/* Random bits leading frame */
#define LRCNT 44

#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <freedv_vhf_framing.h>
#include <golay23.h>
#include <string.h>

/* The main loop of the test driver */
int main(int argc,char *argv[]){
    uint8_t * bit_buffer;
    uint8_t c2_buffer[10];
    struct freedv_vhf_deframer * fvd;
    int i,p,k;
    int bitbufferlen;
    int fsize;
    int ftype;
    int first_tol;
    
    if(argc<2){
        fprintf(stderr,"Usage: %s [A|B]\n",argv[0]);
        exit(1);
    }
    
    if(strcmp(argv[1],"A")==0){
        ftype = FREEDV_VHF_FRAME_A;
        first_tol = 2;
    }else if(strcmp(argv[1],"B")==0){
        ftype = FREEDV_HF_FRAME_B;
        first_tol = 5;
    }else{
        fprintf(stderr,"Usage: %s [A|B]\n",argv[0]);
        exit(1);
    }
    
    srand(1);
    golay23_init();
    
    /* Set up the deframer */
    fvd = fvhff_create_deframer(ftype,1);

    fsize = fvhff_get_frame_size(fvd);
    bitbufferlen = (LRCNT+fsize*FRCNT);
    
    /* Allocate bit buffer */
    bit_buffer = (uint8_t *) malloc(sizeof(uint8_t)*bitbufferlen);
    p = 0;
    
    /* Fill out front of buffer */
    for(i=0; i<LRCNT; i++){
        bit_buffer[p++] = rand()&0x1;
    }
    
    /* Place frames */
    for(i=0; i<FRCNT; i++){
        /* Encode frame index into golay codeword to protect from test BER*/
        k = golay23_encode((i+1)&0x0FFF);
        c2_buffer[5] = (k    )&0xFF;
        c2_buffer[1] = (k>>8 )&0xFF;
        c2_buffer[0] = (k>>16)&0x7F;
        /* Frame the bits */
        fvhff_frame_bits(ftype, &bit_buffer[p+(i*fsize)], c2_buffer,NULL,NULL);
    }
    
    /* Flip bits */
    for(i=0; i<bitbufferlen; i++){
        if( (rand()&0xFFFFFF) < (int)(TESTBER*0xFFFFFF)){
            bit_buffer[i] = bit_buffer[i]?0:1;
        }
    }
    
    p=0;
    int first_extract = 0;
    int total_extract = 0;
    int err_count = 0;
    printf("\n");
    /* Deframe some bits */
    for(i=0; i<bitbufferlen; i+=fsize){
        if( fvhff_deframe_bits(fvd, c2_buffer, NULL, NULL, &bit_buffer[i])){
            /* Extract golay23 codeword */
            k  = ((int)c2_buffer[5])    ;
            k |= ((int)c2_buffer[1])<<8 ;
            k |= ((int)c2_buffer[0])<<16;
            k = k & 0x7FFFFF;
            /* Decode frame index */
            p = golay23_decode(k);
            err_count += golay23_count_errors(k,p);
            p = p>>11;

            printf("%d,\t",p);
            total_extract++;
            if(first_extract==0)
                first_extract=p;
        }
    }
    printf("\n");
    float measured_ber = (float)err_count/(float)(23*total_extract);
    
    printf("First extracted frame %d\n",first_extract);
    printf("Extracted %d frames of %d, %f hit rate\n",total_extract,FRCNT,((float)total_extract/(float)FRCNT));
    printf("Bit error rate %f measured from golay code\n",measured_ber);
    printf("Bit error rate %f measured by deframer\n",fvd->ber_est);
    printf("Bit error rate %f measured by deframer\n",(float)fvd->total_uw_err/(float)fvd->total_uw_bits);
    /* Check test condition */
    if(first_extract<first_tol){
        printf("Test passed at test BER of %f!\n",TESTBER);
        exit(0);
    }else{
        printf("** Test failed at test BER of %f!\n",TESTBER);
        exit(1);
    }
    fvhff_destroy_deframer(fvd);
}