Package ca.nengo.math.impl

Examples of ca.nengo.math.impl.FourierFunction


  /*
   * Test method for 'ca.nengo.math.impl.FourierFunction.FourierFunction(float[], float[], float[])'
   */
  public void testFourierFunctionFloatArrayFloatArrayFloatArray() {
    FourierFunction f = new FourierFunction(new float[]{1.5f}, new float[]{5f}, new float[]{.2f});
    assertClose(0.0000f, f.map(new float[]{.2f}));
    assertClose(1.5451f, f.map(new float[]{1.5f}));
    assertClose(2.9389f, f.map(new float[]{2.8f}));
  }
View Full Code Here


  /*
   * Test method for 'ca.nengo.math.impl.FourierFunction.getDimension()'
   */
  public void testGetDimension() {
    FourierFunction f = new FourierFunction(new float[]{1f}, new float[]{1f}, new float[]{0f});
    assertEquals(1, f.getDimension());
  }
View Full Code Here

  /*
   * Test method for 'ca.nengo.math.impl.FourierFunction.multiMap(float[][])'
   */
  public void testMultiMap() {
    FourierFunction f = new FourierFunction(new float[]{1f}, new float[]{1f}, new float[]{0f});

    float[] from1 = new float[]{.5f};
    float val1 = f.map(from1);
    float[] from2 = new float[]{.6f};
    float val2 = f.map(from2);
   
    float[] vals = f.multiMap(new float[][]{from1, from2});
    assertClose(vals[0], val1);
    assertClose(vals[1], val2);
  }
View Full Code Here

    public void testNothing() {
    }

    //functional test
    public static void main(String[] args) {
        Function current = new FourierFunction(1f, 5f, 1f, (long) Math.random());
        Function rate = new SigmoidFunction(0, 5, 0, 40);
        PoissonSpikeGenerator generator = new PoissonSpikeGenerator(rate);

        float T = 1;
        float dt = .0005f;
        int steps = (int) Math.floor(T/dt);
        float[] spikeTimes = new float[steps];
        int spikes = 0;
        for (int i = 0; i < steps; i++) {
            float time = dt * i;
            float c1 = current.map(new float[]{time});
            float c2 = current.map(new float[]{time+dt});
            boolean spike = ((SpikeOutput) generator.run(new float[]{time, time+dt}, new float[]{c1, c2})).getValues()[0];

            if (spike) {
                spikeTimes[spikes] = time+dt;
                spikes++;
View Full Code Here

      fail("Values " + a + " and " + b + " are not close enough");
    }
  }
 
  public void testClone() throws CloneNotSupportedException {
    FourierFunction f1 = new FourierFunction(new float[]{1.5f}, new float[]{5f}, new float[]{.2f});
    FourierFunction f2 = (FourierFunction) f1.clone();
    f2.setFrequencies(new float[][]{new float[]{2f}});
    assertClose(0.0000f, f1.map(new float[]{.2f}));
    assertClose(1.5451f, f1.map(new float[]{1.5f}));
    assertClose(2.9389f, f1.map(new float[]{2.8f}));
  }
View Full Code Here

    float[][] evalPoints = new float[100][];
    for (int i = 0; i < evalPoints.length; i++) {
      evalPoints[i] = new float[]{(float) i / (float) evalPoints.length};
    }
   
    Function target = new FourierFunction(frequencies, amplitudes, phases);
    float[][] values = new float[frequencies.length][];
    for (int i = 0; i < frequencies.length; i++) {
      Function component = new FourierFunction(new float[]{frequencies[i]}, new float[]{1}, new float[]{phases[i]});
      values[i] = new float[evalPoints.length];
      for (int j = 0; j < evalPoints.length; j++) {
        values[i][j] = component.map(evalPoints[j]);
      }
    }
   
    GradientDescentApproximator.Constraints constraints = new GradientDescentApproximator.Constraints() {
      private static final long serialVersionUID = 1L;
View Full Code Here

    try {
      root = nrf.findRoot(func, -5, 15, 0.0001f);
    } catch (RuntimeException e) { // failure after too many attempts
    }
   
    func = new FourierFunction(new float[]{1, 0.5f, 1}, new float[]{1, 1, 0.5f}, new float[]{0, -0.5f, 0.2f});
    root = nrf.findRoot(func, -5, 5, 0.0001f);
    TestUtil.assertClose(func.map(new float[]{root}), 0, 0.001f);
   
    func = new IdentityFunction(1, 0);
    root = nrf.findRoot(func, -5, 5, 0.0001f);
View Full Code Here

    for (int i = 0; i < evalPoints.length; i++) {
      evalPoints[i] = new float[]{(float) i / (float) evalPoints.length};
    }
   
    //testing with eval points
    Function target = new FourierFunction(frequencies, amplitudes, phases);
    float[][] values = new float[frequencies.length][];
    for (int i = 0; i < frequencies.length; i++) {
      Function component = new FourierFunction(new float[]{frequencies[i]}, new float[]{1}, new float[]{phases[i]});
      values[i] = new float[evalPoints.length];
      for (int j = 0; j < evalPoints.length; j++) {
        values[i][j] = component.map(evalPoints[j]);
      }
    }
   
    WeightedCostApproximator.Factory factory = new WeightedCostApproximator.Factory(0f);
    LinearApproximator approximator = factory.getApproximator(evalPoints, values);
    float[] coefficients = approximator.findCoefficients(target);
   
    float approx;
    for (int j = 0; j < evalPoints.length; j++) {
      approx = 0f;
      for (int i = 0; i < frequencies.length; i++) {
        approx += coefficients[i] * values[i][j];
      }
      TestUtil.assertClose(approx, target.map(evalPoints[j]), 0.0001f);
    }
   
    //testing with eval signals
    TimeSeries1DImpl targetsig = (TimeSeries1DImpl)TimeSeriesFunction.makeSeries(new FourierFunction(frequencies, amplitudes, phases), 0.0f, 0.001f, 1.0f, Units.UNK);
    float[] times = targetsig.getTimes();
    float[][][] valuesig = new float[frequencies.length][1][];
    for (int i = 0; i < frequencies.length; i++) {
      Function component = new FourierFunction(new float[]{frequencies[i]}, new float[]{1}, new float[]{phases[i]});
      valuesig[i][0] = new float[times.length];
      for (int j = 0; j < times.length; j++) {
        valuesig[i][0][j] = component.map(new float[]{times[j]});
      }
    }
    float[][][] evalsigs = new float[1][1][];
    evalsigs[0][0] = times;
    factory = new WeightedCostApproximator.Factory(0f);
View Full Code Here

            StringBuilder base = new StringBuilder("[");
            StringBuilder high = new StringBuilder("[");
            StringBuilder power = new StringBuilder("[");
           
            for (int i = 0; i < myFunctions.length; i++) {          
                FourierFunction func = (FourierFunction)myFunctions[i];
               
                if (func.getFundamental() == 0.0f) {
                    throw new ScriptGenException("Cannot generate a Fourier Function that was built by specifiying all frequencies, amplitudes and phases");
                }

                base.append(func.getFundamental());
                high.append(func.getCutoff());
                power.append(func.getRms());
                if ((i + 1) < myFunctions.length) {
                    base.append(",");
                    high.append(",");
                    power.append(",");
                }
            }

            base.append("]");
            high.append("]");
            power.append("]");

            py.append(String.format("%s.make_fourier_input('%s', dimensions=%d, base=%s, high=%s, power=%s)\n",
                        scriptData.get("netName"),
                        myName,
                        myFunctions.length,
                        base,
                        high,
                        power));
        } else {
            StringBuilder funcs = new StringBuilder("[");
            for (int i = 0; i < myFunctions.length; i++) {
             
              String functionName = String.format("Function%c%s%c%d",
                          (Character)scriptData.get("spaceDelim"),
                          myName.replaceAll("\\p{Blank}|\\p{Punct}", ((Character)scriptData.get("spaceDelim")).toString()),
                          (Character)scriptData.get("spaceDelim"),
                          i);

                if (myFunctions[i] instanceof ConstantFunction) {
                    ConstantFunction func = (ConstantFunction)myFunctions[i];
                   
                    py.append(String.format("%s = ConstantFunction(%d, %.3f)\n",
                                      functionName,
                                      func.getDimension(),
                                      func.getValue()));
                   
                } else if (myFunctions[i] instanceof FourierFunction) {
                    FourierFunction func = (FourierFunction)myFunctions[i];

                    py.append(String.format("%s = FourierFunction(%f, %f, %f, %d)\n",
                          functionName,
                                func.getFundamental(),
                                func.getCutoff(),
                                func.getRms(),
                                func.getSeed()));
                } else if (myFunctions[i] instanceof PostfixFunction) {
                    PostfixFunction func = (PostfixFunction)myFunctions[i];

                    py.append(String.format("%s = PostfixFunction('%s', %d)\n",
                          functionName,
                                func.getExpression(),
                                func.getDimension()));
                }

                funcs.append(functionName);
               
                if ((i + 1) < myFunctions.length) {
View Full Code Here

TOP

Related Classes of ca.nengo.math.impl.FourierFunction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.