comparison spandsp-0.0.6pre17/tests/dds_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 * dds_tests.c
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 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: dds_tests.c,v 1.26 2009/06/02 14:55:36 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page dds_tests_page Direct digital synthesis tests
31 \section dds_tests_page_sec_1 What does it do?
32 ???.
33
34 \section dds_tests_page_sec_2 How does it work?
35 ???.
36 */
37
38 #if defined(HAVE_CONFIG_H)
39 #include "config.h"
40 #endif
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <memory.h>
46 #include <sndfile.h>
47
48 #include "spandsp.h"
49 #include "spandsp-sim.h"
50
51 #define OUTPUT_FILE_NAME "dds.wav"
52 #define OUTPUT_FILE_NAME_COMPLEX "complex_dds.wav"
53
54 #define SAMPLES_PER_CHUNK 8000
55
56 int main(int argc, char *argv[])
57 {
58 int i;
59 uint32_t phase;
60 int32_t phase_inc;
61 int outframes;
62 complexf_t camp;
63 int16_t buf[2*SAMPLES_PER_CHUNK];
64 SNDFILE *outhandle;
65 power_meter_t meter;
66 power_meter_t meter_i;
67 power_meter_t meter_q;
68 int scale;
69
70 power_meter_init(&meter, 10);
71
72 printf("Non-complex DDS tests.\n");
73 if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL)
74 {
75 fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_FILE_NAME);
76 exit(2);
77 }
78
79 phase = 0;
80
81 printf("Test with 123.456789Hz.\n");
82 phase_inc = dds_phase_rate(123.456789f);
83 scale = dds_scaling_dbm0(-10.0f);
84 for (i = 0; i < SAMPLES_PER_CHUNK; i++)
85 {
86 buf[i] = alaw_to_linear(linear_to_alaw((dds(&phase, phase_inc)*scale) >> 15));
87 power_meter_update(&meter, buf[i]);
88 }
89 outframes = sf_writef_short(outhandle, buf, SAMPLES_PER_CHUNK);
90 if (outframes != SAMPLES_PER_CHUNK)
91 {
92 fprintf(stderr, " Error writing audio file\n");
93 exit(2);
94 }
95 printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
96 if (fabs(power_meter_current_dbm0(&meter) + 10.0f) > 0.05f)
97 {
98 printf("Test failed.\n");
99 exit(2);
100 }
101
102 printf("Test with 12.3456789Hz.\n");
103 phase_inc = dds_phase_rate(12.3456789f);
104 for (i = 0; i < SAMPLES_PER_CHUNK; i++)
105 {
106 buf[i] = alaw_to_linear(linear_to_alaw(dds(&phase, phase_inc)));
107 power_meter_update(&meter, buf[i]);
108 }
109 outframes = sf_writef_short(outhandle, buf, SAMPLES_PER_CHUNK);
110 if (outframes != SAMPLES_PER_CHUNK)
111 {
112 fprintf(stderr, " Error writing audio file\n");
113 exit(2);
114 }
115 printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
116 /* Use a wider tolerance for this very low frequency - the power meter will ripple */
117 if (fabs(power_meter_current_dbov(&meter) + 3.02f) > 0.2f)
118 {
119 printf("Test failed.\n");
120 exit(2);
121 }
122
123 printf("Test with 2345.6789Hz.\n");
124 phase_inc = dds_phase_rate(2345.6789f);
125 scale = dds_scaling_dbov(-10.0f);
126 for (i = 0; i < SAMPLES_PER_CHUNK; i++)
127 {
128 buf[i] = alaw_to_linear(linear_to_alaw((dds(&phase, phase_inc)*scale) >> 15));
129 power_meter_update(&meter, buf[i]);
130 }
131 outframes = sf_writef_short(outhandle, buf, SAMPLES_PER_CHUNK);
132 if (outframes != SAMPLES_PER_CHUNK)
133 {
134 fprintf(stderr, " Error writing audio file\n");
135 exit(2);
136 }
137 printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
138 if (fabs(power_meter_current_dbov(&meter) + 10.0f) > 0.05f)
139 {
140 printf("Test failed.\n");
141 exit(2);
142 }
143
144 printf("Test with 3456.789Hz.\n");
145 phase_inc = dds_phase_rate(3456.789f);
146 for (i = 0; i < SAMPLES_PER_CHUNK; i++)
147 {
148 buf[i] = alaw_to_linear(linear_to_alaw(dds(&phase, phase_inc)));
149 power_meter_update(&meter, buf[i]);
150 }
151 outframes = sf_writef_short(outhandle, buf, SAMPLES_PER_CHUNK);
152 if (outframes != SAMPLES_PER_CHUNK)
153 {
154 fprintf(stderr, " Error writing audio file\n");
155 exit(2);
156 }
157 printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
158 if (fabs(power_meter_current_dbov(&meter) + 3.02f) > 0.05f)
159 {
160 printf("Test failed.\n");
161 exit(2);
162 }
163
164 if (sf_close(outhandle) != 0)
165 {
166 fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME);
167 exit(2);
168 }
169
170 printf("Complex DDS tests,\n");
171 if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME_COMPLEX, 2)) == NULL)
172 {
173 fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_FILE_NAME_COMPLEX);
174 exit(2);
175 }
176
177 power_meter_init(&meter_i, 7);
178 power_meter_init(&meter_q, 7);
179
180 phase = 0;
181 phase_inc = dds_phase_ratef(123.456789f);
182 for (i = 0; i < SAMPLES_PER_CHUNK; i++)
183 {
184 camp = dds_complexf(&phase, phase_inc);
185 buf[2*i] = camp.re*10000.0f;
186 buf[2*i + 1] = camp.im*10000.0f;
187 power_meter_update(&meter_i, buf[2*i]);
188 power_meter_update(&meter_q, buf[2*i]);
189 }
190 outframes = sf_writef_short(outhandle, buf, SAMPLES_PER_CHUNK);
191 if (outframes != SAMPLES_PER_CHUNK)
192 {
193 fprintf(stderr, " Error writing audio file\n");
194 exit(2);
195 }
196 printf("Level is %fdBOv/%fdBm0, %fdBOv/%fdBm0\n",
197 power_meter_current_dbov(&meter_i),
198 power_meter_current_dbm0(&meter_i),
199 power_meter_current_dbov(&meter_q),
200 power_meter_current_dbm0(&meter_q));
201 if (fabs(power_meter_current_dbov(&meter_i) + 13.42f) > 0.05f
202 ||
203 fabs(power_meter_current_dbov(&meter_q) + 13.42f) > 0.05f)
204 {
205 printf("Test failed.\n");
206 exit(2);
207 }
208
209 if (sf_close(outhandle) != 0)
210 {
211 fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME_COMPLEX);
212 exit(2);
213 }
214
215 printf("Tests passed.\n");
216 return 0;
217 }
218 /*- End of function --------------------------------------------------------*/
219 /*- End of file ------------------------------------------------------------*/

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