我试图计算unity3d中两个麦克风输入信号之间的互相关,然后对该相关值进行FFT。问题是,它正确计算相关性,但它使游戏滞后很多。 难道我做错了什么?我应该不在update()中这样做吗? 我需要获得实时相关值。
public class MicInput : MonoBehaviour {
AudioSource aud1, aud2;
int signalLength = 3200;
public float[] _samples1 = new float[3200];
public float[] _samples2 = new float[3200];
public double[] _newSamples1 = new double[3200];
//public double[] _newSamples2 = new double[512];
public float[] cc2 = new float[9];
public double[] dft = new double[5];
// Use this for initialization
void Start ()
{
aud1 = GetComponent<AudioSource>();
aud1.clip = Microphone.Start(Microphone.devices[3], true, 10, 16000);
aud1.loop = true;
aud1.mute = false;
aud2 = GetComponent<AudioSource>();
aud2.clip = Microphone.Start(Microphone.devices[4], true, 10, 16000);
aud2.loop = true;
aud2.mute = false;
}
// Update is called once per frame
void Update () {
aud1.clip.GetData(_samples1, 0);
aud2.clip.GetData(_samples2, 0);
cc2 = MyCrossCorr(_samples1, _samples2);
}
float[] MyCrossCorr(float[] arr1, float[] arr2)
{
int lx = arr1.Length;
int ly = arr2.Length;
int jmin, jmax, index;
index = 0;
int lconv = lx + ly - 1;
float[] z = new float[lconv];
for (int i = 0; i < lconv; i++)
{
if (i >= ly)
{
jmin = i - ly + 1;
}
else
{
jmin = 0;
}
if (i < lx)
{
jmax = i;
}
else
{
jmax = lx -1;
}
for (int j = jmin; j <= jmax; j++)
{
index = ly - i + j - 1;
z[i] = z[i] + (arr1[j] * arr2[index]);
}
}
//Debug.Log(z);
return z;
}
}