comparison spandsp-0.0.3/spandsp-0.0.3/src/fsk.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * fsk.c - FSK modem transmit and receive parts
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: fsk.c,v 1.27 2006/11/19 14:07:24 steveu Exp $
26 */
27
28 /*! \file */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <inttypes.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #if defined(HAVE_TGMATH_H)
38 #include <tgmath.h>
39 #endif
40 #if defined(HAVE_MATH_H)
41 #include <math.h>
42 #endif
43 #include <assert.h>
44
45 #include "spandsp/telephony.h"
46 #include "spandsp/complex.h"
47 #include "spandsp/dds.h"
48 #include "spandsp/power_meter.h"
49 #include "spandsp/async.h"
50 #include "spandsp/fsk.h"
51
52 fsk_spec_t preset_fsk_specs[] =
53 {
54 {
55 "V21 ch 1",
56 1080 + 100,
57 1080 - 100,
58 -14,
59 -30,
60 300
61 },
62 {
63 "V21 ch 2",
64 1750 + 100,
65 1750 - 100,
66 -14,
67 -30,
68 300
69 },
70 {
71 "V23 ch 1",
72 2100,
73 1300,
74 -14,
75 -30,
76 1200
77 },
78 {
79 "V23 ch 2",
80 450,
81 390,
82 -14,
83 -30,
84 75
85 },
86 {
87 "Bell103 ch 1",
88 2125 - 100,
89 2125 + 100,
90 -14,
91 -30,
92 300
93 },
94 {
95 "Bell103 ch 2",
96 1170 - 100,
97 1170 + 100,
98 -14,
99 -30,
100 300
101 },
102 {
103 "Bell202",
104 2200,
105 1200,
106 -14,
107 -30,
108 1200
109 },
110 {
111 "Weitbrecht", /* Used for TDD (Telecomc Device for the Deaf) */
112 1800,
113 1400,
114 -14,
115 -30,
116 45 /* Actually 45.45 */
117 }
118 };
119
120 fsk_tx_state_t *fsk_tx_init(fsk_tx_state_t *s,
121 fsk_spec_t *spec,
122 get_bit_func_t get_bit,
123 void *user_data)
124 {
125 s->baud_rate = spec->baud_rate;
126 s->get_bit = get_bit;
127 s->user_data = user_data;
128
129 s->phase_rates[0] = dds_phase_rate((float) spec->freq_zero);
130 s->phase_rates[1] = dds_phase_rate((float) spec->freq_one);
131 s->scaling = dds_scaling_dbm0((float) spec->tx_level);
132 /* Initialise fractional sample baud generation. */
133 s->phase_acc = 0;
134 s->baud_inc = (s->baud_rate*0x10000)/SAMPLE_RATE;
135 s->baud_frac = 0;
136 s->current_phase_rate = s->phase_rates[1];
137
138 s->shutdown = FALSE;
139 return s;
140 }
141 /*- End of function --------------------------------------------------------*/
142
143 int fsk_tx(fsk_tx_state_t *s, int16_t *amp, int len)
144 {
145 int sample;
146 int bit;
147
148 if (s->shutdown)
149 return 0;
150 /* Make the transitions between 0 and 1 phase coherent, but instantaneous
151 jumps. There is currently no interpolation for bauds that end mid-sample.
152 Mainstream users will not care. Some specialist users might have a problem
153 with they, if they care about accurate transition timing. */
154 for (sample = 0; sample < len; sample++)
155 {
156 if ((s->baud_frac += s->baud_inc) >= 0x10000)
157 {
158 s->baud_frac -= 0x10000;
159 if ((bit = s->get_bit(s->user_data)) == PUTBIT_END_OF_DATA)
160 {
161 s->shutdown = TRUE;
162 break;
163 }
164 s->current_phase_rate = s->phase_rates[bit & 1];
165 }
166 amp[sample] = dds_mod(&(s->phase_acc), s->current_phase_rate, s->scaling, 0);
167 }
168 return sample;
169 }
170 /*- End of function --------------------------------------------------------*/
171
172 void fsk_tx_power(fsk_tx_state_t *s, float power)
173 {
174 s->scaling = dds_scaling_dbm0(power);
175 }
176 /*- End of function --------------------------------------------------------*/
177
178 void fsk_tx_set_get_bit(fsk_tx_state_t *s, get_bit_func_t get_bit, void *user_data)
179 {
180 s->get_bit = get_bit;
181 s->user_data = user_data;
182 }
183 /*- End of function --------------------------------------------------------*/
184
185 void fsk_rx_signal_cutoff(fsk_rx_state_t *s, float cutoff)
186 {
187 s->min_power = power_meter_level_dbm0(cutoff);
188 }
189 /*- End of function --------------------------------------------------------*/
190
191 float fsk_rx_signal_power(fsk_rx_state_t *s)
192 {
193 return power_meter_dbm0(&s->power);
194 }
195 /*- End of function --------------------------------------------------------*/
196
197 void fsk_rx_set_put_bit(fsk_rx_state_t *s, put_bit_func_t put_bit, void *user_data)
198 {
199 s->put_bit = put_bit;
200 s->user_data = user_data;
201 }
202 /*- End of function --------------------------------------------------------*/
203
204 fsk_rx_state_t *fsk_rx_init(fsk_rx_state_t *s,
205 fsk_spec_t *spec,
206 int sync_mode,
207 put_bit_func_t put_bit,
208 void *user_data)
209 {
210 int chop;
211
212 memset(s, 0, sizeof(*s));
213 s->baud_rate = spec->baud_rate;
214 s->sync_mode = sync_mode;
215 s->min_power = power_meter_level_dbm0((float) spec->min_level);
216 s->put_bit = put_bit;
217 s->user_data = user_data;
218
219 /* Detect by correlating against the tones we want, over a period
220 of one baud. The correlation must be quadrature. */
221
222 /* First we need the quadrature tone generators to correlate
223 against. */
224 s->phase_rate[0] = dds_phase_rate((float) spec->freq_zero);
225 s->phase_rate[1] = dds_phase_rate((float) spec->freq_one);
226 s->phase_acc[0] = 0;
227 s->phase_acc[1] = 0;
228 s->last_sample = 0;
229
230 /* The correlation should be over one baud. */
231 s->correlation_span = SAMPLE_RATE/spec->baud_rate;
232 /* But limit it for very slow baud rates, so we do not overflow our
233 buffer. */
234 if (s->correlation_span > FSK_MAX_WINDOW_LEN)
235 s->correlation_span = FSK_MAX_WINDOW_LEN;
236
237 /* We need to scale, to avoid overflow in the correlation. */
238 s->scaling_shift = 0;
239 chop = s->correlation_span;
240 while (chop != 0)
241 {
242 s->scaling_shift++;
243 chop >>= 1;
244 }
245
246 /* Initialise the baud/bit rate tracking. */
247 s->baud_inc = (s->baud_rate*0x10000)/SAMPLE_RATE;
248 s->baud_pll = 0;
249
250 /* Initialise a power detector, so sense when a signal is present. */
251 power_meter_init(&(s->power), 4);
252 s->carrier_present = FALSE;
253 return s;
254 }
255 /*- End of function --------------------------------------------------------*/
256
257 int fsk_rx(fsk_rx_state_t *s, const int16_t *amp, int len)
258 {
259 int buf_ptr;
260 int baudstate;
261 int sample;
262 int j;
263 int32_t dot;
264 int32_t sum;
265 int32_t power;
266 icomplex_t ph;
267
268 buf_ptr = s->buf_ptr;
269
270 for (sample = 0; sample < len; sample++)
271 {
272 /* If there isn't much signal, don't demodulate - it will only produce
273 useless junk results. */
274 /* TODO: The carrier signal has no hysteresis! */
275 power = power_meter_update(&(s->power), amp[sample] - s->last_sample);
276 s->last_sample = amp[sample];
277 if (power < s->min_power)
278 {
279 if (s->carrier_present)
280 {
281 s->put_bit(s->user_data, PUTBIT_CARRIER_DOWN);
282 s->carrier_present = FALSE;
283 }
284 continue;
285 }
286 if (!s->carrier_present)
287 {
288 s->put_bit(s->user_data, PUTBIT_CARRIER_UP);
289 s->carrier_present = TRUE;
290 }
291 /* Non-coherent FSK demodulation by correlation with the target tones
292 over a one baud interval. The slow V.xx specs. are too open ended
293 to allow anything fancier to be used. The dot products are calculated
294 using a sliding window approach, so the compute load is not that great. */
295 /* The *totally* asynchronous character to character behaviour of these
296 modems, when carrying async. data, seems to force a sample by sample
297 approach. */
298 for (j = 0; j < 2; j++)
299 {
300 s->dot_i[j] -= s->window_i[j][buf_ptr];
301 s->dot_q[j] -= s->window_q[j][buf_ptr];
302
303 ph = dds_complex(&(s->phase_acc[j]), s->phase_rate[j]);
304 s->window_i[j][buf_ptr] = (ph.re*amp[sample]) >> s->scaling_shift;
305 s->window_q[j][buf_ptr] = (ph.im*amp[sample]) >> s->scaling_shift;
306
307 s->dot_i[j] += s->window_i[j][buf_ptr];
308 s->dot_q[j] += s->window_q[j][buf_ptr];
309 }
310 dot = s->dot_i[0] >> 15;
311 sum = dot*dot;
312 dot = s->dot_q[0] >> 15;
313 sum += dot*dot;
314 dot = s->dot_i[1] >> 15;
315 sum -= dot*dot;
316 dot = s->dot_q[1] >> 15;
317 sum -= dot*dot;
318 baudstate = (sum < 0);
319
320 if (s->lastbit != baudstate)
321 {
322 s->lastbit = baudstate;
323 if (s->sync_mode)
324 {
325 /* For synchronous use (e.g. HDLC channels in FAX modems), nudge
326 the baud phase gently, trying to keep it centred on the bauds. */
327 if (s->baud_pll < 0x8000)
328 s->baud_pll += (s->baud_inc >> 3);
329 else
330 s->baud_pll -= (s->baud_inc >> 3);
331 }
332 else
333 {
334 /* For async. operation, believe transitions completely, and
335 sample appropriately. This allows instant start on the first
336 transition. */
337 /* We must now be about half way to a sampling point. We do not do
338 any fractional sample estimation of the transitions, so this is
339 the most accurate baud alignment we can do. */
340 s->baud_pll = 0x8000;
341 }
342
343 }
344 if ((s->baud_pll += s->baud_inc) >= 0x10000)
345 {
346 /* We should be in the middle of a baud now, so report the current
347 state as the next bit */
348 s->baud_pll -= 0x10000;
349 s->put_bit(s->user_data, baudstate);
350 }
351 if (++buf_ptr >= s->correlation_span)
352 buf_ptr = 0;
353 }
354 s->buf_ptr = buf_ptr;
355 return 0;
356 }
357 /*- End of function --------------------------------------------------------*/
358 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.