private static Real1 binaryOp(float ar, Real1 rb, Binary ab) { Sampling sb = rb.getSampling(); Sampling sc = sb; Real1 rc = new Real1(sc); float[] vb = rb.getValues(); float[] vc = rc.getValues(); ab.apply(vc.length,ar,vb,0,vc,0); return rc; } private static Real1 binaryOp(Real1 ra, float br, Binary ab) {
/** * Returns the sum this + ar of this function and constant ar. * @param ar a constant. * @return the sum. */ public Real1 plus(float ar) { return add(this,ar); }
/** * Returns the quotient ra / br of function ra and constant br. * @param ra a function. * @param br a constant. * @return the sum. */ public static Real1 div(Real1 ra, float br) { return binaryOp(ra,br,_div); }
private void updateViews() { Real1 h = computeImpulseResponse(); Real1[] ap = computeAmplitudeAndPhaseResponses(); Real1 a = ap[0]; Real1 p = ap[1]; if (_hView==null) { _hView = _plotPanelH.addSequence(h.getSampling(),h.getValues()); _aView = _plotPanelAP.addPoints(0,0,a.getSampling(),a.getValues()); _pView = _plotPanelAP.addPoints(1,0,p.getSampling(),p.getValues()); } else { _hView.set(h.getSampling(),h.getValues()); _aView.set(a.getSampling(),a.getValues()); _pView.set(p.getSampling(),p.getValues()); } }
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); }
/** * Returns a sampled function with values zero. * @param s the sampling. * @return the function. */ public static Real1 zero(Sampling s) { return new Real1(s); }
/** * Returns a sampled linear (ramp) function. * The function values are fv+iv*dv, for iv in [0:nv). * @param fv the first function value. * @param dv the function value delta. * @param nv the number of values. * @return the function. */ public static Real1 ramp(double fv, double dv, int nv) { return Real1.ramp(fv,dv,new Sampling(nv)); }
/** * Returns a function with constant values. * @param ar the constant. * @param n the number of samples. * @return the function. */ public static Real1 fill(double ar, int n) { return Real1.fill(ar,new Sampling(n)); }
public void testMath() { Real1 ra = RAMP1; Real1 rb = RAMP1; Real1 rc = ra.plus(rb); Real1 re = RAMP2; assertEquals(re,rc); }
void assertEquals(Real1 e, Real1 a) { Sampling se = e.getSampling(); Sampling sa = a.getSampling(); assertTrue(sa.isEquivalentTo(se)); float[] ve = e.getValues(); float[] va = a.getValues(); int n = ve.length; for (int i=0; i<n; ++i) assertEquals(ve[i],va[i],TINY); } }
/** * Returns a function with constant values. * @param ar the constant. * @param s the sampling. * @return the function. */ public static Real1 fill(double ar, Sampling s) { int n = s.getCount(); return new Real1(s, fillfloat((float)ar,n)); }
private static Real1 binaryOp(Real1 ra, float br, Binary ab) { Sampling sa = ra.getSampling(); Sampling sc = sa; Real1 rc = new Real1(sc); float[] va = ra.getValues(); float[] vc = rc.getValues(); ab.apply(vc.length,va,0,br,vc,0); return rc; } private interface Binary {
/** * Returns a sampled function with values zero. * @param n the number of samples. * @return the function. */ public static Real1 zero(int n) { return new Real1(new Sampling(n)); }
/** * Returns the product ra * rb of two functions ra and rb. * The samplings of ra and rb must be equivalent. * @param ra a function. * @param rb a function. * @return the difference. */ public static Real1 mul(Real1 ra, Real1 rb) { return binaryOp(ra,rb,_mul); }
/** * Returns the sum this + ra of functions this and ra. * The samplings of this and ra must be equivalent. * @param ra a function. * @return the sum. */ public Real1 plus(Real1 ra) { return add(this,ra); }
private static Real1 binaryOp(Real1 ra, Real1 rb, Binary ab) { Sampling sa = ra.getSampling(); Sampling sb = rb.getSampling(); Check.argument(sa.isEquivalentTo(sb),"samplings equivalent"); Sampling sc = sa; Real1 rc = new Real1(sc); float[] va = ra.getValues(); float[] vb = rb.getValues(); float[] vc = rc.getValues(); ab.apply(vc.length,va,0,vb,0,vc,0); return rc; } private static Real1 binaryOp(float ar, Real1 rb, Binary ab) {
private Real1 computeImpulseResponse() { // Arrays of poles and zeros. int np = _poles.size(); int nz = _zeros.size(); Cdouble[] poles = new Cdouble[np]; Cdouble[] zeros = new Cdouble[nz]; _poles.toArray(poles); _zeros.toArray(zeros); // Array for impulse response h. int n = 101; float[] h = new float[n]; // If at least one pole or zero, then construct and apply the filter. // Otherwise, the impulse response is simply an impulse. if (np>0 || nz>0) { float[] impulse = new float[n]; impulse[0] = 1.0f; RecursiveCascadeFilter f = new RecursiveCascadeFilter(poles,zeros,1.0); f.applyForward(impulse,h); } else { h[0] = 1.0f; } Sampling s = new Sampling(n); return new Real1(s,h); }
/** * Returns the sum ra + br of function ra and constant br. * @param ra a function. * @param br a constant. * @return the sum. */ public static Real1 add(Real1 ra, float br) { return binaryOp(ra,br,_add); }
/** * 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); }