function myRRgen(tmax,Pe,Pn,seed) % tmax : maximum time % Pe : Probability of ectopy ~ 1 per hr % Pn : Probability of noise ~ 16 per hr % seed : a 32-bit random variable that can be used to initialize the generator t = 0; % sum of intervals since the beginning of the simulation, in seconds ts = 0; % t, in sample intervals */ tsp = 0; % previous value of ts */ % Initialise and runn RR interval generator, pass in length and % probability of artefacts and nosie initialize(seed, tmax, Pe, Pn); % generate RR interval */ while ((t += generate()) < tmax) { % add RR interval to running time */ % calculate and output a quantized RR interval */ ts = ()(SPS*t + 0.5); printf("%5.3f\n", (ts - tsp)/(()SPS)); tsp = ts; end end % main function new_rr = modify_ectopic( rr2, rr1) % shorten beat by 80% +/- 10% randomly chosen */ new_rr; delta; rrint; delta = (()rand()/RAND_MAX); delta = 0.2*(delta-0.5); % length of last rr interval */ rrint = rr2 - rr1; % shorten it by 0.7 +/- a bit */ rrint = (0.7 - delta)*rrint; % work out new timing */ new_rr = rr1 + rrint; end %---------------------------------------------------------------------------*/ % generate an extra beat (artefact) */ %---------------------------------------------------------------------------*/ make_noise( rr2, rr1) { % shorten beat by 80% +/- 10% randomly chosen */ new_rr; delta; rrint; delta = (()rand()/RAND_MAX); delta = ((delta*(SPS-(2.0/SPS))) + (1.0/SPS))/SPS; % length of last rr interval */ rrint = rr2 - rr1; % shorten it by 0.5 +/- a bit */ rrint = delta*rrint; % work out new timing */ new_rr = rr1 + rrint; return(new_rr); } %--------------------------------------------------------------------------*/ % CHECK IF ECTOPICS SHOULD BE ADDED */ %--------------------------------------------------------------------------*/ int check_for_ectopic( Pe) { % Pe=0.0003;*/ % probability of ectopic (~1 per hour)*/ % no state or hr dependency */ int rtn=NO; rand_no; % check to see if we really want ectopic beats */ if(Pe==0) return(NO); % if we do ... */ % pick a random number between 0 and 1*/ rand_no = (()rand()/RAND_MAX); if(rand_no=0;j--) { k=(*idum)/IQ; *idum=IA*(*idum-k*IQ)-IR*k; if (*idum < 0) *idum += IM; if (j < NTAB) iv[j] = *idum; } iy=iv[0]; } k=(*idum)/IQ; *idum=IA*(*idum-k*IQ)-IR*k; if (*idum < 0) *idum += IM; j=iy/NDIV; iy=iv[j]; iv[j] = *idum; if ((temp=AM*iy) > RNMX) return RNMX; else return temp; } %--------------------------------------------------------------------------*/ % GAUSSIAN DEVIATES */ %--------------------------------------------------------------------------*/ gasdev( *idum) { ran1( *idum); static int iset=0; static gset; fac,rsq,v1,v2; if (iset == 0) { do { v1=2.0*ran1(idum)-1.0; v2=2.0*ran1(idum)-1.0; rsq=v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; return v2*fac; } else { iset=0; return gset; } } %--------------------------------------------------------------------------*/ % FFT */ %--------------------------------------------------------------------------*/ void four1( data[], unsigned nn, int isign) { unsigned n,mmax,m,j,istep,i; double wtemp,wr,wpr,wpi,wi,theta; tempr,tempi; n=nn << 1; j=1; for (i=1;i i) { SWAP(data[j],data[i]); SWAP(data[j+1],data[i+1]); } m=n >> 1; while (m >= 2 && j > m) { j -= m; m >>= 1; } j += m; } mmax=2; while (n > mmax) { istep=mmax << 1; theta=isign*(6.28318530717959/mmax); wtemp=sin(0.5*theta); wpr = -2.0*wtemp*wtemp; wpi=sin(theta); wr=1.0; wi=0.0; for (m=1;m dRRamp) { u = ()rand()/RAND_MAX; dRR = 0.5*dRRamp*(1.0 + ()exp(gasdev(&rseed))/10.0)*u/fabs(u); } rrmean = RR0[time] + dRR; rrtrend = RRtrendamp*(2*()rand()/RAND_MAX - 1); rrstd = RRstdmin + (RRstdmax - RRstdmin)*()rand()/RAND_MAX; lfhfratio = LFHFmin + (LFHFmax-LFHFmin)*()rand()/RAND_MAX; % generate RR over this block */ trblock(tr0, flo, fhi, flostd, fhistd, lfhfratio, rrmean, rrtrend, rrstd, Nb); for(j=1;j<=Nb;j++) trpeaks[i+j] = trpeaks[i] + tr0[j]; i += Nb; % convert beat time stamp back to nearest second */ time = ()(trpeaks[i]); rra = tr0[Nb] - tr0[Nb-1]; % printf("time = %ld\n",time); */ % for(j=2;j<=Nb;j++) printf("%d %e\n",j,tr0[j]- tr0[j-1]); exit(1); */ % generate record */ while(time < N) { fhi = 0.2 + 0.1*()rand()/RAND_MAX; % choose a new block time in beats */ Nb = blocklength(); % choose a transition duration in beats */ Nt = translength(); % choose rr generation states: rrmean, rrstd, lfhfratio */ dRR = dRRamp*()rand()/RAND_MAX; rrmean = RR0[time] + dRR; rrtrend = RRtrendamp*(2.0*()rand()/RAND_MAX - 1.0); rrstd = RRstdmin + (RRstdmax - RRstdmin)*()rand()/RAND_MAX; lfhfratio = LFHFmin + (LFHFmax - LFHFmin)*()rand()/RAND_MAX; % generate RR over this block */ trblock(tr0, flo, fhi, flostd, fhistd, lfhfratio, rrmean, rrtrend, rrstd, Nb); rrb = tr0[2] - tr0[1]; % generate RR during transition between blcoks with rr-intervals rra, rrb */ rrtrend = 0.0; trblock(trb, flo, fhi, flostd, fhistd, lfhfratio, rrmean, rrtrend, rrstd, Nt); trtrans(tr1, Nt, rra, rrb, trb); % fill in transition and new block */ for(j=1;j<=Nt;j++) trpeaks[i+j] = trpeaks[i] + tr1[j]; i += Nt; for(j=1;j<=Nb;j++) trpeaks[i+j] = trpeaks[i] + tr0[j]; i += Nb; % remember last rr-interval */ rra = rrb; time = ()(trpeaks[i]); } Nbeats = i; % check for adding ectopics or artefacts */ % loop over entire data set */ j=1; trpeaks_new[1] = trpeaks[1]; for(i=2;i<=Nbeats;i++){ % find out what heart rate is at a particular rpeak time */ hr=60.0/RR0[()(trpeaks[i])]; % check so see if we (randomly) want to make this ectopic */ test = check_for_ectopic(Pe); % no state dependency */ if (test!=NO){ trpeaks_new[j] = modify_ectopic(trpeaks[i],trpeaks[i-1]); } if (test == NO){ % just keep the current beat */ test = check_for_noise(state[i], hr, Pn); % hr and state dependent */ if (test!=NO){ % we have noise, add an extra beat */ trpeaks_new[j] = make_noise(trpeaks[i],trpeaks[i-1]); j++; } % regardless, keep the current beat */ trpeaks_new[j] = trpeaks[i]; } j++; } free_vector(RR0,1,2*N); free_vector(trpeaks,1,2*N); free_vector(tr0,1,2*N); free_vector(tr1,1,2*N); free_vector(trb,1,2*N); } % This function is called once per RR interval. It should return the length of the next (simulated) RR interval in seconds. */ generate(void) { rr; count++; rr= trpeaks_new[count+1]-trpeaks_new[count]; % trap rounding errors */ if(rr<(2.0/SPS)) rr = 2.0/SPS; return(rr); } int main(int argc, char **argv) { t = 0.0; % sum of intervals since the beginning of the simulation, in seconds */ ts = 0; % t, in sample intervals */ tsp = 0; % previous value of ts */ tmax = 24*60*60; % 24 hours, in seconds */ seed; % a 32-bit random variable that can be used to initialize the generator */ Pe = 0.0003; % Probability of ectopy ~ 1 per hr */ Pn = 0.0048; % Probability of noise ~ 16 per hr */ atol(); if (argc < 2) { fprintf(stderr, "usage: %s seed [tmax] P(ectopy) P(noise)\n", argv[0]); exit(1); } seed = atol(argv[1]); if (argc > 2) tmax = atol(argv[2]); else tmax=115; if(tmax<115) tmax=115; if(argc>3) Pe = atof(argv[3]); if(argc>4) Pn = atof(argv[4]); % Initialise and runn RR interval generator, pass in length and probability of artefacts and nosie */ initialize(seed, tmax, Pe, Pn); % generate RR interval */ while ((t += generate()) < tmax) { % add RR interval to running time */ % calculate and output a quantized RR interval */ ts = ()(SPS*t + 0.5); printf("%5.3f\n", (ts - tsp)/(()SPS)); tsp = ts; } exit(0); }