annotate dsp/chromagram/ConstantQ.cpp @ 0:d7116e3183f8

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