comparison dsp/rateconversion/Decimator.cpp @ 225:49844bc8a895

* Queen Mary C++ DSP library
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 05 Apr 2006 17:35:59 +0000
parents
children f7edcd9138bd
comparison
equal deleted inserted replaced
-1:000000000000 225:49844bc8a895
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 "Decimator.h"
12
13 //////////////////////////////////////////////////////////////////////
14 // Construction/Destruction
15 //////////////////////////////////////////////////////////////////////
16
17 Decimator::Decimator( unsigned int inLength, unsigned int decFactor )
18 {
19
20 m_inputLength = 0;
21 m_outputLength = 0;
22 m_decFactor = 1;
23
24 initialise( inLength, decFactor );
25 }
26
27 Decimator::~Decimator()
28 {
29 deInitialise();
30 }
31
32 void Decimator::initialise( unsigned int inLength, unsigned int decFactor)
33 {
34 m_inputLength = inLength;
35 m_decFactor = decFactor;
36 m_outputLength = m_inputLength / m_decFactor;
37
38 decBuffer = new double[ m_inputLength ];
39
40 if( m_decFactor == 4 )
41 {
42 //////////////////////////////////////////////////
43 b[ 0 ] = 0.10133306904918619;
44 b[ 1 ] = -0.2447523353702363;
45 b[ 2 ] = 0.33622528590120965;
46 b[ 3 ] = -0.13936581560633518;
47 b[ 4 ] = -0.13936581560633382;
48 b[ 5 ] = 0.3362252859012087;
49 b[ 6 ] = -0.2447523353702358;
50 b[ 7 ] = 0.10133306904918594;
51
52 a[ 0 ] = 1;
53 a[ 1 ] = -3.9035590278139427;
54 a[ 2 ] = 7.5299379980621133;
55 a[ 3 ] = -8.6890803793177511;
56 a[ 4 ] = 6.4578667096099176;
57 a[ 5 ] = -3.0242979431223631;
58 a[ 6 ] = 0.83043385136748382;
59 a[ 7 ] = -0.094420800837809335;
60 //////////////////////////////////////////////////
61 }
62 else if( m_decFactor == 2 )
63 {
64 //////////////////////////////////////////////////
65 b[ 0 ] = 0.20898944260075727;
66 b[ 1 ] = 0.40011234879814367;
67 b[ 2 ] = 0.819741973072733;
68 b[ 3 ] = 1.0087419911682323;
69 b[ 4 ] = 1.0087419911682325;
70 b[ 5 ] = 0.81974197307273156;
71 b[ 6 ] = 0.40011234879814295;
72 b[ 7 ] = 0.20898944260075661;
73
74 a[ 0 ] = 1;
75 a[ 1 ] = 0.0077331184208358217;
76 a[ 2 ] = 1.9853971155964376;
77 a[ 3 ] = 0.19296739275341004;
78 a[ 4 ] = 1.2330748872852182;
79 a[ 5 ] = 0.18705341389316466;
80 a[ 6 ] = 0.23659265908013868;
81 a[ 7 ] = 0.032352924250533946;
82 }
83 else
84 {
85 //////////////////////////////////////////////////
86 b[ 0 ] = 1;
87 b[ 1 ] = 0;
88 b[ 2 ] = 0;
89 b[ 3 ] = 0;
90 b[ 4 ] = 0;
91 b[ 5 ] = 0;
92 b[ 6 ] = 0;
93 b[ 7 ] = 0;
94
95 a[ 0 ] = 1;
96 a[ 1 ] = 0;
97 a[ 2 ] = 0;
98 a[ 3 ] = 0;
99 a[ 4 ] = 0;
100 a[ 5 ] = 0;
101 a[ 6 ] = 0;
102 a[ 7 ] = 0;
103 }
104
105 resetFilter();
106 }
107
108 void Decimator::deInitialise()
109 {
110 delete [] decBuffer;
111 }
112
113 void Decimator::resetFilter()
114 {
115 Input = Output = 0;
116
117 o1=o2=o3=o4=o5=o6=o7=0;
118 }
119
120 void Decimator::doAntiAlias(double *src, double *dst, unsigned int length)
121 {
122
123 for( unsigned int i = 0; i < length; i++ )
124 {
125 Input = (double)src[ i ];
126
127 Output = Input * b[ 0 ] + o1;
128
129 o1 = Input * b[ 1 ] - Output * a[ 1 ] + o2;
130 o2 = Input * b[ 2 ] - Output * a[ 2 ] + o3;
131 o3 = Input * b[ 3 ] - Output * a[ 3 ] + o4;
132 o4 = Input * b[ 4 ] - Output * a[ 4 ] + o5;
133 o5 = Input * b[ 5 ] - Output * a[ 5 ] + o6;
134 o6 = Input * b[ 6 ] - Output * a[ 6 ] + o7;
135 o7 = Input * b[ 7 ] - Output * a[ 7 ] ;
136
137 dst[ i ] = Output;
138 }
139
140 }
141
142 void Decimator::process(double *src, double *dst)
143 {
144 if( m_decFactor != 1 )
145 {
146 doAntiAlias( src, decBuffer, m_inputLength );
147 }
148 unsigned idx = 0;
149
150 for( unsigned int i = 0; i < m_outputLength; i++ )
151 {
152 dst[ idx++ ] = decBuffer[ m_decFactor * i ];
153 }
154 }