changeset 111:fe4440881a08

Make kernel a bit more robust to weird parameters
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 14 May 2014 14:59:38 +0100
parents fdd32f995b0d
children a45b51ea00a2
files cpp-qm-dsp/CQKernel.cpp
diffstat 1 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/cpp-qm-dsp/CQKernel.cpp	Wed May 14 14:04:34 2014 +0100
+++ b/cpp-qm-dsp/CQKernel.cpp	Wed May 14 14:59:38 2014 +0100
@@ -79,6 +79,18 @@
         (m_p.Q * m_p.sampleRate /
          (m_p.minFrequency * pow(2, (bpo - 1.0) / bpo)));
 
+    if (minNK == 0 || maxNK == 0) {
+        // most likely pathological parameters of some sort
+        cerr << "WARNING: CQKernel::generateKernel: minNK or maxNK is zero (minNK == " << minNK << ", maxNK == " << maxNK << "), not generating a kernel" << endl;
+        m_p.atomSpacing = 0;
+        m_p.firstCentre = 0;
+        m_p.fftSize = 0;
+        m_p.atomsPerFrame = 0;
+        m_p.lastCentre = 0;
+        m_p.fftHop = 0;
+        return;
+    }
+
     m_p.atomSpacing = round(minNK * atomHopFactor);
     m_p.firstCentre = m_p.atomSpacing * ceil(ceil(maxNK / 2.0) / m_p.atomSpacing);
     m_p.fftSize = MathUtilities::nextPowerOfTwo
@@ -87,7 +99,7 @@
     m_p.atomsPerFrame = floor
         (1.0 + (m_p.fftSize - ceil(maxNK / 2.0) - m_p.firstCentre) / m_p.atomSpacing);
 
-    cerr << "atomsPerFrame = " << m_p.atomsPerFrame << " (atomHopFactor = " << atomHopFactor << ")" << endl;
+    cerr << "atomsPerFrame = " << m_p.atomsPerFrame << " (atomHopFactor = " << atomHopFactor << ", atomSpacing = " << m_p.atomSpacing << ", fftSize = " << m_p.fftSize << ", maxNK = " << maxNK << ", firstCentre = " << m_p.firstCentre << ")" << endl;
 
     m_p.lastCentre = m_p.firstCentre + (m_p.atomsPerFrame - 1) * m_p.atomSpacing;
 
@@ -280,6 +292,8 @@
     // straightforward matrix multiply (taking into account m_kernel's
     // slightly-sparse representation)
 
+    if (m_kernel.data.empty()) return vector<C>();
+
     int nrows = m_p.binsPerOctave * m_p.atomsPerFrame;
 
     vector<C> rv(nrows, C());
@@ -302,6 +316,8 @@
     // conjugate-transpose of the kernel because we expect to be doing
     // more forward transforms than inverse ones.
 
+    if (m_kernel.data.empty()) return vector<C>();
+
     int ncols = m_p.binsPerOctave * m_p.atomsPerFrame;
     int nrows = m_p.fftSize;