File: tfsk.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 (232 lines) | stat: -rw-r--r-- 7,456 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*---------------------------------------------------------------------------*\

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

  C test driver for fsk_mod and fsk_demod in fsk.c. Reads a file with input
  bits/rf and spits out modulated/demoduladed samples and a dump of internal
  state. To run unit test, see octave/tfsk.m

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

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


//#define MODEMPROBE_ENABLE

#include "modem_probe.h"
#include <stdio.h>

/* Note: This is a dirty hack to force fsk.c to compile with modem probing enabled */
#include "fsk.c"

#define ST_BITS 10000
#define ST_FS 8000
#define ST_RS 100
#define ST_F1 1200
#define ST_Fs 400
#define ST_EBNO 8
#define ST_M 2

#define TEST_SELF_FULL 1    /* No-arg self test */
#define TEST_MOD 2          /* Test modulator using in and out file */
#define TEST_MOD_H 3          /* Test modulator using in and out file */
#define TEST_DEMOD 4        /* Test demodulator using in and out file */
#define TEST_DEMOD_H 5      /* Test demodulator using in and out file */


int main(int argc,char *argv[]){
    struct FSK *fsk;
    int Fs,Rs,f1,fs,M, lock_nin;
    FILE *fin,*fout;

    uint8_t *bitbuf = NULL;
    float *modbuf = NULL;
    uint8_t *bitbufp;
    float *modbufp;

    size_t bitbufsize = 0;
    size_t modbufsize = 0;

    int test_type;
    
    int i;
    
    fin = NULL;
    fout = NULL;
    
    /* Set up full self-test */
    if(argc == 1){
        test_type = TEST_SELF_FULL;
        modem_probe_init("fsk2","fsk2_tfsk_log.txt");
        Fs = ST_FS;
        Rs = ST_RS;
        f1 = ST_F1;
        fs = ST_Fs;
        M = ST_M;
        lock_nin = 0;
    } else if (argc<10){
    /* Not running any test */
        printf("Usage: %s [(M|D|DX) Mode TXFreq1 TXFreqSpace SampleRate SymbolRate lock_nin InputFile OutputFile OctaveLogFile]\n",argv[0]);
        exit(1);
    } else {
    /* Running stim-drivin test */
        /* Mod test */
        if(strcmp(argv[1],"MX")==0){
            test_type = TEST_MOD_H;
        } else if(strcmp(argv[1],"M")==0 || strcmp(argv[1],"m")==0) {
            test_type = TEST_MOD;
        /* Demod test */
        } else if(strcmp(argv[1],"DX")==0)  {
            test_type = TEST_DEMOD_H;
        } else if(strcmp(argv[1],"D")==0 || strcmp(argv[1],"d")==0) {
            test_type = TEST_DEMOD;
        } else {
            printf("Must specify mod or demod test with M or D\n");
            exit(1);
        }
        /* Extract parameters */
        M = atoi(argv[2]);
        f1 = atoi(argv[3]);
        fs = atoi(argv[4]);
        Fs = atoi(argv[5]);
        Rs = atoi(argv[6]);
        lock_nin = atoi(argv[7]);
        
        /* Open files */
        fin = fopen(argv[8],"r");
        fout = fopen(argv[9],"w");
        
        if(fin == NULL || fout == NULL){
            printf("Couldn't open test vector files\n");
            exit(1);
        }
        /* Init modem probing */
        modem_probe_init("fsk",argv[10]);
        
    }
    
	srand(1);
    /* set up FSK */
    if(test_type == TEST_DEMOD_H || test_type == TEST_MOD_H){
        fsk = fsk_create_hbr(Fs,Rs,M,10,FSK_DEFAULT_NSYM,f1,fs);
        if(test_type == TEST_DEMOD_H)
            test_type = TEST_DEMOD;
        else
            test_type = TEST_MOD;
    }else{
        fsk = fsk_create(Fs,Rs,M,f1,fs);
    }
    fsk_set_freq_est_limits(fsk, 300, 2800);
    fsk->lock_nin = lock_nin;
    
    /* Modulate! */
    if(test_type == TEST_MOD || test_type == TEST_SELF_FULL){
        /* Generate random bits for self test */
        if(test_type == TEST_SELF_FULL){
            bitbufsize = ST_BITS;
            bitbuf = (uint8_t*) malloc(sizeof(uint8_t)*ST_BITS);
            for(i=0; i<ST_BITS; i++){
                /* Generate a randomish bit */
                bitbuf[i] = (uint8_t)(rand()&0x01);
            }
        } else { /* Load bits from a file */
            /* Figure out how many bits are in the input file */
            fseek(fin, 0L, SEEK_END);
            bitbufsize = ftell(fin);
            fseek(fin, 0L, SEEK_SET);
            bitbuf = malloc(sizeof(uint8_t)*bitbufsize);
            i = 0;
            /* Read in some bits */
            bitbufp = bitbuf;
            while( fread(bitbufp,sizeof(uint8_t),fsk->Nbits,fin) == fsk->Nbits){
                i++;
                bitbufp+=fsk->Nbits;
                /* Make sure we don't break the buffer */
                if(i*fsk->Nbits > bitbufsize){
                    bitbuf = realloc(bitbuf,sizeof(uint8_t)*(bitbufsize+fsk->Nbits));
                    bitbufsize += fsk->Nbits;
                }
            }
        }
        /* Allocate modulation buffer */
        modbuf = (float*)malloc(sizeof(float)*(bitbufsize/fsk->Nbits)*fsk->N*4);
        modbufsize = (bitbufsize/fsk->Nbits)*fsk->N;
        /* Do the modulation */
        modbufp = modbuf;
        bitbufp = bitbuf;
        while( bitbufp < bitbuf+bitbufsize){
            fsk_mod(fsk, modbufp, bitbufp, fsk->Nbits);
            modbufp += fsk->N;
            bitbufp += fsk->Nbits;
        }
        /* For a mod-only test, write out the result */
        if(test_type == TEST_MOD){
            fwrite(modbuf,sizeof(float),modbufsize,fout);
            free(modbuf);
        }
        /* Free bit buffer */
        free(bitbuf);
    }
    
    /* Now test the demod */
    if(test_type == TEST_DEMOD || test_type == TEST_SELF_FULL){
        free(modbuf);
        modbuf = malloc(sizeof(float)*(fsk->N+fsk->Ts*2));
        bitbuf = malloc(sizeof(uint8_t)*fsk->Nbits);
        /* Demod-only test */
        if(test_type == TEST_DEMOD){
            while( fread(modbuf,sizeof(float),fsk_nin(fsk),fin) == fsk_nin(fsk) ){
                int n = fsk_nin(fsk);
                COMP modbuf_comp[n];
                for(i=0; i<n; i++) {
                    modbuf_comp[i].real = modbuf[i];
                    modbuf_comp[i].imag = 0.0;
                }
                fsk_demod(fsk,bitbuf,modbuf_comp);
                fwrite(bitbuf,sizeof(uint8_t),fsk->Nbits,fout);
            }
        }
        /* Demod after channel imp. and mod */
        else{
            bitbufp = bitbuf;
            modbufp = modbuf;
            while( modbufp < modbuf + modbufsize){
                int n = fsk_nin(fsk);
                COMP modbuf_comp[n];
                for(i=0; i<n; i++) {
                    modbuf_comp[i].real = modbuf[i];
                    modbuf_comp[i].imag = 0.0;
                }
                fsk_demod(fsk,bitbuf,modbuf_comp);
                modbufp += fsk_nin(fsk);
            }
        }
        free(bitbuf);
    }
    
    modem_probe_close();
    if(test_type == TEST_DEMOD || test_type == TEST_MOD){
        fclose(fin);
        fclose(fout);
    }
    fsk_destroy(fsk);
    exit(0);
}