/** * Computes the 1-D cross-correlation of specified sequences x and y. * @param lx the length of x. * @param kx the sample index of x[0]. * @param x array[lx] of x values. * @param ly the length of y. * @param ky the sample index of y[0]. * @param y array[ly] of y values. * @param lz the length of z. * @param kz the sample index of z[0]. * @param z array[lz] of z values. */ public static void xcor( int lx, int kx, float[] x, int ly, int ky, float[] y, int lz, int kz, float[] z) { boolean copy = x==y; x = reverse(lx,x,copy); kx = 1-kx-lx; conv(lx,kx,x,ly,ky,y,lz,kz,z); if (!copy) reverse(lx,x,false); }
/** * Computes the 1-D convolution of specified sequences x and y. * @param lx the length of x. * @param kx the sample index of x[0]. * @param x array[lx] of x values. * @param ly the length of y. * @param ky the sample index of y[0]. * @param y array[ly] of y values. * @param lz the length of z. * @param kz the sample index of z[0]. * @param z array[lz] of z values. */ public static void conv( int lx, int kx, float[] x, int ly, int ky, float[] y, int lz, int kz, float[] z) { convFast(lx,kx,x,ly,ky,y,lz,kz,z); }
private static void zero(int n1, int n2, float[][] z) { for (int i2=0; i2<n2; ++i2) zero(n1,z[i2]); }
/** * Applies this Hilbert transform filter. * @param n number of samples in input/output arrays. * @param x array[n] of input samples. * @param y array[n] of output samples. */ public void apply(int n, float[] x, float[] y) { Conv.conv(_filter.length,-(_filter.length-1)/2,_filter,n,0,x,n,0,y); }
private static float[][] reverse(int n1, int n2, float[][] z, boolean copy) { if (copy) z = copy(n1,n2,z); for (int i2=0,j2=n2-1; i2<j2; ++i2,--j2) { float[] zt = z[i2]; z[i2] = z[j2]; z[j2] = zt; } for (int i2=0; i2<n2; ++i2) reverse(n1,z[i2],false); return z; }
FftFilter ff = new FftFilter(kh1,kh2,kh3,h); ff.apply(x,y); Conv.conv(nh1,nh2,nh3,-kh1,-kh2,-kh3,h, nx1,nx2,nx3,0,0,0,x, nz1,nz2,nz3,0,0,0,z);
private static float[][][] reverse( int n1, int n2, int n3, float[][][] z, boolean copy) { if (copy) z = copy(n1,n2,n3,z); for (int i3=0,j3=n3-1; i3<j3; ++i3,--j3) { float[][] zt = z[i3]; z[i3] = z[j3]; z[j3] = zt; } for (int i3=0; i3<n3; ++i3) reverse(n1,n2,z[i3],false); return z; } }
public void test1Random() { int ntest = 1000; int kmin = -2; int kmax = 2; int lmin = 1; int lmax = 8; for (int itest=0; itest<ntest; ++itest) { int lx = lmin+_random.nextInt(1+lmax-lmin); int ly = lmin+_random.nextInt(1+lmax-lmin); int lz = lmin+_random.nextInt(1+lmax-lmin); int kx = kmin+_random.nextInt(1+kmax-kmin); int ky = kmin+_random.nextInt(1+kmax-kmin); int kz = kmin+_random.nextInt(1+kmax-kmin); float[] x = randfloat(lx); float[] y = randfloat(ly); float[] zs = zerofloat(lz); float[] zf = zerofloat(lz); if (_random.nextBoolean()) { y = x; ly = lx; ky = kx; } convSimple(lx,kx,x,ly,ky,y,lz,kz,zs); Conv.conv(lx,kx,x,ly,ky,y,lz,kz,zf); assertEquals(zs,zf); xcorSimple(lx,kx,x,ly,ky,y,lz,kz,zs); Conv.xcor(lx,kx,x,ly,ky,y,lz,kz,zf); assertEquals(zs,zf); } }
public void test2Random() { int ntest = 1000; int nmin = 1; int nmax = 8; for (int itest=0; itest<ntest; ++itest) { int nh1 = nmin+_random.nextInt(1+nmax-nmin); int nh2 = nmin+_random.nextInt(1+nmax-nmin); int nx1 = nmin+_random.nextInt(1+nmax-nmin); int nx2 = nmin+_random.nextInt(1+nmax-nmin); int ny1 = nx1; int ny2 = nx2; int nz1 = nx1; int nz2 = nx2; int kh1 = _random.nextInt(nh1); int kh2 = _random.nextInt(nh2); float[][] h = randfloat(nh1,nh2); float[][] x = randfloat(nx1,nx2); float[][] y = randfloat(ny1,ny2); float[][] z = randfloat(nz1,nz2); FftFilter ff = new FftFilter(kh1,kh2,h); ff.apply(x,y); Conv.conv(nh1,nh2,-kh1,-kh2,h,nx1,nx2,0,0,x,nz1,nz2,0,0,z); assertEquals(z,y); } }
private static void zero(int n1, int n2, int n3, float[][][] z) { for (int i3=0; i3<n3; ++i3) zero(n1,n2,z[i3]); }
public void test1Random() { int ntest = 1000; int nmin = 1; int nmax = 8; for (int itest=0; itest<ntest; ++itest) { int nh = nmin+_random.nextInt(1+nmax-nmin); int nx = nmin+_random.nextInt(1+nmax-nmin); int ny = nx; int nz = nx; int kh = _random.nextInt(nh); float[] h = randfloat(nh); float[] x = randfloat(nx); float[] y = randfloat(ny); float[] z = randfloat(nz); FftFilter ff = new FftFilter(kh,h); ff.apply(x,y); Conv.conv(nh,-kh,h,nx,0,x,nz,0,z); assertEquals(z,y); } }
/** * Convolves this function with the specified function. The two functions * must be uniformly sampled with equal sampling intervals. * @param ra the function with which to convolve. * @return the convolution function. */ public Real1 convolve(Real1 ra) { Real1 rx = this; Real1 ry = ra; Sampling sx = rx.getSampling(); Sampling sy = ry.getSampling(); double dx = sx.getDelta(); double dy = sy.getDelta(); Check.state(sx.isUniform(),"sampling is uniform"); Check.argument(sy.isUniform(),"sampling is uniform"); Check.argument(dx==dy,"sampling intervals are equal"); int lx = sx.getCount(); int ly = sy.getCount(); double fx = sx.getFirst(); double fy = sy.getFirst(); float[] x = rx.getValues(); float[] y = ry.getValues(); int lz = lx+ly-1; double dz = dx; double fz = fx+fy; float[] z = new float[lz]; Conv.conv(lx,0,x,ly,0,y,lz,0,z); return new Real1(lz,dz,fz,z); }