forked from NanoComp/harminv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sines.c
208 lines (184 loc) · 5.57 KB
/
sines.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Copyright (C) 2017 Massachusetts Institute of Technology.
*
* This program 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.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "config.h"
#include "check.h"
#include "harminv-int.h"
#include "copyright.h"
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
int verbose = 0;
#define TWOPI 6.2831853071795864769252867665590057683943388
#define NPERIODS 10
void usage(FILE *f)
{
fprintf(f, "Usage: sines [options] <freq>...\n"
"Note that Im[freq] is the decay rate, or its inverse for -T.\n"
"Options: \n"
" -h : this help message\n"
" -V : print version number and copyright\n"
" -v : verbose output\n"
" -T : specify periods instead of frequencies\n"
" -R : real output\n"
" -r : random amplitudes\n"
" -N <a> : add white noise with amplitude <a>\n"
" -s <s> : use seed <s> for random #s (default <s>=time)\n"
" -n <n> : output <n> points (default %d * max period)\n"
" -t <dt> : time step <dt> (default 1.0)\n",
NPERIODS);
}
typedef struct {
double freq, decay, amplitude, phase;
} sinusoid;
#define MAX2(a,b) ((a) > (b) ? (a) : (b))
int main(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind;
int iarg;
int specify_periods = 0;
int random_amplitudes = 0;
int real_output = 0;
sinusoid *sines = NULL;
int nsines = 0, nalloc = 0, n = 0;
double max_period = 0;
double dt = 1.0;
double noise = 0.0;
int i, is;
srand(time(NULL));
while ((c = getopt(argc, argv, "hVvTRrn:t:N:s:")) != -1)
switch (c) {
case 'h':
usage(stdout);
return EXIT_SUCCESS;
case 'V':
printf("sines " PACKAGE_VERSION " by Steven G. Johnson.\n"
"Test program for harminv.\n"
COPYRIGHT);
return EXIT_SUCCESS;
case 'v':
verbose = 1;
break;
case 'T':
specify_periods = 1;
break;
case 'R':
real_output = 1;
break;
case 'r':
random_amplitudes = 1;
break;
case 'N':
noise = atof(optarg);
break;
case 's':
srand(atoi(optarg));
break;
case 'n':
n = atoi(optarg);
if (n < 1) {
fprintf(stderr, "sines: "
"invalid non-positive -n argument %d\n", n);
return EXIT_FAILURE;
}
break;
case 't':
dt = atof(optarg);
break;
default:
fprintf(stderr, "sines: invalid argument -%c\n", c);
usage(stderr);
return EXIT_FAILURE;
}
if (optind == argc) { /* no parameters left */
fprintf(stderr, "sines: missing required frequency(ies)\n");
usage(stderr);
return EXIT_FAILURE;
}
for (iarg = optind; iarg < argc; ++iarg) {
sinusoid s = { 0, 0, 0, 0 };
if (sscanf(argv[iarg], "%lf+%lfi", &s.freq, &s.decay) < 1) {
fprintf(stderr, "sines: invalid argument \"%s\"\n",
argv[iarg]);
return EXIT_FAILURE;
}
if (specify_periods) {
if (s.freq == 0) {
fprintf(stderr, "sines: invalid argument \"%s\""
": 0 not a valid period\n", argv[iarg]);
return EXIT_FAILURE;
}
s.freq = 1/s.freq;
if (s.decay != 0)
s.decay = 1/s.decay;
}
if (s.decay == 0 || fabs(1/s.freq) > 1/s.decay)
max_period = MAX2(max_period, fabs(1/s.freq));
else
max_period = MAX2(max_period, 1/s.decay);
if (random_amplitudes) {
s.amplitude = rand() * 1.0/RAND_MAX;
s.phase = rand() * TWOPI/RAND_MAX - TWOPI/2;
}
else {
s.amplitude = (iarg - optind + 1);
s.phase = (iarg - optind + 1) * TWOPI / (argc-optind) - TWOPI/2;
}
if (verbose)
printf("# mode: frequency = %g (period %g), decay = %g (lifetime %g), amplitude = %g, phase = %g\n", s.freq, 1/s.freq, s.decay, s.decay != 0 ? 1/s.decay : 0, s.amplitude, s.phase);
if (nsines >= nalloc) {
nalloc = (nalloc + 1) * 2;
sines = (sinusoid*) realloc(sines, sizeof(sinusoid) * nalloc);
CHECK(sines, "out of memory");
}
sines[nsines++] = s;
}
if (n == 0 && max_period == 0) {
fprintf(stderr, "sines: must specify -n or non-zero period\n");
return EXIT_FAILURE;
}
if (n == 0)
n = (int) (max_period / fabs(dt) + 0.5) * NPERIODS;
for (i = 0; i < n; ++i) {
cmplx output = 0;
for (is = 0; is < nsines; ++is)
output += sines[is].amplitude *
cexp(I * (sines[is].phase - TWOPI*sines[is].freq * i*dt)
- sines[is].decay * i*dt);
output += noise * (rand() * 2.0/RAND_MAX - 1);
if (real_output)
printf("%0.17e\n", creal(output));
else
printf("%0.17e%+0.17ei\n", creal(output), cimag(output));
}
free(sines);
return EXIT_SUCCESS;
}
#ifdef F77_DUMMY_MAIN
# ifdef __cplusplus
extern "C"
# endif
int F77_DUMMY_MAIN() { return 1; }
#endif