add queen mary DSP library
[ardour2.git] / libs / qm-dsp / dsp / signalconditioning / DFProcess.cpp
blobbbe308b98517e2d6c7f7630d802bbd3dcea9b60f
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
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"
19 #include <cstring>
21 //////////////////////////////////////////////////////////////////////
22 // Construction/Destruction
23 //////////////////////////////////////////////////////////////////////
25 DFProcess::DFProcess( DFProcConfig Config )
27 filtSrc = NULL;
28 filtDst = NULL;
29 m_filtScratchIn = NULL;
30 m_filtScratchOut = NULL;
32 m_FFOrd = 0;
34 initialise( Config );
37 DFProcess::~DFProcess()
39 deInitialise();
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()
65 delete [] filtSrc;
67 delete [] filtDst;
69 delete [] m_filtScratchIn;
71 delete [] m_filtScratchOut;
73 delete m_FiltFilt;
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)
90 int i,k,j,l;
91 int index = 0;
93 double val = 0;
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++)
108 y[ j ] = src[ j ];
110 scratch[ index ] = MathUtilities::median( y, k );
111 index++;
114 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
116 if (index >= m_length) break;
119 l = 0;
120 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
122 y[ l ] = src[ j ];
123 l++;
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);
135 l = 0;
136 for( j = k; j < m_length; j++)
138 y[ l ] = src[ j ];
140 l++;
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 )
153 if( val > 0 )
155 dst[ i ] = val;
157 else
159 dst[ i ] = 0;
162 else
164 dst[ i ] = val;
168 delete [] y;
169 delete [] scratch;
173 void DFProcess::removeDCNormalize( double *src, double*dst )
175 double DFmax = 0;
176 double DFMin = 0;
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;