/** * Constructs a function with specified sampling and values zero. * @param n the number (count) of samples. * @param d the sampling interval (delta). * @param f the value of the first sample. */ public Real1(int n, double d, double f) { this(new Sampling(n,d,f)); }
/** * Interpolates one real value y(x). * @param sxu sampling of input samples. * @param yu input array of uniformly sampled values y(x). * @param xi value x at which to interpolate. * @return interpolated value y(x). */ public float interpolate(Sampling sxu, float[] yu, double xi) { Check.argument(sxu.isUniform(),"input sampling is uniform"); return interpolate(sxu.getCount(),sxu.getDelta(),sxu.getFirst(),yu,xi); }
/** * Returns the value of the sample nearest to the specified value. * @param x the value. * @return the value of the nearest sample. */ public double valueOfNearest(double x) { return getValue(indexOfNearest(x)); }
/** * Returns the value of the sample nearest to the specified value, * assuming uniform sampling. The specified and returned values need * not be in the bounds of this sampling, which must be uniform. * @param x the value. * @return the value of the nearest sample. */ public double valueOfNearestExtended(double x) { return getValueExtended(indexOfNearestExtended(x)); }
/** * Determines whether the specified value is in the bounds of this sampling. * A value is in bounds if in the range [first,last] defined by the first * and last sample values. * @param x the value. * @return true, if in bounds; false, otherwise. */ public boolean isInBounds(double x) { return getFirst()<=x && x<=getLast(); }
Sampling us = new Sampling(n,d,f); Sampling vs = new Sampling(v); assertTrue(us.isUniform()); assertTrue(vs.isUniform()); assertTrue(us.isEquivalentTo(vs)); int j = us.indexOf(v[i]); assertEquals(i,j); j = vs.indexOf(v[i]); assertEquals(i,j); int[] overlap = us.overlapWith(vs); assertEquals(n,overlap[0]); assertEquals(0,overlap[1]); assertEquals(0,overlap[2]); Sampling sm = us.mergeWith(vs); assertTrue(sm.isEquivalentTo(us)); assertTrue(sm.isEquivalentTo(vs)); Sampling sr = us.shift(10*d); overlap = us.overlapWith(sr); assertEquals(n-10,overlap[0]); assertEquals(10,overlap[1]); assertEquals(0,overlap[2]); Sampling sl = us.shift(-10*d); overlap = us.overlapWith(sl);
public float[][] grid(Sampling s1, Sampling s2) { int n1 = s1.getCount(); int n2 = s2.getCount(); double d1 = s1.getDelta(); double d2 = s2.getDelta(); double f1 = s1.getFirst(); double f2 = s2.getFirst(); double l1 = s1.getLast(); double l2 = s2.getLast(); f1 -= 0.5*d1; l1 += 0.5*d1; f2 -= 0.5*d2; l2 += 0.5*d2; float[][] g = new float[n2][n1]; float[][] c = new float[n2][n1]; for (int i=0; i<_n; ++i) { double x1 = _x1[i]; double x2 = _x2[i]; if (f1<=x1 && x1<=l1 && f2<=x2 && x2<=l2) { int i1 = s1.indexOfNearest(x1); int i2 = s2.indexOfNearest(x2); g[i2][i1] += _f[i]; c[i2][i1] += 1.0f; } } for (int i2=0; i2<n2; ++i2) { for (int i1=0; i1<n1; ++i1) { g[i2][i1] = (c[i2][i1]>0.0f)?g[i2][i1]/c[i2][i1]:_fnull; } } return g; }
int nt = t.getCount(); int ns = s.getCount(); double tf = t.getFirst(); double sf = s.getFirst(); double tl = t.getLast(); double sl = s.getLast(); int it = 0; int is = 0; } else { if (tf<sf) { it = t.indexOf(sf); } else { is = s.indexOf(tf); js = s.indexOf(tl); } else { jt = t.indexOf(sl); if (mt!=ms) return false; if (!t.isUniform() || !s.isUniform()) { double tiny = tinyWith(s); for (jt=it,js=is; jt!=mt; ++jt,++js) { if (!almostEqual(t.value(jt),s.value(js),tiny)) return false;
private void updateTensorEllipses() { int n1 = _s1.getCount(); int n2 = _s2.getCount(); double d1 = _s1.getDelta(); double d2 = _s2.getDelta(); double f1 = _s1.getFirst(); double f2 = _s2.getFirst(); int j1 = (n1-1-(m1-1)*ns)/2; // index of first ellipse along x1 axis int j2 = (n2-1-(m2-1)*ns)/2; // index of first ellipse along x2 axis e1 = new Sampling(m1,ns*d1,f1+j1*d1); e2 = new Sampling(m2,ns*d2,f2+j2*d2); int m1 = e1.getCount(); int m2 = e2.getCount(); int nm = m1*m2; // total number of ellipses double ratio = min(e1.getDelta()/d1,e2.getDelta()/d2); double r = (emax>0.0)?0.48*ratio/sqrt(emax):0.0; r *= _scale; float[] u = new float[2]; for (int i2=0,im=0; i2<m2; ++i2) { double x2 = e2.getValue(i2); int j2 = _s2.indexOfNearest(x2); double x1 = e1.getValue(i1); int j1 = _s1.indexOfNearest(x1);
/** * Gets sampling for the Fourier transform of this function. The first * sample value will be zero, because the Fourier transform of a real * function has conjugate-symmetry. * <p> * A minimum number of Fourier transform samples must be specified, and * the number of samlpes in the returned sampling will not be less than * the larger of that specified minimum and the number of samples in this * function. * @param nmin the minimum number of samples after Fourier transform. * @return the Fourier transform sampling. */ public Sampling getFourierSampling(int nmin) { int nt = _s.getCount(); double dt = _s.getDelta(); int nfft = FftReal.nfftSmall(max(nmin,nt)); int nf = nfft/2+1; double df = 1.0/(nfft*dt); double ff = 0.0; return new Sampling(nf,df,ff); }
public void testBoundsAndForUniform() { Sampling s = new Sampling(11,1.0,0.0); assertEquals(s.isInBoundsExtended(-0.5001),false); assertEquals(s.isInBoundsExtended(-0.4999),true); assertEquals(s.isInBoundsExtended(10.4999),true); assertEquals(s.isInBoundsExtended(10.5001),false); assertEquals(s.getValueExtended(-1),-1.0); assertEquals(s.getValueExtended(0),0.0); assertEquals(s.getValueExtended(10),10.0); assertEquals(s.getValueExtended(11),11.0); assertEquals(s.indexOfNearestExtended(-0.5001),-1); assertEquals(s.indexOfNearestExtended(-0.4999),0); assertEquals(s.indexOfNearestExtended(0.0),0); assertEquals(s.indexOfNearestExtended(0.4999),0); assertEquals(s.indexOfNearestExtended(0.5001),1); assertEquals(s.indexOfNearestExtended(1.0),1); assertEquals(s.indexOfNearestExtended(1.4999),1); assertEquals(s.indexOfNearestExtended(1.5001),2); assertEquals(s.normalizedDifferenceExtended( 1.5,s.indexOfFloorExtended(1.5)),0.5); s = new Sampling(10,1.0,-5.0); assertEquals(s.indexOfFloorExtended(-6.0),-1); assertEquals(s.indexOfFloorExtended(-5.9999),-1); assertEquals(s.indexOfFloorExtended(-5.5),-1); assertEquals(s.indexOfFloorExtended(-5.0001),-1); assertEquals(s.indexOfFloorExtended(-5.0),0); assertEquals(s.indexOfFloorExtended(-4.9999),0); assertEquals(s.indexOfFloorExtended(-4.5),0); assertEquals(s.indexOfFloorExtended(-4.0001),0); assertEquals(s.indexOfFloorExtended(-4.0),1);
/** * Returns a sampled linear (ramp) function. * The function values are fv+dv*(s-f), for s = f, f+d, ..., f+d*(n-1), * where n, d, and f are the sampling count, delta, and first value. * @param fv the first function value. * @param dv the function value delta. * @param s the sampling. * @return the function. */ public static Real1 ramp(double fv, double dv, Sampling s) { int n = s.getCount(); double d = s.getDelta(); double f = s.getFirst(); return new Real1(s, rampfloat((float)(fv-f*dv),(float)(d*dv),n)); }
/** * Constructs a function with specified sampling and values zero. * @param s the sampling. */ public Real1(Sampling s) { _s = s; _v = new float[s.getCount()]; }
private void phase(int sign1, int sign2, int sign3, float[][][] f) { double fx1 = _sx1.getFirst(); double fx2 = _sx2.getFirst(); double fx3 = _sx3.getFirst(); if (fx1==0.0 && fx2==0.0 && fx3==0.0) return; int nk1 = (_complex)?_nfft1:_nfft1/2+1; int nk2 = _nfft2; int nk3 = _nfft3; double dp1 = sign1*2.0*PI*_sk1.getDelta()*fx1; double dp2 = sign2*2.0*PI*_sk2.getDelta()*fx2; double dp3 = sign3*2.0*PI*_sk3.getDelta()*fx3; for (int i3=0; i3<nk3; ++i3) { for (int i2=0; i2<nk2; ++i2) { double p23 = i2*dp2+i3*dp3; float[] f32 = f[i3][i2]; for (int i1=0,ir=0,ii=1; i1<nk1; ++i1,ir+=2,ii+=2) { float p = (float)(i1*dp1+p23); float cosp = cos(p); float sinp = sin(p); float fr = f32[ir]; float fi = f32[ii]; f32[ir] = fr*cosp-fi*sinp; f32[ii] = fi*cosp+fr*sinp; } } } } }
/** * Determines whether the specified value is in the bounds of this sampling, * which is assumed to be uniform. A value is in bounds if in the range * [first-0.5*delta,last+0.5*delta] defined by the first and last sample * values and the sampling interval delta. In effect, this method extends * the bounds of this sampling by one-half sample when testing the value. * @param x the value. * @return true, if in bounds; false, otherwise. */ public boolean isInBoundsExtended(double x) { Check.state(isUniform(),"sampling is uniform"); double dhalf = 0.5*_d; return getFirst()-dhalf<=x && x<=getLast()+dhalf; }
/** * Sets the sampled functions f(x1,x2) for this view. * If compatible samplings for x1 and x2 have already been specified, * then this method uses them. Otherwise, this method assumes zero * first sample values and unit sampling intervals. * Function values are converted to colors using a direct color model. * @param f array[nc][n2][n1] of sampled function values f(x1,x2), * where nc is the number (three or four) of color components. */ public void set(float[][][] f) { if (_s1!=null && _s1.getCount()==f[0][0].length && _s2!=null && _s2.getCount()==f[0].length) { set(_s1,_s2,f); } else { set(new Sampling(f[0][0].length),new Sampling(f[0].length),f); } }
double xmax, double ymax, double zmax) _xmin = max(xmin,_sx.getFirst()); _ymin = max(ymin,_sy.getFirst()); _zmin = max(zmin,_sz.getFirst()); _xmax = min(xmax,_sx.getLast()); _ymax = min(ymax,_sy.getLast()); _zmax = min(zmax,_sz.getLast()); int kxmin = _sx.indexOfNearest(_xmin); int kymin = _sy.indexOfNearest(_ymin); int kzmin = _sz.indexOfNearest(_zmin); int kxmax = _sx.indexOfNearest(_xmax); int kymax = _sy.indexOfNearest(_ymax); int kzmax = _sz.indexOfNearest(_zmax); boolean stale; if (_axis==Axis.X) {
public void testResample() { Real1 ra = FILL1; Sampling sa = ra.getSampling(); int n1 = sa.getCount(); double d1 = sa.getDelta(); int m1 = n1/3; Sampling sb = sa.shift(-m1*d1); Real1 rb = ra.resample(sb); float[] vb = rb.getValues(); for (int i1=0; i1<m1; ++i1) assertEquals(0.0,vb[i1],0.0); for (int i1=m1; i1<n1; ++i1) assertEquals(1.0,vb[i1],0.0); Sampling sc = sa.shift(m1*d1); Real1 rc = ra.resample(sc); float[] vc = rc.getValues(); for (int i1=0; i1<n1-m1; ++i1) assertEquals(1.0,vc[i1],0.0); for (int i1=n1-m1; i1<n1; ++i1) assertEquals(0.0,vc[i1],0.0); }