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