complex_vector_float_tests.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * complex_vector_float_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2006,2008 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #if defined(HAVE_CONFIG_H)
  26. #include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31. #include <string.h>
  32. #include "spandsp.h"
  33. static void cvec_mulf_dumb(complexf_t z[], const complexf_t x[], const complexf_t y[], int n)
  34. {
  35. int i;
  36. for (i = 0; i < n; i++)
  37. {
  38. z[i].re = x[i].re*y[i].re - x[i].im*y[i].im;
  39. z[i].im = x[i].re*y[i].im + x[i].im*y[i].re;
  40. }
  41. }
  42. /*- End of function --------------------------------------------------------*/
  43. static int test_cvec_mulf(void)
  44. {
  45. int i;
  46. complexf_t x[100];
  47. complexf_t y[100];
  48. complexf_t za[100];
  49. complexf_t zb[100];
  50. complexf_t ratio;
  51. for (i = 0; i < 99; i++)
  52. {
  53. x[i].re = rand();
  54. x[i].im = rand();
  55. y[i].re = rand();
  56. y[i].im = rand();
  57. }
  58. cvec_mulf(za, x, y, 99);
  59. cvec_mulf_dumb(zb, x, y, 99);
  60. for (i = 0; i < 99; i++)
  61. printf("(%f,%f) (%f,%f) (%f,%f)\n", za[i].re, za[i].im, x[i].re, x[i].im, y[i].re, y[i].im);
  62. for (i = 0; i < 99; i++)
  63. {
  64. ratio.re = za[i].re/zb[i].re;
  65. ratio.im = za[i].im/zb[i].im;
  66. if ((ratio.re < 0.9999 || ratio.re > 1.0001)
  67. ||
  68. (ratio.im < 0.9999 || ratio.im > 1.0001))
  69. {
  70. printf("cvec_mulf() - (%f,%f) (%f,%f)\n", za[i].re, za[i].im, zb[i].re, zb[i].im);
  71. printf("Tests failed\n");
  72. exit(2);
  73. }
  74. }
  75. return 0;
  76. }
  77. /*- End of function --------------------------------------------------------*/
  78. static complexf_t cvec_dot_prodf_dumb(const complexf_t x[], const complexf_t y[], int n)
  79. {
  80. int i;
  81. complexf_t z;
  82. complexf_t z1;
  83. z = complex_setf(0.0f, 0.0f);
  84. for (i = 0; i < n; i++)
  85. {
  86. z1 = complex_mulf(&x[i], &y[i]);
  87. z = complex_addf(&z, &z1);
  88. }
  89. return z;
  90. }
  91. /*- End of function --------------------------------------------------------*/
  92. static int test_cvec_dot_prodf(void)
  93. {
  94. int i;
  95. complexf_t x[100];
  96. complexf_t y[100];
  97. complexf_t zsa;
  98. complexf_t zsb;
  99. complexf_t ratio;
  100. for (i = 0; i < 99; i++)
  101. {
  102. x[i].re = rand();
  103. x[i].im = rand();
  104. y[i].re = rand();
  105. y[i].im = rand();
  106. }
  107. for (i = 1; i < 99; i++)
  108. {
  109. zsa = cvec_dot_prodf(x, y, i);
  110. zsb = cvec_dot_prodf_dumb(x, y, i);
  111. ratio.re = zsa.re/zsb.re;
  112. ratio.im = zsa.im/zsb.im;
  113. if ((ratio.re < 0.9999 || ratio.re > 1.0001)
  114. ||
  115. (ratio.im < 0.9999 || ratio.im > 1.0001))
  116. {
  117. printf("cvec_dot_prodf() - (%f,%f) (%f,%f)\n", zsa.re, zsa.im, zsb.re, zsb.im);
  118. printf("Tests failed\n");
  119. exit(2);
  120. }
  121. }
  122. return 0;
  123. }
  124. /*- End of function --------------------------------------------------------*/
  125. int main(int argc, char *argv[])
  126. {
  127. test_cvec_mulf();
  128. test_cvec_dot_prodf();
  129. printf("Tests passed.\n");
  130. return 0;
  131. }
  132. /*- End of function --------------------------------------------------------*/
  133. /*- End of file ------------------------------------------------------------*/