Skip to content

Commit a72a5a2

Browse files
committed
Add data type conversions for analog input
1 parent 3064c43 commit a72a5a2

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

OpenEphys.Onix/OpenEphys.Onix/AnalogInput.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ public class AnalogInput : Source<AnalogInputDataFrame>
1616

1717
public int BufferSize { get; set; } = 100;
1818

19+
public AnalogIODataType DataType { get; set; } = AnalogIODataType.S16;
20+
1921
public unsafe override IObservable<AnalogInputDataFrame> Generate()
2022
{
2123
var bufferSize = BufferSize;
24+
var dataType = DataType;
25+
var depth = dataType == AnalogIODataType.Volts ? Depth.F32 : Depth.S16;
26+
var scale = dataType == AnalogIODataType.Volts ? AnalogIO.VoltsPerDivision : 1;
27+
var shift = 0.0;
2228
return Observable.Using(
2329
() => DeviceManager.ReserveDevice(DeviceName),
2430
disposable => disposable.Subject.SelectMany(deviceInfo =>
@@ -39,7 +45,13 @@ public unsafe override IObservable<AnalogInputDataFrame> Generate()
3945
clockBuffer[sampleIndex] = frame.Clock;
4046
if (++sampleIndex >= bufferSize)
4147
{
42-
var analogData = BufferHelper.CopyBuffer(analogDataBuffer, bufferSize, AnalogIO.ChannelCount, Depth.S16);
48+
var analogData = BufferHelper.CopyConvertBuffer(
49+
analogDataBuffer,
50+
bufferSize,
51+
AnalogIO.ChannelCount,
52+
depth,
53+
scale,
54+
shift);
4355
observer.OnNext(new AnalogInputDataFrame(clockBuffer, hubSyncCounterBuffer, analogData));
4456
hubSyncCounterBuffer = new ulong[bufferSize];
4557
clockBuffer = new ulong[bufferSize];

OpenEphys.Onix/OpenEphys.Onix/BufferHelper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,25 @@ public static Mat CopyBuffer<TBuffer>(
2121
CV.Transpose(bufferHeader, amplifierData);
2222
return amplifierData;
2323
}
24+
25+
public static Mat CopyConvertBuffer<TBuffer>(
26+
TBuffer[] buffer,
27+
int sampleCount,
28+
int channelCount,
29+
Depth depth,
30+
double scale = 1,
31+
double shift = 0)
32+
where TBuffer : unmanaged
33+
{
34+
using var bufferHeader = Mat.CreateMatHeader(
35+
buffer,
36+
channelCount,
37+
sampleCount,
38+
depth,
39+
channels: 1);
40+
var data = new Mat(bufferHeader.Rows, bufferHeader.Cols, bufferHeader.Depth, 1);
41+
CV.ConvertScale(bufferHeader, data, scale, shift);
42+
return data;
43+
}
2444
}
2545
}

OpenEphys.Onix/OpenEphys.Onix/ConfigureAnalogIO.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static class AnalogIO
162162

163163
// constants
164164
public const int ChannelCount = 12;
165+
public const double VoltsPerDivision = 0.00030517578125;
165166

166167
// managed registers
167168
public const uint ENABLE = 0;
@@ -203,4 +204,10 @@ public enum AnalogIODirection
203204
Input = 0,
204205
Output = 1
205206
}
207+
208+
public enum AnalogIODataType
209+
{
210+
S16,
211+
Volts
212+
}
206213
}

0 commit comments

Comments
 (0)