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