aboutsummaryrefslogtreecommitdiff
path: root/src/fm.c
blob: e1fbe37b7690ec417e4f0daf3637b487e7f1472d (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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*---------------------------------------------------------------------------*\

  FILE........: fm.c
  AUTHOR......: David Rowe
  DATE CREATED: February 2015

  Functions that implement analog FM modulation and demodulation, see
  also octave/fm.m.

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

/*
  Copyright (C) 2015 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/>.
*/

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

                               DEFINES

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

#define FILT_MEM 200

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

                               INCLUDES

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

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "codec2_fm.h"
#include "fm_fir_coeff.h"
#include "comp_prim.h"

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

                               FUNCTIONS

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

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

  FUNCTION....: fm_create
  AUTHOR......: David Rowe
  DATE CREATED: 24 Feb 2015

  Create and initialise an instance of the "modem".  Returns a pointer
  to the modem states or NULL on failure.  One set of states is
  sufficient for a full duplex modem.

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

struct FM *fm_create(int nsam)
{
    struct FM *fm;

    fm = (struct FM*)malloc(sizeof(struct FM));
    if (fm == NULL)
	return NULL;
    fm->rx_bb = (COMP*)malloc(sizeof(COMP)*(FILT_MEM+nsam));
    assert(fm->rx_bb != NULL);

    fm->rx_bb_filt_prev.real = 0.0;
    fm->rx_bb_filt_prev.imag = 0.0;
    fm->lo_phase.real = 1.0;
    fm->lo_phase.imag = 0.0;

    fm->tx_phase = 0;

    fm->rx_dem_mem = (float*)malloc(sizeof(float)*(FILT_MEM+nsam));
    assert(fm->rx_dem_mem != NULL);

    fm->nsam = nsam;

    return fm;
}


void fm_destroy(struct FM *fm_states)
{
    free(fm_states->rx_bb);
    free(fm_states->rx_dem_mem);
    free(fm_states);
}

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

  FUNCTION....: fm_demod
  AUTHOR......: David Rowe
  DATE CREATED: 24 Feb 2015

  Demodulate a FM signal to baseband audio.

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

void fm_demod(struct FM *fm_states, float rx_out[], float rx[])
{
  float  Fs = fm_states->Fs;
  float  fc = fm_states->fc;
  float  wc = 2*M_PI*fc/Fs;
  float  fd = fm_states->fd;
  float  wd = 2*M_PI*fd/Fs;
  COMP  *rx_bb = fm_states->rx_bb + FILT_MEM;
  COMP   wc_rect, rx_bb_filt, rx_bb_diff;
  float  rx_dem;
  /*
  float acc;
  */
  float *rx_dem_mem = fm_states->rx_dem_mem + FILT_MEM;
  int    nsam = fm_states->nsam;
  float  mag;
  int    i,k;

  wc_rect.real = cosf(wc); wc_rect.imag = -sinf(wc);

  for(i=0; i<nsam; i++) {

      /* down to complex baseband */

      fm_states->lo_phase = cmult(fm_states->lo_phase, wc_rect);
      rx_bb[i] = fcmult(rx[i], fm_states->lo_phase);

      /* input FIR filter */

      rx_bb_filt.real = 0.0; rx_bb_filt.imag = 0.0;

      for(k=0; k<FILT_MEM/2; k++) {
          rx_bb_filt.real += rx_bb[i-k].real * bin[k+FILT_MEM/4];
          rx_bb_filt.imag += rx_bb[i-k].imag * bin[k+FILT_MEM/4];
      }

      //rx_bb_filt = rx_bb[i];
      //printf("%f %f %f\n", rx[i], wc_rect.real, wc_rect.imag);
      //printf("%f %f %f\n", rx[i], fm_states->lo_phase.real, fm_states->lo_phase.imag);
      //printf("%f %f %f\n", rx[i], rx_bb[i].real, rx_bb[i].imag);
      //printf("%f %f\n", rx_bb_filt.real, rx_bb_filt.imag);
      /*
         Differentiate first, in rect domain, then find angle, this
         puts signal on the positive side of the real axis and helps
         atan2() behaive.
      */

      rx_bb_diff = cmult(rx_bb_filt, cconj(fm_states->rx_bb_filt_prev));
      fm_states->rx_bb_filt_prev = rx_bb_filt;

      rx_dem = atan2f(rx_bb_diff.imag, rx_bb_diff.real);

      /* limit maximum phase jumps, to remove static type noise at low SNRs */

      if (rx_dem > wd)
          rx_dem = wd;
      if (rx_dem < -wd)
          rx_dem = -wd;

      rx_dem *= (1/wd);
      //printf("%f %f\n", rx_bb_diff.real, rx_bb_diff.imag);
      rx_dem_mem[i] = rx_dem;
      /*
      acc = 0;
        for(k=0; k<FILT_MEM; k++) {
          acc += rx_dem_mem[i-k] * bout[k];
      }
      */
      rx_out[i] = rx_dem;
  }

  /* update filter memories */

  rx_bb      -= FILT_MEM;
  rx_dem_mem -= FILT_MEM;
  for(i=0; i<FILT_MEM; i++) {
      rx_bb[i] = rx_bb[i+nsam];
      rx_dem_mem[i] = rx_dem_mem[i+nsam];
  }

  /* normalise digital oscillator as the magnitude can drift over time */

  mag = cabsolute(fm_states->lo_phase);
  fm_states->lo_phase.real /= mag;
  fm_states->lo_phase.imag /= mag;

}

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

  FUNCTION....: fm_mod
  AUTHOR......: Brady O'Brien
  DATE CREATED: Sept. 10 2015

  Modulate an FM signal from a baseband modulating signal

  struct FM *fm - FM state structure. Can be reused from fm_demod.
  float tx_in[] - nsam baseband samples to be modulated
  float tx_out[] - nsam samples in which to place the modulated FM

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

void fm_mod(struct FM *fm_states, float tx_in[], float tx_out[]) {
  float  Fs = fm_states->Fs;    //Sampling freq
  float  fc = fm_states->fc;    //Center freq
  float  wc = 2*M_PI*fc/Fs;     //Center freq in rads/samp
  float  fd = fm_states->fd;    //Max deviation in cycles/samp
  float  wd = 2*M_PI*fd/Fs;     //Max deviation in rads/samp
  int  nsam = fm_states->nsam;  //Samples per batch of modulation
  float tx_phase = fm_states->tx_phase; //Transmit phase in rads
  float w;			//Temp variable for phase of VFO during loop
  int i;

  //Go through the samples, spin the oscillator, and generate some FM
  for(i=0; i<nsam; i++){
      w = wc + wd*tx_in[i];   //Calculate phase of VFO
      tx_phase += w;          //Spin TX oscillator

      //TODO: Add pre-emphasis and pre-emph AGC for voice

      //Make sure tx_phase stays from 0 to 2PI.
      //If tx_phase goes above 4PI, It's because fc+fd*tx_in[i] is way too large for the sample
      // rate.
      if(tx_phase > 2*M_PI)
          tx_phase -= 2*M_PI;
      tx_out[i] = cosf(tx_phase);
  }
  //Save phase back into state struct
  fm_states->tx_phase = tx_phase;
}

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

  FUNCTION....: fm_mod
  AUTHOR......: Brady O'Brien
  DATE CREATED: Sept. 10 2015

  Modulate an FM signal from a baseband modulating signal. Output signal is
   in complex domain

  struct FM *fm - FM state structure. Can be reused from fm_demod.
  float tx_in[] - nsam baseband samples to be modulated
  COMP tx_out[] - nsam samples in which to place the modulated FM

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

void fm_mod_comp(struct FM *fm_states, float tx_in[], COMP tx_out[]){
  float  Fs = fm_states->Fs;    //Sampling freq
  float  fc = fm_states->fc;    //Center freq
  float  wc = 2*M_PI*fc/Fs;     //Center freq in rads/samp
  float  fd = fm_states->fd;    //Max deviation in cycles/samp
  float  wd = 2*M_PI*fd/Fs;     //Max deviation in rads/samp
  int  nsam = fm_states->nsam;  //Samples per batch of modulation
  float tx_phase = fm_states->tx_phase; //Transmit phase in rads
  float w;			//Temp variable for phase of VFO during loop
  int i;

  //Go through the samples, spin the oscillator, and generate some FM
  for(i=0; i<nsam; i++){
      w = wc + wd*tx_in[i];   //Calculate phase of VFO
      tx_phase += w;          //Spin TX oscillator
      
      //TODO: Add pre-emphasis and pre-emph AGC for voice

      //Make sure tx_phase stays from 0 to 2PI.
      //If tx_phase goes above 4PI, It's because fc+fd*tx_in[i] is way too large for the sample
      // rate.
      if(tx_phase > 2*M_PI)
          tx_phase -= 2*M_PI;

      tx_out[i].real = cosf(tx_phase);
      tx_out[i].imag = sinf(tx_phase);
  }
  //Save phase back into state struct
  fm_states->tx_phase = tx_phase;
}