/******************************************************************************* * Function Name : WDG_PeriodValueConfig * Description : Set the prescaler and counter reload value * Input : Amount of time (us) needed * Return : None *******************************************************************************/ void WDG_PeriodValueConfig ( u32 Time ) { unsigned int a; unsigned long n, b; n = Time * (RCCU_FrequencyValue(RCCU_PCLK) / 1000000); FindFactors(n, &a, &b); WDG->PR = a - 1; WDG->VR = b - 1; }
void EDFSplit(vec_ZZ_pEX& v, const ZZ_pEX& f, const ZZ_pEX& b, long d) { ZZ_pEX a, g, h; ZZ_pEXModulus F; vec_ZZ_pE roots; build(F, f); long n = F.n; long r = n/d; random(a, n); TraceMap(g, a, d, F, b); MinPolyMod(h, g, F, r); FindRoots(roots, h); FindFactors(v, f, g, roots); }
Filter *Bilinear(double fp, double fs, double ap, double as, double fc, int *out_n, Complex* (*FindPoles)()) { Complex *poles, *Ak, cplx; Filter *filter; int n, i; double T, T2, Ws, Wp, ws, wp, gain; Complex ac, a1c, bc, b1c, Ac, A1c, T2c; /* Pre distorsion */ wp = 2 * PI * fp/fc; ws = 2 * PI * fs/fc; Wp = 2 * tan(wp/2)*fc; Ws = 2 * tan(ws/2)*fc; T = 1/fc; T2 = T/2; poles = FindPoles(Wp, Ws, ap, as, fc, out_n, &gain); n = *out_n; Ak = FindFactors(poles, fc, n, gain); /* Remind that 1/Ak are normalized such that H(0) = T */ for(i = 0; i < n; i++) { Ak[i].re *= T; Ak[i].im *= T; } filter = (Filter *)malloc(sizeof(Filter)); /* Allocate storage for output filter */ if(n % 2) { /* If n is odd there is a real pole */ filter->num_parallels = (n + 1) / 2; filter->units = (FilterUnit *)malloc(sizeof(FilterUnit) * (n + 1) / 2); filter->units[0].num_degree = 2; filter->units[0].num = (double *)malloc(sizeof(double) * 2); filter->units[0].den_degree = 2; filter->units[0].den = (double *)malloc(sizeof(double) * 2); for(i = 1; i < (n + 1) / 2; i++) /* For each pair of coniugate poles */ { filter->units[i].num_degree = 3; filter->units[i].num = (double *)malloc(sizeof(double) * 3); filter->units[i].den_degree = 3; filter->units[i].den = (double *)malloc(sizeof(double) * 3); } } else /* In n is even there are only pairs of coniugate poles */ { filter->num_parallels = n / 2; filter->units = (FilterUnit *)malloc(sizeof(FilterUnit) * n / 2); for(i = 0; i < n / 2; i++) /* For each pair of coniugate poles */ { filter->units[i].num_degree = 3; filter->units[i].num = (double *)malloc(sizeof(double) * 3); filter->units[i].den_degree = 3; filter->units[i].den = (double *)malloc(sizeof(double) * 3); } } /* Compute filter coefficients */ if(n % 2) { filter->units[0].num[0] = filter->units[0].num[1] = T2/Ak[0].re; filter->units[0].den[0] = 1 - poles[0].re * T2; filter->units[0].den[1] = -1 - poles[0].re * T2; T2c.re = T2; T2c.im = 0; for(i = 1; i < (n + 1)/2; i++) { Ac = DivC(T2c, Ak[2 * i]); A1c = DivC(T2c, Ak[2 * i - 1]); ac.re = 1 - poles[2 * i].re * T2; ac.im = -poles[2 * i].im * T2; a1c.re = 1 - poles[2 * i - 1].re * T2; a1c.im = -poles[2 * i - 1].im * T2; bc.re = 1 + poles[2 * i].re * T2; bc.im = poles[2 * i].im * T2; b1c.re = 1 + poles[2 * i - 1].re * T2; b1c.im = poles[2 * i - 1].im * T2; cplx = AddC(MulC(A1c, ac), MulC(Ac, a1c)); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[0] = cplx.re; cplx = SubC(AddC(MulC(A1c, ac), MulC(Ac, a1c)), AddC(MulC(A1c, bc), MulC(Ac, b1c))); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[1] = cplx.re; cplx = AddC(MulC(A1c, bc), MulC(Ac, b1c)); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[2] = -cplx.re; cplx = MulC(ac, a1c); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[0] = cplx.re; cplx = AddC(MulC(a1c, bc), MulC(b1c, ac)); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[1] = -cplx.re; cplx = MulC(bc, b1c); if(fabs(cplx.im) > 1E-4) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[2] = cplx.re; } } else /* n even */ { T2c.re = T2; T2c.im = 0; for(i = 0; i < n/2; i++) { Ac = DivC(T2c, Ak[2 * i]); A1c = DivC(T2c, Ak[2 * i + 1]); ac.re = 1 - poles[2 * i].re * T2; ac.im = -poles[2 * i].im * T2; a1c.re = 1 - poles[2 * i + 1].re * T2; a1c.im = -poles[2 * i + 1].im * T2; bc.re = 1 + poles[2 * i].re * T2; bc.im = poles[2 * i].im * T2; b1c.re = 1 + poles[2 * i + 1].re * T2; b1c.im = poles[2 * i + 1].im * T2; cplx = AddC(MulC(A1c, ac), MulC(Ac, a1c)); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[0] = cplx.re; cplx = SubC(AddC(MulC(A1c, ac), MulC(Ac, a1c)), AddC(MulC(A1c, bc), MulC(Ac, b1c))); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[1] = cplx.re; cplx = AddC(MulC(A1c, bc), MulC(Ac, b1c)); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].num[2] = -cplx.re; cplx = MulC(ac, a1c); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[0] = cplx.re; cplx = AddC(MulC(a1c, bc), MulC(b1c, ac)); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[1] = -cplx.re; cplx = MulC(bc, b1c); if(fabs(cplx.im) > 1E-5) { printf("\nInternal error in Bilinear conversion"); } filter->units[i].den[2] = cplx.re; } } free((char *)poles); free((char *)Ak); return filter; }
void SFBerlekamp(vec_ZZ_pX& factors, const ZZ_pX& ff, long verbose) { ZZ_pX f = ff; if (!IsOne(LeadCoeff(f))) Error("SFBerlekamp: bad args"); if (deg(f) == 0) { factors.SetLength(0); return; } if (deg(f) == 1) { factors.SetLength(1); factors[0] = f; return; } double t; const ZZ& p = ZZ_p::modulus(); long n = deg(f); ZZ_pXModulus F; build(F, f); ZZ_pX g, h; if (verbose) { cerr << "computing X^p..."; t = GetTime(); } PowerXMod(g, p, F); if (verbose) { cerr << (GetTime()-t) << "\n"; } vec_long D; long r; vec_ZZVec M; if (verbose) { cerr << "building matrix..."; t = GetTime(); } BuildMatrix(M, n, g, F, verbose); if (verbose) { cerr << (GetTime()-t) << "\n"; } if (verbose) { cerr << "diagonalizing..."; t = GetTime(); } NullSpace(r, D, M, verbose); if (verbose) { cerr << (GetTime()-t) << "\n"; } if (verbose) cerr << "number of factors = " << r << "\n"; if (r == 1) { factors.SetLength(1); factors[0] = f; return; } if (verbose) { cerr << "factor extraction..."; t = GetTime(); } vec_ZZ_p roots; RandomBasisElt(g, D, M); MinPolyMod(h, g, F, r); if (deg(h) == r) M.kill(); FindRoots(roots, h); FindFactors(factors, f, g, roots); ZZ_pX g1; vec_ZZ_pX S, S1; long i; while (factors.length() < r) { if (verbose) cerr << "+"; RandomBasisElt(g, D, M); S.kill(); for (i = 0; i < factors.length(); i++) { const ZZ_pX& f = factors[i]; if (deg(f) == 1) { append(S, f); continue; } build(F, f); rem(g1, g, F); if (deg(g1) <= 0) { append(S, f); continue; } MinPolyMod(h, g1, F, min(deg(f), r-factors.length()+1)); FindRoots(roots, h); S1.kill(); FindFactors(S1, f, g1, roots); append(S, S1); } swap(factors, S); } if (verbose) { cerr << (GetTime()-t) << "\n"; } if (verbose) { cerr << "degrees:"; long i; for (i = 0; i < factors.length(); i++) cerr << " " << deg(factors[i]); cerr << "\n"; } }
Filter *Invariant(double fp, double fs, double ap, double as, double fc, int *out_n, Complex* (*FindPoles)()) { Complex *poles, *Ak, cplx1, cplx2; Filter *filter; int n, i; double T, Ws, Wp, gain; Complex Tc; Ws = 2 * PI * fs; Wp = 2 * PI * fp; T = 1/fc; Tc.re = T; Tc.im = 0; poles = FindPoles(Wp, Ws, ap, as, fc, out_n, &gain); n = *out_n; Ak = FindFactors(poles, fc, n, gain); filter = (Filter *)malloc(sizeof(Filter)); /* Allocate storage for output filter */ if(n % 2) { /* If n is odd there is a real pole */ filter->num_parallels = (n + 1) / 2; filter->units = (FilterUnit *)malloc(sizeof(FilterUnit) * (n + 1) / 2); filter->units[0].num_degree = 1; filter->units[0].num = (double *)malloc(sizeof(double)); filter->units[0].den_degree = 2; filter->units[0].den = (double *)malloc(sizeof(double) * 2); for(i = 1; i < (n + 1) / 2; i++) /* For each pair of coniugate poles */ { filter->units[i].num_degree = 2; filter->units[i].num = (double *)malloc(sizeof(double) * 2); filter->units[i].den_degree = 3; filter->units[i].den = (double *)malloc(sizeof(double) * 3); } } else /* In n is even there are only pairs of coniugate poles */ { filter->num_parallels = n / 2; filter->units = (FilterUnit *)malloc(sizeof(FilterUnit) * n / 2); for(i = 0; i < n / 2; i++) /* For each pair of coniugate poles */ { filter->units[i].num_degree = 2; filter->units[i].num = (double *)malloc(sizeof(double) * 2); filter->units[i].den_degree = 3; filter->units[i].den = (double *)malloc(sizeof(double) * 3); } } /* Compute filter coefficients */ if(n % 2) { filter->units[0].num[0] = 1/Ak[0].re; filter->units[0].den[0] = 1; filter->units[0].den[1] = -exp(poles[0].re * T); for(i = 1; i < (n + 1)/2; i++) { cplx1 = MulC(Ak[2 * i], Ak[2 * i - 1]); cplx2 = AddC(Ak[2 * i], Ak[2 * i - 1]); if(fabs(cplx1.im) > 1E-4 || fabs(cplx2.im) > 1E-4) /* Must be real */ { printf("\nInternal error in Invariant conversion"); } filter->units[i].num[0] = cplx2.re/cplx1.re; cplx2 = AddC(MulC(Ak[2 * i], ExpC(MulC(Tc, poles[2 * i]))), MulC(Ak[2 * i - 1], ExpC(MulC(Tc, poles[2 * i - 1])))); if(fabs(cplx2.im) > 1E-4) /* Must be real */ { printf("\nInternal error in Invariant conversion"); } filter->units[i].num[1] = -cplx2.re/cplx1.re; filter->units[i].den[0] = 1; filter->units[i].den[1] = - 2 * exp(poles[2 * i].re * T) * cos(poles[2 * i].im * T); filter->units[i].den[2] = exp(2 * poles[2 * i].re * T); } } else /* n even */ for(i = 0; i < n/2; i++) { cplx1 = MulC(Ak[2 * i], Ak[2 * i + 1]); cplx2 = AddC(Ak[2 * i], Ak[2 * i + 1]); if(fabs(cplx1.im) > 1E-4 || fabs(cplx2.im) > 1E-4) /* Must be real */ { printf("\nInternal error in Invariant conversion"); } filter->units[i].num[0] = cplx2.re/cplx1.re; cplx2 = AddC(MulC(Ak[2 * i], ExpC(MulC(Tc, poles[2 * i]))), MulC(Ak[2 * i + 1], ExpC(MulC(Tc, poles[2 * i + 1])))); if(fabs(cplx2.im) > 1E-4) /* Must be real */ { printf("\nInternal error in Invariant conversion"); } filter->units[i].num[1] = -cplx2.re/cplx1.re; filter->units[i].den[0] = 1; filter->units[i].den[1] = - 2 * exp(poles[2 * i].re * T) * cos(poles[2 * i].im * T); filter->units[i].den[2] = exp(2 * poles[2 * i].re * T); } free((char *)poles); free((char *)Ak); return filter; }