00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * v29rx.h - ITU V.29 modem receive part 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2003 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 2 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00025 * 00026 * $Id: v29rx.h,v 1.29 2005/12/08 16:51:00 steveu Exp $ 00027 */ 00028 00029 /*! \file */ 00030 00031 #if !defined(_V29RX_H_) 00032 #define _V29RX_H_ 00033 00034 /*! \page v29rx_page The V.29 receiver 00035 \section v29rx_page_sec_1 What does it do? 00036 The V.29 receiver implements the receive side of a V.29 modem. This can operate 00037 at data rates of 9600, 7200 and 4800 bits/s. The audio input is a stream of 16 00038 bit samples, at 8000 samples/second. The transmit and receive side of V.29 00039 modems operate independantly. V.29 is mostly used for FAX transmission, where it 00040 provides the standard 9600 and 7200 bits/s rates (the 4800 bits/s mode is not 00041 used for FAX). 00042 00043 \section v29rx_page_sec_2 How does it work? 00044 V.29 use QAM modulation. It specifies a training sequence at the start of 00045 transmission, which makes the design of a V.29 receiver relatively 00046 straightforward. The first stage of the training sequence consists of 128 00047 symbols, alternating between two constellation positions. The receiver monitors 00048 the signal power, to sense the possible presence of a valid carrier. When the 00049 alternating signal begins, the power rising above a minimum threshold (-26dBm0) 00050 causes the main receiver computation to begin. The initial measured power is 00051 used to quickly set the gain of the receiver. After this initial setting, the 00052 front end gain is locked, and the adaptive equalizer tracks any subsequent 00053 signal level variation. The signal is multiplied by a complex carrier, generated 00054 by a DDS, at 8000 samples/second. It is then fed at 24000 samples/second (i.e. 00055 signal, zero, zero, signal, zero, zero, ...) to a root raised cosine pulse 00056 shaping filter. This interpolates the samples, pulse shapes, and performs fractional 00057 sample delay at the same time. 48 sets of filter coefficients are used to 00058 achieve a set of finely spaces fractional sample delays, between zero and 00059 one sample. By choosing every fifth sample, and the appropriate set of filter 00060 coefficients, the properly tuned symbol tracker can select data samples at 4800 00061 samples/second from points within 1.125 degrees of the centre and mid-points of 00062 each symbol. It feeds these to a T/2 adaptive equalizer. The Gardner algorithm is 00063 used to track the symbolsm, and tune the sampling. Initially this algorithm is very 00064 lightly damped, to ensure the symbol alignment pulls in quickly. Because the sampling 00065 rate will not be precisely the same as the transmitter's (the spec. says the symbol 00066 timing should be within 0.01%), the receiver constantly evaluates and corrects 00067 this sampling throughout its operation. During the symbol timing maintainence phase, 00068 the algorithm uses heavier damping to stabilise the sampling points, 00069 00070 00071 Gardner plus integrate and dump approach to 00072 updates. This heavy damping achieves several things - the Gardner algorithm is 00073 statistically based, so the statistics must be smoothed; a number of samples 00074 must be fed to the equalizer buffer before the equalizer output actually 00075 responds to a step change in the sampling; we need to prevent rapid fluctuations 00076 in the sampling position, due to the optimal position being close to a boundary. 00077 00078 The carrier is specified as 1700Hz +- 1Hz at the transmitter, and 1700 +-7Hz at 00079 the receiver. The receive carrier would only be this inaccurate if the link 00080 includes FDM sections. These are being phased out, but the design must still 00081 allow for the worst case. Using an initial 1700Hz signal for demodulation gives 00082 a worst case rotation rate for the constellation of about one degree per symbol. 00083 Once the Gardner algorithm has been given time to lock to the symbol timing of 00084 the initial alternating pattern, the phase of the demodulated signal is recorded 00085 on two successive symbols - once for each of the constellation positions. The 00086 receiver then tracks the symbol alternations, until a large phase jump occurs. 00087 This signifies the start of the next phase of the training sequence. At this 00088 point the total phase shift between the original recorded symbol phase, and the 00089 symbol phase just before the phase jump occurred is used to provide a coarse 00090 estimation of the rotation rate of the constellation, and it current absolute 00091 angle of rotation. These are used to update the current carrier phase and phase 00092 update rate in the carrier DDS. The working data already in the pulse shaping 00093 filter and equalizer buffers is given a similar step rotation to pull it all 00094 into line. From this point on, a heavily damped integrate and dump approach, 00095 based on the angular difference between each received constellation position and 00096 its expected position, is sufficient to track the carrier, and maintain phase 00097 alignment. A fast rough approximator for the arc-tangent function is adequate 00098 for the estimation of the angular error. 00099 00100 The next phase of the training sequence is a scrambled sequence of two 00101 particular symbols. We train the T/2 adaptive equalizer using this sequence. The 00102 scrambling makes the signal sufficiently diverse to ensure the equalizer 00103 converges to the proper generalised solution. At the end of this sequence, the 00104 equalizer should be sufficiently well adapted that is can correctly resolve the 00105 full QAM constellation. However, the equalizer continues to adapt throughout 00106 operation of the modem, fine tuning on the more complex data patterns of the 00107 full QAM constellation. 00108 00109 In the last phase of the training sequence, the modem enters normal data 00110 operation, with a short defined period of all ones as data. As in most high 00111 speed modems, data in a V.29 modem passes through a scrambler, to whiten the 00112 spectrum of the signal. The transmitter should initialise its data scrambler, 00113 and pass the ones through it. At the end of the ones, real data begins to pass 00114 through the scrambler, and the transmit modem is in normal operation. The 00115 receiver tests that ones are really received, in order to verify the modem 00116 trained correctly. If all is well, the data following the ones is fed to the 00117 application, and the receive modem is up and running. Unfortunately, some 00118 transmit side of some real V.29 modems fail to initialise their scrambler before 00119 sending the ones. This means the first 23 received bits (the length of the 00120 scrambler register) cannot be trusted for the test. The receive modem, 00121 therefore, only tests that bits starting at bit 24 are really ones. 00122 */ 00123 00124 #define V29_EQUALIZER_PRE_LEN 15 /* this much before the real event */ 00125 #define V29_EQUALIZER_POST_LEN 15 /* this much after the real event */ 00126 #define V29_EQUALIZER_MASK 31 /* one less than a power of 2 >= (2*V29_EQUALIZER_LEN + 1) */ 00127 00128 #define V29RX_FILTER_STEPS 27 00129 00130 typedef void (qam_report_handler_t)(void *user_data, const complex_t *constel, const complex_t *target, int symbol); 00131 00132 /*! 00133 V.29 modem receive side descriptor. This defines the working state for a 00134 single instance of a V.29 modem receiver. 00135 */ 00136 typedef struct 00137 { 00138 /*! \brief The bit rate of the modem. Valid values are 4800, 7200 and 9600. */ 00139 int bit_rate; 00140 /*! \brief The callback function used to put each bit received. */ 00141 put_bit_func_t put_bit; 00142 /*! \brief A user specified opaque pointer passed to the put_bit routine. */ 00143 void *user_data; 00144 /*! \brief A callback function which may be enabled to report every symbol's 00145 constellation position. */ 00146 qam_report_handler_t *qam_report; 00147 /*! \brief A user specified opaque pointer passed to the qam_report callback 00148 routine. */ 00149 void *qam_user_data; 00150 00151 /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */ 00152 complex_t rrc_filter[2*V29RX_FILTER_STEPS]; 00153 /*! \brief Current offset into the RRC pulse shaping filter buffer. */ 00154 int rrc_filter_step; 00155 00156 /*! \brief The register for the data scrambler. */ 00157 unsigned int scramble_reg; 00158 /*! \brief The register for the training scrambler. */ 00159 uint8_t training_scramble_reg; 00160 int in_training; 00161 int training_cd; 00162 int training_count; 00163 float training_error; 00164 int carrier_present; 00165 00166 /*! \brief The current phase of the carrier (i.e. the DDS parameter). */ 00167 uint32_t carrier_phase; 00168 /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */ 00169 int32_t carrier_phase_rate; 00170 float carrier_track_p; 00171 float carrier_track_i; 00172 00173 power_meter_t power; 00174 int32_t carrier_on_power; 00175 int32_t carrier_off_power; 00176 float agc_scaling; 00177 00178 int constellation_state; 00179 00180 float eq_delta; 00181 /*! \brief The adaptive equalizer coefficients */ 00182 complex_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN]; 00183 complex_t eq_buf[V29_EQUALIZER_MASK + 1]; 00184 /*! \brief Current offset into equalizer buffer. */ 00185 int eq_step; 00186 int eq_put_step; 00187 int eq_skip; 00188 00189 /*! \brief Integration variable for damping the Gardner algorithm tests. */ 00190 int gardner_integrate; 00191 /*! \brief Current step size of Gardner algorithm integration. */ 00192 int gardner_step; 00193 /*! \brief The total gardner timing correction, since the carrier came up. 00194 This is only for performance analysis purposes. */ 00195 int gardner_total_correction; 00196 /*! \brief The current fractional phase of the baud timing. */ 00197 int baud_phase; 00198 00199 /*! \brief Starting phase angles for the coarse carrier aquisition step. */ 00200 int32_t start_angles[2]; 00201 /*! \brief History list of phase angles for the coarse carrier aquisition step. */ 00202 int32_t angles[16]; 00203 /*! \brief Error and flow logging control */ 00204 logging_state_t logging; 00205 } v29_rx_state_t; 00206 00207 #ifdef __cplusplus 00208 extern "C" { 00209 #endif 00210 00211 /*! Initialise a V.29 modem receive context. 00212 \brief Initialise a V.29 modem receive context. 00213 \param s The modem context. 00214 \param rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00215 \param put_bit The callback routine used to put the received data. 00216 \param user_data An opaque pointer passed to the put_bit routine. 00217 \return A pointer to the modem context, or NULL if there was a problem. */ 00218 v29_rx_state_t *v29_rx_init(v29_rx_state_t *s, int rate, put_bit_func_t put_bit, void *user_data); 00219 00220 /*! Reinitialise an existing V.29 modem receive context. 00221 \brief Reinitialise an existing V.29 modem receive context. 00222 \param s The modem context. 00223 \param rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00224 \return 0 for OK, -1 for bad parameter */ 00225 int v29_rx_restart(v29_rx_state_t *s, int rate); 00226 00227 /*! Release a V.29 modem receive context. 00228 \brief Release a V.29 modem receive context. 00229 \param s The modem context. 00230 \return 0 for OK */ 00231 int v29_rx_release(v29_rx_state_t *s); 00232 00233 /*! Change the put_bit function associated with a V.29 modem receive context. 00234 \brief Change the put_bit function associated with a V.29 modem receive context. 00235 \param s The modem context. 00236 \param put_bit The callback routine used to handle received bits. 00237 \param user_data An opaque pointer. */ 00238 void v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data); 00239 00240 /*! Process a block of received V.29 modem audio samples. 00241 \brief Process a block of received V.29 modem audio samples. 00242 \param s The modem context. 00243 \param amp The audio sample buffer. 00244 \param len The number of samples in the buffer. 00245 \return The number of samples unprocessed. */ 00246 int v29_rx(v29_rx_state_t *s, const int16_t *amp, int len); 00247 00248 /*! Get a snapshot of the current equalizer coefficients. 00249 \brief Get a snapshot of the current equalizer coefficients. 00250 \param s The modem context. 00251 \param coeffs The vector of complex coefficients. 00252 \return The number of coefficients in the vector. */ 00253 int v29_rx_equalizer_state(v29_rx_state_t *s, complex_t **coeffs); 00254 00255 /*! Get the current received carrier frequency. 00256 \param s The modem context. 00257 \return The frequency, in Hertz. */ 00258 float v29_rx_carrier_frequency(v29_rx_state_t *s); 00259 00260 /*! Get the current symbol timing correction since startup. 00261 \param s The modem context. 00262 \return The correction. */ 00263 float v29_rx_symbol_timing_correction(v29_rx_state_t *s); 00264 00265 /*! Get the current received signal power. 00266 \param s The modem context. 00267 \return The signal power, in dBm0. */ 00268 float v29_rx_signal_power(v29_rx_state_t *s); 00269 00270 /*! Set the power level at which the carrier detection will cut in 00271 \param s The modem context. 00272 \param cutoff The signal cutoff power, in dBm0. */ 00273 void v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff); 00274 00275 /*! Set a handler routine to process QAM status reports 00276 \param s The modem context. 00277 \param handler The handler routine. 00278 \param user_data An opaque pointer passed to the handler routine. */ 00279 void v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t *handler, void *user_data); 00280 00281 #ifdef __cplusplus 00282 } 00283 #endif 00284 00285 #endif 00286 /*- End of file ------------------------------------------------------------*/