Search in sources :

Example 1 with FftGenerator

use of com.serotonin.m2m2.view.quantize2.FftGenerator in project ma-modules-public by infiniteautomation.

the class PointValueFftCalculator method calculate.

/**
 * Generate FFT
 * @return
 */
public FftGenerator calculate(DateTime from, DateTime to) {
    PointValueDao pvd = Common.databaseProxy.newPointValueDao();
    long count = pvd.dateRangeCount(vo.getId(), from.getMillis(), to.getMillis());
    final FftGenerator generator = new FftGenerator(count);
    // Make the call to get the data and quantize it
    pvd.getPointValuesBetween(vo.getId(), from.getMillis(), to.getMillis(), new MappedRowCallback<PointValueTime>() {

        @Override
        public void row(PointValueTime pvt, int row) {
            generator.data(pvt);
        }
    });
    generator.done(getEndValue());
    return generator;
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) FftGenerator(com.serotonin.m2m2.view.quantize2.FftGenerator)

Example 2 with FftGenerator

use of com.serotonin.m2m2.view.quantize2.FftGenerator in project ma-modules-public by infiniteautomation.

the class PointValueFftCalculator method streamData.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeStream#streamData(java.io.Writer)
	 */
@Override
public void streamData(JsonGenerator jgen) {
    this.setupDates();
    DateTime startTime = new DateTime(from);
    DateTime endTime = new DateTime(to);
    FftGenerator generator = this.calculate(startTime, endTime);
    double[] fftData = generator.getValues();
    double sampleRateHz = 1000d / generator.getAverageSamplePeriodMs();
    double dataLength = (double) fftData.length;
    // Output The Real Steady State Mangitude
    try {
        jgen.writeStartObject();
        // Amplitude
        jgen.writeNumberField("value", fftData[0]);
        double frequency = 0;
        jgen.writeNumberField("frequency", frequency);
        jgen.writeEndObject();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    double realComponent, imaginaryComponent, frequency;
    if (fftData.length % 2 == 0) {
        for (int i = 2; i < fftData.length / 2; i++) {
            try {
                realComponent = fftData[i * 2];
                imaginaryComponent = fftData[2 * i + 1];
                Complex c = new Complex(realComponent, imaginaryComponent);
                jgen.writeStartObject();
                // Amplitude
                jgen.writeNumberField("value", c.abs());
                if (this.returnFrequency)
                    frequency = (double) i * sampleRateHz / dataLength;
                else
                    frequency = 1d / ((double) i * sampleRateHz / dataLength);
                jgen.writeNumberField("frequency", frequency);
                jgen.writeEndObject();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        for (int i = 2; i < (fftData.length - 1) / 2; i++) {
            try {
                realComponent = fftData[i * 2];
                imaginaryComponent = fftData[2 * i + 1];
                Complex c = new Complex(realComponent, imaginaryComponent);
                jgen.writeStartObject();
                // Amplitude
                jgen.writeNumberField("value", c.abs());
                if (this.returnFrequency)
                    frequency = (double) i * sampleRateHz / dataLength;
                else
                    frequency = 1d / ((double) i * sampleRateHz / dataLength);
                jgen.writeNumberField("frequency", frequency);
                jgen.writeEndObject();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // Write the last value out as it isn't in order in the array
        try {
            realComponent = fftData[fftData.length / 2];
            imaginaryComponent = fftData[1];
            Complex c = new Complex(realComponent, imaginaryComponent);
            jgen.writeStartObject();
            // Amplitude
            jgen.writeNumberField("value", c.abs());
            if (this.returnFrequency)
                frequency = (double) (((fftData.length - 1) / 2) - 1) * sampleRateHz / dataLength;
            else
                frequency = 1d / ((double) (((fftData.length - 1) / 2) - 1) * sampleRateHz / dataLength);
            jgen.writeNumberField("frequency", frequency);
            jgen.writeEndObject();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : FftGenerator(com.serotonin.m2m2.view.quantize2.FftGenerator) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) Complex(org.apache.commons.math3.complex.Complex)

Aggregations

FftGenerator (com.serotonin.m2m2.view.quantize2.FftGenerator)2 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 IOException (java.io.IOException)1 Complex (org.apache.commons.math3.complex.Complex)1 DateTime (org.joda.time.DateTime)1