spandsp
0.0.6
|
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 Lesser General Public License version 2.1, 00015 * as published by the Free Software Foundation. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU Lesser General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU Lesser General Public 00023 * License along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00025 */ 00026 00027 /*! \file */ 00028 00029 #if !defined(_SPANDSP_DC_RESTORE_H_) 00030 #define _SPANDSP_DC_RESTORE_H_ 00031 00032 /*! \page dc_restore_page Removing DC bias from a signal 00033 00034 \section dc_restore_page_sec_1 What does it do? 00035 00036 Telecoms signals often contain considerable DC, but DC upsets a lot of signal 00037 processing functions. Placing a zero DC restorer at the front of the processing 00038 chain can often simplify the downstream processing. 00039 00040 \section dc_restore_page_sec_2 How does it work? 00041 00042 The DC restorer uses a leaky integrator to provide a long-ish term estimate of 00043 the DC bias in the signal. A 32 bit estimate is used for the 16 bit audio, so 00044 the noise introduced by the estimation can be keep in the lower bits, and the 16 00045 bit DC value, which is subtracted from the signal, is fairly clean. The 00046 following code fragment shows the algorithm used. dc_bias is a 32 bit integer, 00047 while the sample and the resulting clean_sample are 16 bit integers. 00048 00049 dc_bias += ((((int32_t) sample << 15) - dc_bias) >> 14); 00050 clean_sample = sample - (dc_bias >> 15); 00051 */ 00052 00053 /*! 00054 Zero DC restoration descriptor. This defines the working state for a single 00055 instance of DC content filter. 00056 */ 00057 typedef struct 00058 { 00059 int32_t state; 00060 } dc_restore_state_t; 00061 00062 #if defined(__cplusplus) 00063 extern "C" 00064 { 00065 #endif 00066 00067 static __inline__ void dc_restore_init(dc_restore_state_t *dc) 00068 { 00069 dc->state = 0; 00070 } 00071 /*- End of function --------------------------------------------------------*/ 00072 00073 static __inline__ int16_t dc_restore(dc_restore_state_t *dc, int16_t sample) 00074 { 00075 dc->state += ((((int32_t) sample << 15) - dc->state) >> 14); 00076 return (int16_t) (sample - (dc->state >> 15)); 00077 } 00078 /*- End of function --------------------------------------------------------*/ 00079 00080 static __inline__ int16_t dc_restore_estimate(dc_restore_state_t *dc) 00081 { 00082 return (int16_t) (dc->state >> 15); 00083 } 00084 /*- End of function --------------------------------------------------------*/ 00085 00086 #if defined(__cplusplus) 00087 } 00088 #endif 00089 00090 #endif 00091 /*- End of file ------------------------------------------------------------*/