3. Module reference

The Bitis modules defines one class realizing a BTS signal with a set of methods implementing several unary or binary operators over the signal. There are also some functions for data interface.

3.1. Objects and methods

class bitis.Signal(start=None, edges=None, end=None, slevel=0, tscale=1.0)

Implements the concept of “Binary Timed Signal”: a memory representation of a binary signal as sequence of the times of signal edges (signal changes). start sets the signal start time. edges can be used to initialize the signal edges sequence, it must be a list of times (integers or floats). May be empty. The signal level before the first change is specified by slevel. Also a time scale factor can be specified by tscale, at present not used.

__add__(other)

Concatenate (join) other to self.

__and__(other)

Compute the logic and of two given signal objects: self and other. Return a signal object with the and of the two input signals. Can be used as the bitwise and operator as in the following example (signal a,b,c are instances of the Signal class):

signal_c = signal_a & signal_b
__eq__(other)

Equality test between two signals. Return True if the two signals are equal. Otherwise, return False. Can be used as the equality operator as in the following example (signal a,b are instances of the Signal class):

if signal_a == signal_b:
    print 'signal a and b are equal' 
__invert__(inplace=False)

Compute the logic not of the given signal object: self. If inplace is false, return the result as a new signal object. Otherwise, return the result as self. Can be used as the bitwise not operator as in the following example (signal a,b are instances of the Signal class):

signal_b = ~ signal_a
__len__()

Return the length of the change times sequence.

__ne__(other)

Inequality test between two signals. Return True if the two signals are not equal. Otherwise, return False. Can be used as the inequality operator as in the following example (signal a,b are instances of the Signal class):

if signal_a != signal_b:
    print 'signal a and b are different'
__nonzero__()

Return true if the signal is not void, return false otherwise.

__or__(other)

Compute the logic or of two given signal objects: self and other. Return a signal object with the or of the two input signals. Can be used as the bitwise or operator as in the following example (signal a,b,c are instances of the Signal class):

signal_c = signal_a | signal-b
__xor__(other)

Compute the logic xor of two given signal objects: self and signal. Return a signal object with the xor of the two input signals. Can be used as the bitwise xor operator as in the following example (signal a,b,c are instances of the Signal class):

signal_c = signal_a ^ signal_b
append(other)

Return the self signal modified by appending other signal to it. If there is a time gap between the signals, fill it. The start time of other must be greater or equal to end time of self. The end level of self must be equal to the start level of other. Otherwise, no append is done.

chop(period, origin=None, max_chops=1000)

Divide the signal into several time contiguous signals with the same elapse time equal to period. The dividing times sequence starts at origin and has an element every period time, except for the last element. It has end time = period * max_chops, if max_chops is reached. Otherwise, it has the end time of the chopped signal. Return a list with the chopped signals. If origin is before the signal start, it is moved forward by an integer times of period, until it falls into the signal domain. If origin is none, it is set to self start time by default. If origin is after the signal end, no chop occours, an empty list is returned. If self is void, return an empty chop list.

clone()

Return a deep copy with the same attributes/values of signal object.

clone_into(other)

Return a full copy of signal object into other. Each other attribute is assigned a deep copy of the value of the same attribute in signal object. Return other.

correlation(other, mask=None, step_size=1.0, skip=0, width=None, normalize=False)

Return the correlation function of two given signal objects: self and other.

mask: signal or None, same elapse of other. Compute correlation only where mask == 1. If None, compute correlation on the whole intersection of self and other.

step_size: positive float, the time pitch of the correlation function.

skip: positive float, the time elapse at the start of the correlation function not to be computed.

width: is the time elapse where to compute the correlation function. If None, compute the correlation function for each time shift of self that has an intersection with other, having an elapse time >= step_size.

normalize: boolean, controls the values of the correlation function. If True, values are normalized in the range -1 +1. If False, values are absolute: the integral of xor between shifted self and other signals.

Return pattern ( corr, shift )

corr: list of floats. The values of the correlation function.

shift: list of floats. The time shift applied to self to slide it over other signal for each value of corr.

elapse()

Ruturn the signal elapse time: end time - start time. If self is void, return zero.

end_level()

Return the logic level at the end of a signal object.

integral(level=1, normalize=False)

Return the integral of a signal object: the elapsed time of all periods in which the signal is at the level specified by level. Output can be absolute (normalize=False) or can be normalized (normalize=True): absolute integral averaged over the whole signal domain. The summation is operated on the signal domain only. If self is void, return none.

jitter(stddev=0)

Add a gaussian jitter to the change times of self signal object with the given standard deviation stddev and zero mean. Signal start and end times are unchanged.

join(other, inplace=False)

Join two signals (self and other) in one signal. End time of self must be less or equal to start time of other. If there is a time gap between the joining signals, fill it. Return a signal object with the join result. If inplace is false, a new signal object is returned. If inplace is true, the join result is put into self and self is returned. Signals with different levels at self end and at other start cannot be joined (join return a void signal).

level(time, tpos=0)

Return the logic level and the number of edges of a signal object at a given time (time) and a given edge position where to start searching (tpos). The edge position is the number of signal edges before time. When time is equal to an edge time that edge is considered before time. time must be in the signal time domain, otherwise None is returned. tpos must point to an edge before time.

newer(split, inplace=False)

Split self into two signals at time split and return the part after split time. If split is equal to a signal change time, the change is put into the return signal. If split is at or before signal start, return self. If split is at or after signal end, return the void signal. If inplace is false, a new signal object is returned. If inplace is true, self is changed to the newer part and returned. If self is void, the void signal is returned.

older(split, inplace=False)

Split self into two signals at time split and return the part before split time. If split is equal to a signal change time, the change is not put into the return signal. If split is at or before signal start, return the void signal. If split is at or after signal end, return self. If inplace is false, a new signal object is returned. If inplace is true, self is changed to the older part and returned. If self is void, the void signal is returned.

phase(other, mask, resolutions, period=None)

Find the phase between self and other. Phase is the time shift that applied to self gives the maximum correlation: self (t + phase) * other (t) is maximum (* means correlation). For faster computation, the phase can be computed by progressive smaller resolutions.

mask: signal or None, same elapse of other. Compute correlation only where mask == 1. If None, compute correlation on the whole intersection of self and other.

resolutions: tuple or list of positive float, at least one element, sequence of resolutions from coarser to finest, the time step used in the computation of correlation.

period: None or positive float. If None, phase is computed as absolute time shift. If float, phase is the time shift with respect to nearest integer multiple of period, its range is - period/2. <= phase < + period/2..

Return pattern ( phase, corr_phase, corrs, shifts )

phase: float, the computed phase.

corr_phase: float, the correlation function value at phase shift.

corrs: list of lists of floats, positive. For each resolution value specified in resolutions, the unnormalized values of the correlation function.

shifts: list of lists of floats. The time shift values corresponding to the correlation function values in corrs.

plot(*args, **kargs)

Graphic plot of signal self as square wave. Requires Matplotlib. *args and **kargs are passed on to matplotlib functions.

plotchar(charnum, origin=None, end=None, max_flat=None)

Semigraphic plot of signal self with unicode line drawing characters (U+25xx). Require locale setting.

charnum: integer, the maximum length of the string of the rendering characters.

origin: float, the rendering start time. If None, start is set to the signal start time.

end: float, the rendering end time. If None, end is set to the signal end time.

max_flat: integer, the maximum number of consecutive horizontal line characters. When reached, no more horizontal chars are added and a lower case ‘x’ char is put in the middle of this sequence to mark the character drop. If None, compression is disbled.

Return pattern ( topchars, botchars )

topchars: utf-8 encoded string. The top row of unicode characters rendering the semigraphic plot.

botchars: the same as topchars, but for the bottom row.

reverse(inplace=False)

Reverse the signal change times sequence: last change becomes the first and viceversa. Time intervals between edges are preserved. If inplace is false, return the result as a new signal object. Otherwise, return the result as self.

shift(offset, inplace=False)

Add offset to signal start and end times and to each signal change time. If inplace is false, return the result as a new signal object. Otherwise, return the result as self.

split(split, inplace=False)

Split self into two signals at time split.

Return pattern ( older, newer )

older: signal, the part of self from start time to split time.

newer: signal, the part of self from split time to signal end.

If split is equal to a signal change time, the change is put into the newer part. If split is at or before signal start, older is the void signal and newer is self. If split is at or after signal end, older is self and newer is the void signal. If inplace is false, newer is returned as a new signal object. If inplace is true, self is changed to newer and returned as newer. If self is void, both older and newer are the void signal.

stream(other, elapse, buf_step=1.0)

Append other signal to self signal. If self signal elapse time becomes greater than elapse, delete from the older part of self until its elapse time is less or equal than elapse. The elapsed time fo the deleted part is forced to a integer multiple of buf_step.

validate()

Validate signal attributes. Complete type and value checking of signal object attributes. If a check fails, an exception is raised.

3.2. Functions

bitis.bin2pwm(bincode, elapse_0, elapse_1, period, active=1, origin=0, tscale=1.0)

Convert a binary code into a pulse width modulation signal in BTS format. Return a Signal class object. bincode is a tuple or a list of tuples: (bit_length, bits). bit_length is an integer with the number of bits. bits is an integer or a long integer with the binary code. First bit is the LSB, last bit is the MSB. period is the period of pwm pulses. elapse_0 is the elapse time of a pulse coding a 0 bit. elapse_1, the same for a 1 bit. active is the active pulse level. origin is the time of the leading edge of the first signal pulse.

bitis.pwm2bin(pwm, elapse_0, elapse_1, period=None, active=1, origin=0, threshold=0.2)

Convert a pulse width modulation signal in BTS format to binary code. Return a tuple: see bincode in bin2pwm. pwm is the signal to decode. For the other arguments see bin2pwm. If period is not defined, conversion is done by testing only the active pulse level elapse against a threshold computed as mean of elapse_0 and elapse_1. No check is done on pulse period and decoding consider every pulse. If period is set to the modulation pulse period, conversion is done by synchronous symbols correlation. The signal is chopped with the given period, starting from origin, start of symbol periods, until signal end. Each signal chop, corresponding to one symbol time, is correlated with both models of 0 and 1 pulses. The better value above threshold is taken as result.

bitis.code2mod(code, symbols, origin=0, tscale=1.0)

Modulate a code sequence into a modulation signal in BTS format. For each number in code, the symbol in symbols with index equal to number is appended to the modulation signal. Return a Signal class object. code is list of integer. symbols is a list of signal objects, one for each coding symbol. origin is the start time of the first coded symbol.

bitis.mod2code(mod, symbols, mask=None, origin=None, tscale=1.0)

Demodulate a modulation signal in BTS format by maximal correlation symbol estimation. Return the demodulated code sequence (list of int), the corresponding normalized correlation, list of float) of all symbols and the time where the demodulation ends. symbols is a list of signal objects, one for each coding symbol. The symbols start time is assumed as phase difference with respect to the signal start time. mask is a signal objects. Symbol correlation is computed only where mask = 1. origin is the start time of the first coded symbol. If not defined, it is set to start time of mod. All symbols must have the same elapse time that is the symbol period. The same holds for mask.

bitis.serial_tx(chars, times, char_bits=8, parity='off', stop_bits=2, baud=50, tscale=1.0)

Simulate a serial asynchronous transmitting interface. Return a BTS signal with the serial line pulses coding a given list of characters, according to the following serial parameters. The list of chars is the input to the serial transmitter. times is the list of the start bit rising edge time of each char in chars. If times are too fast with respect to the current baud rate, a char fifo behavoiur is activated. char_bits is the character size in bits (5,6,7,8). parity is the parity bit even, odd or off (parity absent). stop_bits is the number of stop bits (1,2). baud is the serial line speed, any positive value is allowed. The serial line is assumed active high.

bitis.serial_rx(sline, char_bits=8, parity='off', stop_bits=2, baud=50)

Simulate a serial asynchronous receiving interface. Return a list of the received characters, a list of their start times and a list of their status: 0 = ok, 1 = parity error. sline is a BTS signal with the serial line pulses coding the characters to be received. For the keyword arguments see serial_tx. The serial line pulses are sampled at the given baud rate like a real asynchronous serial interface.

bitis.noise(start, origin, end, period_mean=1, period_stddev=1, width_mean=1, width_stddev=1, active='random')

Return a signal object with random pulses. start is the noise signal start. origin is the time of the first pulse trailing edge. end is the signal end time. Pulses period and width follow a gaussian distribution: period_mean and period_stddev are the given mean and standard deviation of pulses period, width_mean and width_stddev are the given mean and standard deviation of the pulse width at 1 level. active is the active pulse level, can be 0,1,’random’.

bitis.square(start, origin, end, period, width, active=1)

Return a signal object with a square wave with constant period and constant duty cycle. start is the start time. origin is the time of the first pulse trailing edge. end is the signal end time. period is the pulse period. width is the pulse width at active level.

bitis.test()

Return a signal object with a test signal. The signal has a sequence of primes as edges timing.

Table Of Contents

Previous topic

2. Usage examples

Next topic

4. BTS format

This Page