简体   繁体   中英

Compare Two Byte Arrays & Get The Percentage in Java?

We want to create A system in Java Programming Language to compare two audio files and get the percentage of the Comparison. The files is being converted into fingerprints as byte arrays.

Can anyone help me to give a solution to compare two byte arrays and get the similarity as a percentage?

Using musicg API. You have to use the Wave objects, not their fingerprints but if you can generate fingerprints you can get the Wave object easily.

Wave waveA = ...
Wave waveB = ...
FingerprintSimilarity similarity;
similarity = waveA.getFingerprintSimilarity(waveB);
float result = similarity.getSimilarity();

result is the similarity as a float. Multiply by 100 to get a percentage

/** Returns percentage (0.0-100.0) of not matching bytes. If arrays are not of equal length, nonexisting bytes in the smaller array will be treated as not matching. */
public double compareByteArrays(byte[] a, byte[] b) {
  int n = Math.min(a.length, b.length), nLarge = Math.max(a.length, b.length);
  int unequalCount = nLarge - n;
  for (int i=0; i<n; i++) 
    if (a[i] != b[i]) unequalCount++;
  return unequalCount * 100.0 / nLarge;
}

This would actually just compare the bytes itself (as asked in the title). You could also do some sort of distance between your two vectors (distance in feature space). Or you could do one of a million other things that you can find on google scholar ...

EDIT: You told us that you use the musicg-api , therefore you can compare different Waves like this:

String track1 = "track1.wav", track2 = "track2.wav";
Wave wave1 = new Wave(track1), wave2 = new Wave(track2);

FingerprintSimilarity similarity;

// compare fingerprints:
similarity = wave1.getFingerprintSimilarity(wave2);
System.out.println("clip is found at "
                + similarity.getsetMostSimilarTimePosition() + "s in "
                + song1+" with similarity " + similarity.getSimilarity());

Aha! I found the function to compare two wave files by their fingerprints. The musicg-api function that does the job is = FingerprintSimilarityComputer

Here is my C# code, but you get the JAVA idea too:

public static int MatchFingerPrint(Byte[] SuspectFingerPrint, Byte[] SampleFingerPrint)
        {

            FingerprintSimilarityComputer fpComputer = new FingerprintSimilarityComputer(SuspectFingerPrint, SampleFingerPrint);
            FingerprintSimilarity fpmSimilarity = fpComputer.getFingerprintsSimilarity();
            return (int)(fpmSimilarity.getScore()*100.0f);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM