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

时间:2022-02-27 23:07:56

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.

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

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

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

3 个解决方案

#1


2  

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.

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

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

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

结果是浮动的相似性。乘以100得到一个百分比

#2


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

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:

编辑:您告诉我们您使用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());

#3


0  

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

#1


2  

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.

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

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

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

结果是浮动的相似性。乘以100得到一个百分比

#2


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

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:

编辑:您告诉我们您使用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());

#3


0  

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