annotate dsp/chromagram/Chromagram.cpp @ 309:d5014ab8b0e5

* Add GPL and README; some tidying
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 13 Dec 2010 14:55:28 +0000
parents 255e431ae3d4
children f6ccde089491
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@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 <iostream>
c@225 17 #include <cmath>
c@241 18 #include "maths/MathUtilities.h"
c@225 19 #include "Chromagram.h"
c@225 20
c@225 21 //----------------------------------------------------------------------------
c@225 22
c@276 23 Chromagram::Chromagram( ChromaConfig Config ) :
c@276 24 m_skGenerated(false)
c@225 25 {
c@225 26 initialise( Config );
c@225 27 }
c@225 28
c@225 29 int Chromagram::initialise( ChromaConfig Config )
c@225 30 {
c@225 31 m_FMin = Config.min; // min freq
c@225 32 m_FMax = Config.max; // max freq
c@225 33 m_BPO = Config.BPO; // bins per octave
c@259 34 m_normalise = Config.normalise; // if frame normalisation is required
c@225 35
c@225 36 // No. of constant Q bins
c@225 37 m_uK = ( unsigned int ) ceil( m_BPO * log(m_FMax/m_FMin)/log(2.0));
c@225 38
c@225 39 // Create array for chroma result
c@225 40 m_chromadata = new double[ m_BPO ];
c@225 41
c@225 42 // Create Config Structure for ConstantQ operator
c@225 43 CQConfig ConstantQConfig;
c@225 44
c@225 45 // Populate CQ config structure with parameters
c@225 46 // inherited from the Chroma config
c@225 47 ConstantQConfig.FS = Config.FS;
c@225 48 ConstantQConfig.min = m_FMin;
c@225 49 ConstantQConfig.max = m_FMax;
c@225 50 ConstantQConfig.BPO = m_BPO;
c@225 51 ConstantQConfig.CQThresh = Config.CQThresh;
c@225 52
c@225 53 // Initialise ConstantQ operator
c@225 54 m_ConstantQ = new ConstantQ( ConstantQConfig );
c@225 55
c@225 56 // Initialise working arrays
c@225 57 m_frameSize = m_ConstantQ->getfftlength();
c@225 58 m_hopSize = m_ConstantQ->gethop();
c@225 59
c@289 60 // Initialise FFT object
c@289 61 m_FFT = new FFTReal(m_frameSize);
c@289 62
c@225 63 m_FFTRe = new double[ m_frameSize ];
c@225 64 m_FFTIm = new double[ m_frameSize ];
c@225 65 m_CQRe = new double[ m_uK ];
c@225 66 m_CQIm = new double[ m_uK ];
c@225 67
c@257 68 m_window = 0;
c@257 69 m_windowbuf = 0;
c@257 70
c@225 71 return 1;
c@225 72 }
c@225 73
c@225 74 Chromagram::~Chromagram()
c@225 75 {
c@225 76 deInitialise();
c@225 77 }
c@225 78
c@225 79 int Chromagram::deInitialise()
c@225 80 {
c@257 81 delete[] m_windowbuf;
c@257 82 delete m_window;
c@257 83
c@225 84 delete [] m_chromadata;
c@225 85
c@225 86 delete m_FFT;
c@225 87
c@225 88 delete m_ConstantQ;
c@225 89
c@225 90 delete [] m_FFTRe;
c@225 91 delete [] m_FFTIm;
c@225 92 delete [] m_CQRe;
c@225 93 delete [] m_CQIm;
c@225 94 return 1;
c@225 95 }
c@225 96
c@225 97 //----------------------------------------------------------------------------------
c@225 98 // returns the absolute value of complex number xx + i*yy
c@225 99 double Chromagram::kabs(double xx, double yy)
c@225 100 {
c@225 101 double ab = sqrt(xx*xx + yy*yy);
c@225 102 return(ab);
c@225 103 }
c@225 104 //-----------------------------------------------------------------------------------
c@225 105
c@225 106
c@225 107 void Chromagram::unityNormalise(double *src)
c@225 108 {
c@225 109 double min, max;
c@225 110
c@225 111 double val = 0;
c@225 112
c@225 113 MathUtilities::getFrameMinMax( src, m_BPO, & min, &max );
c@225 114
c@225 115 for( unsigned int i = 0; i < m_BPO; i++ )
c@225 116 {
c@225 117 val = src[ i ] / max;
c@225 118
c@225 119 src[ i ] = val;
c@225 120 }
c@225 121 }
c@225 122
c@225 123
c@257 124 double* Chromagram::process( const double *data )
c@225 125 {
c@276 126 if (!m_skGenerated) {
c@276 127 // Generate CQ Kernel
c@276 128 m_ConstantQ->sparsekernel();
c@276 129 m_skGenerated = true;
c@276 130 }
c@276 131
c@257 132 if (!m_window) {
c@257 133 m_window = new Window<double>(HammingWindow, m_frameSize);
c@257 134 m_windowbuf = new double[m_frameSize];
c@257 135 }
c@257 136
c@257 137 for (int i = 0; i < m_frameSize; ++i) {
c@257 138 m_windowbuf[i] = data[i];
c@257 139 }
c@257 140 m_window->cut(m_windowbuf);
c@257 141
c@228 142 // FFT of current frame
c@298 143 m_FFT->process(false, m_windowbuf, m_FFTRe, m_FFTIm);
c@228 144
c@228 145 return process(m_FFTRe, m_FFTIm);
c@228 146 }
c@228 147
c@257 148 double* Chromagram::process( const double *real, const double *imag )
c@228 149 {
c@276 150 if (!m_skGenerated) {
c@276 151 // Generate CQ Kernel
c@276 152 m_ConstantQ->sparsekernel();
c@276 153 m_skGenerated = true;
c@276 154 }
c@276 155
c@228 156 // initialise chromadata to 0
c@228 157 for (unsigned i = 0; i < m_BPO; i++) m_chromadata[i] = 0;
c@225 158
c@225 159 double cmax = 0.0;
c@225 160 double cval = 0;
c@225 161
c@225 162 // Calculate ConstantQ frame
c@228 163 m_ConstantQ->process( real, imag, m_CQRe, m_CQIm );
c@225 164
c@225 165 // add each octave of cq data into Chromagram
c@225 166 const unsigned octaves = (int)floor(double( m_uK/m_BPO))-1;
c@228 167 for (unsigned octave = 0; octave <= octaves; octave++)
c@225 168 {
c@225 169 unsigned firstBin = octave*m_BPO;
c@228 170 for (unsigned i = 0; i < m_BPO; i++)
c@225 171 {
c@225 172 m_chromadata[i] += kabs( m_CQRe[ firstBin + i ], m_CQIm[ firstBin + i ]);
c@225 173 }
c@225 174 }
c@225 175
c@259 176 MathUtilities::normalise(m_chromadata, m_BPO, m_normalise);
c@225 177
c@225 178 return m_chromadata;
c@225 179 }
c@225 180
c@225 181