05-05-2014 11:42 AM
I am looking for an efficient way of converting a digital waveform to the bit stream that is being sent.
I have attempted using the following but run into out of memory problems for large samples (> 4M):
static byte[] GetByteStream(NationalInstruments.DigitalWaveform[] waveforms)
{
int samplePerChannel = (int)(waveforms[0].Samples.Count);
byte[] bytes = new byte[samplePerChannel];
BitArray bits = new BitArray(waveforms.Length);
for (int count = 0; count < samplesPerChannel; count++)
{
int bitCounter = 0;
foreach (DigitalWaveform waveform in waveforms)
{
bits[bitCounter] = (waveform.Samples[count].States[0] == DigitalState.ForceUp);
bitCounter++;
}
bits.CopyTo(bytes, count);
}
return bytes;
}
The other problem is it takes a long time.
Is there a native measurement studio function that can easily do this? In LabVIEW I would so this:
I am aware of using: uint[,] data = reader.ReadMultiSamplePortUInt32(-1); which is the data format that I'm needing. However, I also want to save the waveform as a TDMS file which means I need to use aquire the waveform not just the digital data.
Thanks.
05-06-2014 11:20 AM
I haven't tried out this code and I'm more of a C++ guy than a C# guy, but a couple of thoughts on performance strike me right off the bat.
First, you are passing in the array of waveforms in the function header. Your setup has them being passed by value, which will create copies for each function call, which will bloat the memory footprint of your waveforms. Try passing by reference and operating on the waveform directly.
Second, each function call performs a new allocation on a unknown length, being the length of your waveform. Assuming you leave the deallocation to the garbage collector, this can result in frequent allocations/deallocations. It may even result in fragmentation which will slow the memory manager as blocks get reorganized or causing out of memory error when the reported space available is still quite high. Try creating a permanent data space for manipulation that persists between function calls.