File: gmskframesync_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 (92 lines) | stat: -rw-r--r-- 3,226 bytes parent folder | download | duplicates (2)
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
// Example demonstrating the GMSK flexible frame synchronizer.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "liquid.h"
#define OUTPUT_FILENAME "gmskframesync_example.m"

// callback function
int callback(unsigned char *  _header,
             int              _header_valid,
             unsigned char *  _payload,
             unsigned int     _payload_len,
             int              _payload_valid,
             framesyncstats_s _stats,
             void *           _userdata)
{
    printf("***** gmskframesync callback invoked *****\n");
    return 0;
}

int main(int argc, char*argv[])
{
    unsigned int k           = 2;       // samples/symbol
    unsigned int m           = 3;       // filter delay (symbols)
    float        BT          = 0.5f;    // filter bandwidth-time product
    unsigned int payload_len = 40;      // length of payload (bytes)
    crc_scheme   check       = LIQUID_CRC_32;
    fec_scheme   fec0        = LIQUID_FEC_HAMMING128;
    fec_scheme   fec1        = LIQUID_FEC_NONE;
    unsigned int i;

    // allocate memory for payload and initialize
    unsigned char header[8] = {0,1,2,3,4,5,6,7};
    unsigned char payload[payload_len];
    memset(payload, 0x00, payload_len);

    // create frame generator and assemble
    gmskframegen fg = gmskframegen_create_set(k, m, BT);
    gmskframegen_assemble(fg, header, payload, payload_len, check, fec0, fec1);

    // create frame synchronizer
    gmskframesync fs = gmskframesync_create_set(k, m, BT, callback, NULL);

    // allocate buffer for storing entire frame
    unsigned int num_samples = gmskframegen_getframelen(fg) + 800;
    float complex buf[num_samples];
    memset(buf, 0x00, num_samples*sizeof(float complex));

    // generate frame in one shot with sample offset
    gmskframegen_write(fg, buf+250, num_samples-250);

    // add channel gain and noise
    for (i=0; i<num_samples; i++)
        buf[i] = 0.3*buf[i] + 0.01f*(randnf() + randnf()*_Complex_I)*M_SQRT1_2;

    // push samples through synchronizer
    gmskframesync_execute(fs, buf, num_samples);
    gmskframesync_print(fs);

    // destroy objects
    gmskframegen_destroy(fg);
    gmskframesync_destroy(fs);

    // export output
    FILE * fid = fopen(OUTPUT_FILENAME,"w");
    if (fid == NULL) {
        fprintf(stderr,"error: %s, could not open '%s' for writing\n", argv[0], OUTPUT_FILENAME);
        exit(1);
    }
    fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
    fprintf(fid,"\n");
    fprintf(fid,"clear all\n");
    fprintf(fid,"close all\n");
    fprintf(fid,"\n");
    fprintf(fid,"num_samples = %u;\n", num_samples);
    fprintf(fid,"y = zeros(1,num_samples);\n");
    fprintf(fid,"\n");
    for (i=0; i<num_samples; i++)
        fprintf(fid,"y(%6u) = %12.4e + j*%12.4e;\n", i+1, crealf(buf[i]), cimagf(buf[i]));
    fprintf(fid,"\n");
    fprintf(fid,"t = 0:(num_samples-1);\n");
    fprintf(fid,"figure;\n");
    fprintf(fid,"plot(t, real(y), t,imag(y));\n");
    fprintf(fid,"xlabel('time');\n");
    fprintf(fid,"ylabel('received signal');\n");
    fprintf(fid,"legend('real','imag',0);\n");
    fclose(fid);
    printf("results written to '%s'\n", OUTPUT_FILENAME);
    return 0;
}