aboutsummaryrefslogtreecommitdiff
path: root/src/fmfsk_mod.c
blob: f2826eb9c7efbe55dd6e4006516d231e74a01618 (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
/*---------------------------------------------------------------------------*\

  FILE........: fmfsk_mod.c
  AUTHOR......: Brady O'Brien
  DATE CREATED: 7 February 2016

  C test driver for fmfsk_mod in fmfsk.c. Reads in a set of bits to modulate
   from a file, passed as a parameter, and writes modulated output to
   another file
   

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

/*
  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 "fmfsk.h"
#include "codec2_fdmdv.h"

int main(int argc,char *argv[]){
    struct FMFSK *fmfsk;
    int Fs,Rb;
    int i;
    FILE *fin,*fout;
    uint8_t *bitbuf;
    int16_t *rawbuf;
    float *modbuf;
    
    if(argc<4){
        fprintf(stderr,"usage: %s SampleFreq BitRate InputOneBitPerCharFile OutputModRawFile\n",argv[0]);
        exit(1);
    }
    
    /* Extract parameters */
    Fs = atoi(argv[1]);
    Rb = atoi(argv[2]);
    
    if(strcmp(argv[3],"-")==0){
		fin = stdin;
	}else{
		fin = fopen(argv[3],"r");
	}
	
	if(strcmp(argv[4],"-")==0){
		fout = stdout;
	}else{
		fout = fopen(argv[4],"w");
	}
    
    
    /* set up FMFSK */
    fmfsk = fmfsk_create(Fs,Rb);
    
    if(fin==NULL || fout==NULL || fmfsk==NULL){
        fprintf(stderr,"Couldn't open test vector files\n");
        exit(1);
    }
    
    /* allocate buffers for processing */
    bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*fmfsk->nbit);
    rawbuf = (int16_t*)malloc(sizeof(int16_t)*fmfsk->N);
    modbuf = (float*)malloc(sizeof(float)*fmfsk->N);
    
    /* Modulate! */
    while( fread(bitbuf,sizeof(uint8_t),fmfsk->nbit,fin) == fmfsk->nbit ){
        fmfsk_mod(fmfsk,modbuf,bitbuf);
        for(i=0; i<fmfsk->N; i++){
	    rawbuf[i] = (int16_t)(modbuf[i]*(float)FDMDV_SCALE);
	}
        fwrite(rawbuf,sizeof(int16_t),fmfsk->N,fout);
        
	if(fout == stdin){
	    fflush(fout);
	}
    }
    
    free(modbuf);
    free(rawbuf);
    free(bitbuf);

    fmfsk_destroy(fmfsk);

    fclose(fin);
    fclose(fout);

    exit(0);
}