Mercurial > hg > audiodb
comparison audioDB.cpp @ 125:26ec0906adb9
Be a bit more careful about qNorm handling and freeing: don't assign qNorm
anywhere, but use a (new) interior pointer instead.
Also fix test 0023 for the correct results.
author | mas01cr |
---|---|
date | Wed, 17 Oct 2007 14:44:53 +0000 |
parents | 18a64ac14d2a |
children | f789aa32382f |
comparison
equal
deleted
inserted
replaced
124:394c8419217c | 125:26ec0906adb9 |
---|---|
1391 double* ps; | 1391 double* ps; |
1392 double tmp1,tmp2; | 1392 double tmp1,tmp2; |
1393 | 1393 |
1394 // Copy the L2 norm values to core to avoid disk random access later on | 1394 // Copy the L2 norm values to core to avoid disk random access later on |
1395 memcpy(sNorm, l2normTable, dbVectors*sizeof(double)); | 1395 memcpy(sNorm, l2normTable, dbVectors*sizeof(double)); |
1396 double* qnPtr = qNorm; | |
1396 double* snPtr = sNorm; | 1397 double* snPtr = sNorm; |
1397 for(i=0; i<dbH->numFiles; i++){ | 1398 for(i=0; i<dbH->numFiles; i++){ |
1398 if(trackTable[i]>=sequenceLength){ | 1399 if(trackTable[i]>=sequenceLength){ |
1399 tmp1=*snPtr; | 1400 tmp1=*snPtr; |
1400 j=1; | 1401 j=1; |
1450 if(verbosity>4) { | 1451 if(verbosity>4) { |
1451 cerr << "silence thresh: " << SILENCE_THRESH; | 1452 cerr << "silence thresh: " << SILENCE_THRESH; |
1452 } | 1453 } |
1453 w=sequenceLength-1; | 1454 w=sequenceLength-1; |
1454 i=1; | 1455 i=1; |
1455 tmp1=*qNorm; | 1456 tmp1=*qnPtr; |
1456 while(w--) | 1457 while(w--) |
1457 *qNorm+=qNorm[i++]; | 1458 *qnPtr+=qnPtr[i++]; |
1458 ps = qNorm+1; | 1459 ps = qnPtr+1; |
1459 w=numVectors-sequenceLength; // +1 -1 | 1460 w=numVectors-sequenceLength; // +1 -1 |
1460 while(w--){ | 1461 while(w--){ |
1461 tmp2=*ps; | 1462 tmp2=*ps; |
1462 *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1); | 1463 *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1); |
1463 tmp1=tmp2; | 1464 tmp1=tmp2; |
1464 ps++; | 1465 ps++; |
1465 } | 1466 } |
1466 ps = qNorm; | 1467 ps = qnPtr; |
1467 qMeanL2 = 0; | 1468 qMeanL2 = 0; |
1468 w=numVectors-sequenceLength+1; | 1469 w=numVectors-sequenceLength+1; |
1469 while(w--){ | 1470 while(w--){ |
1470 *ps=sqrt(*ps); | 1471 *ps=sqrt(*ps); |
1471 qMeanL2+=*ps++; | 1472 qMeanL2+=*ps++; |
1552 else{ | 1553 else{ |
1553 if(verbosity>1) { | 1554 if(verbosity>1) { |
1554 cerr << "query point: " << queryPoint << endl; cerr.flush(); | 1555 cerr << "query point: " << queryPoint << endl; cerr.flush(); |
1555 } | 1556 } |
1556 query=query+queryPoint*dbH->dim; | 1557 query=query+queryPoint*dbH->dim; |
1557 qNorm=qNorm+queryPoint; | 1558 qnPtr=qnPtr+queryPoint; |
1558 numVectors=wL; | 1559 numVectors=wL; |
1559 } | 1560 } |
1560 | 1561 |
1561 double ** D = 0; // Differences query and target | 1562 double ** D = 0; // Differences query and target |
1562 double ** DD = 0; // Matched filter distance | 1563 double ** DD = 0; // Matched filter distance |
1682 } | 1683 } |
1683 | 1684 |
1684 // Search for minimum distance by shingles (concatenated vectors) | 1685 // Search for minimum distance by shingles (concatenated vectors) |
1685 for(j=0;j<=numVectors-wL;j+=HOP_SIZE) | 1686 for(j=0;j<=numVectors-wL;j+=HOP_SIZE) |
1686 for(k=0;k<=trackTable[track]-wL;k+=HOP_SIZE){ | 1687 for(k=0;k<=trackTable[track]-wL;k+=HOP_SIZE){ |
1687 thisDist=2-(2/(qNorm[j]*sNorm[trackIndexOffset+k]))*DD[j][k]; | 1688 thisDist=2-(2/(qnPtr[j]*sNorm[trackIndexOffset+k]))*DD[j][k]; |
1688 if(verbosity>10) { | 1689 if(verbosity>10) { |
1689 cerr << thisDist << " " << qNorm[j] << " " << sNorm[trackIndexOffset+k] << endl; | 1690 cerr << thisDist << " " << qnPtr[j] << " " << sNorm[trackIndexOffset+k] << endl; |
1690 } | 1691 } |
1691 // Gather chi^2 statistics | 1692 // Gather chi^2 statistics |
1692 if(thisDist<minSample) | 1693 if(thisDist<minSample) |
1693 minSample=thisDist; | 1694 minSample=thisDist; |
1694 else if(thisDist>maxSample) | 1695 else if(thisDist>maxSample) |
1697 sampleCount++; | 1698 sampleCount++; |
1698 sampleSum+=thisDist; | 1699 sampleSum+=thisDist; |
1699 logSampleSum+=log(thisDist); | 1700 logSampleSum+=log(thisDist); |
1700 } | 1701 } |
1701 | 1702 |
1702 // diffL2 = fabs(qNorm[j] - sNorm[trackIndexOffset+k]); | 1703 // diffL2 = fabs(qnPtr[j] - sNorm[trackIndexOffset+k]); |
1703 // Power test | 1704 // Power test |
1704 if(!USE_THRESH || | 1705 if(!USE_THRESH || |
1705 // Threshold on mean L2 of Q and S sequences | 1706 // Threshold on mean L2 of Q and S sequences |
1706 (USE_THRESH && qNorm[j]>SILENCE_THRESH && sNorm[trackIndexOffset+k]>SILENCE_THRESH && | 1707 (USE_THRESH && qnPtr[j]>SILENCE_THRESH && sNorm[trackIndexOffset+k]>SILENCE_THRESH && |
1707 // Are both query and target windows above mean energy? | 1708 // Are both query and target windows above mean energy? |
1708 (qNorm[j]>qMeanL2*.25 && sNorm[trackIndexOffset+k]>sMeanL2[track]*.25))) // && diffL2 < DIFF_THRESH ))) | 1709 (qnPtr[j]>qMeanL2*.25 && sNorm[trackIndexOffset+k]>sMeanL2[track]*.25))) // && diffL2 < DIFF_THRESH ))) |
1709 thisDist=thisDist; // Computed above | 1710 thisDist=thisDist; // Computed above |
1710 else | 1711 else |
1711 thisDist=1000000.0; | 1712 thisDist=1000000.0; |
1712 | 1713 |
1713 // k-NN match algorithm | 1714 // k-NN match algorithm |
1895 double tmp1,tmp2; | 1896 double tmp1,tmp2; |
1896 | 1897 |
1897 // Copy the L2 norm values to core to avoid disk random access later on | 1898 // Copy the L2 norm values to core to avoid disk random access later on |
1898 memcpy(sNorm, l2normTable, dbVectors*sizeof(double)); | 1899 memcpy(sNorm, l2normTable, dbVectors*sizeof(double)); |
1899 double* snPtr = sNorm; | 1900 double* snPtr = sNorm; |
1901 double* qnPtr = qNorm; | |
1900 for(i=0; i<dbH->numFiles; i++){ | 1902 for(i=0; i<dbH->numFiles; i++){ |
1901 if(trackTable[i]>=sequenceLength){ | 1903 if(trackTable[i]>=sequenceLength){ |
1902 tmp1=*snPtr; | 1904 tmp1=*snPtr; |
1903 j=1; | 1905 j=1; |
1904 w=sequenceLength-1; | 1906 w=sequenceLength-1; |
1953 if(verbosity>4) { | 1955 if(verbosity>4) { |
1954 cerr << "silence thresh: " << SILENCE_THRESH; | 1956 cerr << "silence thresh: " << SILENCE_THRESH; |
1955 } | 1957 } |
1956 w=sequenceLength-1; | 1958 w=sequenceLength-1; |
1957 i=1; | 1959 i=1; |
1958 tmp1=*qNorm; | 1960 tmp1=*qnPtr; |
1959 while(w--) | 1961 while(w--) |
1960 *qNorm+=qNorm[i++]; | 1962 *qnPtr+=qnPtr[i++]; |
1961 ps = qNorm+1; | 1963 ps = qnPtr+1; |
1962 w=numVectors-sequenceLength; // +1 -1 | 1964 w=numVectors-sequenceLength; // +1 -1 |
1963 while(w--){ | 1965 while(w--){ |
1964 tmp2=*ps; | 1966 tmp2=*ps; |
1965 *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1); | 1967 *ps=*(ps-1)-tmp1+*(ps+sequenceLength-1); |
1966 tmp1=tmp2; | 1968 tmp1=tmp2; |
1967 ps++; | 1969 ps++; |
1968 } | 1970 } |
1969 ps = qNorm; | 1971 ps = qnPtr; |
1970 qMeanL2 = 0; | 1972 qMeanL2 = 0; |
1971 w=numVectors-sequenceLength+1; | 1973 w=numVectors-sequenceLength+1; |
1972 while(w--){ | 1974 while(w--){ |
1973 *ps=sqrt(*ps); | 1975 *ps=sqrt(*ps); |
1974 qMeanL2+=*ps++; | 1976 qMeanL2+=*ps++; |
2055 else{ | 2057 else{ |
2056 if(verbosity>1) { | 2058 if(verbosity>1) { |
2057 cerr << "query point: " << queryPoint << endl; cerr.flush(); | 2059 cerr << "query point: " << queryPoint << endl; cerr.flush(); |
2058 } | 2060 } |
2059 query=query+queryPoint*dbH->dim; | 2061 query=query+queryPoint*dbH->dim; |
2060 qNorm=qNorm+queryPoint; | 2062 qnPtr=qnPtr+queryPoint; |
2061 numVectors=wL; | 2063 numVectors=wL; |
2062 } | 2064 } |
2063 | 2065 |
2064 double ** D = 0; // Differences query and target | 2066 double ** D = 0; // Differences query and target |
2065 double ** DD = 0; // Matched filter distance | 2067 double ** DD = 0; // Matched filter distance |
2185 } | 2187 } |
2186 | 2188 |
2187 // Search for minimum distance by shingles (concatenated vectors) | 2189 // Search for minimum distance by shingles (concatenated vectors) |
2188 for(j=0;j<=numVectors-wL;j+=HOP_SIZE) | 2190 for(j=0;j<=numVectors-wL;j+=HOP_SIZE) |
2189 for(k=0;k<=trackTable[track]-wL;k+=HOP_SIZE){ | 2191 for(k=0;k<=trackTable[track]-wL;k+=HOP_SIZE){ |
2190 thisDist=2-(2/(qNorm[j]*sNorm[trackIndexOffset+k]))*DD[j][k]; | 2192 thisDist=2-(2/(qnPtr[j]*sNorm[trackIndexOffset+k]))*DD[j][k]; |
2191 if(verbosity>10) { | 2193 if(verbosity>10) { |
2192 cerr << thisDist << " " << qNorm[j] << " " << sNorm[trackIndexOffset+k] << endl; | 2194 cerr << thisDist << " " << qnPtr[j] << " " << sNorm[trackIndexOffset+k] << endl; |
2193 } | 2195 } |
2194 // Gather chi^2 statistics | 2196 // Gather chi^2 statistics |
2195 if(thisDist<minSample) | 2197 if(thisDist<minSample) |
2196 minSample=thisDist; | 2198 minSample=thisDist; |
2197 else if(thisDist>maxSample) | 2199 else if(thisDist>maxSample) |
2200 sampleCount++; | 2202 sampleCount++; |
2201 sampleSum+=thisDist; | 2203 sampleSum+=thisDist; |
2202 logSampleSum+=log(thisDist); | 2204 logSampleSum+=log(thisDist); |
2203 } | 2205 } |
2204 | 2206 |
2205 // diffL2 = fabs(qNorm[j] - sNorm[trackIndexOffset+k]); | 2207 // diffL2 = fabs(qnPtr[j] - sNorm[trackIndexOffset+k]); |
2206 // Power test | 2208 // Power test |
2207 if(!USE_THRESH || | 2209 if(!USE_THRESH || |
2208 // Threshold on mean L2 of Q and S sequences | 2210 // Threshold on mean L2 of Q and S sequences |
2209 (USE_THRESH && qNorm[j]>SILENCE_THRESH && sNorm[trackIndexOffset+k]>SILENCE_THRESH && | 2211 (USE_THRESH && qnPtr[j]>SILENCE_THRESH && sNorm[trackIndexOffset+k]>SILENCE_THRESH && |
2210 // Are both query and target windows above mean energy? | 2212 // Are both query and target windows above mean energy? |
2211 (qNorm[j]>qMeanL2*.25 && sNorm[trackIndexOffset+k]>sMeanL2[track]*.25))) // && diffL2 < DIFF_THRESH ))) | 2213 (qnPtr[j]>qMeanL2*.25 && sNorm[trackIndexOffset+k]>sMeanL2[track]*.25))) // && diffL2 < DIFF_THRESH ))) |
2212 thisDist=thisDist; // Computed above | 2214 thisDist=thisDist; // Computed above |
2213 else | 2215 else |
2214 thisDist=1000000.0; | 2216 thisDist=1000000.0; |
2215 if(thisDist>=0 && thisDist<=radius){ | 2217 if(thisDist>=0 && thisDist<=radius){ |
2216 distances[0]++; // increment count | 2218 distances[0]++; // increment count |