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.70 2009/04/12 09:12:11 steveu Exp $ 00026 */ 00027 00028 /*! \file */ 00029 00030 #if !defined(_SPANDSP_V29RX_H_) 00031 #define _SPANDSP_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 /*! Samples before the target position in the equalizer buffer */ 00126 #define V29_EQUALIZER_PRE_LEN 16 00127 /*! Samples after the target position in the equalizer buffer */ 00128 #define V29_EQUALIZER_POST_LEN 14 00129 00130 /*! The number of taps in the pulse shaping/bandpass filter */ 00131 #define V29_RX_FILTER_STEPS 27 00132 00133 typedef void (*qam_report_handler_t)(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol); 00134 00135 /*! 00136 V.29 modem receive side descriptor. This defines the working state for a 00137 single instance of a V.29 modem receiver. 00138 */ 00139 typedef struct v29_rx_state_s v29_rx_state_t; 00140 00141 #if defined(__cplusplus) 00142 extern "C" 00143 { 00144 #endif 00145 00146 /*! Initialise a V.29 modem receive context. 00147 \brief Initialise a V.29 modem receive context. 00148 \param s The modem context. 00149 \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00150 \param put_bit The callback routine used to put the received data. 00151 \param user_data An opaque pointer passed to the put_bit routine. 00152 \return A pointer to the modem context, or NULL if there was a problem. */ 00153 SPAN_DECLARE(v29_rx_state_t *) v29_rx_init(v29_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data); 00154 00155 /*! Reinitialise an existing V.29 modem receive context. 00156 \brief Reinitialise an existing V.29 modem receive context. 00157 \param s The modem context. 00158 \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600. 00159 \param old_train TRUE if a previous trained values are to be reused. 00160 \return 0 for OK, -1 for bad parameter */ 00161 SPAN_DECLARE(int) v29_rx_restart(v29_rx_state_t *s, int bit_rate, int old_train); 00162 00163 /*! Release a V.29 modem receive context. 00164 \brief Release a V.29 modem receive context. 00165 \param s The modem context. 00166 \return 0 for OK */ 00167 SPAN_DECLARE(int) v29_rx_release(v29_rx_state_t *s); 00168 00169 /*! Free a V.29 modem receive context. 00170 \brief Free a V.29 modem receive context. 00171 \param s The modem context. 00172 \return 0 for OK */ 00173 SPAN_DECLARE(int) v29_rx_free(v29_rx_state_t *s); 00174 00175 /*! Get the logging context associated with a V.29 modem receive context. 00176 \brief Get the logging context associated with a V.29 modem receive context. 00177 \param s The modem context. 00178 \return A pointer to the logging context */ 00179 SPAN_DECLARE(logging_state_t *) v29_rx_get_logging_state(v29_rx_state_t *s); 00180 00181 /*! Change the put_bit function associated with a V.29 modem receive context. 00182 \brief Change the put_bit function associated with a V.29 modem receive context. 00183 \param s The modem context. 00184 \param put_bit The callback routine used to handle received bits. 00185 \param user_data An opaque pointer. */ 00186 SPAN_DECLARE(void) v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data); 00187 00188 /*! Change the modem status report function associated with a V.29 modem receive context. 00189 \brief Change the modem status report function associated with a V.29 modem receive context. 00190 \param s The modem context. 00191 \param handler The callback routine used to report modem status changes. 00192 \param user_data An opaque pointer. */ 00193 SPAN_DECLARE(void) v29_rx_set_modem_status_handler(v29_rx_state_t *s, modem_rx_status_func_t handler, void *user_data); 00194 00195 /*! Process a block of received V.29 modem audio samples. 00196 \brief Process a block of received V.29 modem audio samples. 00197 \param s The modem context. 00198 \param amp The audio sample buffer. 00199 \param len The number of samples in the buffer. 00200 \return The number of samples unprocessed. */ 00201 SPAN_DECLARE(int) v29_rx(v29_rx_state_t *s, const int16_t amp[], int len); 00202 00203 /*! Fake processing of a missing block of received V.29 modem audio samples. 00204 (e.g due to packet loss). 00205 \brief Fake processing of a missing block of received V.29 modem audio samples. 00206 \param s The modem context. 00207 \param len The number of samples to fake. 00208 \return The number of samples unprocessed. */ 00209 SPAN_DECLARE(int) v29_rx_fillin(v29_rx_state_t *s, int len); 00210 00211 /*! Get a snapshot of the current equalizer coefficients. 00212 \brief Get a snapshot of the current equalizer coefficients. 00213 \param s The modem context. 00214 \param coeffs The vector of complex coefficients. 00215 \return The number of coefficients in the vector. */ 00216 #if defined(SPANDSP_USE_FIXED_POINT) 00217 SPAN_DECLARE(int) v29_rx_equalizer_state(v29_rx_state_t *s, complexi16_t **coeffs); 00218 #else 00219 SPAN_DECLARE(int) v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs); 00220 #endif 00221 00222 /*! Get the current received carrier frequency. 00223 \param s The modem context. 00224 \return The frequency, in Hertz. */ 00225 SPAN_DECLARE(float) v29_rx_carrier_frequency(v29_rx_state_t *s); 00226 00227 /*! Get the current symbol timing correction since startup. 00228 \param s The modem context. 00229 \return The correction. */ 00230 SPAN_DECLARE(float) v29_rx_symbol_timing_correction(v29_rx_state_t *s); 00231 00232 /*! Get the current received signal power. 00233 \param s The modem context. 00234 \return The signal power, in dBm0. */ 00235 SPAN_DECLARE(float) v29_rx_signal_power(v29_rx_state_t *s); 00236 00237 /*! Set the power level at which the carrier detection will cut in 00238 \param s The modem context. 00239 \param cutoff The signal cutoff power, in dBm0. */ 00240 SPAN_DECLARE(void) v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff); 00241 00242 /*! Set a handler routine to process QAM status reports 00243 \param s The modem context. 00244 \param handler The handler routine. 00245 \param user_data An opaque pointer passed to the handler routine. */ 00246 SPAN_DECLARE(void) v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t handler, void *user_data); 00247 00248 #if defined(__cplusplus) 00249 } 00250 #endif 00251 00252 #endif 00253 /*- End of file ------------------------------------------------------------*/