/** * Applies this filter. * @param x input array. * @return filtered array. */ public float[] apply(float[] x) { float[] y = new float[x.length]; apply(x,y); return y; }
private void updateFilter1() { if (_ff1==null) { KaiserWindow kw = KaiserWindow.fromErrorAndWidth(_aerror,_kwidth); int nh = ((int)kw.getLength()+1)/2*2+1; int nh1 = nh; int kh1 = (nh1-1)/2; _h1 = new float[nh1]; double kus = 2.0*_kupper; double kls = 2.0*_klower; for (int i1=0; i1<nh1; ++i1) { double x1 = i1-kh1; double w1 = kw.evaluate(x1); double r = x1; double kur = 2.0*_kupper*r; double klr = 2.0*_klower*r; _h1[i1] = (float)(w1*(kus*h1(kur)-kls*h1(klr))); } _ff1 = new FftFilter(_h1); _ff1.setExtrapolation(ffExtrap(_extrapolation)); _ff1.setFilterCaching(_filterCaching); } }
/** * Applies this filter. * Input and output arrays may be the same array. * @param x input array. * @param y output array. */ public void apply(float[] x, float[] y) { Check.state(_h1!=null,"1D filter is available"); int nx1 = x.length; updateFfts(nx1); float[] xfft = new float[_nfft1+2]; copy(nx1,x,xfft); extrapolate(xfft); _fft1.realToComplex(-1,xfft,xfft); int nk1 = _nfft1/2+1; for (int ik1=0,k1r=0,k1i=1; ik1<nk1; ++ik1,k1r+=2,k1i+=2) { float xr = xfft[k1r]; float xi = xfft[k1i]; float hr = _h1fft[k1r]; float hi = _h1fft[k1i]; xfft[k1r] = xr*hr-xi*hi; xfft[k1i] = xr*hi+xi*hr; } if (!_filterCaching) _h1fft = null; _fft1.complexToReal(1,xfft,xfft); copy(nx1,xfft,y); }
private void extrapolate(float[][] xfft) { if (_extrapolation==Extrapolation.ZERO_SLOPE) { for (int i2=0; i2<_nx2; ++i2) extrapolate(xfft[i2]); int mr2 = _nx2+_kh2; float[] xr2 = xfft[_nx2-1]; for (int i2=_nx2; i2<mr2; ++i2) copy(xr2,xfft[i2]); int ml2 = _nfft2+_kh2-_nh2+1; float[] xl2 = xfft[0]; for (int i2=ml2; i2<_nfft2; ++i2) copy(xl2,xfft[i2]); } }
private void updateFilter2() { if (_ff2==null) { KaiserWindow kw = KaiserWindow.fromErrorAndWidth(_aerror,_kwidth); int nh = ((int)kw.getLength()+1)/2*2+1; int nh1 = nh; int nh2 = nh; int kh1 = (nh1-1)/2; int kh2 = (nh2-1)/2; _h2 = new float[nh2][nh1]; double kus = 4.0*_kupper*_kupper; double kls = 4.0*_klower*_klower; for (int i2=0; i2<nh2; ++i2) { double x2 = i2-kh2; double w2 = kw.evaluate(x2); for (int i1=0; i1<nh1; ++i1) { double x1 = i1-kh1; double w1 = kw.evaluate(x1); double r = sqrt(x1*x1+x2*x2); double kur = 2.0*_kupper*r; double klr = 2.0*_klower*r; _h2[i2][i1] = (float)(w1*w2*(kus*h2(kur)-kls*h2(klr))); } } _ff2 = new FftFilter(_h2); _ff2.setExtrapolation(ffExtrap(_extrapolation)); _ff2.setFilterCaching(_filterCaching); } }
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); } }
int nx1 = x[0].length; int nx2 = x.length; updateFfts(nx1,nx2); float[][] xfft = new float[_nfft2][_nfft1+2]; copy(nx1,nx2,x,xfft); extrapolate(xfft); _fft1.realToComplex1(-1,_nfft2,xfft,xfft); _fft2.complexToComplex2(-1,_nfft1/2+1,xfft,xfft);
private void extrapolate(float[][][] xfft) { if (_extrapolation==Extrapolation.ZERO_SLOPE) { for (int i3=0; i3<_nx3; ++i3) extrapolate(xfft[i3]); int mr3 = _nx3+_kh3; float[][] xr3 = xfft[_nx3-1]; for (int i3=_nx3; i3<mr3; ++i3) copy(xr3,xfft[i3]); int ml3 = _nfft3+_kh3-_nh3+1; float[][] xl3 = xfft[0]; for (int i3=ml3; i3<_nfft3; ++i3) copy(xl3,xfft[i3]); } } }
_ff3 = new FftFilter(_h3); _ff3.setExtrapolation(ffExtrap(_extrapolation)); _ff3.setFilterCaching(_filterCaching);
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); } }
/** * Applies this filter. * @param x input array. * @return filtered array. */ public float[][] apply(float[][] x) { float[][] y = new float[x.length][x[0].length]; apply(x,y); return y; }
int nx2 = x[0].length; int nx3 = x.length; updateFfts(nx1,nx2,nx3); float[][][] xfft = new float[_nfft3][_nfft2][_nfft1+2]; copy(nx1,nx2,nx3,x,xfft); extrapolate(xfft); _fft1.realToComplex1(-1,_nfft2,_nfft3,xfft,xfft); _fft2.complexToComplex2(-1,_nfft1/2+1,_nfft3,xfft,xfft);
/** * Applies this filter. * @param x input array. * @return filtered array. */ public float[][][] apply(float[][][] x) { float[][][] y = new float[x.length][x[0].length][x[0][0].length]; apply(x,y); return y; }
/** * Applies this filter. * Input and output arrays may be the same array. * @param x input array. * @param y output filtered array. */ public void apply(float[][] x, float[][] y) { updateFilter2(); _ff2.apply(x,y); }
/** * Applies this filter. * Input and output arrays may be the same array. * @param x input array. * @param y output filtered array. */ public void apply(float[] x, float[] y) { updateFilter1(); _ff1.apply(x,y); }
/** * Applies this filter. * Input and output arrays may be the same array. * @param x input array. * @param y output filtered array. */ public void apply(float[][][] x, float[][][] y) { updateFilter3(); _ff3.apply(x,y); }