2
0

complex_vector_int_tests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * complex_vector_int_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2006 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 complexi32_t cvec_dot_prodi16_dumb(const complexi16_t x[], const complexi16_t y[], int n)
  34. {
  35. complexi32_t z;
  36. int i;
  37. z = complex_seti32(0, 0);
  38. for (i = 0; i < n; i++)
  39. {
  40. z.re += ((int32_t) x[i].re*(int32_t) y[i].re - (int32_t) x[i].im*(int32_t) y[i].im);
  41. z.im += ((int32_t) x[i].re*(int32_t) y[i].im + (int32_t) x[i].im*(int32_t) y[i].re);
  42. }
  43. return z;
  44. }
  45. /*- End of function --------------------------------------------------------*/
  46. static int test_cvec_dot_prodi16(void)
  47. {
  48. int i;
  49. complexi32_t za;
  50. complexi32_t zb;
  51. complexi16_t x[99];
  52. complexi16_t y[99];
  53. for (i = 0; i < 99; i++)
  54. {
  55. x[i].re = rand();
  56. x[i].im = rand();
  57. y[i].re = rand();
  58. y[i].im = rand();
  59. }
  60. for (i = 1; i < 99; i++)
  61. {
  62. za = cvec_dot_prodi16(x, y, i);
  63. zb = cvec_dot_prodi16_dumb(x, y, i);
  64. if (za.re != zb.re || za.im != zb.im)
  65. {
  66. printf("Tests failed\n");
  67. exit(2);
  68. }
  69. }
  70. return 0;
  71. }
  72. /*- End of function --------------------------------------------------------*/
  73. static int test_cvec_circular_dot_prodi16(void)
  74. {
  75. int i;
  76. int j;
  77. int pos;
  78. int len;
  79. complexi32_t za;
  80. complexi32_t zb;
  81. complexi16_t x[99];
  82. complexi16_t y[99];
  83. /* Verify that we can do circular sample buffer "dot" linear coefficient buffer
  84. operations properly, by doing two sub-dot products. */
  85. for (i = 0; i < 99; i++)
  86. {
  87. x[i].re = rand();
  88. x[i].im = rand();
  89. y[i].re = rand();
  90. y[i].im = rand();
  91. }
  92. len = 95;
  93. for (pos = 0; pos < len; pos++)
  94. {
  95. za = cvec_circular_dot_prodi16(x, y, len, pos);
  96. zb = complex_seti32(0, 0);
  97. for (i = 0; i < len; i++)
  98. {
  99. j = (pos + i) % len;
  100. zb.re += ((int32_t) x[j].re*(int32_t) y[i].re - (int32_t) x[j].im*(int32_t) y[i].im);
  101. zb.im += ((int32_t) x[j].re*(int32_t) y[i].im + (int32_t) x[j].im*(int32_t) y[i].re);
  102. }
  103. if (za.re != zb.re || za.im != zb.im)
  104. {
  105. printf("Tests failed\n");
  106. exit(2);
  107. }
  108. }
  109. return 0;
  110. }
  111. /*- End of function --------------------------------------------------------*/
  112. int main(int argc, char *argv[])
  113. {
  114. test_cvec_dot_prodi16();
  115. test_cvec_circular_dot_prodi16();
  116. printf("Tests passed.\n");
  117. return 0;
  118. }
  119. /*- End of function --------------------------------------------------------*/
  120. /*- End of file ------------------------------------------------------------*/