Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert BreakoutAnalogOutput data to offset binary #213

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions OpenEphys.Onix1/BreakoutAnalogOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class BreakoutAnalogOutput : Sink<Mat>
/// </summary>
/// <param name="source"> A sequence of 12xN sample matrices containing the analog data to write to channels 0 to 11.</param>
/// <returns> A sequence of 12xN sample matrices containing the analog data that were written to channels 0 to 11.</returns>
public override IObservable<Mat> Process(IObservable<Mat> source)
public override unsafe IObservable<Mat> Process(IObservable<Mat> source)
{
var dataType = DataType;
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
Expand Down Expand Up @@ -87,7 +87,14 @@ public override IObservable<Mat> Process(IObservable<Mat> source)
}

var dataSize = outputBuffer.Step * outputBuffer.Rows;
device.Write(outputBuffer.Data, dataSize);

// twos-complement to offset binary
var dataPtr = (short *)outputBuffer.Data.ToPointer();
const short Mask = -32768;
for (int i = 0; i < dataSize / sizeof(short); i++)
*(dataPtr + i) ^= Mask;

device.Write(outputBuffer.Data, dataSize);
});
});
}
Expand All @@ -108,6 +115,12 @@ public IObservable<short[]> Process(IObservable<short[]> source)
return source.Do(data =>
{
AssertChannelCount(data.Length);
var samples = new ushort[data.Length];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is potentially an opencv operation that would vectorize this. cv.xors is what we should look into.

for (int i = 0; i < samples.Length; i++)
{
const short Mask = -32768;
data[i] ^= Mask; // twos-complement to offset binary
}
device.Write(data);
});
});
Expand All @@ -130,10 +143,10 @@ public IObservable<float[]> Process(IObservable<float[]> source)
return source.Do(data =>
{
AssertChannelCount(data.Length);
var samples = new short[data.Length];
var samples = new ushort[data.Length];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = (short)(data[i] * divisionsPerVolt);
samples[i] = (ushort)(data[i] * divisionsPerVolt + BreakoutAnalogIO.DacMidScale);
}

device.Write(samples);
Expand Down
1 change: 1 addition & 0 deletions OpenEphys.Onix1/ConfigureBreakoutAnalogIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static class BreakoutAnalogIO
// constants
public const int ChannelCount = 12;
public const int NumberOfDivisions = 1 << 16;
public const int DacMidScale = 1 << 15;

// managed registers
public const uint ENABLE = 0;
Expand Down