annotate dsp/rateconversion/Decimator.cpp @ 298:255e431ae3d4

* Key detector: when returning key strengths, use the peak value of the three underlying chromagram correlations (from 36-bin chromagram) corresponding to each key, instead of the mean. Rationale: This is the same method as used when returning the key value, and it's nice to have the same results in both returned value and plot. The peak performed better than the sum with a simple test set of triads, so it seems reasonable to change the plot to match the key output rather than the other way around. * FFT: kiss_fftr returns only the non-conjugate bins, synthesise the rest rather than leaving them (perhaps dangerously) undefined. Fixes an uninitialised data error in chromagram that could cause garbage results from key detector. * Constant Q: remove precalculated values again, I reckon they're not proving such a good tradeoff.
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 05 Jun 2009 15:12:39 +0000
parents 9c403afdd9e9
children e5907ae6de17
rev   line source
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@225 7 This file copyright 2005-2006 Christian Landone.
c@225 8 All rights reserved.
c@225 9 */
c@225 10
c@225 11 #include "Decimator.h"
c@225 12
c@247 13 #include <iostream>
c@247 14
c@225 15 //////////////////////////////////////////////////////////////////////
c@225 16 // Construction/Destruction
c@225 17 //////////////////////////////////////////////////////////////////////
c@225 18
c@225 19 Decimator::Decimator( unsigned int inLength, unsigned int decFactor )
c@225 20 {
c@225 21
c@225 22 m_inputLength = 0;
c@225 23 m_outputLength = 0;
c@225 24 m_decFactor = 1;
c@225 25
c@225 26 initialise( inLength, decFactor );
c@225 27 }
c@225 28
c@225 29 Decimator::~Decimator()
c@225 30 {
c@225 31 deInitialise();
c@225 32 }
c@225 33
c@225 34 void Decimator::initialise( unsigned int inLength, unsigned int decFactor)
c@225 35 {
c@225 36 m_inputLength = inLength;
c@225 37 m_decFactor = decFactor;
c@225 38 m_outputLength = m_inputLength / m_decFactor;
c@225 39
c@225 40 decBuffer = new double[ m_inputLength ];
c@225 41
c@247 42 // If adding new factors here, add them to
c@247 43 // getHighestSupportedFactor in the header as well
c@247 44
c@247 45 if(m_decFactor == 8)
c@247 46 {
c@247 47 //////////////////////////////////////////////////
c@247 48 b[0] = 0.060111378492136;
c@247 49 b[1] = -0.257323420830598;
c@247 50 b[2] = 0.420583503165928;
c@247 51 b[3] = -0.222750785197418;
c@247 52 b[4] = -0.222750785197418;
c@247 53 b[5] = 0.420583503165928;
c@247 54 b[6] = -0.257323420830598;
c@247 55 b[7] = 0.060111378492136;
c@247 56
c@247 57 a[0] = 1;
c@247 58 a[1] = -5.667654878577432;
c@247 59 a[2] = 14.062452278088417;
c@247 60 a[3] = -19.737303840697738;
c@247 61 a[4] = 16.889698874608641;
c@247 62 a[5] = -8.796600612325928;
c@247 63 a[6] = 2.577553446979888;
c@247 64 a[7] = -0.326903916815751;
c@247 65 //////////////////////////////////////////////////
c@247 66 }
c@247 67 else if( m_decFactor == 4 )
c@225 68 {
c@225 69 //////////////////////////////////////////////////
c@225 70 b[ 0 ] = 0.10133306904918619;
c@225 71 b[ 1 ] = -0.2447523353702363;
c@225 72 b[ 2 ] = 0.33622528590120965;
c@225 73 b[ 3 ] = -0.13936581560633518;
c@225 74 b[ 4 ] = -0.13936581560633382;
c@225 75 b[ 5 ] = 0.3362252859012087;
c@225 76 b[ 6 ] = -0.2447523353702358;
c@225 77 b[ 7 ] = 0.10133306904918594;
c@225 78
c@225 79 a[ 0 ] = 1;
c@225 80 a[ 1 ] = -3.9035590278139427;
c@225 81 a[ 2 ] = 7.5299379980621133;
c@225 82 a[ 3 ] = -8.6890803793177511;
c@225 83 a[ 4 ] = 6.4578667096099176;
c@225 84 a[ 5 ] = -3.0242979431223631;
c@225 85 a[ 6 ] = 0.83043385136748382;
c@225 86 a[ 7 ] = -0.094420800837809335;
c@225 87 //////////////////////////////////////////////////
c@225 88 }
c@225 89 else if( m_decFactor == 2 )
c@225 90 {
c@225 91 //////////////////////////////////////////////////
c@225 92 b[ 0 ] = 0.20898944260075727;
c@225 93 b[ 1 ] = 0.40011234879814367;
c@225 94 b[ 2 ] = 0.819741973072733;
c@225 95 b[ 3 ] = 1.0087419911682323;
c@225 96 b[ 4 ] = 1.0087419911682325;
c@225 97 b[ 5 ] = 0.81974197307273156;
c@225 98 b[ 6 ] = 0.40011234879814295;
c@225 99 b[ 7 ] = 0.20898944260075661;
c@225 100
c@225 101 a[ 0 ] = 1;
c@225 102 a[ 1 ] = 0.0077331184208358217;
c@225 103 a[ 2 ] = 1.9853971155964376;
c@225 104 a[ 3 ] = 0.19296739275341004;
c@225 105 a[ 4 ] = 1.2330748872852182;
c@225 106 a[ 5 ] = 0.18705341389316466;
c@225 107 a[ 6 ] = 0.23659265908013868;
c@225 108 a[ 7 ] = 0.032352924250533946;
c@225 109 }
c@225 110 else
c@225 111 {
c@247 112 if ( m_decFactor != 1 ) {
c@247 113 std::cerr << "WARNING: Decimator::initialise: unsupported decimation factor " << m_decFactor << ", no antialiasing filter will be used" << std::endl;
c@247 114 }
c@247 115
c@225 116 //////////////////////////////////////////////////
c@225 117 b[ 0 ] = 1;
c@225 118 b[ 1 ] = 0;
c@225 119 b[ 2 ] = 0;
c@225 120 b[ 3 ] = 0;
c@225 121 b[ 4 ] = 0;
c@225 122 b[ 5 ] = 0;
c@225 123 b[ 6 ] = 0;
c@225 124 b[ 7 ] = 0;
c@225 125
c@225 126 a[ 0 ] = 1;
c@225 127 a[ 1 ] = 0;
c@225 128 a[ 2 ] = 0;
c@225 129 a[ 3 ] = 0;
c@225 130 a[ 4 ] = 0;
c@225 131 a[ 5 ] = 0;
c@225 132 a[ 6 ] = 0;
c@225 133 a[ 7 ] = 0;
c@225 134 }
c@225 135
c@225 136 resetFilter();
c@225 137 }
c@225 138
c@225 139 void Decimator::deInitialise()
c@225 140 {
c@225 141 delete [] decBuffer;
c@225 142 }
c@225 143
c@225 144 void Decimator::resetFilter()
c@225 145 {
c@225 146 Input = Output = 0;
c@225 147
c@225 148 o1=o2=o3=o4=o5=o6=o7=0;
c@225 149 }
c@225 150
c@247 151 void Decimator::doAntiAlias(const double *src, double *dst, unsigned int length)
c@225 152 {
c@225 153
c@225 154 for( unsigned int i = 0; i < length; i++ )
c@225 155 {
c@225 156 Input = (double)src[ i ];
c@225 157
c@225 158 Output = Input * b[ 0 ] + o1;
c@225 159
c@225 160 o1 = Input * b[ 1 ] - Output * a[ 1 ] + o2;
c@225 161 o2 = Input * b[ 2 ] - Output * a[ 2 ] + o3;
c@225 162 o3 = Input * b[ 3 ] - Output * a[ 3 ] + o4;
c@225 163 o4 = Input * b[ 4 ] - Output * a[ 4 ] + o5;
c@225 164 o5 = Input * b[ 5 ] - Output * a[ 5 ] + o6;
c@225 165 o6 = Input * b[ 6 ] - Output * a[ 6 ] + o7;
c@225 166 o7 = Input * b[ 7 ] - Output * a[ 7 ] ;
c@225 167
c@225 168 dst[ i ] = Output;
c@225 169 }
c@225 170
c@225 171 }
c@225 172
c@280 173 void Decimator::doAntiAlias(const float *src, double *dst, unsigned int length)
c@280 174 {
c@280 175
c@280 176 for( unsigned int i = 0; i < length; i++ )
c@280 177 {
c@280 178 Input = (double)src[ i ];
c@280 179
c@280 180 Output = Input * b[ 0 ] + o1;
c@280 181
c@280 182 o1 = Input * b[ 1 ] - Output * a[ 1 ] + o2;
c@280 183 o2 = Input * b[ 2 ] - Output * a[ 2 ] + o3;
c@280 184 o3 = Input * b[ 3 ] - Output * a[ 3 ] + o4;
c@280 185 o4 = Input * b[ 4 ] - Output * a[ 4 ] + o5;
c@280 186 o5 = Input * b[ 5 ] - Output * a[ 5 ] + o6;
c@280 187 o6 = Input * b[ 6 ] - Output * a[ 6 ] + o7;
c@280 188 o7 = Input * b[ 7 ] - Output * a[ 7 ] ;
c@280 189
c@280 190 dst[ i ] = Output;
c@280 191 }
c@280 192
c@280 193 }
c@280 194
c@247 195 void Decimator::process(const double *src, double *dst)
c@225 196 {
c@225 197 if( m_decFactor != 1 )
c@225 198 {
c@225 199 doAntiAlias( src, decBuffer, m_inputLength );
c@225 200 }
c@225 201 unsigned idx = 0;
c@225 202
c@225 203 for( unsigned int i = 0; i < m_outputLength; i++ )
c@225 204 {
c@225 205 dst[ idx++ ] = decBuffer[ m_decFactor * i ];
c@225 206 }
c@225 207 }
c@280 208
c@280 209 void Decimator::process(const float *src, float *dst)
c@280 210 {
c@280 211 if( m_decFactor != 1 )
c@280 212 {
c@280 213 doAntiAlias( src, decBuffer, m_inputLength );
c@280 214 }
c@280 215 unsigned idx = 0;
c@280 216
c@280 217 for( unsigned int i = 0; i < m_outputLength; i++ )
c@280 218 {
c@280 219 dst[ idx++ ] = decBuffer[ m_decFactor * i ];
c@280 220 }
c@280 221 }