Search in sources :

Example 1 with Plot2D

use of spacegraph.space2d.widget.meter.Plot2D in project narchy by automenta.

the class WaveCapture method view.

// private final boolean normalizeDisplayedWave = false;
public Surface view() {
    final Plot2D.Series rawWave, wavelet1d;
    rawWave = new Plot2D.Series("Audio", 1) {

        @Override
        public void update() {
            clear();
            float[] samples = WaveCapture.this.samples;
            if (samples == null)
                return;
            // samples[0] = null;
            int chans = WaveCapture.this.source.channelsPerSample();
            int bufferSamples = Math.min(WaveCapture.this.bufferSamples, samples.length / chans);
            switch(chans) {
                case 1:
                    for (int i = 0; i < bufferSamples; i++) add(samples[i]);
                    break;
                case 2:
                    for (int i = 0; i < bufferSamples; ) // to mono
                    add((samples[i++] + samples[i++]) / 2f);
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        // minValue = -0.5f;
        // maxValue = 0.5f;
        // if (normalizeDisplayedWave) {
        // autorange();
        // } else {
        // minValue = -1;
        // maxValue = +1;
        // }
        // final FloatArrayList history = this.history;
        // 
        // for (int i = 0; i < nSamplesRead; i++) {
        // history.add((float) samples[i]);
        // }
        // 
        // while (history.size() > maxHistory)
        // history.removeAtIndex(0);
        // minValue = Float.POSITIVE_INFINITY;
        // maxValue = Float.NEGATIVE_INFINITY;
        // 
        // history.forEach(v -> {
        // if (Double.isFinite(v)) {
        // if (v < minValue) minValue = v;
        // if (v > maxValue) maxValue = v;
        // }
        // //mean += v;
        // });
        }
    };
    wavelet1d = new Plot2D.Series("Wavelet", 1) {

        final float[] transformedSamples = new float[Util.largestPowerOf2NoGreaterThan(bufferSamples)];

        final AtomicBoolean busy = new AtomicBoolean();

        {
            frame.on((w) -> {
                if (!busy.compareAndSet(false, true))
                    return;
                FloatArrayList history = this;
                // for (short s : ss) {
                // history.add((float)s);
                // }
                // 
                // 
                // while (history.size() > maxHistory)
                // history.removeAtIndex(0);
                // 
                // while (history.size() < maxHistory)
                // history.add(0);
                final int bufferSamples = Math.min(samples.length, WaveCapture.this.bufferSamples);
                float[] ss = transformedSamples;
                // 1d haar wavelet transform
                // OneDHaar.displayOrderedFreqsFromInPlaceHaar(x);
                // the remainder will be zero
                System.arraycopy(samples, 0, ss, 0, bufferSamples);
                OneDHaar.inPlaceFastHaarWaveletTransform(ss);
                sampleFrequency(ss);
                // OneDHaar.displayOrderedFreqsFromInPlaceHaar(samples, System.out);
                // //apache commons math - discrete cosine transform
                // {
                // double[] dsamples = new double[samples.length + 1];
                // for (int i = 0; i < samples.length; i++)
                // dsamples[i] = samples[i];
                // dsamples = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I).transform(dsamples, TransformType.FORWARD);
                // for (int i = 0; i < samples.length; i++)
                // samples[i] = (float) dsamples[i];
                // }
                history.clear();
                for (int i = 0; i < bufferSamples; i++) history.addAll(ss[i]);
                // minValue = Short.MIN_VALUE;
                // maxValue = Short.MAX_VALUE;
                // if (normalizeDisplayedWave) {
                // minValue = Float.POSITIVE_INFINITY;
                // maxValue = Float.NEGATIVE_INFINITY;
                // 
                // history.forEach(v -> {
                // //if (Float.isFinite(v)) {
                // if (v < minValue) minValue = v;
                // if (v > maxValue) maxValue = v;
                // //}
                // //mean += v;
                // });
                // } else {
                // minValue = -1f;
                // maxValue = 1f;
                // }
                // System.out.println(maxHistory + " " + start + " " + end + ": " + minValue + " " + maxValue);
                busy.set(false);
            });
        }

        private void sampleFrequency(float[] freqSamples) {
            int lastFrameIdx = data.length - freqSamplesPerFrame;
            int samples = freqSamples.length;
            float bandWidth = ((float) samples) / freqSamplesPerFrame;
            float sensitivity = 1f;
            final Envelope uniform = (i, k) -> {
                float centerFreq = (0.5f + i) * bandWidth;
                return 1f / (1f + Math.abs(k - centerFreq) / (bandWidth / sensitivity));
            };
            System.arraycopy(data, 0, data, freqSamplesPerFrame, lastFrameIdx);
            float[] h = WaveCapture.this.data;
            // int f = freqOffset;
            // int freqSkip = 1;
            // for (int i = 0; i < freqSamplesPerFrame; i++) {
            // h[n++] = freqSamples[f];
            // f+=freqSkip*2;
            // }
            float max = Float.NEGATIVE_INFINITY, min = Float.POSITIVE_INFINITY;
            for (int i = 0; i < freqSamplesPerFrame; i++) {
                float s = 0;
                for (int k = 0; k < samples; k++) {
                    float fk = freqSamples[k];
                    s += uniform.apply(i, k) * fk;
                }
                if (s > max)
                    max = s;
                if (s < min)
                    min = s;
                h[i] = s;
            }
            if (max != min) {
                // TODO epsilon check
                float range = max - min;
                for (int i = 0; i < freqSamplesPerFrame; i++) dataNorm[i] = (data[i] - min) / range;
            }
        // System.arraycopy(freqSamples, 0, history, 0, freqSamplesPerFrame);
        }
    };
    rawWave.range(-1, +1);
    wavelet1d.range(-1, +1);
    // , bufferSamples, 450, 60);
    Plot2D audioPlot = new Plot2D(bufferSamples, Plot2D.Line);
    audioPlot.add(rawWave);
    Plot2D audioPlot2 = new Plot2D(bufferSamples, Plot2D.Line);
    audioPlot2.add(wavelet1d);
    BitmapMatrixView freqHistory = new BitmapMatrixView(freqSamplesPerFrame, historyFrames, (x, y) -> {
        if (data == null)
            // HACK
            return 0;
        float kw = (data[y * freqSamplesPerFrame + x]);
        // int kw = (int)(v*255);
        return Draw.rgbInt(kw >= 0 ? kw : 0, kw < 0 ? -kw : 0, 0);
    });
    Gridding v = new Gridding(audioPlot, audioPlot2, freqHistory);
    if (source instanceof AudioSource)
        v.add(new FloatSlider(((AudioSource) source).gain));
    frame.on(() -> {
        freqHistory.update();
        audioPlot.update();
        audioPlot2.update();
    // wav2.update();
    });
    return v;
}
Also used : Loop(jcog.exe.Loop) FloatSlider(spacegraph.space2d.widget.slider.FloatSlider) Surface(spacegraph.space2d.Surface) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneDHaar(jcog.math.OneDHaar) Draw(spacegraph.video.Draw) Util(jcog.Util) FloatArrayList(org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList) Gridding(spacegraph.space2d.container.Gridding) Topic(jcog.event.Topic) Plot2D(spacegraph.space2d.widget.meter.Plot2D) ListTopic(jcog.event.ListTopic) BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) FloatSlider(spacegraph.space2d.widget.slider.FloatSlider) BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Gridding(spacegraph.space2d.container.Gridding) Plot2D(spacegraph.space2d.widget.meter.Plot2D) FloatArrayList(org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList)

Example 2 with Plot2D

use of spacegraph.space2d.widget.meter.Plot2D in project narchy by automenta.

the class Line1DCalibrate method conceptPlot.

public static Gridding conceptPlot(NAR nar, Iterable<FloatSupplier> concepts, int plotHistory) {
    // TODO make a lambda Grid constructor
    Gridding grid = new Gridding(VERTICAL);
    List<Plot2D> plots = $.newArrayList();
    for (FloatSupplier t : concepts) {
        Plot2D p = new Plot2D(plotHistory, Plot2D.Line);
        p.add(t.toString(), t::asFloat, 0f, 1f);
        grid.add(p);
        plots.add(p);
    }
    grid.layout();
    nar.onCycle(f -> {
        plots.forEach(Plot2D::update);
    });
    return grid;
}
Also used : Gridding(spacegraph.space2d.container.Gridding) FloatSupplier(jcog.math.FloatSupplier) List(java.util.List) Line1DSimplest(nars.test.agent.Line1DSimplest) Plot2D(spacegraph.space2d.widget.meter.Plot2D)

Example 3 with Plot2D

use of spacegraph.space2d.widget.meter.Plot2D in project narchy by automenta.

the class Recog2D method conceptTraining.

Surface conceptTraining(BeliefVector tv, NAR nar) {
    // LinkedHashMap<TaskConcept, BeliefVector.Neuron> out = tv.out;
    Plot2D p;
    int history = 256;
    Gridding g = new Gridding(p = new Plot2D(history, Plot2D.Line).add("Reward", () -> reward), new AspectAlign(new CameraSensorView(sp, this), AspectAlign.Align.Center, sp.width, sp.height), new Gridding(beliefTableCharts(nar, List.of(tv.concepts), 16)), new Gridding(IntStream.range(0, tv.concepts.length).mapToObj(i -> new spacegraph.space2d.widget.text.Label(String.valueOf(i)) {

        @Override
        protected void paintBelow(GL2 gl) {
            Concept c = tv.concepts[i];
            BeliefVector.Neuron nn = tv.neurons[i];
            float freq, conf;
            Truth t = nar.beliefTruth(c, nar.time());
            if (t != null) {
                conf = t.conf();
                freq = t.freq();
            } else {
                conf = nar.confMin.floatValue();
                float defaultFreq = // interpret no-belief as maybe
                0.5f;
                // Float.NaN  //use NaN to force learning of negation as separate from no-belief
                freq = defaultFreq;
            }
            Draw.colorBipolar(gl, 2f * (freq - 0.5f));
            float m = 0.5f * conf;
            Draw.rect(gl, bounds);
            if (tv.verify) {
                float error = nn.error;
                if (error != error) {
                // training phase
                // Draw.rect(gl, m / 2, m / 2, 1 - m, 1 - m);
                } else {
                // verification
                // draw backgroudn/border
                // gl.glColor3f(error, 1f - error, 0f);
                // 
                // float fontSize = 0.08f;
                // gl.glColor3f(1f, 1f, 1f);
                // Draw.text(gl, c.term().toString(), fontSize, m / 2, 1f - m / 2, 0);
                // Draw.text(gl, "err=" + n2(error), fontSize, m / 2, m / 2, 0);
                }
            }
        }
    }).toArray(Surface[]::new)));
    final int[] frames = { 0 };
    onFrame(() -> {
        if (frames[0]++ % imagePeriod == 0) {
            nextImage();
        }
        redraw();
        // if (neural.get()) {
        // if (nar.time() < trainFrames) {
        outs.expect(image);
        if (neural.get()) {
            train.update(mlpLearn, mlpSupport);
        }
        p.update();
    // s.update();
    });
    return g;
}
Also used : GL2(com.jogamp.opengl.GL2) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) Tense(nars.time.Tense) BeliefTableChart(nars.gui.BeliefTableChart) Bitmap2DSensor(nars.util.signal.Bitmap2DSensor) nars(nars) Rectangle2D(java.awt.geom.Rectangle2D) Surface(spacegraph.space2d.Surface) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AspectAlign(spacegraph.space2d.container.AspectAlign) Draw(spacegraph.video.Draw) MLPMap(jcog.learn.MLPMap) Truth(nars.truth.Truth) Lists(com.google.common.collect.Lists) IntToFloatFunction(org.eclipse.collections.api.block.function.primitive.IntToFloatFunction) Gridding(spacegraph.space2d.container.Gridding) Plot2D(spacegraph.space2d.widget.meter.Plot2D) TaskConcept(nars.concept.TaskConcept) Concept(nars.concept.Concept) CameraSensorView(nars.video.CameraSensorView) Term(nars.term.Term) IntFunction(java.util.function.IntFunction) BufferedImage(java.awt.image.BufferedImage) Collection(java.util.Collection) PixelBag(nars.video.PixelBag) Util(jcog.Util) java.awt(java.awt) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Scale(nars.video.Scale) SpaceGraph(spacegraph.SpaceGraph) BELIEF(nars.Op.BELIEF) Termed(nars.term.Termed) TaskConcept(nars.concept.TaskConcept) Concept(nars.concept.Concept) GL2(com.jogamp.opengl.GL2) CameraSensorView(nars.video.CameraSensorView) Gridding(spacegraph.space2d.container.Gridding) AspectAlign(spacegraph.space2d.container.AspectAlign) Plot2D(spacegraph.space2d.widget.meter.Plot2D) Truth(nars.truth.Truth)

Aggregations

Gridding (spacegraph.space2d.container.Gridding)3 Plot2D (spacegraph.space2d.widget.meter.Plot2D)3 List (java.util.List)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Util (jcog.Util)2 Surface (spacegraph.space2d.Surface)2 Draw (spacegraph.video.Draw)2 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 GL2 (com.jogamp.opengl.GL2)1 java.awt (java.awt)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 Collection (java.util.Collection)1 IntFunction (java.util.function.IntFunction)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 ListTopic (jcog.event.ListTopic)1 Topic (jcog.event.Topic)1 Loop (jcog.exe.Loop)1