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

  FILE........: fdmdv_mod.c
  AUTHOR......: David Rowe
  DATE CREATED: April 28 2012

  Given an input file of bits outputs a raw file (8kHz, 16 bit shorts)
  of FDMDV modem samples ready to send over a HF radio channel.  The
  input file is assumed to be arranged as codec frames of 56 bits (7
  bytes) which we send as two 28 bit modem frames.

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


/*
  Copyright (C) 2012 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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>

#include "codec2_fdmdv.h"

int main(int argc, char *argv[])
{
    FILE         *fin, *fout;
    struct FDMDV *fdmdv;
    char          *packed_bits;
    int           *tx_bits;
    COMP          tx_fdm[2*FDMDV_NOM_SAMPLES_PER_FRAME];
    short         tx_fdm_scaled[2*FDMDV_NOM_SAMPLES_PER_FRAME];
    int           frames;
    int           i, bit, byte;
    int           sync_bit;
    int           bits_per_fdmdv_frame;
    int           bits_per_codec_frame;
    int           bytes_per_codec_frame;
    int           Nc;
#ifdef CHANNEL_SIM
    COMP          foff_phase_rect;
    float         foff;
#endif

    if (argc < 3) {
	printf("usage: %s InputBitFile OutputModemRawFile [Nc]\n", argv[0]);
	printf("e.g    %s hts1a.c2 hts1a_fdmdv.raw\n", argv[0]);
	exit(1);
    }

    if (strcmp(argv[1], "-")  == 0) fin = stdin;
    else if ( (fin = fopen(argv[1],"rb")) == NULL ) {
	fprintf(stderr, "Error opening input bit file: %s: %s.\n",
         argv[1], strerror(errno));
	exit(1);
    }

    if (strcmp(argv[2], "-") == 0) fout = stdout;
    else if ( (fout = fopen(argv[2],"wb")) == NULL ) {
	fprintf(stderr, "Error opening output modem sample file: %s: %s.\n",
         argv[2], strerror(errno));
	exit(1);
    }

    if (argc == 4) {
        Nc = atoi(argv[3]);

        if ((Nc < 2) || (Nc > FDMDV_NC_MAX) ) {
            fprintf(stderr, "Error number of carriers must be between 2 and %d\n",  FDMDV_NC_MAX);
            exit(1);
        }
   }
    else
        Nc = FDMDV_NC;

    fdmdv = fdmdv_create(Nc);

    bits_per_fdmdv_frame = fdmdv_bits_per_frame(fdmdv);
    bits_per_codec_frame = 2*fdmdv_bits_per_frame(fdmdv);
    bytes_per_codec_frame = (bits_per_codec_frame+7)/8;

    packed_bits = (char*)malloc(bytes_per_codec_frame);
    assert(packed_bits != NULL);
    tx_bits = (int*)malloc(sizeof(int)*bits_per_codec_frame);
    assert(tx_bits != NULL);

#ifdef CHANNEL_SIM
    foff = -100;
    foff_phase_rect.real = 1.0; foff_phase_rect.imag = 0.0;
#endif

    frames = 0;

    while(fread(packed_bits, sizeof(char), bytes_per_codec_frame, fin) == bytes_per_codec_frame) {
	frames++;

	/* unpack bits, MSB first */

	bit = 7; byte = 0;
	for(i=0; i<bits_per_codec_frame; i++) {
	    tx_bits[i] = (packed_bits[byte] >> bit) & 0x1;
	    bit--;
	    if (bit < 0) {
		bit = 7;
		byte++;
	    }
	}

	/* modulate even and odd frames */

	fdmdv_mod(fdmdv, tx_fdm, tx_bits, &sync_bit);
	assert(sync_bit == 1);

	fdmdv_mod(fdmdv, &tx_fdm[FDMDV_NOM_SAMPLES_PER_FRAME], &tx_bits[bits_per_fdmdv_frame], &sync_bit);
	assert(sync_bit == 0);

        #ifdef CHANNEL_SIM
        /* optional freq shift and channel simulation */

        fdmdv_freq_shift(tx_fdm, tx_fdm, foff, &foff_phase_rect, 2*FDMDV_NOM_SAMPLES_PER_FRAME);
        fdmdv_simulate_channel(&sig_pwr_av, tx_fdm, 2*FDMDV_NOM_SAMPLES_PER_FRAME, 10.0);
        #endif

	/* scale and save to disk as shorts */

	for(i=0; i<2*FDMDV_NOM_SAMPLES_PER_FRAME; i++)
	    tx_fdm_scaled[i] = FDMDV_SCALE * tx_fdm[i].real;

 	fwrite(tx_fdm_scaled, sizeof(short), 2*FDMDV_NOM_SAMPLES_PER_FRAME, fout);

	/* if this is in a pipeline, we probably don't want the usual
	   buffering to occur */

        if (fout == stdout) fflush(stdout);
    }

    //fdmdv_dump_osc_mags(fdmdv);

    free(tx_bits);
    free(packed_bits);
    fclose(fin);
    fclose(fout);
    fdmdv_destroy(fdmdv);

    return 0;
}