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 Lesser General Public License version 2.1, 00014 * as 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 Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License 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 5963 2008-07-27 13:06:19Z oron $ 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 #if defined(SPANDSP_USE_FIXED_POINT) 00154 int16_t rrc_filter[2*V29_RX_FILTER_STEPS]; 00155 #else 00156 float rrc_filter[2*V29_RX_FILTER_STEPS]; 00157 #endif 00158 /*! \brief Current offset into the RRC pulse shaping filter buffer. */ 00159 int rrc_filter_step; 00160 00161 /*! \brief The register for the data scrambler. */ 00162 unsigned int scramble_reg; 00163 /*! \brief The register for the training scrambler. */ 00164 uint8_t training_scramble_reg; 00165 /*! \brief The section of the training data we are currently in. */ 00166 int training_stage; 00167 /*! \brief The current step in the table of CD constellation positions. */ 00168 int training_cd; 00169 /*! \brief A count of how far through the current training step we are. */ 00170 int training_count; 00171 /*! \brief A measure of how much mismatch there is between the real constellation, 00172 and the decoded symbol positions. */ 00173 float training_error; 00174 /*! \brief The value of the last signal sample, using the a simple HPF for signal power estimation. */ 00175 int16_t last_sample; 00176 /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.29 signal. */ 00177 int signal_present; 00178 /*! \brief Whether or not a carrier drop was detected and the signal delivery is pending. */ 00179 int carrier_drop_pending; 00180 /*! \brief A count of the current consecutive samples below the carrier off threshold. */ 00181 int low_samples; 00182 /*! \brief A highest magnitude sample seen. */ 00183 int16_t high_sample; 00184 /*! \brief TRUE if the previous trained values are to be reused. */ 00185 int old_train; 00186 00187 /*! \brief The current phase of the carrier (i.e. the DDS parameter). */ 00188 uint32_t carrier_phase; 00189 /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */ 00190 int32_t carrier_phase_rate; 00191 /*! \brief The carrier update rate saved for reuse when using short training. */ 00192 int32_t carrier_phase_rate_save; 00193 /*! \brief The proportional part of the carrier tracking filter. */ 00194 float carrier_track_p; 00195 /*! \brief The integral part of the carrier tracking filter. */ 00196 float carrier_track_i; 00197 00198 /*! \brief A power meter, to measure the HPF'ed signal power in the channel. */ 00199 power_meter_t power; 00200 /*! \brief The power meter level at which carrier on is declared. */ 00201 int32_t carrier_on_power; 00202 /*! \brief The power meter level at which carrier off is declared. */ 00203 int32_t carrier_off_power; 00204 /*! \brief The scaling factor accessed by the AGC algorithm. */ 00205 float agc_scaling; 00206 /*! \brief The previous value of agc_scaling, needed to reuse old training. */ 00207 float agc_scaling_save; 00208 00209 int constellation_state; 00210 00211 /*! \brief The current delta factor for updating the equalizer coefficients. */ 00212 float eq_delta; 00213 #if defined(SPANDSP_USE_FIXED_POINTx) 00214 /*! \brief The adaptive equalizer coefficients. */ 00215 complexi_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN]; 00216 /*! \brief A saved set of adaptive equalizer coefficients for use after restarts. */ 00217 complexi_t eq_coeff_save[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN]; 00218 /*! \brief The equalizer signal buffer. */ 00219 complexi_t eq_buf[V29_EQUALIZER_MASK + 1]; 00220 #else 00221 complexf_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN]; 00222 complexf_t eq_coeff_save[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN]; 00223 complexf_t eq_buf[V29_EQUALIZER_MASK + 1]; 00224 #endif 00225 /*! \brief Current offset into the equalizer buffer. */ 00226 int eq_step; 00227 /*! \brief Current write offset into the equalizer buffer. */ 00228 int eq_put_step; 00229 /*! \brief Symbol counter to the next equalizer update. */ 00230 int eq_skip; 00231 00232 /*! \brief The current half of the baud. */ 00233 int baud_half; 00234 #if defined(SPANDSP_USE_FIXED_POINTx) 00235 /*! Low band edge filter for symbol sync. */ 00236 int32_t symbol_sync_low[2]; 00237 /*! High band edge filter for symbol sync. */ 00238 int32_t symbol_sync_high[2]; 00239 /*! DC filter for symbol sync. */ 00240 int32_t symbol_sync_dc_filter[2]; 00241 /*! Baud phase for symbol sync. */ 00242 int32_t baud_phase; 00243 #else 00244 /*! Low band edge filter for symbol sync. */ 00245 float symbol_sync_low[2]; 00246 /*! High band edge filter for symbol sync. */ 00247 float symbol_sync_high[2]; 00248 /*! DC filter for symbol sync. */ 00249 float symbol_sync_dc_filter[2]; 00250 /*! Baud phase for symbol sync. */ 00251 float baud_phase; 00252 #endif 00253 00254 /*! \brief The total symbol timing correction since the carrier came up. 00255 This is only for performance analysis purposes. */ 00256 int total_baud_timing_correction; 00257 00258 /*! \brief Starting phase angles for the coarse carrier aquisition step. */ 00259 int32_t start_angles[2]; 00260 /*! \brief History list of phase angles for the coarse carrier aquisition step. */ 00261 int32_t angles[16]; 00262 /*! \brief Error and flow logging control */ 00263 logging_state_t logging; 00264 } v29_rx_state_t; 00265 00266 #if defined(__cplusplus) 00267 extern "C" 00268 { 00269 #endif 00270 00271 /*! Initialise a V.29 modem receive context. 00272 \brief Initialise a V.29 modem receive context. 00273 \param s The modem context. 00274 \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00275 \param put_bit The callback routine used to put the received data. 00276 \param user_data An opaque pointer passed to the put_bit routine. 00277 \return A pointer to the modem context, or NULL if there was a problem. */ 00278 v29_rx_state_t *v29_rx_init(v29_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data); 00279 00280 /*! Reinitialise an existing V.29 modem receive context. 00281 \brief Reinitialise an existing V.29 modem receive context. 00282 \param s The modem context. 00283 \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00284 \param old_train TRUE if a previous trained values are to be reused. 00285 \return 0 for OK, -1 for bad parameter */ 00286 int v29_rx_restart(v29_rx_state_t *s, int bit_rate, int old_train); 00287 00288 /*! Free a V.29 modem receive context. 00289 \brief Free a V.29 modem receive context. 00290 \param s The modem context. 00291 \return 0 for OK */ 00292 int v29_rx_free(v29_rx_state_t *s); 00293 00294 /*! Change the put_bit function associated with a V.29 modem receive context. 00295 \brief Change the put_bit function associated with a V.29 modem receive context. 00296 \param s The modem context. 00297 \param put_bit The callback routine used to handle received bits. 00298 \param user_data An opaque pointer. */ 00299 void v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data); 00300 00301 /*! Process a block of received V.29 modem audio samples. 00302 \brief Process a block of received V.29 modem audio samples. 00303 \param s The modem context. 00304 \param amp The audio sample buffer. 00305 \param len The number of samples in the buffer. 00306 \return The number of samples unprocessed. */ 00307 int v29_rx(v29_rx_state_t *s, const int16_t amp[], int len); 00308 00309 /*! Get a snapshot of the current equalizer coefficients. 00310 \brief Get a snapshot of the current equalizer coefficients. 00311 \param s The modem context. 00312 \param coeffs The vector of complex coefficients. 00313 \return The number of coefficients in the vector. */ 00314 int v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs); 00315 00316 /*! Get the current received carrier frequency. 00317 \param s The modem context. 00318 \return The frequency, in Hertz. */ 00319 float v29_rx_carrier_frequency(v29_rx_state_t *s); 00320 00321 /*! Get the current symbol timing correction since startup. 00322 \param s The modem context. 00323 \return The correction. */ 00324 float v29_rx_symbol_timing_correction(v29_rx_state_t *s); 00325 00326 /*! Get the current received signal power. 00327 \param s The modem context. 00328 \return The signal power, in dBm0. */ 00329 float v29_rx_signal_power(v29_rx_state_t *s); 00330 00331 /*! Set the power level at which the carrier detection will cut in 00332 \param s The modem context. 00333 \param cutoff The signal cutoff power, in dBm0. */ 00334 void v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff); 00335 00336 /*! Set a handler routine to process QAM status reports 00337 \param s The modem context. 00338 \param handler The handler routine. 00339 \param user_data An opaque pointer passed to the handler routine. */ 00340 void v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t *handler, void *user_data); 00341 00342 #if defined(__cplusplus) 00343 } 00344 #endif 00345 00346 #endif 00347 /*- End of file ------------------------------------------------------------*/