circulant.c

filter: Statementstmt 83.6% 41 / 49 Branchbran 75.0% 12 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.5% 53 / 65 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« com.c ↑ Summary paxson.c »
1 /* This program simulates fractional Gaussian noise or fractional */
2 /* Brownian motion using the Wood and Chan algorithm. */
3 /* The C-packages Ranlib and Meschach are used, both available */
4 /* via Netlib (http://www.netlib.org). */
5  
6 /* References: */
7 /* 1) R.B. Davies and D.S. Harte (1987), */
8 /* Tests for Hurst effect, */
9 /* Biometrika, Vol. 74, pp. 95--102. */
10 /* 2) C.R. Dietrich and G.N. Newsam (1997), */
11 /* Fast and exact simulation of stationary Gaussian processes through */
12 /* circulant embedding of the covariance matrix, */
13 /* SIAM Journal Sci. Comput., Vol. 18, pp. 1088--1107. */
14 /* 3) A. Wood and G. Chan (1994), */
15 /* Simulation of Stationary Gaussian Processes in [0,1]^d, */
16 /* Journal of Comp. and Graphical Statistics, Vol. 3, pp. 409--432. */
17  
18 /* Copyright Ton Dieker */
19 /* Centre of Mathematics and Computer Science (CWI) Amsterdam */
20 /* April 2002 */
21  
22 /* ton@cwi.nl */
23 /* Adapted to work without meschach by Walter Szeliga Mar 05 */
24  
25  
26 #include "circulant.h"
27 #include "fft.h"
28  
29 /* the autocovariance function of fractional Gaussian noise */
30 extern double covariance(long i, double H);
31  
32  
33 1 complex *eigenvalues(double n, double H) {
34 /* computes the eigenvalues of the circulant matrix that embeds the */
35 /* covariance matrix */
36  
37 complex *c;
38 1 long i, size = pow(2,n+1);
39 1 c = malloc(size*sizeof(complex));
40  
41 129 ▶ for (i=0; i<size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<size; i++) 128 1 129
42 {
43 128 c[i].im = 0;
44 128 ▶ if (i<=pow(2,n))
Branch: 1 branch
BranchTrueFalseTotal
if (i<=pow(2,n)) 65 63 128
45 65 c[i].re = covariance(i, H);
46 else
47 63 c[i].re = c[size-i].re;
48 }
49  
50 1 fft(size,c,1,1.0);
51  
52 129 ▶ for (i=0; i<size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<size; i++) { 128 1 129
53 128 ▶ if (c[i].re <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c[i].re <= 0) { 0 128 128
54 0 printf("The program should be modified to deal with this ");
55 0 printf("covariance function.\n");
56 0 printf("See A. Wood and G. Chan (1994),\n");
57 0 printf("Simulation of Stationary Gaussian Processes in [0,1]^d,\n");
58 0 printf("Journal of Computational and Graphical ");
59 0 printf("Statistics, Vol. 3, pp. 409-432\n");
60 0 exit(0);
61 }
62 }
63  
64 1 return c;
65 }
66  
67  
68 1 void computeSandT(long m, complex *ev, complex *SandT) {
69 /* simulates two auxiliary vectors that serve as input */
70 /* in the FFT algorithm */
71 long i;
72  
73 1 SandT[0].re = sqrt(ev[0].re)*snorm()/sqrt(m);
74 1 SandT[0].im = 0;
75 1 SandT[m/2].re = sqrt(ev[m/2].re)*snorm()/sqrt(m);
76 1 SandT[m/2].im = 0;
77
78 64 ▶ for (i=1; i<m/2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i<m/2; i++) { 63 1 64
79 63 SandT[i].re = sqrt(ev[i].re)*snorm()/sqrt(2*m);
80 63 SandT[i].im = sqrt(ev[i].re)*snorm()/sqrt(2*m);
81 63 SandT[m-i].re = SandT[i].re;
82 63 SandT[m-i].im = -SandT[i].im;
83 }
84 1 }
85  
86  
87 1 void circulant(long *n, double *H, double *L, int *cum,
88 long *seed1, long *seed2, double *output) {
89 /* function that generates a fractional Brownian motion or fractional */
90 /* Gaussian noise sample. */
91 /* Input: *n determines the sample size N by N=2^(*n) */
92 /* *H the Hurst parameter of the trace */
93 /* *L the sample is generated on [0,L] */
94 /* *cum = 0: fractional Gaussian noise is produced */
95 /* = 1: fractional Brownian motion is produced */
96 /* *seed1 seed1 for the random generator */
97 /* *seed2 seed2 for the random generator */
98 /* Output: *seed1 new seed1 of the random generator */
99 /* *seed2 new seed2 of the random generator */
100 /* *output the resulting sample is stored in this array */
101 int *ifac;
102 complex *ev;
103 complex *SandT;
104 double *wsave;
105 long i, generator;
106 double scaling;
107  
108 /* set random generator and seeds */
109 1 snorm();
110 1 generator = 1;
111 1 gscgn(1, &generator);
112 1 setall(*seed1,*seed2);
113  
114 /* compute the eigenvalues of the circulant matrix */
115 1 ev = eigenvalues(*n, *H);
116  
117 /* compute the input vectors for the FFT algorithm */
118 1 SandT = malloc(pow(2,*n+1)*sizeof(complex));
119 1 computeSandT(pow(2,*n+1), ev, SandT);
120  
121 /* real part of Fourier transform of S + iT gives sample path */
122 1 fft(pow(2,*n+1),SandT,1,1.0);
123
124 /* rescale to obtain a sample of size 2^(*n) on [0,L] */
125 1 realloc(SandT,pow(2,*n));
126 1 scaling = pow(*L/pow(2,*n),*H);
127 65 ▶ for(i=0; i<pow(2,*n); i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<pow(2,*n); i++) 64 1 65
128 {
129 64 output[i] = scaling*(SandT[i].re);
130 64 ▶ if (*cum && i>0)
Branch: 2 branches
BranchTrueFalseTotal
if (*cum && i>0) 0 64 64
if (*cum && i>0) 0 0 0
131 {
132 0 output[i] += output[i-1];
133 }
134 }
135
136 /* store the new random seeds and free memory */
137 1 getsd(seed1,seed2);
138  
139 1 free(ev);
140 1 free(SandT);
141 1 }
« com.c ↑ Summary paxson.c »