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