1 #include "../hrtf/mysofa.h"
2 #include "../hrtf/tools.h"
3 #include "tests.h"
4 #include <float.h>
5 #include <math.h>
6 #include <stdio.h>
7 
test_interpolate()8 void test_interpolate() {
9   struct MYSOFA_HRTF *hrtf = NULL;
10   int err = 0;
11   int i;
12   float *fir;
13   float delays[2];
14   float *res;
15   int neighborhood[6] = {-1, -1, -1, -1, -1, -1};
16   float c[3];
17 
18   hrtf = mysofa_load("tests/MIT_KEMAR_normal_pinna.old.sofa", &err);
19 
20   if (!hrtf) {
21     CU_FAIL_FATAL("Error reading file.");
22     return;
23   }
24 
25   mysofa_tocartesian(hrtf);
26 
27   fir = malloc(hrtf->N * hrtf->R * sizeof(float));
28 
29   res = mysofa_interpolate(hrtf, hrtf->SourcePosition.values, 0, neighborhood,
30                            fir, delays);
31   CU_ASSERT(res == hrtf->DataIR.values);
32   CU_ASSERT(delays[0] == 0);
33   CU_ASSERT(delays[1] == 0);
34 
35   c[0] = (hrtf->SourcePosition.values[0] + hrtf->SourcePosition.values[3]) / 2;
36   c[1] = (hrtf->SourcePosition.values[1] + hrtf->SourcePosition.values[4]) / 2;
37   c[2] = (hrtf->SourcePosition.values[2] + hrtf->SourcePosition.values[5]) / 2;
38   neighborhood[0] = 1;
39 
40   res = mysofa_interpolate(hrtf, c, 0, neighborhood, fir, delays);
41   CU_ASSERT(res == fir);
42   CU_ASSERT(delays[0] == 0);
43   CU_ASSERT(delays[1] == 0);
44 
45   for (i = 0; i < hrtf->N * hrtf->R; i++) {
46 #ifdef VDEBUG
47     printf("%f %f %f\n", res[i], hrtf->DataIR.values[i],
48            hrtf->DataIR.values[i + hrtf->N * hrtf->R]);
49 #endif
50     CU_ASSERT(fequals(res[i], (hrtf->DataIR.values[i] +
51                                hrtf->DataIR.values[i + hrtf->N * hrtf->R]) /
52                                   2));
53   }
54 
55   /* TODO add some tests... */
56 
57   mysofa_free(hrtf);
58   free(fir);
59 }
60