比较两个字节数组&获得Java的百分比?

时间:2012-08-24 08:46:55

标签: java arrays compare bytearray

我们想用Java编程语言创建一个系统来比较两个音频文件并获得比较的百分比。这些文件被转换为指纹作为字节数组。

任何人都可以帮我提供比较两个字节数组的解决方案并获得相似度百分比吗?

3 个答案:

答案 0 :(得分:2)

使用musicg API。您必须使用Wave对象,而不是指纹,但如果您可以生成指纹,则可以轻松获取Wave对象。

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

result是浮动的相似性。乘以100得到百分比

答案 1 :(得分:2)

/** 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;
}

这实际上只是比较字节本身(如标题中所述)。您还可以在两个向量之间进行某种距离(特征空间中的距离)。或者你可以在google scholar上找到其他一百万件事中的一件......

编辑:您告诉我们您使用 musicg-api ,因此您可以比较不同的Wave:

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());

答案 2 :(得分:0)

啊哈!我找到了通过指纹比较两个波形文件的功能。执行该工作的musicg-api函数是= FingerprintSimilarityComputer

这是我的C#代码,但你也得到了JAVA的想法:

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

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