apprcirc.c

filter: Statementstmt 96.8% 31 / 32 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.4% 38 / 42 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« paxson.c ↑ Summary hosking.c »
1 /* This program simulates fractional Gaussian noise or fractional */
2 /* Brownian motion using the approximate circulant algorithm. */
3 /* The C-packages Ranlib and Meschach are used, both available */
4 /* via Netlib (http://www.netlib.org). */
5
6 /* Reference: */
7 /* A.B. Dieker and M. Mandjes (2002), */
8 /* On spectral simulation of fractional Brownian motion, */
9 /* submitted for publication. */
10
11 /* Copyright Ton Dieker */
12 /* Centre of Mathematics and Computer Science (CWI) Amsterdam */
13 /* April 2002 */
14
15 /* ton@cwi.nl */
16 /* Modified to remove the dependence on meschach by Walter Szeliga */
17 /* Mar 2005 */
18
19
20 #include "apprcirc.h"
21 #include "fft.h"
22
23 1 void apprcirc(long *n, double *Hurst, double *L, int *cum, long *seed1,
24 long *seed2, double *output) {
25 /* function that generates a fractional Brownian motion or fractional */
26 /* Gaussian noise sample using the approximate circulant method. */
27 /* Input: *n determines the sample size N by N=2^(*n) */
28 /* *Hurst the Hurst parameter of the trace */
29 /* *L the sample is generated on [0,L] */
30 /* *cum = 0: fractional Gaussian noise is produced */
31 /* = 1: fractional Brownian motion is produced */
32 /* *seed1 seed1 for the random generator */
33 /* *seed2 seed2 for the random generator */
34 /* Output: *seed1 new seed1 of the random generator */
35 /* *seed2 new seed2 of the random generator */
36 /* *output the resulting sample is stored in this array */
37 long i, N, halfN, generator;
38 double scaling, H;
39 double *pow_spec;
40 double aux;
41 complex *a;
42
43 1 halfN = pow(2,*n);
44 1 H = *Hurst;
45 1 N = 2*halfN;
46
47 /* set random generator and seeds */
48 1 snorm();
49 1 generator = 1;
50 1 gscgn(1, &generator);
51 1 setall(*seed1,*seed2);
52
53 /* allocate memory */
54 1 pow_spec = (double*) malloc((halfN+1)*sizeof(double));
55
56 /* approximate spectral density */
57 1 FGN_spectrum(pow_spec,halfN,H);
58
59 1 a = malloc(N*sizeof(complex));
60 1 a[0].re = sqrt(2*(pow(N,2*H)-pow(N-1,2*H)))*snorm();
61 1 a[0].im = 0.;
62 1 a[halfN].re = sqrt(2*pow_spec[halfN])*snorm();
63 1 a[halfN].im = 0.;
64 64 ▶ for(i=1; i<halfN; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=1; i<halfN; i++) { 63 1 64
65 63 aux = sqrt(pow_spec[i]);
66 63 a[i].re = aux*snorm();
67 63 a[i].im = aux*snorm();
68 }
69 64 ▶ for(i=halfN+1; i<N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=halfN+1; i<N; i++) { 63 1 64
70 63 a[i].re = a[N-i].re;
71 63 a[i].im = -a[N-i].im;
72 }
73
74 /* real part of Fourier transform of a_re + i a_im gives sample path */
75 1 fft(N,a,1,1.0);
76
77 /* rescale to obtain a sample of size 2^(*n) on [0,L] */
78 1 scaling = pow(*L/halfN,H)/sqrt(2*N);
79 65 ▶ for(i=0;i<halfN;i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<halfN;i++) { 64 1 65
80 64 output[i] = scaling*(a[i].re);
81 64 ▶ if (*cum && i>0) {
Branch: 2 branches
BranchTrueFalseTotal
if (*cum && i>0) { 0 64 64
if (*cum && i>0) { 0 0 0
82 0 output[i] += output[i-1];
83 }
84 }
85
86 /* store the new random seeds and free memory */
87 1 getsd(seed1,seed2);
88
89 1 free(pow_spec);
90 1 free(a);
91 1 }
« paxson.c ↑ Summary hosking.c »