简体   繁体   English

找到c中值之间的欧氏距离

[英]find euclidean distance between values in c

i need your help. 我需要你的帮助。 I want to find euclidean distance between each value of the two mfc files generated in sphinx3. 我想找到sphinx3中生成的两个mfc文件的每个值之间的欧几里德距离。 My problem is that i have to pass two wav files such as a.wav and b.wav in which b.wav is a subpart of a.wav. 我的问题是我必须传递两个wav文件,例如a.wav和b.wav,其中b.wav是a.wav的子部分。 I have generated .mfc in text format and it is like this 我用文本格式生成了.mfc,就像这样

11.143 1.3739 -0.18189 -0.46588 0.081962 -0.053194 -0.039629 -0.19989 -0.28369 -0.41381 -0.38511 -0.25862 -0.017158
11.013 1.2289 -0.28845 -0.51773 -0.049813 -0.0025347 -0.10207 -0.23056 -0.33524 -0.27521 -0.37585 -0.36463 -0.06067
10.999 1.2356 -0.1489 -0.39382 -0.029506 0.054779 0.12997 -0.11465 -0.27684 -0.40508 -0.4854 -0.31248 -0.19142
10.977 1.235 -0.18344 -0.61799 -0.12633 -0.049641 0.039414 -0.16939 -0.1884 -0.34448 -0.27235 -0.20559 -0.23409
10.987 1.2966 -0.12835 -0.4269 -0.14144 -0.14519 -0.06445 -0.19406 -0.19799 -0.29742 -0.41959 -0.17827 -0.029767
11.035 1.4875 -0.23199 -0.51662 -0.045337 -0.024595 -0.079227 -0.20181 -0.25853 -0.43788 -0.47611 -0.33845 -0.082532

Now i want to compute euclidean distance between each value of a.mfc and b.mfc. 现在我想计算a.mfc和b.mfc的每个值之间的欧几里德距离。 This is possible in Matlab but it is taking too much time. 这在Matlab中是可能的,但它花费了太多时间。 I want this to be done in C language. 我希望这可以用C语言完成。 Any help will be greatly appreciated. 任何帮助将不胜感激。

Thank you. 谢谢。

Your question is not particularly well formulated. 你的问题没有特别好。 AFAIK mfc files contain vectors with 13 elements. AFAIK mfc文件包含具有13个元素的向量。 Some untested code would be: 一些未经测试的代码将是:

#include <stdio.h>
#include <math.h>

#define VECSIZE 13

void readvec(char* s, float* v) {
  // read one vector from s into v
  char* h;
  h = strtok(s, " ");
  int i = 0;
  while (h != NULL && i < VECSIZE) {
    v[i++] = atof(h);
    h = strtok(NULL, " ");
  }
  while (i < VECSIZE)
    v[i++] = .0;
}

bool compareFiles() {
  FILE* a = fopen("a.wav", "r");
  FILE* b = fopen("b.wav", "r");
  FILE* res = fopen("res.wav", "w");  // results file

  if (a == NULL || b == NULL || res == NULL)
    return false;

  char* sa[1024];
  char* sb[1024];
  float va[VECSIZE];
  float vb[VECSIZE];
  while (!feof(a) && !feof(b)) {
    fgets(sa, 1024, a);
    readvec(sa, va);
    fgets(sb, 1024, b);
    readvec(sb, vb);
    float diff =.0;
    for (int i = 0; i < VECSIZE; i++)
      diff += pow(va[i] - vb[i], 2);
    diff = sqrt(diff);
    fprintf(res, "%f\n", diff);
  }
  fclose(a);
  fclose(b);
  fclose(res);
  return true;
}

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

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