Mercurial > hg > qm-dsp
comparison dsp/signalconditioning/DFProcess.cpp @ 0:d7116e3183f8
* Queen Mary C++ DSP library
author | cannam |
---|---|
date | Wed, 05 Apr 2006 17:35:59 +0000 |
parents | |
children | 2e3f5d2d62c1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7116e3183f8 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 QM DSP Library | |
5 | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2005-2006 Christian Landone. | |
8 All rights reserved. | |
9 */ | |
10 | |
11 #include "DFProcess.h" | |
12 #include "dsp/maths/MathUtilities.h" | |
13 | |
14 ////////////////////////////////////////////////////////////////////// | |
15 // Construction/Destruction | |
16 ////////////////////////////////////////////////////////////////////// | |
17 | |
18 DFProcess::DFProcess( DFProcConfig Config ) | |
19 { | |
20 filtSrc = NULL; | |
21 filtDst = NULL; | |
22 m_filtScratchIn = NULL; | |
23 m_filtScratchOut = NULL; | |
24 | |
25 m_FFOrd = 0; | |
26 | |
27 initialise( Config ); | |
28 } | |
29 | |
30 DFProcess::~DFProcess() | |
31 { | |
32 deInitialise(); | |
33 } | |
34 | |
35 void DFProcess::initialise( DFProcConfig Config ) | |
36 { | |
37 m_length = Config.length; | |
38 m_winPre = Config.winPre; | |
39 m_winPost = Config.winPost; | |
40 m_alphaNormParam = Config.AlphaNormParam; | |
41 | |
42 m_isMedianPositive = Config.isMedianPositive; | |
43 | |
44 filtSrc = new double[ m_length ]; | |
45 filtDst = new double[ m_length ]; | |
46 | |
47 | |
48 //Low Pass Smoothing Filter Config | |
49 m_FilterConfigParams.ord = Config.LPOrd; | |
50 m_FilterConfigParams.ACoeffs = Config.LPACoeffs; | |
51 m_FilterConfigParams.BCoeffs = Config.LPBCoeffs; | |
52 | |
53 m_FiltFilt = new FiltFilt( m_FilterConfigParams ); | |
54 } | |
55 | |
56 void DFProcess::deInitialise() | |
57 { | |
58 delete [] filtSrc; | |
59 | |
60 delete [] filtDst; | |
61 | |
62 delete [] m_filtScratchIn; | |
63 | |
64 delete [] m_filtScratchOut; | |
65 | |
66 delete m_FiltFilt; | |
67 } | |
68 | |
69 void DFProcess::process(double *src, double* dst) | |
70 { | |
71 removeDCNormalize( src, filtSrc ); | |
72 | |
73 m_FiltFilt->process( filtSrc, filtDst, m_length ); | |
74 | |
75 medianFilter( filtDst, dst ); | |
76 } | |
77 | |
78 | |
79 void DFProcess::medianFilter(double *src, double *dst) | |
80 { | |
81 unsigned int i,k,j,l; | |
82 unsigned int index = 0; | |
83 | |
84 double val = 0; | |
85 | |
86 double* y = new double[ m_winPost + m_winPre + 1]; | |
87 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) ); | |
88 | |
89 double* scratch = new double[ m_length ]; | |
90 | |
91 for( i = 0; i < m_winPre; i++) | |
92 { | |
93 k = i + m_winPost + 1; | |
94 | |
95 for( j = 0; j < k; j++) | |
96 { | |
97 y[ j ] = src[ j ]; | |
98 } | |
99 scratch[ index ] = MathUtilities::median( y, k ); | |
100 index++; | |
101 } | |
102 | |
103 for( i = 0; i < ( m_length - ( m_winPost + m_winPre ) ); i ++) | |
104 { | |
105 | |
106 l = 0; | |
107 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++) | |
108 { | |
109 y[ l ] = src[ j ]; | |
110 l++; | |
111 } | |
112 | |
113 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 )); | |
114 } | |
115 | |
116 for( i = std::max( m_length - m_winPost, (unsigned)1); i < m_length; i++) | |
117 { | |
118 k = std::max( i - m_winPre, (unsigned)1); | |
119 | |
120 l = 0; | |
121 for( j = k; j < m_length; j++) | |
122 { | |
123 y[ l ] = src[ j ]; | |
124 | |
125 l++; | |
126 } | |
127 | |
128 scratch[ index++ ] = MathUtilities::median( y, l); | |
129 } | |
130 | |
131 | |
132 for( i = 0; i < m_length; i++ ) | |
133 { | |
134 val = src[ i ] - scratch[ i ];// - 0.033; | |
135 | |
136 if( m_isMedianPositive ) | |
137 { | |
138 if( val > 0 ) | |
139 { | |
140 dst[ i ] = val; | |
141 } | |
142 else | |
143 { | |
144 dst[ i ] = 0; | |
145 } | |
146 } | |
147 else | |
148 { | |
149 dst[ i ] = val; | |
150 } | |
151 } | |
152 | |
153 delete [] y; | |
154 delete [] scratch; | |
155 } | |
156 | |
157 | |
158 void DFProcess::removeDCNormalize( double *src, double*dst ) | |
159 { | |
160 double DFmax = 0; | |
161 double DFMin = 0; | |
162 double DFAlphaNorm = 0; | |
163 | |
164 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax ); | |
165 | |
166 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm ); | |
167 | |
168 for( unsigned int i = 0; i< m_length; i++) | |
169 { | |
170 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm; | |
171 } | |
172 } |