spandsp 0.0.6
v29rx.h
Go to the documentation of this file.
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 
00026 /*! \file */
00027 
00028 #if !defined(_SPANDSP_V29RX_H_)
00029 #define _SPANDSP_V29RX_H_
00030 
00031 /*! \page v29rx_page The V.29 receiver
00032 \section v29rx_page_sec_1 What does it do?
00033 The V.29 receiver implements the receive side of a V.29 modem. This can operate
00034 at data rates of 9600, 7200 and 4800 bits/s. The audio input is a stream of 16
00035 bit samples, at 8000 samples/second. The transmit and receive side of V.29
00036 modems operate independantly. V.29 is mostly used for FAX transmission, where it
00037 provides the standard 9600 and 7200 bits/s rates (the 4800 bits/s mode is not
00038 used for FAX). 
00039 
00040 \section v29rx_page_sec_2 How does it work?
00041 V.29 operates at 2400 baud for all three bit rates. It uses 16-QAM modulation for
00042 9600bps, 8-QAM for 7200bps, and 4-PSK for 4800bps. A training sequence is specified
00043 at the start of transmission, which makes the design of a V.29 receiver relatively
00044 straightforward.
00045 
00046 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 settling, the
00052 front end gain is locked, and the adaptive equalizer tracks any subsequent
00053 signal level variation. The signal is oversampled to 24000 samples/second (i.e.
00054 signal, zero, zero, signal, zero, zero, ...) and fed to a complex root raised
00055 cosine pulse shaping filter. This filter has been modified from the conventional
00056 root raised cosine filter, by shifting it up the band, to be centred at the nominal
00057 carrier frequency. This filter interpolates the samples, pulse shapes, and performs
00058 a fractional sample delay at the same time. 48 sets of filter coefficients are used to
00059 achieve a set of finely spaces fractional sample delays, between zero and
00060 one sample. By choosing every fifth sample, and the appropriate set of filter
00061 coefficients, the properly tuned symbol tracker can select data samples at 4800
00062 samples/second from points within 1.125 degrees of the centre and mid-points of
00063 each symbol. The output of the filter is multiplied by a complex carrier, generated
00064 by a DDS. The result is a baseband signal, requiring no further filtering, apart from
00065 an adaptive equalizer. The baseband signal is fed to a T/2 adaptive equalizer.
00066 A band edge component maximisation algorithm is used to tune the sampling, so the samples
00067 fed to the equalizer are close to the mid point and edges of each symbol. Initially
00068 the algorithm is very lightly damped, to ensure the symbol alignment pulls in
00069 quickly. Because the sampling rate will not be precisely the same as the
00070 transmitter's (the spec. says the symbol timing should be within 0.01%), the
00071 receiver constantly evaluates and corrects this sampling throughout its
00072 operation. During the symbol timing maintainence phase, the algorithm uses
00073 a heavier damping.
00074 
00075 The carrier is specified as 1700Hz +-1Hz at the transmitter, and 1700 +-7Hz at
00076 the receiver. The receive carrier would only be this inaccurate if the link
00077 includes FDM sections. These are being phased out, but the design must still
00078 allow for the worst case. Using an initial 1700Hz signal for demodulation gives
00079 a worst case rotation rate for the constellation of about one degree per symbol.
00080 Once the symbol timing synchronisation algorithm has been given time to lock to
00081 the symbol timing of the initial alternating pattern, the phase of the demodulated
00082 signal is recorded on two successive symbols - once for each of the constellation
00083 positions. The receiver then tracks the symbol alternations, until a large phase jump
00084 occurs. This signifies the start of the next phase of the training sequence. At this
00085 point the total phase shift between the original recorded symbol phase, and the
00086 symbol phase just before the phase jump occurred is used to provide a coarse
00087 estimation of the rotation rate of the constellation, and it current absolute
00088 angle of rotation. These are used to update the current carrier phase and phase
00089 update rate in the carrier DDS. The working data already in the pulse shaping
00090 filter and equalizer buffers is given a similar step rotation to pull it all
00091 into line. From this point on, a heavily damped integrate and dump approach,
00092 based on the angular difference between each received constellation position and
00093 its expected position, is sufficient to track the carrier, and maintain phase
00094 alignment. A fast rough approximator for the arc-tangent function is adequate
00095 for the estimation of the angular error. 
00096 
00097 The next phase of the training sequence is a scrambled sequence of two
00098 particular symbols. We train the T/2 adaptive equalizer using this sequence. The
00099 scrambling makes the signal sufficiently diverse to ensure the equalizer
00100 converges to the proper generalised solution. At the end of this sequence, the
00101 equalizer should be sufficiently well adapted that is can correctly resolve the
00102 full QAM constellation. However, the equalizer continues to adapt throughout
00103 operation of the modem, fine tuning on the more complex data patterns of the
00104 full QAM constellation. 
00105 
00106 In the last phase of the training sequence, the modem enters normal data
00107 operation, with a short defined period of all ones as data. As in most high
00108 speed modems, data in a V.29 modem passes through a scrambler, to whiten the
00109 spectrum of the signal. The transmitter should initialise its data scrambler,
00110 and pass the ones through it. At the end of the ones, real data begins to pass
00111 through the scrambler, and the transmit modem is in normal operation. The
00112 receiver tests that ones are really received, in order to verify the modem
00113 trained correctly. If all is well, the data following the ones is fed to the
00114 application, and the receive modem is up and running. Unfortunately, some
00115 transmit side of some real V.29 modems fail to initialise their scrambler before
00116 sending the ones. This means the first 23 received bits (the length of the
00117 scrambler register) cannot be trusted for the test. The receive modem,
00118 therefore, only tests that bits starting at bit 24 are really ones. 
00119 */
00120 
00121 typedef void (*qam_report_handler_t)(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol);
00122 
00123 /*!
00124     V.29 modem receive side descriptor. This defines the working state for a
00125     single instance of a V.29 modem receiver.
00126 */
00127 typedef struct v29_rx_state_s v29_rx_state_t;
00128 
00129 #if defined(__cplusplus)
00130 extern "C"
00131 {
00132 #endif
00133 
00134 /*! Initialise a V.29 modem receive context.
00135     \brief Initialise a V.29 modem receive context.
00136     \param s The modem context.
00137     \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
00138     \param put_bit The callback routine used to put the received data.
00139     \param user_data An opaque pointer passed to the put_bit routine.
00140     \return A pointer to the modem context, or NULL if there was a problem. */
00141 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);
00142 
00143 /*! Reinitialise an existing V.29 modem receive context.
00144     \brief Reinitialise an existing V.29 modem receive context.
00145     \param s The modem context.
00146     \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
00147     \param old_train TRUE if a previous trained values are to be reused.
00148     \return 0 for OK, -1 for bad parameter */
00149 SPAN_DECLARE(int) v29_rx_restart(v29_rx_state_t *s, int bit_rate, int old_train);
00150 
00151 /*! Release a V.29 modem receive context.
00152     \brief Release a V.29 modem receive context.
00153     \param s The modem context.
00154     \return 0 for OK */
00155 SPAN_DECLARE(int) v29_rx_release(v29_rx_state_t *s);
00156 
00157 /*! Free a V.29 modem receive context.
00158     \brief Free a V.29 modem receive context.
00159     \param s The modem context.
00160     \return 0 for OK */
00161 SPAN_DECLARE(int) v29_rx_free(v29_rx_state_t *s);
00162 
00163 /*! Get the logging context associated with a V.29 modem receive context.
00164     \brief Get the logging context associated with a V.29 modem receive context.
00165     \param s The modem context.
00166     \return A pointer to the logging context */
00167 SPAN_DECLARE(logging_state_t *) v29_rx_get_logging_state(v29_rx_state_t *s);
00168 
00169 /*! Change the put_bit function associated with a V.29 modem receive context.
00170     \brief Change the put_bit function associated with a V.29 modem receive context.
00171     \param s The modem context.
00172     \param put_bit The callback routine used to handle received bits.
00173     \param user_data An opaque pointer. */
00174 SPAN_DECLARE(void) v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
00175 
00176 /*! Change the modem status report function associated with a V.29 modem receive context.
00177     \brief Change the modem status report function associated with a V.29 modem receive context.
00178     \param s The modem context.
00179     \param handler The callback routine used to report modem status changes.
00180     \param user_data An opaque pointer. */
00181 SPAN_DECLARE(void) v29_rx_set_modem_status_handler(v29_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
00182 
00183 /*! Process a block of received V.29 modem audio samples.
00184     \brief Process a block of received V.29 modem audio samples.
00185     \param s The modem context.
00186     \param amp The audio sample buffer.
00187     \param len The number of samples in the buffer.
00188     \return The number of samples unprocessed. */
00189 SPAN_DECLARE_NONSTD(int) v29_rx(v29_rx_state_t *s, const int16_t amp[], int len);
00190 
00191 /*! Fake processing of a missing block of received V.29 modem audio samples.
00192     (e.g due to packet loss).
00193     \brief Fake processing of a missing block of received V.29 modem audio samples.
00194     \param s The modem context.
00195     \param len The number of samples to fake.
00196     \return The number of samples unprocessed. */
00197 SPAN_DECLARE_NONSTD(int) v29_rx_fillin(v29_rx_state_t *s, int len);
00198 
00199 /*! Get a snapshot of the current equalizer coefficients.
00200     \brief Get a snapshot of the current equalizer coefficients.
00201     \param s The modem context.
00202     \param coeffs The vector of complex coefficients.
00203     \return The number of coefficients in the vector. */
00204 #if defined(SPANDSP_USE_FIXED_POINT)
00205 SPAN_DECLARE(int) v29_rx_equalizer_state(v29_rx_state_t *s, complexi16_t **coeffs);
00206 #else
00207 SPAN_DECLARE(int) v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs);
00208 #endif
00209 
00210 /*! Get the current received carrier frequency.
00211     \param s The modem context.
00212     \return The frequency, in Hertz. */
00213 SPAN_DECLARE(float) v29_rx_carrier_frequency(v29_rx_state_t *s);
00214 
00215 /*! Get the current symbol timing correction since startup.
00216     \param s The modem context.
00217     \return The correction. */
00218 SPAN_DECLARE(float) v29_rx_symbol_timing_correction(v29_rx_state_t *s);
00219 
00220 /*! Get the current received signal power.
00221     \param s The modem context.
00222     \return The signal power, in dBm0. */
00223 SPAN_DECLARE(float) v29_rx_signal_power(v29_rx_state_t *s);
00224 
00225 /*! Set the power level at which the carrier detection will cut in
00226     \param s The modem context.
00227     \param cutoff The signal cutoff power, in dBm0. */
00228 SPAN_DECLARE(void) v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff);
00229 
00230 /*! Set a handler routine to process QAM status reports
00231     \param s The modem context.
00232     \param handler The handler routine.
00233     \param user_data An opaque pointer passed to the handler routine. */
00234 SPAN_DECLARE(void) v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t handler, void *user_data);
00235 
00236 #if defined(__cplusplus)
00237 }
00238 #endif
00239 
00240 #endif
00241 /*- End of file ------------------------------------------------------------*/