changeset 10:0a56b08b373f

Fix to output bin counting
author Chris Cannam
date Wed, 12 Mar 2014 13:06:14 +0000
parents ec39b9aa9f78
children ccb271dd0b84
files LowFreq.cpp
diffstat 1 files changed, 17 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/LowFreq.cpp	Wed Mar 12 12:47:47 2014 +0000
+++ b/LowFreq.cpp	Wed Mar 12 13:06:14 2014 +0000
@@ -252,9 +252,9 @@
     d.sampleType = OutputDescriptor::FixedSampleRate;
     d.sampleRate = getOutputSampleRate();
 
-    cerr << "output descriptor effective sample rate = " << d.sampleRate << endl;
-    cerr << "input sample rate = " << m_inputSampleRate << endl;
-    cerr << "input rate / output rate = " << m_inputSampleRate / d.sampleRate << endl;
+//    cerr << "output descriptor effective sample rate = " << d.sampleRate << endl;
+//    cerr << "input sample rate = " << m_inputSampleRate << endl;
+//    cerr << "input rate / output rate = " << m_inputSampleRate / d.sampleRate << endl;
     
     char namebuf[50];
     for (int i = 0; i < m_n; ++i) {
@@ -331,7 +331,7 @@
     // We want to return a number of bins n between frequencies fmin
     // and fmax.
 
-    // The bin frequency for bin j of an n-point fft is fs/n*j (where
+    // The bin frequency for bin i of an n-point fft is fs/n*i (where
     // fs is the sample rate), with the top half aliasing the bottom
     // half. To get a max bin frequency of fmax, in theory we can
     // downsample to a sample rate of fmax*2 and then run an m-point
@@ -351,6 +351,7 @@
     m_window = new Window<double>(HanningWindow, getFFTSize());
     m_buffer = std::vector<double>();
 
+/*
     cerr << "LowFreq::reset: freq range " << m_fmin << " to " << m_fmax << endl;
     cerr << "LowFreq::reset: block size " << m_blockSize
 	 << ", input sample rate " << m_inputSampleRate << endl;
@@ -358,6 +359,7 @@
 	 << ", target step " << getTargetStepSize()
 	 << ", resampler latency " << m_resampler->getLatency()
 	 << ", fft size " << getFFTSize() << endl;
+*/
 
     // Resampler's declared latency is its output latency. We need to
     // drop that number of samples from the start of its output...
@@ -381,8 +383,8 @@
 LowFreq::getTargetSampleRate() const
 {
     int tfs = int(ceil(m_fmax)) * 4;
-    cerr << "LowFreq::getTargetSampleRate: for range " << m_fmin << " -> " << m_fmax
-	 << " target rate is " << tfs << endl;
+//    cerr << "LowFreq::getTargetSampleRate: for range " << m_fmin << " -> " << m_fmax
+//	 << " target rate is " << tfs << endl;
     return tfs;
 }
 
@@ -432,23 +434,27 @@
 {
     // See note in reset() above
     int fftSize = 4 * int(ceil(m_fmax / ((m_fmax - m_fmin) / m_n)));
-    cerr << "LowFreq::getFFTSize: for range " << m_fmin << " -> " << m_fmax
-	 << " and n = " << m_n << ", fft size is " << fftSize << endl;
+//    cerr << "LowFreq::getFFTSize: for range " << m_fmin << " -> " << m_fmax
+//	 << " and n = " << m_n << ", fft size is " << fftSize << endl;
     return fftSize;
 }
 
 int
 LowFreq::getFirstOutputBin() const
 {
-    int first = int(ceil(m_fmin / getFFTSize()));
-    cerr << "LowFreq::getFirstOutputBin: for range " << m_fmin << " -> " << m_fmax
-	 << " and n = " << m_n << ", bin is " << first << endl;
+    // Frequency of fft bin i is fs/n*i. We want the first bin whose
+    // frequency is at least m_fmin.
+    int first = int(ceil((m_fmin * getFFTSize()) / getTargetSampleRate()));
+//    cerr << "LowFreq::getFirstOutputBin: for range " << m_fmin << " -> " << m_fmax
+//	 << " and n = " << m_n << ", bin is " << first << endl;
     return first;
 }
 
 float
 LowFreq::getOutputBinFrequency(int i) const
 {
+    // That is, frequency of the i'th output bin that we actually
+    // return -- the (i + getFirstOutputBin())'th bin in the fft
     return (float(getTargetSampleRate()) / getFFTSize())
 	* (i + getFirstOutputBin());
 }