| c@225 | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */ | 
| c@225 | 2 | 
| c@225 | 3 /* | 
| c@225 | 4     QM DSP Library | 
| c@225 | 5 | 
| c@225 | 6     Centre for Digital Music, Queen Mary, University of London. | 
| c@225 | 7     This file copyright 2005-2006 Christian Landone. | 
| c@225 | 8     All rights reserved. | 
| c@225 | 9 */ | 
| c@225 | 10 | 
| c@225 | 11 #include "FiltFilt.h" | 
| c@225 | 12 | 
| c@225 | 13 ////////////////////////////////////////////////////////////////////// | 
| c@225 | 14 // Construction/Destruction | 
| c@225 | 15 ////////////////////////////////////////////////////////////////////// | 
| c@225 | 16 | 
| c@225 | 17 FiltFilt::FiltFilt( FiltFiltConfig Config ) | 
| c@225 | 18 { | 
| c@225 | 19     m_filtScratchIn = NULL; | 
| c@225 | 20     m_filtScratchOut = NULL; | 
| c@225 | 21     m_ord = 0; | 
| c@225 | 22 | 
| c@225 | 23     initialise( Config ); | 
| c@225 | 24 } | 
| c@225 | 25 | 
| c@225 | 26 FiltFilt::~FiltFilt() | 
| c@225 | 27 { | 
| c@225 | 28     deInitialise(); | 
| c@225 | 29 } | 
| c@225 | 30 | 
| c@225 | 31 void FiltFilt::initialise( FiltFiltConfig Config ) | 
| c@225 | 32 { | 
| c@225 | 33     m_ord = Config.ord; | 
| c@225 | 34     m_filterConfig.ord = Config.ord; | 
| c@225 | 35     m_filterConfig.ACoeffs = Config.ACoeffs; | 
| c@225 | 36     m_filterConfig.BCoeffs = Config.BCoeffs; | 
| c@225 | 37 | 
| c@225 | 38     m_filter = new Filter( m_filterConfig ); | 
| c@225 | 39 } | 
| c@225 | 40 | 
| c@225 | 41 void FiltFilt::deInitialise() | 
| c@225 | 42 { | 
| c@225 | 43     delete m_filter; | 
| c@225 | 44 } | 
| c@225 | 45 | 
| c@225 | 46 | 
| c@225 | 47 void FiltFilt::process(double *src, double *dst, unsigned int length) | 
| c@225 | 48 { | 
| c@225 | 49     unsigned int i; | 
| c@225 | 50 | 
| c@225 | 51     unsigned int nFilt = m_ord + 1; | 
| c@225 | 52     unsigned int nFact = 3 * ( nFilt - 1); | 
| c@225 | 53     unsigned int nExt	= length + 2 * nFact; | 
| c@225 | 54 | 
| c@225 | 55 | 
| c@225 | 56     m_filtScratchIn = new double[ nExt ]; | 
| c@225 | 57     m_filtScratchOut = new double[ nExt ]; | 
| c@225 | 58 | 
| c@225 | 59 | 
| c@225 | 60     for( i = 0; i< nExt; i++ ) | 
| c@225 | 61     { | 
| c@225 | 62 	m_filtScratchIn[ i ] = 0.0; | 
| c@225 | 63 	m_filtScratchOut[ i ] = 0.0; | 
| c@225 | 64     } | 
| c@225 | 65 | 
| c@225 | 66     // Edge transients reflection | 
| c@225 | 67     double sample0 = 2 * src[ 0 ]; | 
| c@225 | 68     double sampleN = 2 * src[ length - 1 ]; | 
| c@225 | 69 | 
| c@225 | 70     unsigned int index = 0; | 
| c@225 | 71     for( i = nFact; i > 0; i-- ) | 
| c@225 | 72     { | 
| c@225 | 73 	m_filtScratchIn[ index++ ] = sample0 - src[ i ]; | 
| c@225 | 74     } | 
| c@225 | 75     index = 0; | 
| c@225 | 76     for( i = 0; i < nFact; i++ ) | 
| c@225 | 77     { | 
| c@225 | 78 	m_filtScratchIn[ (nExt - nFact) + index++ ] = sampleN - src[ (length - 2) - i ]; | 
| c@225 | 79     } | 
| c@225 | 80 | 
| c@225 | 81     index = 0; | 
| c@225 | 82     for( i = 0; i < length; i++ ) | 
| c@225 | 83     { | 
| c@225 | 84 	m_filtScratchIn[ i + nFact ] = src[ i ]; | 
| c@225 | 85     } | 
| c@225 | 86 | 
| c@225 | 87     //////////////////////////////// | 
| c@225 | 88     // Do  0Ph filtering | 
| c@225 | 89     m_filter->process( m_filtScratchIn, m_filtScratchOut, nExt); | 
| c@225 | 90 | 
| c@225 | 91     // reverse the series for FILTFILT | 
| c@225 | 92     for ( i = 0; i < nExt; i++) | 
| c@225 | 93     { | 
| c@225 | 94 	m_filtScratchIn[ i ] = m_filtScratchOut[ nExt - i - 1]; | 
| c@225 | 95     } | 
| c@225 | 96 | 
| c@225 | 97     // do FILTER again | 
| c@225 | 98     m_filter->process( m_filtScratchIn, m_filtScratchOut, nExt); | 
| c@225 | 99 | 
| c@225 | 100     // reverse the series back | 
| c@225 | 101     for ( i = 0; i < nExt; i++) | 
| c@225 | 102     { | 
| c@225 | 103 	m_filtScratchIn[ i ] = m_filtScratchOut[ nExt - i - 1 ]; | 
| c@225 | 104     } | 
| c@225 | 105     for ( i = 0;i < nExt; i++) | 
| c@225 | 106     { | 
| c@225 | 107 	m_filtScratchOut[ i ] = m_filtScratchIn[ i ]; | 
| c@225 | 108     } | 
| c@225 | 109 | 
| c@225 | 110     index = 0; | 
| c@225 | 111     for( i = 0; i < length; i++ ) | 
| c@225 | 112     { | 
| c@225 | 113 	dst[ index++ ] = m_filtScratchOut[ i + nFact ]; | 
| c@225 | 114     } | 
| c@225 | 115 | 
| c@225 | 116     delete [] m_filtScratchIn; | 
| c@225 | 117     delete [] m_filtScratchOut; | 
| c@225 | 118 | 
| c@225 | 119 } | 
| c@225 | 120 | 
| c@225 | 121 void FiltFilt::reset() | 
| c@225 | 122 { | 
| c@225 | 123 | 
| c@225 | 124 } |