Best Python code snippet using assertpy_python
bitalloc.py
Source:bitalloc.py  
1# from inspect import getsourcefile2# import os.path as path, sys3# current_dir = path.dirname(path.abspath(getsourcefile(lambda:0)))4# sys.path.insert(0, current_dir[:current_dir.rfind(path.sep)])5# from lib.abbreviations import *6from window import *7from mdct import *8from psychoac import *9# import provided.bitalloc_sol as solution10infinity = float('inf')11negative_infinity = float('-inf')12# Test_Booleans = [True, True, True, True]13# test_uniform_bitalloc = Test_Booleans[0]14# test_constant_SNR_bitalloc = Test_Booleans[1]15# test_constant_MNR_bitalloc = Test_Booleans[2]16# test_bitalloc = Test_Booleans[3]17# Question 1.b)18def BitAllocUniform(bitBudget, maxMantBits, nBands, nLines, SMR=None):19    """20    Return a hard-coded vector that, in the case of the signal use in HW#4,21    gives the allocation of mantissa bits in each scale factor band when22    bits are uniformely distributed for the mantissas.23    """24    allocation = ones(nBands, dtype=int)25    bits_per_line = int(bitBudget / float(sum(nLines)))26    allocation = allocation * bits_per_line27    # distribute left over bits28    remaining_bits = bitBudget - (sum(allocation*nLines))29    if remaining_bits:30        line = 031        while remaining_bits > 0:32            remaining_bits -= nLines[line%nBands]33            if(remaining_bits < 0):34                break35            if (allocation[line%nBands] < maxMantBits):36                allocation[line%nBands] += 137            line += 138    # zero out 1 bit allocations (Mid-Tread)39    allocation[allocation < 2] = 040    # no overflow41    allocation[allocation > maxMantBits] = maxMantBits42    return allocation43def BitAllocConstSNR(bitBudget, maxMantBits, nBands, nLines, peakSPL):44    """45    Return a hard-coded vector that, in the case of the signal use in HW#4,46    gives the allocation of mantissa bits in each scale factor band when47    bits are distributed for the mantissas to try and keep a constant48    quantization noise floor (assuming a noise floor 6 dB per bit below49    the peak SPL line in the scale factor band).50    """51    allocation = zeros(nBands, dtype=int)52    remaining_bits = bitBudget53    noise_floor = peakSPL * ones(nBands)54    dB_per_bit = 6.055    while remaining_bits > 0:56        max_smr_band = noise_floor.argmax()57        if allocation[max_smr_band] < maxMantBits and (remaining_bits - nLines[max_smr_band]) >= 0:58            allocation[max_smr_band] += 159            remaining_bits -= nLines[max_smr_band]60        noise_floor[max_smr_band] -= dB_per_bit61    # zero out 1 bit allocations (Mid-Tread)62    allocation[allocation < 2] = 063    # no overflow64    allocation[allocation > maxMantBits] = maxMantBits65    return allocation66def BitAllocConstMNR(bitBudget, maxMantBits, nBands, nLines, SMR):67    """68    Return a hard-coded vector that, in the case of the signal use in HW#4,69    gives the allocation of mantissa bits in each scale factor band when70    bits are distributed for the mantissas to try and keep the quantization71    noise floor a constant distance below (or above, if bit starved) the72    masked threshold curve (assuming a quantization noise floor 6 dB per73    bit below the peak SPL line in the scale factor band).74    """75    allocation = zeros_like(nLines, dtype=int)76    remaining_bits = bitBudget77    dB_per_bit = 6.078    noise_floor = SMR.copy()79    while remaining_bits > 0:80        max_smr_band = argmax(noise_floor)81        if allocation[max_smr_band] < maxMantBits and (remaining_bits - nLines[max_smr_band]) >= 0:82            allocation[max_smr_band] += 183            remaining_bits -= nLines[max_smr_band]84        noise_floor[max_smr_band] -= dB_per_bit85    # zero out 1 bit allocations (Mid-Tread)86    allocation[allocation < 2] = 087    # no overflow88    allocation[allocation > maxMantBits] = maxMantBits89    return allocation90# Question 1.c)91def BitAlloc(bitBudget, maxMantBits, nBands, nLines, SMR):92    """93    Allocates bits to scale factor bands so as to flatten the NMR across the spectrum94       Arguments:95           bitBudget is total number of mantissa bits to allocate96           maxMantBits is max mantissa bits that can be allocated per line97           nBands is total number of scale factor bands98           nLines[nBands] is number of lines in each scale factor band99           SMR[nBands] is signal-to-mask ratio in each scale factor band100        Return:101            bits[nBands] is number of bits allocated to each scale factor band102        Logic:103           Maximizing SMR over block gives optimization result that:104               R(i) = P/N + (1 bit/ 6 dB) * (SMR[i] - avgSMR)105           where P is the pool of bits for mantissas and N is number of bands106           This result needs to be adjusted if any R(i) goes below 2 (in which107           case we set R(i)=0) or if any R(i) goes above maxMantBits (in108           which case we set R(i)=maxMantBits).  (Note: 1 Mantissa bit is109           equivalent to 0 mantissa bits when you are using a midtread quantizer.)110           We will not bother to worry about slight variations in bit budget due111           rounding of the above equation to integer values of R(i).112    """113    allocation = zeros(nBands, dtype=int)114    noise_floor = SMR.copy()115    dB_per_bit = 6.0116    remaining_bits = bitBudget117    optimal = False118    if optimal: # TODO: Fix this119        allocation[allocation >= 2] = array(remaining_bits / sum(nLines[allocation >= 2]) + (1.0 / dB_per_bit) * (SMR[allocation >= 2] - sum(nLines[allocation >= 2] * SMR[allocation >= 2]) / sum(nLines[allocation >= 2])), dtype=int)120        # zero out 1 bit allocations (Mid-Tread)121        allocation[allocation < 2] = 0122        # no overflow123        allocation[allocation > maxMantBits] = maxMantBits124    else:125        counter = 0126        while remaining_bits > 0:127            if (noise_floor==negative_infinity * ones(nBands)).all():128                break129            max_smr_band = argmax(noise_floor)130            if allocation[max_smr_band] < maxMantBits and (remaining_bits - nLines[max_smr_band]) >= 0:131                allocation[max_smr_band] += 1132                remaining_bits -= nLines[max_smr_band]133            elif allocation[max_smr_band] >= maxMantBits:134                noise_floor[max_smr_band] = negative_infinity135            noise_floor[max_smr_band] -= dB_per_bit136            counter += 1137            if counter > bitBudget:138                break139    # zero out 1 bit allocations (Mid-Tread)140    allocation[allocation < 2] = 0141    # no overflow142    allocation[allocation > maxMantBits] = maxMantBits143    return allocation144#-----------------------------------------------------------------------------145#Testing code146if __name__ == "__main__":147    FS = 48000.0148    N = 1024149    # allocation variables150    maxMantBits = 16151    scale = 4152    # construct input signal153    freqs = (420, 530, 640, 840, 4200, 8400)154    amps = (0.60, 0.11, 0.10, 0.08, 0.05, 0.03)155    n = arange(N, dtype=float)156    x = zeros_like(n)157    for i in range(len(freqs)):158        x += amps[i] * cos(2 * pi * freqs[i] * n / FS)159    nLines = AssignMDCTLinesFromFreqLimits(N/2, FS)160    sfb = ScaleFactorBands(nLines)161    nBands = sfb.nBands162    nLines = sfb.nLines163    bitBudget = int(128.0/(FS/1000.0) * N/2 - (4+4) * nBands - 4)164    mdct = (1 << scale) * MDCT(SineWindow(x), N/2, N/2)165    SMR = CalcSMRs(x, mdct, scale, FS, sfb)166    if test_uniform_bitalloc:167        print "BitAllocUniform Test Results:"168        print "Solution:"169        print solution.BitAllocUniform(bitBudget,maxMantBits,nBands,nLines)170        print "Generated:"171        print BitAllocUniform(bitBudget,maxMantBits,nBands,nLines)172    if test_constant_SNR_bitalloc:173        mdct_spl = SPL(4.0 * (mdct**2.0))174        peaks = zeros(sfb.nBands)175        for band in range(nBands):176            max = mdct_spl[sfb.lowerLine[band]]177            for line in range(sfb.lowerLine[band]+1,sfb.upperLine[band]+1):178                SPL = mdct_spl[line]179                if (SPL > max):180                    max = SPL181            peaks[band] = max182        print "BitAllocConstSNR Test Results:"183        print "Solution:"184        print solution.BitAllocConstSNR(bitBudget,maxMantBits,nBands,nLines,peaks).astype(int)185        print "Generated:"186        print BitAllocConstSNR(bitBudget,maxMantBits,nBands,nLines,peaks)187    if test_constant_MNR_bitalloc:188        print "BitAllocConstMNR Test Results:"189        print "Solution:"190        print solution.BitAllocConstMNR(bitBudget,maxMantBits,nBands,nLines,SMR).astype(int)191        print "Generated:"192        print BitAllocConstMNR(bitBudget,maxMantBits,nBands,nLines,SMR)193    if test_bitalloc:194        print "BitAlloc Test Results:"195        print "Solution:"196        print solution.BitAlloc(bitBudget,maxMantBits,nBands,nLines,SMR)197        print "Generated:"...test_booleans.py
Source:test_booleans.py  
...15# You should have received a copy of the GNU General Public License16# along with desnapifier.  If not, see <http://www.gnu.org/licenses/>.17import os18from desnapifier import project19def test_booleans():20    project.convert_project("tests/test_booleans.xml", "test_booleans.sb2")...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
