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