1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file 2005-2006 Christian Landone.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "DFProcess.h"
17 #include "maths/MathUtilities.h"
21 //////////////////////////////////////////////////////////////////////
22 // Construction/Destruction
23 //////////////////////////////////////////////////////////////////////
25 DFProcess::DFProcess( DFProcConfig Config
)
29 m_filtScratchIn
= NULL
;
30 m_filtScratchOut
= NULL
;
37 DFProcess::~DFProcess()
42 void DFProcess::initialise( DFProcConfig Config
)
44 m_length
= Config
.length
;
45 m_winPre
= Config
.winPre
;
46 m_winPost
= Config
.winPost
;
47 m_alphaNormParam
= Config
.AlphaNormParam
;
49 m_isMedianPositive
= Config
.isMedianPositive
;
51 filtSrc
= new double[ m_length
];
52 filtDst
= new double[ m_length
];
55 //Low Pass Smoothing Filter Config
56 m_FilterConfigParams
.ord
= Config
.LPOrd
;
57 m_FilterConfigParams
.ACoeffs
= Config
.LPACoeffs
;
58 m_FilterConfigParams
.BCoeffs
= Config
.LPBCoeffs
;
60 m_FiltFilt
= new FiltFilt( m_FilterConfigParams
);
63 void DFProcess::deInitialise()
69 delete [] m_filtScratchIn
;
71 delete [] m_filtScratchOut
;
76 void DFProcess::process(double *src
, double* dst
)
78 if (m_length
== 0) return;
80 removeDCNormalize( src
, filtSrc
);
82 m_FiltFilt
->process( filtSrc
, filtDst
, m_length
);
84 medianFilter( filtDst
, dst
);
88 void DFProcess::medianFilter(double *src
, double *dst
)
95 double* y
= new double[ m_winPost
+ m_winPre
+ 1];
96 memset( y
, 0, sizeof( double ) * ( m_winPost
+ m_winPre
+ 1) );
98 double* scratch
= new double[ m_length
];
100 for( i
= 0; i
< m_winPre
; i
++)
102 if (index
>= m_length
) break;
104 k
= i
+ m_winPost
+ 1;
106 for( j
= 0; j
< k
; j
++)
110 scratch
[ index
] = MathUtilities::median( y
, k
);
114 for( i
= 0; i
+ m_winPost
+ m_winPre
< m_length
; i
++)
116 if (index
>= m_length
) break;
120 for( j
= i
; j
< ( i
+ m_winPost
+ m_winPre
+ 1); j
++)
126 scratch
[ index
++ ] = MathUtilities::median( y
, (m_winPost
+ m_winPre
+ 1 ));
129 for( i
= std::max( m_length
- m_winPost
, 1); i
< m_length
; i
++)
131 if (index
>= m_length
) break;
133 k
= std::max( i
- m_winPre
, 1);
136 for( j
= k
; j
< m_length
; j
++)
143 scratch
[ index
++ ] = MathUtilities::median( y
, l
);
147 for( i
= 0; i
< m_length
; i
++ )
149 val
= src
[ i
] - scratch
[ i
];// - 0.033;
151 if( m_isMedianPositive
)
173 void DFProcess::removeDCNormalize( double *src
, double*dst
)
177 double DFAlphaNorm
= 0;
179 MathUtilities::getFrameMinMax( src
, m_length
, &DFMin
, &DFmax
);
181 MathUtilities::getAlphaNorm( src
, m_length
, m_alphaNormParam
, &DFAlphaNorm
);
183 for( unsigned int i
= 0; i
< m_length
; i
++)
185 dst
[ i
] = ( src
[ i
] - DFMin
) / DFAlphaNorm
;