c@225
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@225
|
2
|
c@225
|
3 /*
|
c@225
|
4 QM DSP Library
|
c@225
|
5
|
c@225
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@309
|
7 This file 2005-2006 Christian Landone.
|
c@309
|
8
|
c@309
|
9 This program is free software; you can redistribute it and/or
|
c@309
|
10 modify it under the terms of the GNU General Public License as
|
c@309
|
11 published by the Free Software Foundation; either version 2 of the
|
c@309
|
12 License, or (at your option) any later version. See the file
|
c@309
|
13 COPYING included with this distribution for more information.
|
c@225
|
14 */
|
c@225
|
15
|
c@225
|
16 #include "Decimator.h"
|
c@225
|
17
|
c@247
|
18 #include <iostream>
|
c@247
|
19
|
c@225
|
20 //////////////////////////////////////////////////////////////////////
|
c@225
|
21 // Construction/Destruction
|
c@225
|
22 //////////////////////////////////////////////////////////////////////
|
c@225
|
23
|
c@225
|
24 Decimator::Decimator( unsigned int inLength, unsigned int decFactor )
|
c@225
|
25 {
|
c@225
|
26
|
c@225
|
27 m_inputLength = 0;
|
c@225
|
28 m_outputLength = 0;
|
c@225
|
29 m_decFactor = 1;
|
c@225
|
30
|
c@225
|
31 initialise( inLength, decFactor );
|
c@225
|
32 }
|
c@225
|
33
|
c@225
|
34 Decimator::~Decimator()
|
c@225
|
35 {
|
c@225
|
36 deInitialise();
|
c@225
|
37 }
|
c@225
|
38
|
c@225
|
39 void Decimator::initialise( unsigned int inLength, unsigned int decFactor)
|
c@225
|
40 {
|
c@225
|
41 m_inputLength = inLength;
|
c@225
|
42 m_decFactor = decFactor;
|
c@225
|
43 m_outputLength = m_inputLength / m_decFactor;
|
c@225
|
44
|
c@225
|
45 decBuffer = new double[ m_inputLength ];
|
c@225
|
46
|
c@247
|
47 // If adding new factors here, add them to
|
c@247
|
48 // getHighestSupportedFactor in the header as well
|
c@247
|
49
|
c@247
|
50 if(m_decFactor == 8)
|
c@247
|
51 {
|
c@247
|
52 //////////////////////////////////////////////////
|
c@247
|
53 b[0] = 0.060111378492136;
|
c@247
|
54 b[1] = -0.257323420830598;
|
c@247
|
55 b[2] = 0.420583503165928;
|
c@247
|
56 b[3] = -0.222750785197418;
|
c@247
|
57 b[4] = -0.222750785197418;
|
c@247
|
58 b[5] = 0.420583503165928;
|
c@247
|
59 b[6] = -0.257323420830598;
|
c@247
|
60 b[7] = 0.060111378492136;
|
c@247
|
61
|
c@247
|
62 a[0] = 1;
|
c@247
|
63 a[1] = -5.667654878577432;
|
c@247
|
64 a[2] = 14.062452278088417;
|
c@247
|
65 a[3] = -19.737303840697738;
|
c@247
|
66 a[4] = 16.889698874608641;
|
c@247
|
67 a[5] = -8.796600612325928;
|
c@247
|
68 a[6] = 2.577553446979888;
|
c@247
|
69 a[7] = -0.326903916815751;
|
c@247
|
70 //////////////////////////////////////////////////
|
c@247
|
71 }
|
c@247
|
72 else if( m_decFactor == 4 )
|
c@225
|
73 {
|
c@225
|
74 //////////////////////////////////////////////////
|
c@225
|
75 b[ 0 ] = 0.10133306904918619;
|
c@225
|
76 b[ 1 ] = -0.2447523353702363;
|
c@225
|
77 b[ 2 ] = 0.33622528590120965;
|
c@225
|
78 b[ 3 ] = -0.13936581560633518;
|
c@225
|
79 b[ 4 ] = -0.13936581560633382;
|
c@225
|
80 b[ 5 ] = 0.3362252859012087;
|
c@225
|
81 b[ 6 ] = -0.2447523353702358;
|
c@225
|
82 b[ 7 ] = 0.10133306904918594;
|
c@225
|
83
|
c@225
|
84 a[ 0 ] = 1;
|
c@225
|
85 a[ 1 ] = -3.9035590278139427;
|
c@225
|
86 a[ 2 ] = 7.5299379980621133;
|
c@225
|
87 a[ 3 ] = -8.6890803793177511;
|
c@225
|
88 a[ 4 ] = 6.4578667096099176;
|
c@225
|
89 a[ 5 ] = -3.0242979431223631;
|
c@225
|
90 a[ 6 ] = 0.83043385136748382;
|
c@225
|
91 a[ 7 ] = -0.094420800837809335;
|
c@225
|
92 //////////////////////////////////////////////////
|
c@225
|
93 }
|
c@225
|
94 else if( m_decFactor == 2 )
|
c@225
|
95 {
|
c@225
|
96 //////////////////////////////////////////////////
|
c@225
|
97 b[ 0 ] = 0.20898944260075727;
|
c@225
|
98 b[ 1 ] = 0.40011234879814367;
|
c@225
|
99 b[ 2 ] = 0.819741973072733;
|
c@225
|
100 b[ 3 ] = 1.0087419911682323;
|
c@225
|
101 b[ 4 ] = 1.0087419911682325;
|
c@225
|
102 b[ 5 ] = 0.81974197307273156;
|
c@225
|
103 b[ 6 ] = 0.40011234879814295;
|
c@225
|
104 b[ 7 ] = 0.20898944260075661;
|
c@225
|
105
|
c@225
|
106 a[ 0 ] = 1;
|
c@225
|
107 a[ 1 ] = 0.0077331184208358217;
|
c@225
|
108 a[ 2 ] = 1.9853971155964376;
|
c@225
|
109 a[ 3 ] = 0.19296739275341004;
|
c@225
|
110 a[ 4 ] = 1.2330748872852182;
|
c@225
|
111 a[ 5 ] = 0.18705341389316466;
|
c@225
|
112 a[ 6 ] = 0.23659265908013868;
|
c@225
|
113 a[ 7 ] = 0.032352924250533946;
|
c@225
|
114 }
|
c@225
|
115 else
|
c@225
|
116 {
|
c@247
|
117 if ( m_decFactor != 1 ) {
|
c@247
|
118 std::cerr << "WARNING: Decimator::initialise: unsupported decimation factor " << m_decFactor << ", no antialiasing filter will be used" << std::endl;
|
c@247
|
119 }
|
c@247
|
120
|
c@225
|
121 //////////////////////////////////////////////////
|
c@225
|
122 b[ 0 ] = 1;
|
c@225
|
123 b[ 1 ] = 0;
|
c@225
|
124 b[ 2 ] = 0;
|
c@225
|
125 b[ 3 ] = 0;
|
c@225
|
126 b[ 4 ] = 0;
|
c@225
|
127 b[ 5 ] = 0;
|
c@225
|
128 b[ 6 ] = 0;
|
c@225
|
129 b[ 7 ] = 0;
|
c@225
|
130
|
c@225
|
131 a[ 0 ] = 1;
|
c@225
|
132 a[ 1 ] = 0;
|
c@225
|
133 a[ 2 ] = 0;
|
c@225
|
134 a[ 3 ] = 0;
|
c@225
|
135 a[ 4 ] = 0;
|
c@225
|
136 a[ 5 ] = 0;
|
c@225
|
137 a[ 6 ] = 0;
|
c@225
|
138 a[ 7 ] = 0;
|
c@225
|
139 }
|
c@225
|
140
|
c@225
|
141 resetFilter();
|
c@225
|
142 }
|
c@225
|
143
|
c@225
|
144 void Decimator::deInitialise()
|
c@225
|
145 {
|
c@225
|
146 delete [] decBuffer;
|
c@225
|
147 }
|
c@225
|
148
|
c@225
|
149 void Decimator::resetFilter()
|
c@225
|
150 {
|
c@225
|
151 Input = Output = 0;
|
c@225
|
152
|
c@225
|
153 o1=o2=o3=o4=o5=o6=o7=0;
|
c@225
|
154 }
|
c@225
|
155
|
c@247
|
156 void Decimator::doAntiAlias(const double *src, double *dst, unsigned int length)
|
c@225
|
157 {
|
c@225
|
158
|
c@225
|
159 for( unsigned int i = 0; i < length; i++ )
|
c@225
|
160 {
|
c@225
|
161 Input = (double)src[ i ];
|
c@225
|
162
|
c@225
|
163 Output = Input * b[ 0 ] + o1;
|
c@225
|
164
|
c@225
|
165 o1 = Input * b[ 1 ] - Output * a[ 1 ] + o2;
|
c@225
|
166 o2 = Input * b[ 2 ] - Output * a[ 2 ] + o3;
|
c@225
|
167 o3 = Input * b[ 3 ] - Output * a[ 3 ] + o4;
|
c@225
|
168 o4 = Input * b[ 4 ] - Output * a[ 4 ] + o5;
|
c@225
|
169 o5 = Input * b[ 5 ] - Output * a[ 5 ] + o6;
|
c@225
|
170 o6 = Input * b[ 6 ] - Output * a[ 6 ] + o7;
|
c@225
|
171 o7 = Input * b[ 7 ] - Output * a[ 7 ] ;
|
c@225
|
172
|
c@225
|
173 dst[ i ] = Output;
|
c@225
|
174 }
|
c@225
|
175
|
c@225
|
176 }
|
c@225
|
177
|
c@280
|
178 void Decimator::doAntiAlias(const float *src, double *dst, unsigned int length)
|
c@280
|
179 {
|
c@280
|
180
|
c@280
|
181 for( unsigned int i = 0; i < length; i++ )
|
c@280
|
182 {
|
c@280
|
183 Input = (double)src[ i ];
|
c@280
|
184
|
c@280
|
185 Output = Input * b[ 0 ] + o1;
|
c@280
|
186
|
c@280
|
187 o1 = Input * b[ 1 ] - Output * a[ 1 ] + o2;
|
c@280
|
188 o2 = Input * b[ 2 ] - Output * a[ 2 ] + o3;
|
c@280
|
189 o3 = Input * b[ 3 ] - Output * a[ 3 ] + o4;
|
c@280
|
190 o4 = Input * b[ 4 ] - Output * a[ 4 ] + o5;
|
c@280
|
191 o5 = Input * b[ 5 ] - Output * a[ 5 ] + o6;
|
c@280
|
192 o6 = Input * b[ 6 ] - Output * a[ 6 ] + o7;
|
c@280
|
193 o7 = Input * b[ 7 ] - Output * a[ 7 ] ;
|
c@280
|
194
|
c@280
|
195 dst[ i ] = Output;
|
c@280
|
196 }
|
c@280
|
197
|
c@280
|
198 }
|
c@280
|
199
|
c@247
|
200 void Decimator::process(const double *src, double *dst)
|
c@225
|
201 {
|
c@225
|
202 if( m_decFactor != 1 )
|
c@225
|
203 {
|
c@225
|
204 doAntiAlias( src, decBuffer, m_inputLength );
|
c@225
|
205 }
|
c@225
|
206 unsigned idx = 0;
|
c@225
|
207
|
c@225
|
208 for( unsigned int i = 0; i < m_outputLength; i++ )
|
c@225
|
209 {
|
c@225
|
210 dst[ idx++ ] = decBuffer[ m_decFactor * i ];
|
c@225
|
211 }
|
c@225
|
212 }
|
c@280
|
213
|
c@280
|
214 void Decimator::process(const float *src, float *dst)
|
c@280
|
215 {
|
c@280
|
216 if( m_decFactor != 1 )
|
c@280
|
217 {
|
c@280
|
218 doAntiAlias( src, decBuffer, m_inputLength );
|
c@280
|
219 }
|
c@280
|
220 unsigned idx = 0;
|
c@280
|
221
|
c@280
|
222 for( unsigned int i = 0; i < m_outputLength; i++ )
|
c@280
|
223 {
|
c@280
|
224 dst[ idx++ ] = decBuffer[ m_decFactor * i ];
|
c@280
|
225 }
|
c@280
|
226 }
|