Search code examples
pythonbinaryintegersignal-processingsigned

Bitplane decomposition of 16 bit two's complement signed integer signal data?


I am trying to do bit plane decomposition of a 16 bit two's complement signed integer signal data(electrocardiogram) in python so i will get 16 signal data bit plane. I know how to decompose an 8 bit unsigned integer image signal, and i reimplement the code in this problem. I thought that i am supposed to get bit plane data with its values containing negative because it is originaly a 16 bit signed integer, but i got resulting 16 bit unsigned integer signal not 16 bit signed integer signal.
Here's my code:

import numpy as np
def intToTcbin16(value):
    return format(value % (1 << 16), '016b')
def Tcbin16ToInt(bin):
    while len(bin)<16 :
            bin = '0'+bin
    if bin[0] == '0':
            return int(bin, 2)
    else:
            return -1 * (int(''.join('1' if x == '0' else '0' for x in bin), 2) + 1)
def bitplanedecomposesignal(ecgdat):
    lst = []
    for j in range(len(ecgdat)):
        lst.append(intToTcbin16(ecgdat[j]))
    sixteen = (np.array([Tcbin16ToInt(i[0]) for i in lst],dtype = np.int16)*32768)
    fiveteen = (np.array([Tcbin16ToInt(i[1]) for i in lst],dtype = np.int16)*16384)
    fourteen = (np.array([Tcbin16ToInt(i[2]) for i in lst],dtype = np.int16)*8192)
    thirteen = (np.array([Tcbin16ToInt(i[3]) for i in lst],dtype = np.int16)*4096)
    twelve = (np.array([Tcbin16ToInt(i[4]) for i in lst],dtype = np.int16)*2048)
    eleven = (np.array([Tcbin16ToInt(i[5]) for i in lst],dtype = np.int16)*1024)
    ten = (np.array([Tcbin16ToInt(i[6]) for i in lst],dtype = np.int16)*512)
    nine = (np.array([Tcbin16ToInt(i[7]) for i in lst],dtype = np.int16)*256)
    eight = (np.array([Tcbin16ToInt(i[8]) for i in lst],dtype = np.int16)*128)
    seven = (np.array([Tcbin16ToInt(i[9]) for i in lst],dtype = np.int16)*64)
    six = (np.array([Tcbin16ToInt(i[10]) for i in lst],dtype = np.int16)*32)
    five = (np.array([Tcbin16ToInt(i[11]) for i in lst],dtype = np.int16)*16)
    four = (np.array([Tcbin16ToInt(i[12]) for i in lst],dtype = np.int16)*8)
    three = (np.array([Tcbin16ToInt(i[13]) for i in lst],dtype = np.int16)*4)
    two = (np.array([Tcbin16ToInt(i[14]) for i in lst],dtype = np.int16)*2)
    one = (np.array([Tcbin16ToInt(i[15]) for i in lst],dtype = np.int16)*1)
    return sixteen,fiveteen,fourteen,thirteen,twelve,eleven,ten,nine,eight,seven,six,five,four,three,two,one

here is the signal plot before decomposition: beforedecomposition

for example, here is the 16th bitplane signal plot after decomposition: enter image description here

What did i do wrong?how to do it right?and how to recompose it back?


Solution

  • In the sixteen line, change the 32768 to -32768. Everything else looks right.

    Like you said, the planes of the existing bitplanedecomposesignal() code reconstruct the value as if it were unsigned 16-bit data rather than signed. However, if the most significant bit is on, then the represented value is negative, and we should subtract 2^16 = 65536 from the unsigned value. So the most significant bit should contribute 32768 - 65536 = -32768 rather than +32768.

    Example:

    value = −32700 decimal
          = 1000000001000100 binary int16
            ↑        ↑   ↑
           −2^15   2^6   2^2
    
           −2^15 + 2^6 + 2^2 = −32700 decimal = value
    

    Side comment: Numpy has good efficient bitwise functions that you might find useful. I'd consider using np.bitwise_and to extract bitplanes.