comparison spandsp-0.0.6pre17/tests/complex_vector_float_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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 * $Id: complex_vector_float_tests.c,v 1.3 2009/04/26 07:00:39 steveu Exp $
26 */
27
28 #if defined(HAVE_CONFIG_H)
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <string.h>
36
37 #include "spandsp.h"
38
39 static void cvec_mulf_dumb(complexf_t z[], const complexf_t x[], const complexf_t y[], int n)
40 {
41 int i;
42
43 for (i = 0; i < n; i++)
44 {
45 z[i].re = x[i].re*y[i].re - x[i].im*y[i].im;
46 z[i].im = x[i].re*y[i].im + x[i].im*y[i].re;
47 }
48 }
49 /*- End of function --------------------------------------------------------*/
50
51 static int test_cvec_mulf(void)
52 {
53 int i;
54 complexf_t x[100];
55 complexf_t y[100];
56 complexf_t za[100];
57 complexf_t zb[100];
58 complexf_t ratio;
59
60 for (i = 0; i < 99; i++)
61 {
62 x[i].re = rand();
63 x[i].im = rand();
64 y[i].re = rand();
65 y[i].im = rand();
66 }
67 cvec_mulf(za, x, y, 99);
68 cvec_mulf_dumb(zb, x, y, 99);
69 for (i = 0; i < 99; i++)
70 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);
71 for (i = 0; i < 99; i++)
72 {
73 ratio.re = za[i].re/zb[i].re;
74 ratio.im = za[i].im/zb[i].im;
75 if ((ratio.re < 0.9999 || ratio.re > 1.0001)
76 ||
77 (ratio.im < 0.9999 || ratio.im > 1.0001))
78 {
79 printf("cvec_mulf() - (%f,%f) (%f,%f)\n", za[i].re, za[i].im, zb[i].re, zb[i].im);
80 printf("Tests failed\n");
81 exit(2);
82 }
83 }
84 return 0;
85 }
86 /*- End of function --------------------------------------------------------*/
87
88 static complexf_t cvec_dot_prodf_dumb(const complexf_t x[], const complexf_t y[], int n)
89 {
90 int i;
91 complexf_t z;
92 complexf_t z1;
93
94 z = complex_setf(0.0f, 0.0f);
95 for (i = 0; i < n; i++)
96 {
97 z1 = complex_mulf(&x[i], &y[i]);
98 z = complex_addf(&z, &z1);
99 }
100 return z;
101 }
102 /*- End of function --------------------------------------------------------*/
103
104 static int test_cvec_dot_prodf(void)
105 {
106 int i;
107 complexf_t x[100];
108 complexf_t y[100];
109 complexf_t zsa;
110 complexf_t zsb;
111 complexf_t ratio;
112
113 for (i = 0; i < 99; i++)
114 {
115 x[i].re = rand();
116 x[i].im = rand();
117 y[i].re = rand();
118 y[i].im = rand();
119 }
120 for (i = 1; i < 99; i++)
121 {
122 zsa = cvec_dot_prodf(x, y, i);
123 zsb = cvec_dot_prodf_dumb(x, y, i);
124 ratio.re = zsa.re/zsb.re;
125 ratio.im = zsa.im/zsb.im;
126 if ((ratio.re < 0.9999 || ratio.re > 1.0001)
127 ||
128 (ratio.im < 0.9999 || ratio.im > 1.0001))
129 {
130 printf("cvec_dot_prodf() - (%f,%f) (%f,%f)\n", zsa.re, zsa.im, zsb.re, zsb.im);
131 printf("Tests failed\n");
132 exit(2);
133 }
134 }
135 return 0;
136 }
137 /*- End of function --------------------------------------------------------*/
138
139 int main(int argc, char *argv[])
140 {
141 test_cvec_mulf();
142 test_cvec_dot_prodf();
143
144 printf("Tests passed.\n");
145 return 0;
146 }
147 /*- End of function --------------------------------------------------------*/
148 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.