File: flexframesync_reconfig_example.c

package info (click to toggle)
liquid-dsp 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,216 kB
  • sloc: ansic: 115,859; sh: 3,513; makefile: 1,350; python: 274; asm: 11
file content (122 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (5)
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
//
// flexframesync_reconfig_example.c
//
// Demonstrates the reconfigurability of the flexframegen and
// flexframesync objects.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <getopt.h>

#include "liquid.h"

#define OUTPUT_FILENAME  "flexframesync_reconfig_example.m"

void usage()
{
    printf("flexframesync_example [options]\n");
    printf("  u/h   : print usage\n");
    printf("  s     : signal-to-noise ratio [dB], default: 30\n");
    printf("  n     : number of frames, default: 3\n");
}

int main(int argc, char *argv[]) {
    srand( time(NULL) );

    // define parameters
    float SNRdB = 30.0f;
    float noise_floor = -30.0f;
    unsigned int num_frames = 3;

    // get options
    int dopt;
    while((dopt = getopt(argc,argv,"uhvqs:f:m:p:n:")) != EOF){
        switch (dopt) {
        case 'u':
        case 'h': usage();                      return 0;
        case 's': SNRdB = atof(optarg);         break;
        case 'n': num_frames = atoi(optarg);    break;
        default:
            exit(1);
        }
    }

    // create flexframegen object
    flexframegenprops_s fgprops;
    flexframegenprops_init_default(&fgprops);
    flexframegen fg = flexframegen_create(NULL);

    // frame data
    unsigned char   header[14];
    unsigned char * payload = NULL;

    // create flexframesync object with default properties
    flexframesync fs = flexframesync_create(NULL,NULL);

    // channel
    float nstd  = powf(10.0f, noise_floor/20.0f);         // noise std. dev.
    float gamma = powf(10.0f, (SNRdB+noise_floor)/20.0f); // channel gain

    unsigned int i;
    // initialize header, payload
    for (i=0; i<14; i++)
        header[i] = i;

    // frame buffers, properties
    unsigned int  buf_len = 256;
    float complex buf[buf_len];

    unsigned int j;
    for (j=0; j<num_frames; j++) {
        // configure frame generator properties
        unsigned int payload_len = (rand() % 256) + 1;   // random payload length
        fgprops.check            = LIQUID_CRC_NONE;      // data validity check
        fgprops.fec0             = LIQUID_FEC_NONE;      // inner FEC scheme
        fgprops.fec1             = LIQUID_FEC_NONE;      // outer FEC scheme
        fgprops.mod_scheme       = (rand() % 2) ? LIQUID_MODEM_QPSK : LIQUID_MODEM_QAM16;

        // reallocate memory for payload
        payload = realloc(payload, payload_len*sizeof(unsigned char));

        // initialize payload
        for (i=0; i<payload_len; i++)
            payload[i] = rand() & 0xff;

        // set properties and assemble the frame
        flexframegen_setprops(fg, &fgprops);
        flexframegen_assemble(fg, header, payload, payload_len);
        printf("frame %u, ", j);
        flexframegen_print(fg);

        // write the frame in blocks
        int frame_complete = 0;
        while (!frame_complete) {
            // write samples to buffer
            frame_complete = flexframegen_write_samples(fg, buf, buf_len);

            // add channel impairments (gain and noise)
            for (i=0; i<buf_len; i++)
                buf[i] = buf[i]*gamma + nstd * (randnf() + _Complex_I*randnf()) * M_SQRT1_2;

            // push through sync
            flexframesync_execute(fs, buf, buf_len);
        }

    } // num frames

    // print frame data statistics
    flexframesync_print(fs);

    // clean up allocated memory
    flexframegen_destroy(fg);
    flexframesync_destroy(fs);
    free(payload);

    printf("done.\n");
    return 0;
}