Skip to content

Commit

Permalink
Merge branch 'chroma-key-tuning-review'
Browse files Browse the repository at this point in the history
  • Loading branch information
cannam committed May 30, 2019
2 parents 610a16f + e786faa commit 4c406d9
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 145 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.o
*~
*.orig
*.a
doc/html/
tests/test-*
tests/test.log
*.rej
ext/uncertain/
*.obj
build/msvc/Debug/
build/msvc/x64/Release/
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ addons:
- valgrind

script:
- make -f build/linux/Makefile.linux64
- ( cd tests ; make )
- make -f build/linux/Makefile.linux64 test

3 changes: 3 additions & 0 deletions build/general/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ $(LIBRARY): $(OBJECTS)
$(AR) cr $@ $^
$(RANLIB) $@

test: $(LIBRARY)
$(MAKE) -C tests

depend:
makedepend -fbuild/general/Makefile.inc -Y -- $(CFLAGS) -- $(SOURCES)

Expand Down
26 changes: 19 additions & 7 deletions dsp/chromagram/Chromagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ int Chromagram::initialise( ChromaConfig Config )
m_BPO = Config.BPO; // bins per octave
m_normalise = Config.normalise; // if frame normalisation is required

// No. of constant Q bins
m_uK = (int) ceil( m_BPO * log(m_FMax/m_FMin)/log(2.0));
// Extend range to a full octave
double octaves = log(m_FMax / m_FMin) / log(2.0);
m_FMax = m_FMin * pow(2.0, ceil(octaves));

// Create array for chroma result
m_chromadata = new double[ m_BPO ];
Expand All @@ -44,7 +45,7 @@ int Chromagram::initialise( ChromaConfig Config )

// Populate CQ config structure with parameters
// inherited from the Chroma config
ConstantQConfig.FS = Config.FS;
ConstantQConfig.FS = Config.FS;
ConstantQConfig.min = m_FMin;
ConstantQConfig.max = m_FMax;
ConstantQConfig.BPO = m_BPO;
Expand All @@ -53,6 +54,9 @@ int Chromagram::initialise( ChromaConfig Config )
// Initialise ConstantQ operator
m_ConstantQ = new ConstantQ( ConstantQConfig );

// No. of constant Q bins
m_uK = m_ConstantQ->getK();

// Initialise working arrays
m_frameSize = m_ConstantQ->getfftlength();
m_hopSize = m_ConstantQ->gethop();
Expand Down Expand Up @@ -121,7 +125,7 @@ void Chromagram::unityNormalise(double *src)
}


double* Chromagram::process( const double *data )
double *Chromagram::process(const double *data)
{
if (!m_skGenerated) {
// Generate CQ Kernel
Expand All @@ -139,12 +143,20 @@ double* Chromagram::process( const double *data )
}
m_window->cut(m_windowbuf);

// The frequency-domain version expects pre-fftshifted input - so
// we must do the same here
for (int i = 0; i < m_frameSize/2; ++i) {
double tmp = m_windowbuf[i];
m_windowbuf[i] = m_windowbuf[i + m_frameSize/2];
m_windowbuf[i + m_frameSize/2] = tmp;
}

m_FFT->forward(m_windowbuf, m_FFTRe, m_FFTIm);

return process(m_FFTRe, m_FFTIm);
}

double* Chromagram::process( const double *real, const double *imag )
double *Chromagram::process(const double *real, const double *imag)
{
if (!m_skGenerated) {
// Generate CQ Kernel
Expand All @@ -159,8 +171,8 @@ double* Chromagram::process( const double *real, const double *imag )
m_ConstantQ->process( real, imag, m_CQRe, m_CQIm );

// add each octave of cq data into Chromagram
const int octaves = (int)floor(double( m_uK/m_BPO))-1;
for (int octave = 0; octave <= octaves; octave++)
const int octaves = m_uK / m_BPO;
for (int octave = 0; octave < octaves; octave++)
{
int firstBin = octave*m_BPO;
for (int i = 0; i < m_BPO; i++)
Expand Down
37 changes: 31 additions & 6 deletions dsp/chromagram/Chromagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "base/Window.h"
#include "ConstantQ.h"

struct ChromaConfig{
int FS;
struct ChromaConfig {
double FS;
double min;
double max;
int BPO;
Expand All @@ -35,10 +35,35 @@ class Chromagram
public:
Chromagram( ChromaConfig Config );
~Chromagram();

double* process( const double *data ); // time domain
double* process( const double *real, const double *imag ); // frequency domain
void unityNormalise( double* src );

/**
* Process a time-domain input signal of length equal to
* getFrameSize().
*
* The returned buffer contains the chromagram values indexed by
* bin, with the number of values corresponding to the BPO field
* in the ChromaConfig supplied at construction. It is owned by
* the Chromagram object and is reused from one process call to
* the next.
*/
double *process(const double *data);

/**
* Process a frequency-domain input signal generated from a
* time-domain signal of length equal to getFrameSize() that has
* been windowed and "fftshifted" to place the zero index in the
* centre of the frame. The real and imag buffers must each
* contain the full getFrameSize() frequency bins.
*
* The returned buffer contains the chromagram values indexed by
* bin, with the number of values corresponding to the BPO field
* in the ChromaConfig supplied at construction. It is owned by
* the Chromagram object and is reused from one process call to
* the next.
*/
double *process(const double *real, const double *imag);

void unityNormalise(double* src);

// Complex arithmetic
double kabs( double real, double imag );
Expand Down
13 changes: 9 additions & 4 deletions dsp/chromagram/ConstantQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ void ConstantQ::sparsekernel()
// Computing a hamming window
const unsigned hammingLength = (int) ceil( m_dQ * m_FS / ( m_FMin * pow(2,((double)(k))/(double)m_BPO)));

// cerr << "k = " << k << ", q = " << m_dQ << ", m_FMin = " << m_FMin << ", hammingLength = " << hammingLength << " (rounded up from " << (m_dQ * m_FS / ( m_FMin * pow(2,((double)(k))/(double)m_BPO))) << ")" << endl;


unsigned origin = m_FFTLength/2 - hammingLength/2;

for (unsigned i=0; i<hammingLength; i++)
Expand Down Expand Up @@ -159,7 +162,7 @@ void ConstantQ::sparsekernel()
const double squaredBin = squaredModule( transfHammingWindowRe[ j ], transfHammingWindowIm[ j ]);
if (squaredBin <= squareThreshold) continue;

// Insert non-zero position indexes, doubled because they are floats
// Insert non-zero position indexes
sk->is.push_back(j);
sk->js.push_back(k);

Expand Down Expand Up @@ -271,6 +274,7 @@ double* ConstantQ::process( const double* fftdata )
{
const unsigned row = cqbin[i];
const unsigned col = fftbin[i];
if (col == 0) continue;
const double & r1 = real[i];
const double & i1 = imag[i];
const double & r2 = fftdata[ (2*m_FFTLength) - 2*col - 2 ];
Expand Down Expand Up @@ -300,7 +304,7 @@ void ConstantQ::initialise( CQConfig Config )
// work out length of fft required for this constant Q Filter bank
m_FFTLength = (int) pow(2, nextpow2(ceil( m_dQ*m_FS/m_FMin )));

m_hop = m_FFTLength/8; // <------ hop size is window length divided by 32
m_hop = m_FFTLength/8;

// std::cerr << "ConstantQ::initialise: -> fft length = " << m_FFTLength << ", hop = " << m_hop << std::endl;

Expand Down Expand Up @@ -340,10 +344,11 @@ void ConstantQ::process(const double *FFTRe, const double* FFTIm,
{
const unsigned row = cqbin[i];
const unsigned col = fftbin[i];
if (col == 0) continue;
const double & r1 = real[i];
const double & i1 = imag[i];
const double & r2 = FFTRe[ m_FFTLength - col - 1 ];
const double & i2 = FFTIm[ m_FFTLength - col - 1 ];
const double & r2 = FFTRe[ m_FFTLength - col ];
const double & i2 = FFTIm[ m_FFTLength - col ];
// add the multiplication
CQRe[ row ] += (r1*r2 - i1*i2);
CQIm[ row ] += (r1*i2 + i1*r2);
Expand Down
6 changes: 3 additions & 3 deletions dsp/chromagram/ConstantQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "maths/MathAliases.h"
#include "maths/MathUtilities.h"

struct CQConfig{
unsigned int FS; // samplerate
struct CQConfig {
double FS; // samplerate
double min; // minimum frequency
double max; // maximum frequency
unsigned int BPO; // bins per octave
Expand Down Expand Up @@ -58,7 +58,7 @@ class ConstantQ {
void deInitialise();

double* m_CQdata;
unsigned int m_FS;
double m_FS;
double m_FMin;
double m_FMax;
double m_dQ;
Expand Down
Loading

0 comments on commit 4c406d9

Please sign in to comment.