comparison spandsp-0.0.3/spandsp-0.0.3/src/spandsp/v29rx.h @ 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 * v29rx.h - ITU V.29 modem receive part
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: v29rx.h,v 1.41 2006/10/24 13:45:28 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if !defined(_V29RX_H_)
31 #define _V29RX_H_
32
33 /*! \page v29rx_page The V.29 receiver
34 \section v29rx_page_sec_1 What does it do?
35 The V.29 receiver implements the receive side of a V.29 modem. This can operate
36 at data rates of 9600, 7200 and 4800 bits/s. The audio input is a stream of 16
37 bit samples, at 8000 samples/second. The transmit and receive side of V.29
38 modems operate independantly. V.29 is mostly used for FAX transmission, where it
39 provides the standard 9600 and 7200 bits/s rates (the 4800 bits/s mode is not
40 used for FAX).
41
42 \section v29rx_page_sec_2 How does it work?
43 V.29 operates at 2400 baud for all three bit rates. It uses 16-QAM modulation for
44 9600bps, 8-QAM for 7200bps, and 4-PSK for 4800bps. A training sequence is specified
45 at the start of transmission, which makes the design of a V.29 receiver relatively
46 straightforward.
47
48 The first stage of the training sequence consists of 128
49 symbols, alternating between two constellation positions. The receiver monitors
50 the signal power, to sense the possible presence of a valid carrier. When the
51 alternating signal begins, the power rising above a minimum threshold (-26dBm0)
52 causes the main receiver computation to begin. The initial measured power is
53 used to quickly set the gain of the receiver. After this initial settling, the
54 front end gain is locked, and the adaptive equalizer tracks any subsequent
55 signal level variation. The signal is oversampled to 24000 samples/second (i.e.
56 signal, zero, zero, signal, zero, zero, ...) and fed to a complex root raised
57 cosine pulse shaping filter. This filter has been modified from the conventional
58 root raised cosine filter, by shifting it up the band, to be centred at the nominal
59 carrier frequency. This filter interpolates the samples, pulse shapes, and performs
60 a fractional sample delay at the same time. 48 sets of filter coefficients are used to
61 achieve a set of finely spaces fractional sample delays, between zero and
62 one sample. By choosing every fifth sample, and the appropriate set of filter
63 coefficients, the properly tuned symbol tracker can select data samples at 4800
64 samples/second from points within 1.125 degrees of the centre and mid-points of
65 each symbol. The output of the filter is multiplied by a complex carrier, generated
66 by a DDS. The result is a baseband signal, requiring no further filtering, apart from
67 an adaptive equalizer. The baseband signal is fed to a T/2 adaptive equalizer.
68 A band edge component maximisation algorithm is used to tune the sampling, so the samples
69 fed to the equalizer are close to the mid point and edges of each symbol. Initially
70 the algorithm is very lightly damped, to ensure the symbol alignment pulls in
71 quickly. Because the sampling rate will not be precisely the same as the
72 transmitter's (the spec. says the symbol timing should be within 0.01%), the
73 receiver constantly evaluates and corrects this sampling throughout its
74 operation. During the symbol timing maintainence phase, the algorithm uses
75 a heavier damping.
76
77 The carrier is specified as 1700Hz +-1Hz at the transmitter, and 1700 +-7Hz at
78 the receiver. The receive carrier would only be this inaccurate if the link
79 includes FDM sections. These are being phased out, but the design must still
80 allow for the worst case. Using an initial 1700Hz signal for demodulation gives
81 a worst case rotation rate for the constellation of about one degree per symbol.
82 Once the symbol timing synchronisation algorithm has been given time to lock to
83 the symbol timing of the initial alternating pattern, the phase of the demodulated
84 signal is recorded on two successive symbols - once for each of the constellation
85 positions. The receiver then tracks the symbol alternations, until a large phase jump
86 occurs. This signifies the start of the next phase of the training sequence. At this
87 point the total phase shift between the original recorded symbol phase, and the
88 symbol phase just before the phase jump occurred is used to provide a coarse
89 estimation of the rotation rate of the constellation, and it current absolute
90 angle of rotation. These are used to update the current carrier phase and phase
91 update rate in the carrier DDS. The working data already in the pulse shaping
92 filter and equalizer buffers is given a similar step rotation to pull it all
93 into line. From this point on, a heavily damped integrate and dump approach,
94 based on the angular difference between each received constellation position and
95 its expected position, is sufficient to track the carrier, and maintain phase
96 alignment. A fast rough approximator for the arc-tangent function is adequate
97 for the estimation of the angular error.
98
99 The next phase of the training sequence is a scrambled sequence of two
100 particular symbols. We train the T/2 adaptive equalizer using this sequence. The
101 scrambling makes the signal sufficiently diverse to ensure the equalizer
102 converges to the proper generalised solution. At the end of this sequence, the
103 equalizer should be sufficiently well adapted that is can correctly resolve the
104 full QAM constellation. However, the equalizer continues to adapt throughout
105 operation of the modem, fine tuning on the more complex data patterns of the
106 full QAM constellation.
107
108 In the last phase of the training sequence, the modem enters normal data
109 operation, with a short defined period of all ones as data. As in most high
110 speed modems, data in a V.29 modem passes through a scrambler, to whiten the
111 spectrum of the signal. The transmitter should initialise its data scrambler,
112 and pass the ones through it. At the end of the ones, real data begins to pass
113 through the scrambler, and the transmit modem is in normal operation. The
114 receiver tests that ones are really received, in order to verify the modem
115 trained correctly. If all is well, the data following the ones is fed to the
116 application, and the receive modem is up and running. Unfortunately, some
117 transmit side of some real V.29 modems fail to initialise their scrambler before
118 sending the ones. This means the first 23 received bits (the length of the
119 scrambler register) cannot be trusted for the test. The receive modem,
120 therefore, only tests that bits starting at bit 24 are really ones.
121 */
122
123 /* Target length for the equalizer is about 63 taps, to deal with the worst stuff
124 in V.56bis. */
125 #define V29_EQUALIZER_PRE_LEN 15 /* this much before the real event */
126 #define V29_EQUALIZER_POST_LEN 15 /* this much after the real event */
127 #define V29_EQUALIZER_MASK 63 /* one less than a power of 2 >= (2*V29_EQUALIZER_LEN + 1) */
128
129 #define V29_RX_FILTER_STEPS 27
130
131 typedef void (qam_report_handler_t)(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol);
132
133 /*!
134 V.29 modem receive side descriptor. This defines the working state for a
135 single instance of a V.29 modem receiver.
136 */
137 typedef struct
138 {
139 /*! \brief The bit rate of the modem. Valid values are 4800, 7200 and 9600. */
140 int bit_rate;
141 /*! \brief The callback function used to put each bit received. */
142 put_bit_func_t put_bit;
143 /*! \brief A user specified opaque pointer passed to the put_bit routine. */
144 void *user_data;
145 /*! \brief A callback function which may be enabled to report every symbol's
146 constellation position. */
147 qam_report_handler_t *qam_report;
148 /*! \brief A user specified opaque pointer passed to the qam_report callback
149 routine. */
150 void *qam_user_data;
151
152 /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
153 float rrc_filter[2*V29_RX_FILTER_STEPS];
154 /*! \brief Current offset into the RRC pulse shaping filter buffer. */
155 int rrc_filter_step;
156
157 /*! \brief The register for the data scrambler. */
158 unsigned int scramble_reg;
159 /*! \brief The register for the training scrambler. */
160 uint8_t training_scramble_reg;
161 int in_training;
162 int training_cd;
163 int training_count;
164 float training_error;
165 int carrier_present;
166 int16_t last_sample;
167 /*! \brief TRUE if the previous trained values are to be reused. */
168 int old_train;
169
170 /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
171 uint32_t carrier_phase;
172 /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
173 int32_t carrier_phase_rate;
174 /*! \brief The carrier update rate saved for reuse when using short training. */
175 int32_t carrier_phase_rate_save;
176 float carrier_track_p;
177 float carrier_track_i;
178
179 power_meter_t power;
180 int32_t carrier_on_power;
181 int32_t carrier_off_power;
182 float agc_scaling;
183 float agc_scaling_save;
184
185 int constellation_state;
186
187 float eq_delta;
188 /*! \brief The adaptive equalizer coefficients */
189 complexf_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
190 complexf_t eq_coeff_save[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
191 complexf_t eq_buf[V29_EQUALIZER_MASK + 1];
192 /*! \brief Current offset into equalizer buffer. */
193 int eq_step;
194 int eq_put_step;
195 int eq_skip;
196
197 /*! \brief The current half of the baud. */
198 int baud_half;
199 /*! \brief Band edge symbol sync. filter state. */
200 float symbol_sync_low[2];
201 float symbol_sync_high[2];
202 float symbol_sync_dc_filter[2];
203 float baud_phase;
204 /*! \brief The total symbol timing correction since the carrier came up.
205 This is only for performance analysis purposes. */
206 int total_baud_timing_correction;
207
208 /*! \brief Starting phase angles for the coarse carrier aquisition step. */
209 int32_t start_angles[2];
210 /*! \brief History list of phase angles for the coarse carrier aquisition step. */
211 int32_t angles[16];
212 /*! \brief Error and flow logging control */
213 logging_state_t logging;
214 } v29_rx_state_t;
215
216 #ifdef __cplusplus
217 extern "C" {
218 #endif
219
220 /*! Initialise a V.29 modem receive context.
221 \brief Initialise a V.29 modem receive context.
222 \param s The modem context.
223 \param rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
224 \param put_bit The callback routine used to put the received data.
225 \param user_data An opaque pointer passed to the put_bit routine.
226 \return A pointer to the modem context, or NULL if there was a problem. */
227 v29_rx_state_t *v29_rx_init(v29_rx_state_t *s, int rate, put_bit_func_t put_bit, void *user_data);
228
229 /*! Reinitialise an existing V.29 modem receive context.
230 \brief Reinitialise an existing V.29 modem receive context.
231 \param s The modem context.
232 \param rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
233 \param old_train TRUE if a previous trained values are to be reused.
234 \return 0 for OK, -1 for bad parameter */
235 int v29_rx_restart(v29_rx_state_t *s, int rate, int old_train);
236
237 /*! Release a V.29 modem receive context.
238 \brief Release a V.29 modem receive context.
239 \param s The modem context.
240 \return 0 for OK */
241 int v29_rx_release(v29_rx_state_t *s);
242
243 /*! Change the put_bit function associated with a V.29 modem receive context.
244 \brief Change the put_bit function associated with a V.29 modem receive context.
245 \param s The modem context.
246 \param put_bit The callback routine used to handle received bits.
247 \param user_data An opaque pointer. */
248 void v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
249
250 /*! Process a block of received V.29 modem audio samples.
251 \brief Process a block of received V.29 modem audio samples.
252 \param s The modem context.
253 \param amp The audio sample buffer.
254 \param len The number of samples in the buffer.
255 \return The number of samples unprocessed. */
256 int v29_rx(v29_rx_state_t *s, const int16_t amp[], int len);
257
258 /*! Get a snapshot of the current equalizer coefficients.
259 \brief Get a snapshot of the current equalizer coefficients.
260 \param s The modem context.
261 \param coeffs The vector of complex coefficients.
262 \return The number of coefficients in the vector. */
263 int v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs);
264
265 /*! Get the current received carrier frequency.
266 \param s The modem context.
267 \return The frequency, in Hertz. */
268 float v29_rx_carrier_frequency(v29_rx_state_t *s);
269
270 /*! Get the current symbol timing correction since startup.
271 \param s The modem context.
272 \return The correction. */
273 float v29_rx_symbol_timing_correction(v29_rx_state_t *s);
274
275 /*! Get the current received signal power.
276 \param s The modem context.
277 \return The signal power, in dBm0. */
278 float v29_rx_signal_power(v29_rx_state_t *s);
279
280 /*! Set the power level at which the carrier detection will cut in
281 \param s The modem context.
282 \param cutoff The signal cutoff power, in dBm0. */
283 void v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff);
284
285 /*! Set a handler routine to process QAM status reports
286 \param s The modem context.
287 \param handler The handler routine.
288 \param user_data An opaque pointer passed to the handler routine. */
289 void v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t *handler, void *user_data);
290
291 #ifdef __cplusplus
292 }
293 #endif
294
295 #endif
296 /*- End of file ------------------------------------------------------------*/

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