summaryrefslogtreecommitdiff
path: root/libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp
blob: c88641de7b33d4c733904b923ca4d1a459ca8c1c (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
/* -*- 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 "FiltFilt.h"

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

FiltFilt::FiltFilt(Filter::Parameters parameters) :
    m_filter(parameters)
{
    m_ord = m_filter.getOrder();
}

FiltFilt::~FiltFilt()
{
}

void FiltFilt::process(double *src, double *dst, unsigned int length)
{	
    unsigned int i;

    if (length == 0) return;

    if (length < 2) {
	for( i = 0; i < length; i++ ) {
	    dst[i] = src [i];
	}
	return;
    }

    unsigned int nFilt = m_ord + 1;
    unsigned int nFact = 3 * ( nFilt - 1);
    unsigned int nExt	= length + 2 * nFact;

    double *filtScratchIn = new double[ nExt ];
    double *filtScratchOut = new double[ nExt ];
	
    for( i = 0; i< nExt; i++ ) 
    {
	filtScratchIn[ i ] = 0.0;
	filtScratchOut[ i ] = 0.0;
    }

    // Edge transients reflection
    double sample0 = 2 * src[ 0 ];
    double sampleN = 2 * src[ length - 1 ];

    unsigned int index = 0;
    for( i = nFact; i > 0; i-- )
    {
	filtScratchIn[ index++ ] = sample0 - src[ i ];
    }
    index = 0;
    for( i = 0; i < nFact && i + 2 < length; i++ )
    {
	filtScratchIn[ (nExt - nFact) + index++ ] = sampleN - src[ (length - 2) - i ];
    }

    for(; i < nFact; i++ )
    {
	filtScratchIn[ (nExt - nFact) + index++ ] = 0;
    }

    index = 0;
    for( i = 0; i < length; i++ )
    {
	filtScratchIn[ i + nFact ] = src[ i ];
    }
	
    ////////////////////////////////
    // Do  0Ph filtering
    m_filter.process( filtScratchIn, filtScratchOut, nExt);
	
    // reverse the series for FILTFILT 
    for ( i = 0; i < nExt; i++)
    { 
	filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1];
    }

    // do FILTER again 
    m_filter.process( filtScratchIn, filtScratchOut, nExt);
	
    // reverse the series back 
    for ( i = 0; i < nExt; i++)
    {
	filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1 ];
    }
    for ( i = 0;i < nExt; i++)
    {
	filtScratchOut[ i ] = filtScratchIn[ i ];
    }

    index = 0;
    for( i = 0; i < length; i++ )
    {
	dst[ index++ ] = filtScratchOut[ i + nFact ];
    }	

    delete [] filtScratchIn;
    delete [] filtScratchOut;

}

void FiltFilt::reset()
{

}