Skip to content

Commit

Permalink
add noise option -N
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Mar 28, 2002
1 parent 4e75c4a commit 8e8171e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sines.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void usage(FILE *f)
" -v : verbose output\n"
" -T : specify periods instead of frequencies\n"
" -r : random amplitudes\n"
" -N <a> : add white noise with amplitude <a>\n"
" -n <n> : output <n> points (default %d * max period)\n"
" -t <dt> : time step <dt> (default 1.0)\n",
NPERIODS);
Expand All @@ -70,11 +71,12 @@ int main(int argc, char **argv)
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, "hVvTrn:t:")) != -1)
while ((c = getopt(argc, argv, "hVvTrn:t:N:")) != -1)
switch (c) {
case 'h':
usage(stdout);
Expand All @@ -93,6 +95,9 @@ int main(int argc, char **argv)
case 'r':
random_amplitudes = 1;
break;
case 'N':
noise = atof(optarg);
break;
case 'n':
n = atoi(optarg);
if (n < 1) {
Expand Down Expand Up @@ -175,6 +180,7 @@ int main(int argc, char **argv)
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);
printf("%0.17e+%0.17ei\n", creal(output), cimag(output));
}

Expand Down

0 comments on commit 8e8171e

Please sign in to comment.