def test1(self): inblk = 17 outblk = 13 inp = (list(range(i*inblk,(i+1)*inblk)) for i in range(10)) resit = reblock(inp, outblk, dtype=None, fulllast=True, padding=-1) res = list(map(list, resit)) expres = ["0 1 2 3 4 5 6 7 8 9 10 11 12", "13 14 15 16 17 18 19 20 21 22 23 24 25", "26 27 28 29 30 31 32 33 34 35 36 37 38", "39 40 41 42 43 44 45 46 47 48 49 50 51", "52 53 54 55 56 57 58 59 60 61 62 63 64", "65 66 67 68 69 70 71 72 73 74 75 76 77", "78 79 80 81 82 83 84 85 86 87 88 89 90", " 91 92 93 94 95 96 97 98 99 100 101 102 103", "104 105 106 107 108 109 110 111 112 113 114 115 116", "117 118 119 120 121 122 123 124 125 126 127 128 129", "130 131 132 133 134 135 136 137 138 139 140 141 142", "143 144 145 146 147 148 149 150 151 152 153 154 155", "156 157 158 159 160 161 162 163 164 165 166 167 168", "169 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1"] expres = [list(map(int,s.split())) for s in expres] self.assertEqual(res, expres)
def test1(self): inblk = 17 outblk = 13 inp = (range(i*inblk,(i+1)*inblk) for i in xrange(10)) resit = reblock(inp, outblk, dtype=None, fulllast=True, padding=-1) res = map(list, resit) expres = ["0 1 2 3 4 5 6 7 8 9 10 11 12", "13 14 15 16 17 18 19 20 21 22 23 24 25", "26 27 28 29 30 31 32 33 34 35 36 37 38", "39 40 41 42 43 44 45 46 47 48 49 50 51", "52 53 54 55 56 57 58 59 60 61 62 63 64", "65 66 67 68 69 70 71 72 73 74 75 76 77", "78 79 80 81 82 83 84 85 86 87 88 89 90", " 91 92 93 94 95 96 97 98 99 100 101 102 103", "104 105 106 107 108 109 110 111 112 113 114 115 116", "117 118 119 120 121 122 123 124 125 126 127 128 129", "130 131 132 133 134 135 136 137 138 139 140 141 142", "143 144 145 146 147 148 149 150 151 152 153 154 155", "156 157 158 159 160 161 162 163 164 165 166 167 168", "169 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1"] expres = [map(int,s.split()) for s in expres] self.assertEqual(res, expres)
def main(): parser = ArgumentParser() parser.add_argument( "--mask", type=str, default="soft", choices=("hard", "soft"), help="mask strategy", ) parser.add_argument("--outdir", type=str, default="./", help="output directory") parser.add_argument( "--stream-size", type=int, default=1024, help="stream size for simulated realtime from wav (default=%(default)s)", ) parser.add_argument("input", type=str, help="input file") args = parser.parse_args() prefix = args.input.split("/")[-1].split("_")[0] harm_out = os.path.join(args.outdir, prefix + "_harmonic.wav") perc_out = os.path.join(args.outdir, prefix + "_percussive.wav") print("writing files to {0}, {1}".format(harm_out, perc_out)) lharm = 17 lperc = 7 # calculate transform parameters nsgt_scale = OctScale(80, 20000, 12) trlen = args.stream_size # transition length sllen = 4 * args.stream_size # slice length x, fs = librosa.load(args.input, sr=None) xh = numpy.zeros_like(x) xp = numpy.zeros_like(x) hop = trlen chunk_size = hop n_chunks = int(numpy.floor(x.shape[0] // hop)) eps = numpy.finfo(numpy.float32).eps slicq = NSGT_sliced( nsgt_scale, sllen, trlen, fs, real=True, matrixform=True, ) total_time = 0.0 for chunk in range(n_chunks - 1): t1 = cputime() start = chunk * hop end = start + sllen s = x[start:end] signal = (s,) c = slicq.forward(signal) c = list(c) C = numpy.asarray(c) Cmag = numpy.abs(C) H = scipy.ndimage.median_filter(Cmag, size=(1, lharm, 1)) P = scipy.ndimage.median_filter(Cmag, size=(1, 1, lperc)) if args.mask == "soft": # soft mask first tot = numpy.power(H, 2.0) + numpy.power(P, 2.0) + eps Mp = numpy.divide(numpy.power(H, 2.0), tot) Mh = numpy.divide(numpy.power(P, 2.0), tot) else: Mh = numpy.divide(H, P + eps) > 2.0 Mp = numpy.divide(P, H + eps) >= 2.0 Cp = numpy.multiply(Mp, C) Ch = numpy.multiply(Mh, C) # generator for backward transformation outseq_h = slicq.backward(Ch) outseq_p = slicq.backward(Cp) # make single output array from iterator sh_r = next(reblock(outseq_h, len(s), fulllast=False)) sh_r = sh_r.real sp_r = next(reblock(outseq_p, len(s), fulllast=False)) sp_r = sp_r.real xh[start:end] = sh_r xp[start:end] = sp_r t2 = cputime() total_time += t2 - t1 print("Calculation time per iter: %fs" % (total_time / n_chunks)) scipy.io.wavfile.write(harm_out, fs, xh) scipy.io.wavfile.write(perc_out, fs, xp) return 0
t1 = cputime() signal = (s,) # generator for forward transformation c = slicq.forward(signal) # realize transform from generator c = list(c) # generator for backward transformation outseq = slicq.backward(c) # make single output array from iterator s_r = next(reblock(outseq, len(s), fulllast=False)) s_r = s_r.real t2 = cputime() norm = lambda x: np.sqrt(np.sum(np.abs(np.square(np.abs(x))))) rec_err = norm(s-s_r)/norm(s) print("Reconstruction error: %.3e"%rec_err) print("Calculation time: %.3fs"%(t2-t1)) # Compare the sliced coefficients with non-sliced ones if False: # not implemented yet! test_coeff_quality(c, s, g, shift, M, options.sl_len, len(s)) if args.output:
signal = (s,) # generator for forward transformation c = slicq.forward(signal) # realize transform from generator c = list(c) # cl = map(len,c[0]) # print "c",len(cl),cl # generator for backward transformation outseq = slicq.backward(c) # make single output array from iterator s_r = reblock(outseq,len(s),fulllast=False).next() s_r = s_r.real t2 = time() norm = lambda x: N.sqrt(N.sum(N.abs(N.square(N.abs(x))))) rec_err = norm(s-s_r)/norm(s) print "Reconstruction error: %.3e"%rec_err print "Calculation time: %.3f s"%(t2-t1) # Compare the sliced coefficients with non-sliced ones if False: # not implemented yet! test_coeff_quality(c,s,g,shift,M,options.sl_len,len(s)) if options.output:
signal = (s, ) # generator for forward transformation c = slicq.forward(signal) # realize transform from generator c = list(c) # cl = map(len,c[0]) # print "c",len(cl),cl # generator for backward transformation outseq = slicq.backward(c) # make single output array from iterator s_r = reblock(outseq, len(s), fulllast=False).next() s_r = s_r.real t2 = time() norm = lambda x: N.sqrt(N.sum(N.abs(N.square(N.abs(x))))) rec_err = norm(s - s_r) / norm(s) print "Reconstruction error: %.3e" % rec_err print "Calculation time: %.3f s" % (t2 - t1) # Compare the sliced coefficients with non-sliced ones if False: # not implemented yet! test_coeff_quality(c, s, g, shift, M, options.sl_len, len(s)) if options.output: