Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very simple way to compare Chromaprint fingerprints
import chromaprint
popcnt_table_8bit = [
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
]
def popcnt(x):
"""
Count the number of set bits in the given 32-bit integer.
"""
return (popcnt_table_8bit[(x >> 0) & 0xFF] +
popcnt_table_8bit[(x >> 8) & 0xFF] +
popcnt_table_8bit[(x >> 16) & 0xFF] +
popcnt_table_8bit[(x >> 24) & 0xFF])
fps = []
for line in open('fp.log').readlines():
if line.startswith('FINGERPRINT='):
encoded = line.split('=', 1)[1].strip()
fps.append(chromaprint.decode_fingerprint(encoded)[0])
for i, fp1 in enumerate(fps):
for j, fp2 in enumerate(fps):
if i >= j:
continue
error = 0
for x, y in zip(fp1, fp2):
error += popcnt(x ^ y)
print i, j, 1.0 - error / 32.0 / min(len(fp1), len(fp1))
@dvbelkin
Copy link

dvbelkin commented Jun 1, 2017

I thing it's wrong
print i, j, 1.0 - error / 32.0 / min(len(fp1), len(fp1))
may be
print i, j, 1.0 - error / 32.0 / min(len(fp1), len(fp2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment