dc_restore.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * dc_restore.h - General telephony routines to restore the zero D.C.
00005  *                level to audio which has a D.C. bias.
00006  *
00007  * Written by Steve Underwood <steveu@coppice.org>
00008  *
00009  * Copyright (C) 2001 Steve Underwood
00010  *
00011  * All rights reserved.
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00026  *
00027  * $Id: dc_restore.h,v 1.10 2005/11/23 17:09:47 steveu Exp $
00028  */
00029 
00030 /*! \file */
00031 
00032 #if !defined(_DC_RESTORE_H_)
00033 #define _DC_RESTORE_H_
00034 
00035 /*! \page dc_restore_page Removing DC bias from a signal
00036 
00037 \section dc_restore_page_sec_1 What does it do?
00038 
00039 Telecoms signals often contain considerable DC, but DC upsets a lot of signal
00040 processing functions. Placing a zero DC restorer at the front of the processing
00041 chain can often simplify the downstream processing. 
00042 
00043 \section dc_restore_page_sec_2 How does it work?
00044 
00045 The DC restorer uses a leaky integrator to provide a long-ish term estimate of
00046 the DC bias in the signal. A 32 bit estimate is used for the 16 bit audio, so
00047 the noise introduced by the estimation can be keep in the lower bits, and the 16
00048 bit DC value, which is subtracted from the signal, is fairly clean. The
00049 following code fragment shows the algorithm used. dc_bias is a 32 bit integer,
00050 while the sample and the resulting clean_sample are 16 bit integers. 
00051 
00052     dc_bias += ((((int32_t) sample << 15) - dc_bias) >> 14);
00053     clean_sample = sample - (dc_bias >> 15); 
00054 */
00055 
00056 /*!
00057     Zero DC restoration descriptor. This defines the working state for a single
00058     instance of DC content filter.
00059 */
00060 typedef struct
00061 {
00062     int32_t state;
00063 } dc_restore_state_t;
00064 
00065 #ifdef __cplusplus
00066 extern "C" {
00067 #endif
00068 
00069 static __inline__ void dc_restore_init(dc_restore_state_t *dc)
00070 {
00071     dc->state = 0;
00072 }
00073 /*- End of function --------------------------------------------------------*/
00074 
00075 static __inline__ int16_t dc_restore(dc_restore_state_t *dc, int16_t sample)
00076 {
00077     dc->state += ((((int32_t) sample << 15) - dc->state) >> 14);
00078     return  sample - (dc->state >> 15);
00079 }
00080 /*- End of function --------------------------------------------------------*/
00081 
00082 static __inline__ int16_t dc_restore_estimate(dc_restore_state_t *dc)
00083 {
00084     return  (dc->state >> 15);
00085 }
00086 /*- End of function --------------------------------------------------------*/
00087 
00088 static __inline__ int16_t saturate(int32_t amp)
00089 {
00090     if (amp > INT16_MAX)
00091         return  INT16_MAX;
00092     if (amp < INT16_MIN)
00093         return  INT16_MIN;
00094     return (int16_t) amp;
00095 }
00096 /*- End of function --------------------------------------------------------*/
00097 
00098 static __inline__ int16_t fsaturate(double damp)
00099 {
00100     if (damp > 32767.0)
00101         return  INT16_MAX;
00102     if (damp < -32768.0)
00103         return  INT16_MIN;
00104     return lrintf(damp);
00105 }
00106 /*- End of function --------------------------------------------------------*/
00107 
00108 #ifdef __cplusplus
00109 }
00110 #endif
00111 
00112 #endif
00113 /*- End of file ------------------------------------------------------------*/

Generated on Fri Nov 10 09:40:23 2006 for libspandsp by  doxygen 1.5.1