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@309
|
7 This file 2005-2006 Christian Landone.
|
c@309
|
8
|
c@309
|
9 This program is free software; you can redistribute it and/or
|
c@309
|
10 modify it under the terms of the GNU General Public License as
|
c@309
|
11 published by the Free Software Foundation; either version 2 of the
|
c@309
|
12 License, or (at your option) any later version. See the file
|
c@309
|
13 COPYING included with this distribution for more information.
|
c@225
|
14 */
|
c@225
|
15
|
c@225
|
16 #include "DFProcess.h"
|
c@241
|
17 #include "maths/MathUtilities.h"
|
c@225
|
18
|
c@272
|
19 #include <cstring>
|
c@272
|
20
|
c@225
|
21 //////////////////////////////////////////////////////////////////////
|
c@225
|
22 // Construction/Destruction
|
c@225
|
23 //////////////////////////////////////////////////////////////////////
|
c@225
|
24
|
c@225
|
25 DFProcess::DFProcess( DFProcConfig Config )
|
c@225
|
26 {
|
c@225
|
27 filtSrc = NULL;
|
c@225
|
28 filtDst = NULL;
|
c@225
|
29 m_filtScratchIn = NULL;
|
c@225
|
30 m_filtScratchOut = NULL;
|
c@225
|
31
|
c@225
|
32 m_FFOrd = 0;
|
c@225
|
33
|
c@225
|
34 initialise( Config );
|
c@225
|
35 }
|
c@225
|
36
|
c@225
|
37 DFProcess::~DFProcess()
|
c@225
|
38 {
|
c@225
|
39 deInitialise();
|
c@225
|
40 }
|
c@225
|
41
|
c@225
|
42 void DFProcess::initialise( DFProcConfig Config )
|
c@225
|
43 {
|
c@225
|
44 m_length = Config.length;
|
c@225
|
45 m_winPre = Config.winPre;
|
c@225
|
46 m_winPost = Config.winPost;
|
c@225
|
47 m_alphaNormParam = Config.AlphaNormParam;
|
c@225
|
48
|
c@225
|
49 m_isMedianPositive = Config.isMedianPositive;
|
c@225
|
50
|
c@225
|
51 filtSrc = new double[ m_length ];
|
c@225
|
52 filtDst = new double[ m_length ];
|
c@225
|
53
|
c@225
|
54
|
c@225
|
55 //Low Pass Smoothing Filter Config
|
c@225
|
56 m_FilterConfigParams.ord = Config.LPOrd;
|
c@225
|
57 m_FilterConfigParams.ACoeffs = Config.LPACoeffs;
|
c@225
|
58 m_FilterConfigParams.BCoeffs = Config.LPBCoeffs;
|
c@225
|
59
|
c@225
|
60 m_FiltFilt = new FiltFilt( m_FilterConfigParams );
|
c@225
|
61 }
|
c@225
|
62
|
c@225
|
63 void DFProcess::deInitialise()
|
c@225
|
64 {
|
c@225
|
65 delete [] filtSrc;
|
c@225
|
66
|
c@225
|
67 delete [] filtDst;
|
c@225
|
68
|
c@225
|
69 delete [] m_filtScratchIn;
|
c@225
|
70
|
c@225
|
71 delete [] m_filtScratchOut;
|
c@225
|
72
|
c@225
|
73 delete m_FiltFilt;
|
c@225
|
74 }
|
c@225
|
75
|
c@225
|
76 void DFProcess::process(double *src, double* dst)
|
c@225
|
77 {
|
c@283
|
78 if (m_length == 0) return;
|
c@283
|
79
|
c@225
|
80 removeDCNormalize( src, filtSrc );
|
c@225
|
81
|
c@225
|
82 m_FiltFilt->process( filtSrc, filtDst, m_length );
|
c@225
|
83
|
c@225
|
84 medianFilter( filtDst, dst );
|
c@225
|
85 }
|
c@225
|
86
|
c@225
|
87
|
c@225
|
88 void DFProcess::medianFilter(double *src, double *dst)
|
c@225
|
89 {
|
c@299
|
90 int i,k,j,l;
|
c@299
|
91 int index = 0;
|
c@225
|
92
|
c@225
|
93 double val = 0;
|
c@225
|
94
|
c@225
|
95 double* y = new double[ m_winPost + m_winPre + 1];
|
c@225
|
96 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
|
c@225
|
97
|
c@225
|
98 double* scratch = new double[ m_length ];
|
c@225
|
99
|
c@305
|
100 for( i = 0; i < m_winPre; i++)
|
c@225
|
101 {
|
c@299
|
102 if (index >= m_length) break;
|
c@299
|
103
|
c@225
|
104 k = i + m_winPost + 1;
|
c@225
|
105
|
c@225
|
106 for( j = 0; j < k; j++)
|
c@225
|
107 {
|
c@225
|
108 y[ j ] = src[ j ];
|
c@225
|
109 }
|
c@225
|
110 scratch[ index ] = MathUtilities::median( y, k );
|
c@225
|
111 index++;
|
c@225
|
112 }
|
c@225
|
113
|
c@299
|
114 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
|
c@225
|
115 {
|
c@299
|
116 if (index >= m_length) break;
|
c@299
|
117
|
c@225
|
118
|
c@225
|
119 l = 0;
|
c@225
|
120 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
|
c@225
|
121 {
|
c@225
|
122 y[ l ] = src[ j ];
|
c@225
|
123 l++;
|
c@225
|
124 }
|
c@225
|
125
|
c@225
|
126 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
|
c@225
|
127 }
|
c@225
|
128
|
c@299
|
129 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
|
c@225
|
130 {
|
c@299
|
131 if (index >= m_length) break;
|
c@299
|
132
|
c@299
|
133 k = std::max( i - m_winPre, 1);
|
c@225
|
134
|
c@225
|
135 l = 0;
|
c@225
|
136 for( j = k; j < m_length; j++)
|
c@225
|
137 {
|
c@225
|
138 y[ l ] = src[ j ];
|
c@225
|
139
|
c@225
|
140 l++;
|
c@225
|
141 }
|
c@225
|
142
|
c@225
|
143 scratch[ index++ ] = MathUtilities::median( y, l);
|
c@225
|
144 }
|
c@225
|
145
|
c@225
|
146
|
c@225
|
147 for( i = 0; i < m_length; i++ )
|
c@225
|
148 {
|
c@225
|
149 val = src[ i ] - scratch[ i ];// - 0.033;
|
c@225
|
150
|
c@225
|
151 if( m_isMedianPositive )
|
c@225
|
152 {
|
c@225
|
153 if( val > 0 )
|
c@225
|
154 {
|
c@225
|
155 dst[ i ] = val;
|
c@225
|
156 }
|
c@225
|
157 else
|
c@225
|
158 {
|
c@225
|
159 dst[ i ] = 0;
|
c@225
|
160 }
|
c@225
|
161 }
|
c@225
|
162 else
|
c@225
|
163 {
|
c@225
|
164 dst[ i ] = val;
|
c@225
|
165 }
|
c@225
|
166 }
|
c@225
|
167
|
c@225
|
168 delete [] y;
|
c@225
|
169 delete [] scratch;
|
c@225
|
170 }
|
c@225
|
171
|
c@225
|
172
|
c@225
|
173 void DFProcess::removeDCNormalize( double *src, double*dst )
|
c@225
|
174 {
|
c@225
|
175 double DFmax = 0;
|
c@225
|
176 double DFMin = 0;
|
c@225
|
177 double DFAlphaNorm = 0;
|
c@225
|
178
|
c@225
|
179 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
|
c@225
|
180
|
c@225
|
181 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
|
c@225
|
182
|
c@225
|
183 for( unsigned int i = 0; i< m_length; i++)
|
c@225
|
184 {
|
c@225
|
185 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
|
c@225
|
186 }
|
c@225
|
187 }
|