diff audioDB.cpp @ 148:8c1c6a5c1cc3 powertable

Abstract the operation to perform a windowed sequence sum into a function, and use that function in the four places that were already doing this operation.
author mas01cr
date Wed, 31 Oct 2007 13:08:21 +0000
parents a564e6d7a30c
children 12fee2a993bf
line wrap: on
line diff
--- a/audioDB.cpp	Wed Oct 31 12:01:28 2007 +0000
+++ b/audioDB.cpp	Wed Oct 31 13:08:21 2007 +0000
@@ -1588,6 +1588,30 @@
 
 }
 
+// This is a common pattern in sequence queries: what we are doing is
+// taking a window of length seqlen over a buffer of length length,
+// and placing the sum of the elements in that window in the first
+// element of the window: thus replacing all but the last seqlen
+// elements in the buffer the corresponding windowed sum.
+void audioDB::sequence_sum(double *buffer, int length, int seqlen) {
+  double tmp1, tmp2, *ps;
+  int j, w;
+
+  tmp1 = *buffer;
+  j = 1;
+  w = seqlen - 1;
+  while(w--) {
+    *buffer += buffer[j++];
+  }
+  ps = buffer + 1;
+  w = length - seqlen; // +1 - 1
+  while(w--) {
+    tmp2 = *ps;
+    *ps = *(ps - 1) - tmp1 + *(ps + seqlen - 1);
+    tmp1 = tmp2;
+    ps++;
+  }
+}
 
 // k nearest-neighbor (k-NN) search between query and target tracks
 // efficient implementation based on matched filter
@@ -1635,27 +1659,15 @@
   unsigned w = sequenceLength-1;
   unsigned i,j;
   double* ps;
-  double tmp1,tmp2;
 
   // Copy the L2 norm values to core to avoid disk random access later on
   memcpy(sNorm, l2normTable, dbVectors*sizeof(double));
   double* qnPtr = qNorm;
   double* snPtr = sNorm;
   for(i=0; i<dbH->numFiles; i++){
-    if(trackTable[i]>=sequenceLength){
-      tmp1=*snPtr;
-      j=1;
-      w=sequenceLength-1;
-      while(w--)
-	*snPtr+=snPtr[j++];
-      ps = snPtr+1;
-      w=trackTable[i]-sequenceLength; // +1 - 1
-      while(w--){
-	tmp2=*ps;
-	*ps=*(ps-1)-tmp1+*(ps+sequenceLength-1);
-	tmp1=tmp2;
-	ps++;
-      }
+    if(trackTable[i]>=sequenceLength) {
+      sequence_sum(snPtr, trackTable[i], sequenceLength);
+
       ps = snPtr;
       w=trackTable[i]-sequenceLength+1;
       while(w--){
@@ -1697,19 +1709,9 @@
   if(verbosity>4) {
     cerr << "silence thresh: " << SILENCE_THRESH;
   }
-  w=sequenceLength-1;
-  i=1;
-  tmp1=*qnPtr;
-  while(w--)
-    *qnPtr+=qnPtr[i++];
-  ps = qnPtr+1;
-  w=numVectors-sequenceLength; // +1 -1
-  while(w--){
-    tmp2=*ps;
-    *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1);
-    tmp1=tmp2;
-    ps++;
-  }
+
+  sequence_sum(qnPtr, numVectors, sequenceLength);
+
   ps = qnPtr;
   qMeanL2 = 0;
   w=numVectors-sequenceLength+1;
@@ -2072,7 +2074,6 @@
     }
   }
 
-
   // Clean up
   if(trackOffsetTable)
     delete[] trackOffsetTable;
@@ -2092,8 +2093,6 @@
     delete[] timesdata;
   if(meanDBdur)
     delete[] meanDBdur;
-
-
 }
 
 // Radius search between query and target tracks
@@ -2139,7 +2138,6 @@
   unsigned w = sequenceLength-1;
   unsigned i,j;
   double* ps;
-  double tmp1,tmp2;
 
   // Copy the L2 norm values to core to avoid disk random access later on
   memcpy(sNorm, l2normTable, dbVectors*sizeof(double));
@@ -2147,19 +2145,8 @@
   double* qnPtr = qNorm;
   for(i=0; i<dbH->numFiles; i++){
     if(trackTable[i]>=sequenceLength){
-      tmp1=*snPtr;
-      j=1;
-      w=sequenceLength-1;
-      while(w--)
-	*snPtr+=snPtr[j++];
-      ps = snPtr+1;
-      w=trackTable[i]-sequenceLength; // +1 - 1
-      while(w--){
-	tmp2=*ps;
-	*ps=*(ps-1)-tmp1+*(ps+sequenceLength-1);
-	tmp1=tmp2;
-	ps++;
-      }
+      sequence_sum(snPtr, trackTable[i], sequenceLength);
+
       ps = snPtr;
       w=trackTable[i]-sequenceLength+1;
       while(w--){
@@ -2201,19 +2188,9 @@
   if(verbosity>4) {
     cerr << "silence thresh: " << SILENCE_THRESH;
   }
-  w=sequenceLength-1;
-  i=1;
-  tmp1=*qnPtr;
-  while(w--)
-    *qnPtr+=qnPtr[i++];
-  ps = qnPtr+1;
-  w=numVectors-sequenceLength; // +1 -1
-  while(w--){
-    tmp2=*ps;
-    *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1);
-    tmp1=tmp2;
-    ps++;
-  }
+
+  sequence_sum(qnPtr, numVectors, sequenceLength);
+
   ps = qnPtr;
   qMeanL2 = 0;
   w=numVectors-sequenceLength+1;