简体   繁体   English

比较两个字节的数组并获取Java中的百分比?

[英]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. 我们想用Java编程语言创建一个系统来比较两个音频文件并获得比较的百分比。 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. 使用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对象,而不是它们的指纹,但是如果可以生成指纹,则可以轻松获得Wave对象。

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

result is the similarity as a float. result是作为浮点数的相似性。 Multiply by 100 to get a percentage 乘以100得到一个百分比

/** 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 ... 或者您可以做一百万种其他事情之一,您可以在Google学术搜索中找到...

EDIT: You told us that you use the musicg-api , therefore you can compare different Waves like this: 编辑:您告诉我们您使用musicg-api ,因此您可以像这样比较不同的Waves:

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 做这项工作的musicg-api函数是= FingerprintSimilarityComputer

Here is my C# code, but you get the JAVA idea too: 这是我的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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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