E04 Adding float signals

In the first article (Creating a simple reader) we already handled float signals. This in-depth article shows the full interface and how to use. The article uses the ExampleFloatReader class in the Extension Toolkit.

Float Signals

Impulse supports 32bit (float), 64bit (double) floating point data and arrays of float/double (dimension > 1).

    public final int FLOAT_WIDTH_32 = 0x01; // 4 bytes
    public final int FLOAT_WIDTH_64 = 0x02; // 8 bytes

Float data without limited precision (BigDecimal) is under discussion to be added.

Interface

Now lets get to the writer interface:

public interface IFloatSamplesWriter extends ISamplesWriter {

    boolean write(long units, double value);
    boolean write(long units, boolean conflict, float value);
    boolean write(long units, boolean conflict, double value);
    boolean write(long units, boolean conflict, BigDecimal value);
 	boolean write(long units, boolean conflict, float[] value);
	boolean write(long units, boolean conflict, double[] value);

    // scripting
    boolean writeFloat(long units, boolean conflict, float value);
    boolean writeDouble(long units, boolean conflict, double value);
    boolean writeBig(long units, boolean conflict, BigDecimal value);
 	boolean writeFloatArray(long units, boolean conflict, float[] value);
	boolean writeDoubleArray(long units, boolean conflict, double[] value);
 	boolean writeFloatArgs(long units, boolean conflict, float... value);
	boolean writeDoubleArgs(long units, boolean conflict, double... value);
}	

The parameters have the following meaning:

units
No of domain units (e.g. ns)
conflict
Define this sample as a conflict one.
value
The given value in different formats (float/double/BigDecimal)

Creating

Now look at these examples (out of de.toem.impulse.serializer.ExampleFloatReaderReader):

       Scope system = addScope(null, "System");  // null means at root
        Signal float1 = addSignal(system, "Signal1", "My first float Signal", ProcessType.Discrete, 
           SignalType.Float, SignalDescriptor.Float64);
        Signal float2 = addSignal(system, "Signal2", "My second float Signal", ProcessType.Discrete, 
           SignalType.Float, SignalDescriptor.Float64);
        Scope another = addScope(system, "Another");
        Signal float3 = addSignal(another, "Signal3", "Another float", ProcessType.Discrete, 
           SignalType.Float, SignalDescriptor.Float64);
        Signal float4 = addSignal(another, "Signal4", "Rectangle", ProcessType.Discrete,
           SignalType.Float, SignalDescriptor.Float64);
        Signal float5 = addSignal(another, "Signal5", "XY", ProcessType.Discrete, 
           SignalType.Float, new SignalDescriptor(SignalDescriptor.CONTENT_DEFAULT,2,
           ISample.FLOAT_WIDTH_64,ISample.FORMAT_DEFAULT));

To create a float signal, you need to set the signal type SignalType.Float and the signal descriptor with the precision ( e.g. SignalDescriptor.Float64).

Here some examples how to add samples.

       // add array member informations (dimension > 1)
        getWriter(float5).addMember("X", null, ISample.FORMAT_DEFAULT);
        getWriter(float5).addMember("Y", null, ISample.FORMAT_DEFAULT);
        
        // And continue until 1000 ns
        for (; t < 100000; t += 10) {
            
            // write the sin and cos data
            ((IFloatSamplesWriter) getWriter(float1)).write(t, Math.sin((t + 100) / 3000.0) * 10.0);
            ((IFloatSamplesWriter) getWriter(float2)).write(t, Math.cos((t + 200) / 1000.0) * 10.0
               +Math.cos((t + 400) / 100.0) * 3.0+Math.signum(Math.cos((t + 400) / 50.0)) * 1.0
               +Math.cos((t + 100) / 10.0) * 2.0+Math.random()*2.5);
            ((IFloatSamplesWriter) getWriter(float3)).write(t, 
               Math.sin((t + 300) / 6000.0) * 10.0 + 10.00001);
            ((IFloatSamplesWriter) getWriter(float4)).write(t, 
               Math.signum(Math.cos((t + 400) / 500.0)) * 1.0);
            double f =  Math.sin(t  / 30000.0) * 1;
            ((IFloatSamplesWriter) getWriter(float5)).write(t, false, 
               new double[]{Math.sin((t + 100) / 3000.0) * 10.0,
               Math.cos((t + 100) / 3000.0) * 10.0 * f});
        }

Result