hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Analogy for Linux, test program for waveform generation
 *
 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
 * Copyright (C) 2008 Alexis Berlemont <alexis.berlemont@free.fr>
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai 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 Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <math.h>
 
#include "wf_facilities.h"
 
#ifndef PI
#define PI 3.14159265358979323846
#endif
 
void a4l_wf_init_sine(struct waveform_config *config, double *values)
{
   int i;
 
   double ratio = config->wf_frequency / config->spl_frequency;
 
   for (i = 0; i < config->spl_count; i++) {
 
       values[i] = config->wf_offset -
           config->wf_amplitude / 2 +
           0.5 * config->wf_amplitude * cos(i * 2 * PI * ratio);
   }
}
 
void a4l_wf_init_sawtooth(struct waveform_config *config, double *values)
{
   int i;
 
   double ratio = config->wf_frequency / config->spl_frequency;
 
   for (i = 0; i < config->spl_count; i++) {
 
       int period_idx = (int)floor(i * ratio);
 
       values[i] = config->wf_offset -
           config->wf_amplitude / 2 -
           period_idx * config->wf_amplitude +
           i * ratio * config->wf_amplitude;
   }
}
 
void a4l_wf_init_triangular(struct waveform_config *config, double *values)
{
   int i;
 
   double ratio = config->wf_frequency / config->spl_frequency;
 
   for (i = 0; i < config->spl_count; i++) {
 
       int period_idx = (int)floor(i * ratio);
       int half_period_idx = (int)floor(i * 2 * ratio);
       int rise = ((half_period_idx % 2) == 0) ? 1 : 0;
 
       if (rise) {
           values[i] = config->wf_offset -
               config->wf_amplitude / 2 -
               2 * period_idx * config->wf_amplitude +
               2 * i * ratio * config->wf_amplitude;
       } else {
           values[i] = config->wf_offset -
               config->wf_amplitude / 2 +
               2 * (period_idx + 1) * config->wf_amplitude -
               2 * i * ratio * config->wf_amplitude;
       }
   }
}
 
void a4l_wf_init_steps(struct waveform_config *config, double *values)
{
   int i;
 
   double ratio = config->wf_frequency / config->spl_frequency;
 
   for (i = 0; i < config->spl_count; i++) {
       int half_period_idx = (int)floor(i * 2 * ratio);
       int even = (half_period_idx % 2 == 0);
 
       values[i] = config->wf_offset -
           config->wf_amplitude / 2 + even * config->wf_amplitude;
   }
}
 
void a4l_wf_set_sample_count(struct waveform_config *config)
{
   int sample_count = MIN_SAMPLE_COUNT;
   int best_count = MIN_SAMPLE_COUNT;
   double lowest_diff = INFINITY;
 
   while (sample_count < MAX_SAMPLE_COUNT) {
 
       double ratio = (double)sample_count *
           (config->wf_frequency / config->spl_frequency);
       int ceiling = ceil(ratio);
       double diff = (double)ceiling - ratio;
 
       assert(diff >= 0);
 
       if (diff < lowest_diff) {
           lowest_diff = diff;
           best_count = sample_count;
       }
 
       if (diff == 0)
           break;
 
       sample_count++;
   }
 
   if (lowest_diff != 0) {
       fprintf(stderr,
           "Warning: unable to create a contiguous signal\n");
       fprintf(stderr, "Warning: an approximation is performed\n");
   }
 
   config->spl_count = best_count;
}
 
int a4l_wf_check_config(struct waveform_config *config)
{
 
   if (config->wf_amplitude == 0)
       fprintf(stderr, "Warning: the signal will be constant\n");
 
   if (config->wf_frequency * 2 > config->spl_frequency) {
       fprintf(stderr,
           "Error: the sampling frequency is not correct\n");
       fprintf(stderr,
           "Error: sampling frequency >= 2 * signal frequency\n");
       return -EINVAL;
   }
 
   /* TODO: check with find_range */
 
   return 0;
}
 
static void (* init_values[])(struct waveform_config *, double *) = {
   a4l_wf_init_sine,
   a4l_wf_init_sawtooth,
   a4l_wf_init_triangular,
   a4l_wf_init_steps,
};
 
void a4l_wf_init_values(struct waveform_config *config, double *values)
{
   init_values[config->wf_kind](config, values);
}
 
void a4l_wf_dump_values(struct waveform_config *config, double *values)
{
   int i;
 
   for (i = 0; i < config->spl_count; i++)
       fprintf(stderr, "%f\n", values[i]);
}