DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

separating 16 bits of data into one 12 bit and one 4 bit channel

I have 16 bits of binary data and i'm trying to split these into one 12 bit and one 4 bit channel. I know that I could use something like:

Dim Channel1 : Set Channel1 = Block.Channels.Add("Channel1", eI16)
Channel1.Formatter.Bitmask = 4,095 'xxxx 1111 1111 1111


but, i'm not sure how to grab those 4 bits out of the 16 bit channel1 and make channel2. It's like i almost need to say something like:

Dim Channel2 : Set Channel2 = Block.Channels.Add("Channel2", eI16)
Channel2 = Channel1.Formatter.Bitmask = 61,440 '1111 xxxx xxxx xxxx

Can anyone help out???? thanks.
0 Kudos
Message 1 of 22
(5,924 Views)
Here is how you can extract the two channels :

' make sure you are at the right position in the file
Set Block = File.GetBinaryBlock()
Dim Channel1 : Set Channel1 = Block.Channels.Add("Channel1", eI16)
Channel1.Formatter.Bitmask = 4,095 'xxxx 1111 1111 1111
' factor is 1 because you refer to the lower bits.
' You can ommit this step because 1 is the default
Channel1.Factor = 1
' Now get a new binary block. This block will refer to the same
' position in the file like th eblock above.
' This will allow you to define a second channel which refers to
' the exact same bytes on the file, but with a different bitmask
' and scaling.
' If you would use the above binary block, then this block would
' contain two channels which would result in a different layout
' of the data read from disk
Set Block = File.GetBinaryBlock()
Dim Channel2 : Set Channel2 = Block.Channels.Add("Channel2", eI16)
Channel2.Formatter.Bitmask = 61,440 '1111 xxxx xxxx xxxx
Channel2.Factor = 1./CDbl(2^12)
' dont forget to add the channels to the channel group !
oChannelGroup.Channels.AddDirectAccessChannel(oDChannel1)
oChannelGroup.Channels.AddDirectAccessChannel(oDChannel2)

Let me know if this works

Andreas
0 Kudos
Message 2 of 22
(5,892 Views)
Hey Andreas, Thanks for the help. That works great for 16 bits of data! Now, here's a twist. Say I have 8 Bytes of data and:

Channel(1) = eU16
Channel(2) = eByte
Channel(3) = eU16 'at this point, this channel needs to be broken up into 12 bits and 4 bits. xxxx 1111 1111 1111
Channel(4) = 'the 4 MSB bits of Channel(3). 1111 xxxx xxxx xxxx
Channel(5) = eU16
Channel(6) = eByte

How do i grab those bits in the middle of a bunch of data??? Thanks again for all the help!!
0 Kudos
Message 3 of 22
(5,888 Views)
OK...This is still straightforward. Her is what you want to do :

CurrPos = File.Position ' remember where the block starts
Set Block = File.GetBinaryBlock()
' set up all channels which can be read directly
Dim Channel1 : Set Channel1 = Block.Channels.Add("Channel1", eU16)
Dim Channel2 : Set Channel2 = Block.Channels.Add("Channel2", eByte)
Dim ChannelPH : Set ChannelPH = Block.Channels.Add("Dummy", eU16) ' just a placeholder
Dim Channel5 : Set Channel5 = Block.Channels.Add("Channel5", eU16)
Dim Channel6 : Set Channel6 = Block.Channels.Add("Channel6", eByte)
' Now we need a block which has one channel only. This channel will be used to extract the bits.
' The block needs to start where the third schannel of the above block starts :

File.Position = CurrPos + 2+1 ' 2 for the U16 channel, 1 for the byte channel
Set Block = File.GetBinaryBlock()
' Set the scan width. This defines the gaps between the values of a channel
Block.BlockWidth = 2+1+2+2+1 ' U16 + Byte + U16 + U16 + Byte
Dim Channel3 : Set Channel3 = Block.Channels.Add("Channel3", eU16)
' .. apply bitmask and scaling (see my last example)
' Do the same for the second channel
Set Block = File.GetBinaryBlock()
' Set the scan width. This defines the gaps between the values of a channel
Block.BlockWidth = 2+1+2+2+1 ' U16 + Byte + U16 + U16 + Byte
Dim Channel4 : Set Channel4 = Block.Channels.Add("Channel4", eU16)
.. apply bitmask and scaling (see my last example)
Do the same for the second channel


I hope this works as expected. Let me know if you need more help...

Andreas
0 Kudos
Message 4 of 22
(5,880 Views)
Ok, i'm getting an error : Variable is undefined: 'CurrPos'

Also, If I have 64 bytes of data(the same 8 channels repeating), Can i just insert the script once and have it repeat? Right now my script repeats for any size data block. Would this work? Thanks again!!!
0 Kudos
Message 5 of 22
(5,874 Views)
Hi,

It appears that you simply need to declare CurrPos using:

Dim CurrPos

You can use the same script for your data. Just remember that the script is referring to specific data channels, so those data channels should be accessed by the script. If your data channel names will change from inserting new data channels, then you need to account for that in your script.

Good Luck!

Tyler T.
0 Kudos
Message 6 of 22
(5,860 Views)
I did that, and it still gives me errors that File.Position needs an object.

Dim CurrPos
Set CurrPos = File.Position

'then in the middle of my code

Set block = File.GetBinaryBlock()
Set File.Position = CurrPos + 1 + 2 + 1 + 2 + 2 'this gets me to where my "dummy" channel is located.
Block.BlockWidth = 1 + 2 + 1 + 2 + 2 + 1 + 1 + 2 'this is the total length of my data block, before it repeats.

'then i continue setting up my channels that i want in this location.

But, it won't get past the SET CurrPos = File.Position in the beginning of my code. This is frustrating. Thanks so much for all the help guys!!! I really appreciate it!!
0 Kudos
Message 7 of 22
(5,857 Views)
Sorry, when reading the code I saw the problem immediatelly. Instead of "Set CurrPos = File.Position" it should be "CurrPos = File.Position". The position is just the offset from the beginning of the file, not an object. I didn't have a chance to try out the code, so I didn't realize that there was a typo. Let me know whether it works now.

Andreas
0 Kudos
Message 8 of 22
(5,848 Views)
Hey, thanks a lot for the help. I works great!!!

Matt
0 Kudos
Message 9 of 22
(5,833 Views)

Hi there,

I greatly appreciated the solution provided by Andreas, for I've had exactly the same problem to deal with.

There is just one thing bothering me: The setting of the file position (e.g. "File.Position = CurrPos + 2+1") and the block width (e.g. "Block.BlockWidth = 2+1+2+2+1") is pretty error-prone in my case: I've got loads of channels to be extracted bit-wise and all these constant values must be adapted whenever the structure of the data set changes.

So, is there a possibility to have calculated the offsets/positions by the script itself?

Many thanks for your help!

Regards,

Stefan

0 Kudos
Message 10 of 22
(5,380 Views)