annotate dsp/chromagram/ConstantQ.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 07ac3de1e53b
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 QM DSP Library
c@225 4
c@225 5 Centre for Digital Music, Queen Mary, University of London.
c@225 6 This file copyright 2005-2006 Christian Landone.
c@225 7 All rights reserved.
c@225 8 */
c@225 9
c@225 10 #include "ConstantQ.h"
c@225 11 #include "dsp/transforms/FFT.h"
c@225 12
c@225 13 //---------------------------------------------------------------------------
c@225 14 // nextpow2 returns the smallest integer n such that 2^n >= x.
c@225 15 static double nextpow2(double x) {
c@225 16 double y = ceil(log(x)/log(2.0));
c@225 17 return(y);
c@225 18 }
c@225 19
c@225 20 static double squaredModule(const double & xx, const double & yy) {
c@225 21 return xx*xx + yy*yy;
c@225 22 }
c@225 23
c@225 24 //----------------------------------------------------------------------------
c@225 25
c@225 26 ConstantQ::ConstantQ( CQConfig Config )
c@225 27 {
c@225 28 initialise( Config );
c@225 29 }
c@225 30
c@225 31 ConstantQ::~ConstantQ()
c@225 32 {
c@225 33 deInitialise();
c@225 34 }
c@225 35
c@225 36 //----------------------------------------------------------------------------
c@225 37 void ConstantQ::sparsekernel()
c@225 38 {
c@225 39 //generates spectral kernel matrix (upside down?)
c@225 40 // initialise temporal kernel with zeros, twice length to deal w. complex numbers
c@225 41
c@225 42 double* hammingWindowRe = new double [ m_FFTLength ];
c@225 43 double* hammingWindowIm = new double [ m_FFTLength ];
c@225 44 double* transfHammingWindowRe = new double [ m_FFTLength ];
c@225 45 double* transfHammingWindowIm = new double [ m_FFTLength ];
c@225 46
c@225 47 for (unsigned u=0; u < m_FFTLength; u++)
c@225 48 {
c@225 49 hammingWindowRe[u] = 0;
c@225 50 hammingWindowIm[u] = 0;
c@225 51 }
c@225 52
c@225 53
c@225 54 // Here, fftleng*2 is a guess of the number of sparse cells in the matrix
c@225 55 // The matrix K x fftlength but the non-zero cells are an antialiased
c@225 56 // square root function. So mostly is a line, with some grey point.
c@225 57 m_sparseKernelIs.reserve( m_FFTLength*2 );
c@225 58 m_sparseKernelJs.reserve( m_FFTLength*2 );
c@225 59 m_sparseKernelRealValues.reserve( m_FFTLength*2 );
c@225 60 m_sparseKernelImagValues.reserve( m_FFTLength*2 );
c@225 61
c@225 62 // for each bin value K, calculate temporal kernel, take its fft to
c@225 63 //calculate the spectral kernel then threshold it to make it sparse and
c@225 64 //add it to the sparse kernels matrix
c@225 65 double squareThreshold = m_CQThresh * m_CQThresh;
c@225 66
c@225 67 FFT m_FFT;
c@225 68
c@225 69 for (unsigned k = m_uK; k--; )
c@225 70 {
c@225 71 // Computing a hamming window
c@225 72 const unsigned hammingLength = (int) ceil( m_dQ * m_FS / ( m_FMin * pow(2,((double)(k))/(double)m_BPO)));
c@225 73 for (unsigned i=0; i<hammingLength; i++)
c@225 74 {
c@225 75 const double angle = 2*PI*m_dQ*i/hammingLength;
c@225 76 const double real = cos(angle);
c@225 77 const double imag = sin(angle);
c@225 78 const double absol = hamming(hammingLength, i)/hammingLength;
c@225 79 hammingWindowRe[ i ] = absol*real;
c@225 80 hammingWindowIm[ i ] = absol*imag;
c@225 81 }
c@225 82
c@225 83 //do fft of hammingWindow
c@225 84 m_FFT.process( m_FFTLength, 0, hammingWindowRe, hammingWindowIm, transfHammingWindowRe, transfHammingWindowIm );
c@225 85
c@225 86
c@225 87 for (unsigned j=0; j<( m_FFTLength ); j++)
c@225 88 {
c@225 89 // perform thresholding
c@225 90 const double squaredBin = squaredModule( transfHammingWindowRe[ j ], transfHammingWindowIm[ j ]);
c@225 91 if (squaredBin <= squareThreshold) continue;
c@225 92
c@225 93 // Insert non-zero position indexes, doubled because they are floats
c@225 94 m_sparseKernelIs.push_back(j);
c@225 95 m_sparseKernelJs.push_back(k);
c@225 96
c@225 97 // take conjugate, normalise and add to array sparkernel
c@225 98 m_sparseKernelRealValues.push_back( transfHammingWindowRe[ j ]/m_FFTLength);
c@225 99 m_sparseKernelImagValues.push_back(-transfHammingWindowIm[ j ]/m_FFTLength);
c@225 100 }
c@225 101
c@225 102 }
c@225 103
c@225 104 delete [] hammingWindowRe;
c@225 105 delete [] hammingWindowIm;
c@225 106 delete [] transfHammingWindowRe;
c@225 107 delete [] transfHammingWindowIm;
c@225 108
c@225 109 }
c@225 110
c@225 111 //-----------------------------------------------------------------------------
c@225 112 double* ConstantQ::process( double* fftdata )
c@225 113 {
c@225 114 for (unsigned row=0; row<2*m_uK; row++)
c@225 115 {
c@225 116 m_CQdata[ row ] = 0;
c@225 117 m_CQdata[ row+1 ] = 0;
c@225 118 }
c@225 119 const unsigned *fftbin = &(m_sparseKernelIs[0]);
c@225 120 const unsigned *cqbin = &(m_sparseKernelJs[0]);
c@225 121 const double *real = &(m_sparseKernelRealValues[0]);
c@225 122 const double *imag = &(m_sparseKernelImagValues[0]);
c@225 123 const unsigned int sparseCells = m_sparseKernelRealValues.size();
c@225 124
c@225 125 for (unsigned i = 0; i<sparseCells; i++)
c@225 126 {
c@225 127 const unsigned row = cqbin[i];
c@225 128 const unsigned col = fftbin[i];
c@225 129 const double & r1 = real[i];
c@225 130 const double & i1 = imag[i];
c@225 131 const double & r2 = fftdata[ (2*m_FFTLength) - 2*col];
c@225 132 const double & i2 = fftdata[ (2*m_FFTLength) - 2*col+1];
c@225 133 // add the multiplication
c@225 134 m_CQdata[ 2*row ] += (r1*r2 - i1*i2);
c@225 135 m_CQdata[ 2*row+1] += (r1*i2 + i1*r2);
c@225 136 }
c@225 137
c@225 138 return m_CQdata;
c@225 139 }
c@225 140
c@225 141
c@225 142 void ConstantQ::initialise( CQConfig Config )
c@225 143 {
c@225 144 m_FS = Config.FS;
c@225 145 m_FMin = Config.min; // min freq
c@225 146 m_FMax = Config.max; // max freq
c@225 147 m_BPO = Config.BPO; // bins per octave
c@225 148 m_CQThresh = Config.CQThresh;// ConstantQ threshold for kernel generation
c@225 149
c@225 150 m_dQ = 1/(pow(2,(1/(double)m_BPO))-1); // Work out Q value for Filter bank
c@225 151 m_uK = (unsigned int) ceil(m_BPO * log(m_FMax/m_FMin)/log(2.0)); // No. of constant Q bins
c@225 152
c@225 153 // work out length of fft required for this constant Q Filter bank
c@225 154 m_FFTLength = (int) pow(2, nextpow2(ceil( m_dQ*m_FS/m_FMin )));
c@225 155
c@225 156 m_hop = m_FFTLength/8; // <------ hop size is window length divided by 32
c@225 157
c@225 158 // allocate memory for cqdata
c@225 159 m_CQdata = new double [2*m_uK];
c@225 160 }
c@225 161
c@225 162 void ConstantQ::deInitialise()
c@225 163 {
c@225 164 delete [] m_CQdata;
c@225 165 }
c@225 166
c@225 167 void ConstantQ::process(double *FFTRe, double* FFTIm, double *CQRe, double *CQIm)
c@225 168 {
c@225 169 for (unsigned row=0; row<m_uK; row++)
c@225 170 {
c@225 171 CQRe[ row ] = 0;
c@225 172 CQIm[ row ] = 0;
c@225 173 }
c@225 174
c@225 175 const unsigned *fftbin = &(m_sparseKernelIs[0]);
c@225 176 const unsigned *cqbin = &(m_sparseKernelJs[0]);
c@225 177 const double *real = &(m_sparseKernelRealValues[0]);
c@225 178 const double *imag = &(m_sparseKernelImagValues[0]);
c@225 179 const unsigned int sparseCells = m_sparseKernelRealValues.size();
c@225 180
c@225 181 for (unsigned i = 0; i<sparseCells; i++)
c@225 182 {
c@225 183 const unsigned row = cqbin[i];
c@225 184 const unsigned col = fftbin[i];
c@225 185 const double & r1 = real[i];
c@225 186 const double & i1 = imag[i];
c@225 187 const double & r2 = FFTRe[ m_FFTLength- col];
c@225 188 const double & i2 = FFTIm[ m_FFTLength - col];
c@225 189 // add the multiplication
c@225 190 CQRe[ row ] += (r1*r2 - i1*i2);
c@225 191 CQIm[ row ] += (r1*i2 + i1*r2);
c@225 192 }
c@225 193 }