summaryrefslogtreecommitdiff
path: root/libs/qm-dsp/dsp/signalconditioning/DFProcess.cpp
blob: bbe308b98517e2d6c7f7630d802bbd3dcea9b60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    QM DSP Library

    Centre for Digital Music, Queen Mary, University of London.
    This file 2005-2006 Christian Landone.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "DFProcess.h"
#include "maths/MathUtilities.h"

#include <cstring>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DFProcess::DFProcess( DFProcConfig Config )
{
    filtSrc = NULL;
    filtDst = NULL;	
    m_filtScratchIn = NULL;
    m_filtScratchOut = NULL;

    m_FFOrd = 0;

    initialise( Config );
}

DFProcess::~DFProcess()
{
    deInitialise();
}

void DFProcess::initialise( DFProcConfig Config )
{
    m_length = Config.length;
    m_winPre = Config.winPre;
    m_winPost = Config.winPost;
    m_alphaNormParam = Config.AlphaNormParam;

    m_isMedianPositive = Config.isMedianPositive;

    filtSrc = new double[ m_length ];
    filtDst = new double[ m_length ];

	
    //Low Pass Smoothing Filter Config
    m_FilterConfigParams.ord = Config.LPOrd;
    m_FilterConfigParams.ACoeffs = Config.LPACoeffs;
    m_FilterConfigParams.BCoeffs = Config.LPBCoeffs;
	
    m_FiltFilt = new FiltFilt( m_FilterConfigParams );	
}

void DFProcess::deInitialise()
{
    delete [] filtSrc;

    delete [] filtDst;

    delete [] m_filtScratchIn;

    delete [] m_filtScratchOut;

    delete m_FiltFilt;
}

void DFProcess::process(double *src, double* dst)
{
    if (m_length == 0) return;

    removeDCNormalize( src, filtSrc );

    m_FiltFilt->process( filtSrc, filtDst, m_length );

    medianFilter( filtDst, dst );
}


void DFProcess::medianFilter(double *src, double *dst)
{
    int i,k,j,l;
    int index = 0;

    double val = 0;

    double* y = new double[ m_winPost + m_winPre + 1];
    memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );

    double* scratch = new double[ m_length ];

    for( i = 0; i < m_winPre; i++)
    {
        if (index >= m_length) break;

	k = i + m_winPost + 1;

	for( j = 0; j < k; j++)
	{
	    y[ j ] = src[ j ];
	}
	scratch[ index ] = MathUtilities::median( y, k );
	index++;
    }

    for(  i = 0; i + m_winPost + m_winPre < m_length; i ++)
    {
        if (index >= m_length) break;

			 
	l = 0;
	for(  j  = i; j < ( i + m_winPost + m_winPre + 1); j++)
	{
	    y[ l ] = src[ j ];
	    l++;
	}

	scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
    }

    for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
    {
        if (index >= m_length) break;

	k = std::max( i - m_winPre, 1);

	l = 0;
	for( j = k; j < m_length; j++)
	{
	    y[ l ] = src[ j ];

	    l++;
	}
		
	scratch[ index++ ] = MathUtilities::median( y, l); 
    }


    for( i = 0; i < m_length; i++ )
    {
	val = src[ i ] - scratch[ i ];// - 0.033;
		
	if( m_isMedianPositive )
	{
	    if( val > 0 )
	    {
		dst[ i ]  = val;
	    }
	    else
	    {
		dst[ i ]  = 0;
	    }
	}
	else
	{
	    dst[ i ]  = val;
	}
    }
	
    delete [] y;
    delete [] scratch;
}


void DFProcess::removeDCNormalize( double *src, double*dst )
{
    double DFmax = 0;
    double DFMin = 0;
    double DFAlphaNorm = 0;

    MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );

    MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );

    for( unsigned int i = 0; i< m_length; i++)
    {
	dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm; 
    }
}