fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int seed ){ int i; fixedpt dt, stepnum, p; fixedpt gaussR1[OMP_ACCEL], gaussR2[OMP_ACCEL]; // stepnum = fixedpt_rconst(n); stepnum = n << FIXEDPT_FBITS; // ??? janders dt = fixedpt_div(t1, stepnum); fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt); fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt )); int s = seed; p = s0; int tid; for ( i = 1; i <= n; i++ ) { tid = omp_get_thread_num(); if (i & 1) { // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers) s = get_two_normal_fixed(s, gaussR1, gaussR2); } p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA, fixedpt_mul(constB, i & 1 ? gaussR1[tid] : gaussR2[tid])))); // if (tid == 0) { // printf("i = %d, seed = %d, gaussR1 = %d, gaussR1 = %d, p = %d\n", i, s, gaussR1[tid], gaussR2[tid], p); // } // fixedpt_print(p); } return p; }
fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed ){ int i; fixedpt dt, stepnum, p; fixedpt gaussR1 = 0, gaussR2 = 0; // stepnum = fixedpt_rconst(n); stepnum = n << FIXEDPT_FBITS; // ??? janders dt = fixedpt_div(t1, stepnum); fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt); fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt )); p = s0; for ( i = 1; i <= n; i++ ) { if (i & 1) // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers) get_two_normal_fixed_LUT(seed, &gaussR1, &gaussR2); p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA, fixedpt_mul(constB, i & 1 ? gaussR1 : gaussR2)))); // fixedpt_print(p); } return p; }
void overdrive_run(void *obj, sound_t *out, sound_t *in) { overdrive_t *overdrive = (overdrive_t *)obj; // Zona de ganancia lineal if( ((*in) <= fixedpt_rconst(1/3)) && ((*in) > fixedpt_rconst(-1/3)) ) { // Calculo la salida *out = fixedpt_mul(*in, 2); } // Zona de ganancia cuadratica en primer cuadrante else if( ((*in) <= fixedpt_rconst(2/3)) && ((*in) > fixedpt_rconst(1/3)) ) { // Calculo la salida *out = fixedpt_rconst(+2) - fixedpt_mul(*in, +3); *out = fixedpt_rconst(+3) - fixedpt_mul(*out, *out); *out = fixedpt_div(*out, 3); } // Zona de ganancia cuadratica en primer cuadrante else if( ((*in) <= fixedpt_rconst(-1/3)) && ((*in) > fixedpt_rconst(-2/3)) ) { // Calculo la salida *out = fixedpt_rconst(-2) + fixedpt_mul(*in, -3); *out = fixedpt_rconst(-3) + fixedpt_mul(*out, *out); *out = fixedpt_div(*out, 3); } // Zona sin ganancia else { // Calculo la salida *out = *in; } }
int get_two_normal_fixed(int seed, fixedpt *n1, fixedpt *n2) { fixedpt r1, r2; fixedpt twoPI = 411775; // ??? janders -- hard-code 2PI in fixed point to avoid conversion from double // from 2 uniform random numbers r1 and r2, we will generate two Gaussian random numbers deposited into n1 and n2 int s = seed; r1 = get_uniform_fixed (&s); r2 = get_uniform_fixed (&s); int tid = omp_get_thread_num(); n1 += tid; n2 += tid; /* fixedpt ln1 = fixedpt_ln( r1 ); fixedpt mul1 = fixedpt_mul(-1*FIXEDPT_TWO , ln1); fixedpt mul2 = fixedpt_mul( twoPI , r2); fixedpt cos1 = fixedpt_cos(mul2); fixedpt sqrt1 = fixedpt_sqrt ( mul1); fixedpt gaussR1 = fixedpt_mul(sqrt1, cos1); n1 += tid; *n1 = gaussR1; fixedpt sin1 = fixedpt_sin(mul2); fixedpt gaussR2 = fixedpt_mul(sqrt1, sin1); n2 += tid; *n2 = gaussR2;*/ *n1 = fixedpt_mul(fixedpt_sqrt ( fixedpt_mul(-1*FIXEDPT_TWO , fixedpt_ln( r1 )) ), fixedpt_cos(fixedpt_mul( twoPI , r2))); *n2 = fixedpt_mul(fixedpt_sqrt ( fixedpt_mul(-1*FIXEDPT_TWO , fixedpt_ln( r1)) ), fixedpt_sin (fixedpt_mul( twoPI , r2))); // if (tid==0){ // printf("seed = %d, ln1 = %d, mul1 = %d, mul2 = %d, cos1 = %d, sin1 = %d, sqrt1 = %d, gaussR1 = %d, gaussR2 = %d\n", s, ln1, mul1, mul2, cos1, sin1, sqrt1, gaussR1, gaussR2); //} return s; }
tOutputType ActivateNeuron(tNeuronType type, tOutputType accum) { fixedpt fpAccum,fpMaxfwd,tmp; tmp=fixedpt_fromint(0); if(type==INNERN) { fpMaxfwd=fixedpt_fromint(MAX_NEURON_OUT); } else { fpMaxfwd=fixedpt_fromint(MAXFWD); } if((accum<FIXED_POS_LIMIT)&&(accum>FIXED_NEG_LIMIT)) { fpAccum=fixedpt_fromint(accum); } else { if(accum>FIXED_POS_LIMIT) { if(type==INNERN) { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT-1)); } else { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT-MAXFWD)); } } else { if(type==INNERN) { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT+1)); } else { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT+MAXFWD)); } } } switch (type) { case INNERN: tmp=fixedpt_abs(fpAccum); tmp=fixedpt_add(tmp,FIXEDPT_ONE); tmp=fixedpt_div(fpAccum,tmp); tmp=fixedpt_mul(tmp,fpMaxfwd); break; case OUTERN: tmp=fixedpt_abs(fpAccum); tmp=fixedpt_add(tmp,fpMaxfwd); tmp=fixedpt_div(fpAccum,tmp); tmp=fixedpt_mul(tmp,fpMaxfwd); break; } return fixedpt_toint(tmp); }
int main(void) { fixedpt x, y; int n; /* addition */ x = 5; y = 9; printf("int x + y = %d + %d = %d\n", x, y, x + y); printf("fixedpt x + y = %d + %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x + y = %d + %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x + y = %d + %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y)))); /* subtraction */ printf("\nint x - y = %d - %d = %d\n", x, y, x - y); printf("fixedpt x - y = %d - %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x - y = %d - %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x - y = %d - %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y)))); /* multiplication */ printf("\nint x * y = %d * %d = %d\n", x, y, x * y); printf("fixedpt x * y = %d * %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x * y = %d * %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x * y = %d * %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y)))); /* division */ printf("\nint x / y = %d / %d = %d\n", x, y, x / y); printf("fixedpt x / y = %d / %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x / y = %d / %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x / y = %d / %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y)))); return 0; }
void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2) { fixedpt r1, r2; fixedpt twoPI = 411775; // ??? janders -- hard-code 2PI in fixed point to avoid conversion from double // from 2 uniform random numbers r1 and r2, we will generate two Gaussian random numbers deposited into n1 and n2 r1 = get_uniform_fixed (seed); r2 = get_uniform_fixed (seed); *n1 = fixedpt_mul(fixedpt_sqrt ( fixedpt_mul(-1*FIXEDPT_TWO , fixedpt_ln( r1 )) ), fixedpt_cos(fixedpt_mul( twoPI , r2))); *n2 = fixedpt_mul(fixedpt_sqrt ( fixedpt_mul(-1*FIXEDPT_TWO , fixedpt_ln( r1)) ), fixedpt_sin (fixedpt_mul( twoPI , r2))); }
void echo_run(void *obj, sound_t *out, sound_t *in) { echo_t *echo = (echo_t *)obj; sound_t fbk; // Saco la muestra mas vieja de la cadena de delay delay_pull(echo->delay, &fbk); // Calculo el valor que ingresa a la cadena de delay fbk = *in + fixedpt_mul(fbk, feedback); // Guardo la seƱal realimentada delay_push(echo->delay, &fbk); // Calculo la salida *out = fixedpt_mul(*in, fixedpt_rconst(1) - mix) + fixedpt_mul(fbk, mix); }
void wah_run(void *obj, sound_t *out, sound_t *in) { wah_t *wah = (wah_t *)obj; static fixedpt fc; fixedpt q1; fixedpt f1; FILE * fd; fd = fopen("Dump.csv","a+"); // Calculo la constante de damping del filtro q1 = fixedpt_mul(damp, FIXEDPT_TWO); // Calculo el siguiente valor de t para la triangular fc += fixedpt_div(wahf, fss); fc %= FIXEDPT_TWO; // Calcular el valor de la onda triangular f1 = fixedpt_tri(fc); f1 = f1 + FIXEDPT_ONE; f1 = fixedpt_div(f1, FIXEDPT_TWO); f1 = fixedpt_mul(f1, maxf - minf) + minf; // Calcular el valor de la senoidal que modula f1 = fixedpt_mul(f1, FIXEDPT_PI); f1 = fixedpt_div(f1, fss); f1 = fixedpt_sin(f1); f1 = fixedpt_mul(f1, FIXEDPT_TWO); // Calcular el wah wah->yh = *in - wah->yl - fixedpt_mul(wah->yb, q1); wah->yb = wah->yb + fixedpt_mul(wah->yh, f1); wah->yl = wah->yl + fixedpt_mul(wah->yb, f1); wah->yh = fixedpt_div(wah->yh, fixedpt_rconst(3)); wah->yb = fixedpt_div(wah->yb, fixedpt_rconst(2)); wah->yl = fixedpt_div(wah->yl, fixedpt_rconst(2)); fprintf(fd, "%d\t%d\t%d\n", wah->yl, wah->yb, wah->yh); *out = wah->yb; fclose(fd); }
int Reg_ActivateNeuron(unsigned char type, int accum) { //char frc[12]; fixedpt fpAccum,fpMaxfwd,tmp; tmp=fixedpt_fromint(0); if(type==INNER) { fpMaxfwd=fixedpt_fromint(MAX_NEURON_OUT); } else { fpMaxfwd=fixedpt_fromint(MAXFWD); } if((accum<FIXED_POS_LIMIT)&&(accum>FIXED_NEG_LIMIT)) { //printf("accum=%d\r\n",accum); fpAccum=fixedpt_fromint(accum); } else { if(accum>FIXED_POS_LIMIT) { if(type==INNER) { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT-1)); } else { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT-MAXFWD)); } } else { if(type==INNER) { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT+1)); } else { fpAccum=fixedpt_fromint((FIXED_POS_LIMIT+MAXFWD)); } } } switch (type) { case INNER: tmp=fixedpt_abs(fpAccum); tmp=fixedpt_add(tmp,FIXEDPT_ONE); tmp=fixedpt_div(fpAccum,tmp); tmp=fixedpt_mul(tmp,fpMaxfwd); break; case OUTER: tmp=fixedpt_abs(fpAccum); tmp=fixedpt_add(tmp,fpMaxfwd); //fixedpt_str(tmp,frc); //printf("tmp=%s\r\n",frc); tmp=fixedpt_div(fpAccum,tmp); tmp=fixedpt_mul(tmp,fpMaxfwd); break; } //printf("lol"); return fixedpt_toint(tmp); }
int main() { fixedpt A, B, C; printf("fixedptc library version: %s\n", FIXEDPT_VCSID); printf("Using %d-bit precision, %d.%d format\n\n", FIXEDPT_BITS, FIXEDPT_WBITS, FIXEDPT_FBITS); printf("The most precise number: "); fixedpt_print(1); printf("The biggest number: "); fixedpt_print(0x7fffff00); printf("Here are some example numbers:\n"); printf("Random number: "); fixedpt_print(fixedpt_rconst(143.125)); printf("PI: "); fixedpt_print(FIXEDPT_PI); printf("e: "); fixedpt_print(FIXEDPT_E); puts(""); A = fixedpt_rconst(2.5); B = fixedpt_fromint(3); fixedpt_print(A); puts("+"); fixedpt_print(B); C = fixedpt_add(A, B); puts("="); fixedpt_print(C); puts(""); fixedpt_print(A); puts("*"); fixedpt_print(B); puts("="); C = fixedpt_mul(A, B); fixedpt_print(C); puts(""); A = fixedpt_rconst(1); B = fixedpt_rconst(4); C = fixedpt_div(A, B); fixedpt_print(A); puts("/"); fixedpt_print(B); puts("="); fixedpt_print(C); printf("exp(1)="); fixedpt_print(fixedpt_exp(FIXEDPT_ONE)); puts(""); puts("sqrt(pi)="); fixedpt_print(fixedpt_sqrt(FIXEDPT_PI)); puts(""); puts("sqrt(25)="); fixedpt_print(fixedpt_sqrt(fixedpt_rconst(25))); puts(""); puts("sin(pi/2)="); fixedpt_print(fixedpt_sin(FIXEDPT_HALF_PI)); puts(""); puts("sin(3.5*pi)="); fixedpt_print(fixedpt_sin(fixedpt_mul(fixedpt_rconst(3.5), FIXEDPT_PI))); puts(""); puts("4^3.5="); fixedpt_print(fixedpt_pow(fixedpt_rconst(4), fixedpt_rconst(3.5))); puts(""); puts("4^0.5="); fixedpt_print(fixedpt_pow(fixedpt_rconst(4), fixedpt_rconst(0.5))); return (0); }