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