1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
QM DSP Library
Centre for Digital Music, Queen Mary, University of London.
This file 2005-2006 Christian Landone.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include <iostream>
#include <cmath>
#include "maths/MathUtilities.h"
#include "Chromagram.h"
//----------------------------------------------------------------------------
Chromagram::Chromagram( ChromaConfig Config ) :
m_skGenerated(false)
{
initialise( Config );
}
int Chromagram::initialise( ChromaConfig Config )
{
m_FMin = Config.min; // min freq
m_FMax = Config.max; // max freq
m_BPO = Config.BPO; // bins per octave
m_normalise = Config.normalise; // if frame normalisation is required
// No. of constant Q bins
m_uK = ( unsigned int ) ceil( m_BPO * log(m_FMax/m_FMin)/log(2.0));
// Create array for chroma result
m_chromadata = new double[ m_BPO ];
// Create Config Structure for ConstantQ operator
CQConfig ConstantQConfig;
// Populate CQ config structure with parameters
// inherited from the Chroma config
ConstantQConfig.FS = Config.FS;
ConstantQConfig.min = m_FMin;
ConstantQConfig.max = m_FMax;
ConstantQConfig.BPO = m_BPO;
ConstantQConfig.CQThresh = Config.CQThresh;
// Initialise ConstantQ operator
m_ConstantQ = new ConstantQ( ConstantQConfig );
// Initialise working arrays
m_frameSize = m_ConstantQ->getfftlength();
m_hopSize = m_ConstantQ->gethop();
// Initialise FFT object
m_FFT = new FFTReal(m_frameSize);
m_FFTRe = new double[ m_frameSize ];
m_FFTIm = new double[ m_frameSize ];
m_CQRe = new double[ m_uK ];
m_CQIm = new double[ m_uK ];
m_window = 0;
m_windowbuf = 0;
return 1;
}
Chromagram::~Chromagram()
{
deInitialise();
}
int Chromagram::deInitialise()
{
delete[] m_windowbuf;
delete m_window;
delete [] m_chromadata;
delete m_FFT;
delete m_ConstantQ;
delete [] m_FFTRe;
delete [] m_FFTIm;
delete [] m_CQRe;
delete [] m_CQIm;
return 1;
}
//----------------------------------------------------------------------------------
// returns the absolute value of complex number xx + i*yy
double Chromagram::kabs(double xx, double yy)
{
double ab = sqrt(xx*xx + yy*yy);
return(ab);
}
//-----------------------------------------------------------------------------------
void Chromagram::unityNormalise(double *src)
{
double min, max;
double val = 0;
MathUtilities::getFrameMinMax( src, m_BPO, & min, &max );
for( unsigned int i = 0; i < m_BPO; i++ )
{
val = src[ i ] / max;
src[ i ] = val;
}
}
double* Chromagram::process( const double *data )
{
if (!m_skGenerated) {
// Generate CQ Kernel
m_ConstantQ->sparsekernel();
m_skGenerated = true;
}
if (!m_window) {
m_window = new Window<double>(HammingWindow, m_frameSize);
m_windowbuf = new double[m_frameSize];
}
for (int i = 0; i < m_frameSize; ++i) {
m_windowbuf[i] = data[i];
}
m_window->cut(m_windowbuf);
// FFT of current frame
m_FFT->process(false, m_windowbuf, m_FFTRe, m_FFTIm);
return process(m_FFTRe, m_FFTIm);
}
double* Chromagram::process( const double *real, const double *imag )
{
if (!m_skGenerated) {
// Generate CQ Kernel
m_ConstantQ->sparsekernel();
m_skGenerated = true;
}
// initialise chromadata to 0
for (unsigned i = 0; i < m_BPO; i++) m_chromadata[i] = 0;
double cmax = 0.0;
double cval = 0;
// Calculate ConstantQ frame
m_ConstantQ->process( real, imag, m_CQRe, m_CQIm );
// add each octave of cq data into Chromagram
const unsigned octaves = (int)floor(double( m_uK/m_BPO))-1;
for (unsigned octave = 0; octave <= octaves; octave++)
{
unsigned firstBin = octave*m_BPO;
for (unsigned i = 0; i < m_BPO; i++)
{
m_chromadata[i] += kabs( m_CQRe[ firstBin + i ], m_CQIm[ firstBin + i ]);
}
}
MathUtilities::normalise(m_chromadata, m_BPO, m_normalise);
return m_chromadata;
}
|