use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method makeComplex.
// ----------------------------------------------------------------
protected static Complex[] makeComplex(Point2D[] points) {
int N = points.length;
Complex[] samples = new Complex[N];
for (int i = 0; i < N; i++) {
samples[i] = new Complex(points[i].getX(), points[i].getY());
}
return samples;
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method getEllipsePoint.
/**
* Get the reconstructed point for two DFT coefficients G1, G2 at a given
* position t.
* @param G1 first coefficient
* @param G2 second coefficient
* @param m frequency number
* @param t contour position
* @return reconstructed point
*/
public Complex getEllipsePoint(Complex G1, Complex G2, int m, double t) {
Complex p1 = getReconstructionPoint(G1, -m, t);
Complex p2 = getReconstructionPoint(G2, m, t);
return p1.add(p2);
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptorUniform method DFT.
/**
* As above, but the length P of the resulting spectrum (signal, if inverse)
* is explicitly specified.
* @param g signal vector
* @param P length of the resulting DFT spectrum
* @return DFT spectrum
*/
private Complex[] DFT(Complex[] g, int P) {
int M = g.length;
// double[] cosTable = makeCosTable(M); // cosTable[m] == cos(2*pi*m/M)
// double[] sinTable = makeSinTable(M);
Complex[] G = new Complex[P];
// common scale factor (fwd/inverse differ!)
double s = 1.0 / M;
for (int m = P / 2 - P + 1; m <= P / 2; m++) {
double Am = 0;
double Bm = 0;
for (int k = 0; k < M; k++) {
double x = g[k].re;
double y = g[k].im;
// int mk = (m * k) % M; double phi = 2 * Math.PI * mk / M;
double phi = 2 * Math.PI * m * k / M;
double cosPhi = Math.cos(phi);
double sinPhi = Math.sin(phi);
Am = Am + x * cosPhi + y * sinPhi;
Bm = Bm - x * sinPhi + y * cosPhi;
}
G[Arithmetic.mod(m, P)] = new Complex(s * Am, s * Bm);
}
return G;
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class Dft1d method DFT.
public Complex[] DFT(Complex[] g, boolean forward) {
int M = g.length;
// common scale factor
double s = 1 / Math.sqrt(M);
for (int u = 0; u < M; u++) {
double sumRe = 0;
double sumIm = 0;
for (int m = 0; m < M; m++) {
double gRe = g[m].re;
double gIm = g[m].im;
int k = (u * m) % M;
double cosPhi = cosTable[k];
double sinPhi = sinTable[k];
if (forward)
sinPhi = -sinPhi;
// complex multiplication: (gRe + i gIm) * (cosPhi + i sinPhi)
sumRe += gRe * cosPhi - gIm * sinPhi;
sumIm += gRe * sinPhi + gIm * cosPhi;
}
// G[u].re = s * sumRe;
// G[u].im = s * sumIm;
G[u] = new Complex(s * sumRe, s * sumIm);
}
return G;
}