/assn2/src/tests/test.c

https://github.com/gnidan/cs650 · C · 274 lines · 63 code · 73 blank · 138 comment · 12 complexity · 3e17cb56906ceaf498a3dec6f5b78e82 MD5 · raw file

  1. #include <complex.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "out.c"
  6. #define M_PI 3.14159265358979323846
  7. /**
  8. * @brief Compare the observed column of the DFT matrix with the actual column.
  9. *
  10. * @param col The observed column.
  11. * @param col_num Which column in the matrix we're looking at.
  12. * @param size The size of the DFT matrix.
  13. *
  14. * @return 1 if the columns match, 0 otherwise
  15. */
  16. unsigned int compare_col (complex double *col, size_t col_num, size_t size)
  17. {
  18. static const double TOL = 0.001;
  19. double omega_r, omega_i, o_r, o_i, a, b;
  20. unsigned int i;
  21. /* 0th column should be all 1.0 + 0.0 * I */
  22. if ( col_num == 0 )
  23. for ( i = 0; i < size; ++i )
  24. if ( (fabs (creal(col[i]) - 1.0) > TOL) ||
  25. (fabs (cimag(col[i]) - 0.0) > TOL) )
  26. return 0;
  27. /* 0th row should always be 1.0 + 0.0 * I */
  28. if ( (fabs (creal(col[0]) - 1.0) > TOL ) ||
  29. (fabs (cimag(col[0]) - 0.0) > TOL ) )
  30. return 0;
  31. /* calculate the primitive nth root of unity */
  32. omega_r = cos (2 * M_PI / (float) size);
  33. omega_i = sin (2 * M_PI / (float) size);
  34. /* set o_r and o_i to represents omeaga^col_num */
  35. o_r = 1.0;
  36. o_i = 0.0;
  37. for ( i = 0; i < col_num; ++i )
  38. {
  39. a = o_r;
  40. b = o_i;
  41. o_r = a * omega_r - b * omega_i;
  42. o_i = a * omega_i + b * omega_r;
  43. }
  44. /* now, o_r + o_i * i is omega^col_num */
  45. omega_r = o_r;
  46. omega_i = o_i;
  47. for ( i = 1; i < size; ++i )
  48. {
  49. if ( (fabs (creal(col[i]) - o_r) > TOL) ||
  50. (fabs (cimag(col[i]) - o_i) > TOL) )
  51. return 0;
  52. a = o_r;
  53. b = o_i;
  54. o_r = a * omega_r - b * omega_i;
  55. o_i = a * omega_i + b * omega_r;
  56. }
  57. return 1;
  58. }
  59. /**
  60. * @brief Verify that the DFT of the given size is correct.
  61. *
  62. * @param s The size of the DFT being computed.
  63. * @param fptr A pointer to the function which computes the DFT of the given
  64. * size.
  65. *
  66. * @return 1 if the function is correct, 0 otherwise
  67. */
  68. unsigned int test_func(size_t s,
  69. void (*fptr)(complex double *, complex double *)) {
  70. complex double *x = malloc(sizeof(complex double) * s);
  71. complex double *y = malloc(sizeof(complex double) * s);
  72. for (size_t col = 0; col < s; col++) {
  73. for (size_t i = 0; i < s; i++) {
  74. x[i] = col==i;
  75. y[i] = 0;
  76. }
  77. fptr(y, x);
  78. if ( !compare_col (y, col, s) )
  79. return 0;
  80. /* printf("col = %zu\n", col); */
  81. /* for (size_t i = 0; i < s; i++) { */
  82. /* printf("%f + %f i\n", creal(y[i]), cimag(y[i])); */
  83. /* } */
  84. /* printf("\n"); */
  85. }
  86. /* printf("\n\n"); */
  87. return 1;
  88. }
  89. int main(int argc, char *argv[]) {
  90. /* printf("F(2)\n"); */
  91. /* test_func(2, func_2); */
  92. /* printf("F(3)\n"); */
  93. /* test_func(3, func_3); */
  94. /* printf("F(4)\n"); */
  95. /* test_func(4, func_4); */
  96. /* printf("F(5)\n"); */
  97. /* test_func(5, func_5); */
  98. /* printf("F(6)\n"); */
  99. /* test_func(6, func_6); */
  100. /* printf("F(7)\n"); */
  101. /* test_func(7, func_7); */
  102. /* printf("F(8)\n"); */
  103. /* test_func(8, func_8); */
  104. /* printf("F(8)\n"); */
  105. /* test_func(8, func_8); */
  106. /* printf("F(64) -- "); */
  107. /* if ( !test_func(64, func) ) */
  108. /* printf ("PASSSED\n"); */
  109. printf ("F(64) -- %s\n", test_func (64, func) ? "PASSED" : "FAILED");
  110. /* printf("F(9)\n"); */
  111. /* test_func(9, func_9); */
  112. /* printf("F(10)\n"); */
  113. /* test_func(10, func_10); */
  114. /* printf("F(11)\n"); */
  115. /* test_func(11, func_11); */
  116. /* printf("F(12)\n"); */
  117. /* test_func(12, func_12); */
  118. /* printf("F(13)\n"); */
  119. /* test_func(13, func_13); */
  120. /* printf("F(14)\n"); */
  121. /* test_func(14, func_14); */
  122. /* printf("F(15)\n"); */
  123. /* test_func(15, func_15); */
  124. /* printf("F(16)\n"); */
  125. /* test_func(16, func_16); */
  126. /* printf("F(17)\n"); */
  127. /* test_func(17, func_17); */
  128. /* printf("F(18)\n"); */
  129. /* test_func(18, func_18); */
  130. /* printf("F(18)\n"); */
  131. /* test_func(18, func_18); */
  132. /* printf("F(19)\n"); */
  133. /* test_func(19, func_19); */
  134. /* printf("F(20)\n"); */
  135. /* test_func(20, func_20); */
  136. /* printf("F(21)\n"); */
  137. /* test_func(21, func_21); */
  138. /* printf("F(22)\n"); */
  139. /* test_func(22, func_22); */
  140. /* printf("F(23)\n"); */
  141. /* test_func(23, func_23); */
  142. /* printf("F(24)\n"); */
  143. /* test_func(24, func_24); */
  144. /* printf("F(25)\n"); */
  145. /* test_func(25, func_25); */
  146. /* printf("F(26)\n"); */
  147. /* test_func(26, func_26); */
  148. /* printf("F(27)\n"); */
  149. /* test_func(27, func_27); */
  150. /* printf("F(28)\n"); */
  151. /* test_func(28, func_28); */
  152. /* printf("F(28)\n"); */
  153. /* test_func(28, func_28); */
  154. /* printf("F(29)\n"); */
  155. /* test_func(29, func_29); */
  156. /* printf("F(30)\n"); */
  157. /* test_func(30, func_30); */
  158. /* printf("F(31)\n"); */
  159. /* test_func(31, func_31); */
  160. /* printf("F(32)\n"); */
  161. /* test_func(32, func_32); */
  162. /* printf("F(33)\n"); */
  163. /* test_func(33, func_33); */
  164. /* printf("F(34)\n"); */
  165. /* test_func(34, func_34); */
  166. /* printf("F(35)\n"); */
  167. /* test_func(35, func_35); */
  168. /* printf("F(36)\n"); */
  169. /* test_func(36, func_36); */
  170. /* printf("F(37)\n"); */
  171. /* test_func(37, func_37); */
  172. /* printf("F(38)\n"); */
  173. /* test_func(38, func_38); */
  174. /* printf("F(38)\n"); */
  175. /* test_func(38, func_38); */
  176. /* printf("F(39)\n"); */
  177. /* test_func(39, func_39); */
  178. /* printf("F(30)\n"); */
  179. /* test_func(30, func_30); */
  180. /* printf("F(31)\n"); */
  181. /* test_func(31, func_31); */
  182. /* printf("F(32)\n"); */
  183. /* test_func(32, func_32); */
  184. /* printf("F(33)\n"); */
  185. /* test_func(33, func_33); */
  186. /* printf("F(34)\n"); */
  187. /* test_func(34, func_34); */
  188. /* printf("F(35)\n"); */
  189. /* test_func(35, func_35); */
  190. /* printf("F(36)\n"); */
  191. /* test_func(36, func_36); */
  192. /* printf("F(37)\n"); */
  193. /* test_func(37, func_37); */
  194. /* printf("F(38)\n"); */
  195. /* test_func(38, func_38); */
  196. /* printf("F(38)\n"); */
  197. /* test_func(38, func_38); */
  198. /* printf("F(39)\n"); */
  199. /* test_func(39, func_39); */
  200. return 0;
  201. }