Beispiel #1
0
    def runTest(self):
        # create a SpreadDico
        pySig = Signal(op.join(audio_filepath, "glocs.wav"), mono=True)
        pySig.crop(0, 5 * pySig.fs)
        pySig.pad(2048)

        dico = [128, 1024, 8192]

        parallelProjections.initialize_plans(np.array(dico), np.array([2] * len(dico)))

        classicDIco = mdct_dico.Dico(dico)
        spreadDico = mdct_dico.SpreadDico(dico, all_scales=True, penalty=0, maskSize=3)

        self.assertEqual(spreadDico.mask_times, [3, 3, 3])

        classicDIco.initialize(pySig)
        spreadDico.initialize(pySig)

        classicDIco.update(pySig, 2)
        spreadDico.update(pySig, 2)

        classicAtom1 = classicDIco.get_best_atom(0)
        spreadAtom1 = spreadDico.get_best_atom(0)
        #        print classicAtom1, spreadAtom1
        self.assertEqual(classicAtom1, spreadAtom1)

        pySig.subtract(classicAtom1)
        classicDIco.update(pySig, 2)
        spreadDico.update(pySig, 2)

        classicAtom2 = classicDIco.get_best_atom(0)
        spreadAtom2 = spreadDico.get_best_atom(0)

        self.assertNotEqual(classicAtom2, spreadAtom2)
Beispiel #2
0
    def runTest(self):
        name = "orchestra"
        pySig = Signal(op.join(audio_filepath, "Bach_prelude_40s.wav"), mono=True, normalize=True)
        pySig.crop(0, 5 * pySig.fs)
        pySig.pad(16384)
        sigEnergy = np.sum(pySig.data ** 2)
        dico = [128, 1024, 8192]
        nbAtoms = 200

        classicDIco = mdct_dico.Dico(dico)
        spreadDico = mdct_dico.SpreadDico(dico, all_scales=True, penalty=0.1, maskSize=10)

        approxClassic, decayClassic = mp.mp(pySig, classicDIco, 20, nbAtoms)
        approxSpread, decaySpread = mp.mp(pySig, spreadDico, 20, nbAtoms, pad=False)
        import matplotlib.pyplot as plt

        plt.figure(figsize=(16, 8))
        plt.subplot(121)
        approxClassic.plot_tf(ylim=[0, 4000])
        plt.title("Classic decomposition : 200 atoms 3xMDCT")
        plt.subplot(122)
        approxSpread.plot_tf(ylim=[0, 4000])
        plt.title("Decomposition with TF masking: 200 atoms 3xMDCT")
        #        plt.savefig(name + '_TestTFMasking.eps')

        plt.figure()
        plt.plot([10 * np.log10(i / sigEnergy) for i in decayClassic])
        plt.plot([10 * np.log10(i / sigEnergy) for i in decaySpread], "r")
        plt.legend(("Classic decomposition", "Spreading Atoms"))
        plt.ylabel("Residual energy decay(dB)")
        plt.xlabel("Iteration")
Beispiel #3
0
    def runTest(self):
        pySig = Signal(op.join(audio_filepath, "glocs.wav"), mono=True)
        pySig.crop(0, 5 * pySig.fs)
        pySig.pad(2048)

        scale = 1024
        parallelProjections.initialize_plans(np.array([scale]), np.array([2]))

        classicBlock = mdct_block.Block(scale, pySig, 0, debug_level=3)

        spreadBlock = mdct_block.SpreadBlock(scale, pySig, 0, debug_level=3, penalty=0, maskSize=5)

        # compute the projections, should be equivalent
        classicBlock.update(pySig, 0, -1)
        spreadBlock.update(pySig, 0, -1)

        maxClassicAtom1 = classicBlock.get_max_atom()
        print maxClassicAtom1.length, maxClassicAtom1.frame,
        print maxClassicAtom1.freq_bin, maxClassicAtom1.mdct_value
        maxSpreadcAtom1 = spreadBlock.get_max_atom()
        print maxSpreadcAtom1.length, maxSpreadcAtom1.frame,
        print maxSpreadcAtom1.freq_bin, maxSpreadcAtom1.mdct_value
        # assert equality using the inner comparison method of MDCT atoms
        self.assertEqual(maxClassicAtom1, maxSpreadcAtom1)

        # verifying the masking index construction
        mask_frame_width = 2
        mask_bin_width = 1
        spreadBlock.compute_mask(maxSpreadcAtom1, mask_bin_width, mask_frame_width, 0.5)

        c_frame = int(np.ceil(maxSpreadcAtom1.time_position / (scale / 2)))
        c_bin = int(maxSpreadcAtom1.reduced_frequency * scale)

        z1 = np.arange(int(c_frame - mask_frame_width), int(c_frame + mask_frame_width) + 1)
        z2 = np.arange(int(c_bin - mask_bin_width), int(c_bin + mask_bin_width) + 1)
        #        x, y = np.meshgrid(z1, z2)
        #        print spreadBlock.mask_index_x
        #        np.testing.assert_array_equal(spreadBlock.mask_index_x, z1)
        #        np.testing.assert_array_equal(spreadBlock.mask_index_y, z2)

        pySig.subtract(maxSpreadcAtom1)

        # recompute the projections
        classicBlock.update(pySig, 0, -1)
        spreadBlock.update(pySig, 0, -1)

        #        plt.show()
        maxClassicAtom2 = classicBlock.get_max_atom()
        print maxClassicAtom2.length, maxClassicAtom2.frame, maxClassicAtom2.freq_bin, maxClassicAtom2.mdct_value
        maxSpreadcAtom2 = spreadBlock.get_max_atom()
        print maxSpreadcAtom2.length, maxSpreadcAtom2.frame, maxSpreadcAtom2.freq_bin, maxSpreadcAtom2.mdct_value
        self.assertNotEqual(maxClassicAtom2, maxSpreadcAtom2)

        parallelProjections.clean_plans()
Beispiel #4
0
    def runTest(self):
        name = "orchestra"
        pySig = Signal(op.join(audio_filepath, "glocs.wav"), mono=True, normalize=True)
        pySig.crop(0, 5 * pySig.fs)
        pySig.pad(16384)
        sigEnergy = np.sum(pySig.data ** 2)
        dico = [128, 1024, 8192]
        nbAtoms = 200

        classicDIco = mdct_dico.Dico(dico, useC=False)
        spreadDico = mdct_dico.SpreadDico(
            dico, all_scales=False, spread_scales=[1024, 8192], penalty=0.1, mask_time=2, mask_freq=2
        )

        approxClassic, decayClassic = mp.mp(pySig, classicDIco, 20, nbAtoms)
        approxSpread, decaySpread = mp.mp(pySig, spreadDico, 20, nbAtoms, pad=False)

        plt.figure(figsize=(16, 8))
        plt.subplot(121)
        approxClassic.plot_tf(ylim=[0, 4000])
        plt.title("Classic decomposition : 200 atoms 3xMDCT")
        plt.subplot(122)
        approxSpread.plot_tf(ylim=[0, 4000])
        plt.title("Decomposition with TF masking: 200 atoms 3xMDCT")
        #        plt.savefig(name + '_TestTFMasking.eps')

        plt.figure()
        plt.plot([10 * np.log10(i / sigEnergy) for i in decayClassic])
        plt.plot([10 * np.log10(i / sigEnergy) for i in decaySpread], "r")
        plt.legend(("Classic decomposition", "Spreading Atoms"))
        plt.ylabel("Residual energy decay(dB)")
        plt.xlabel("Iteration")
        #        plt.savefig(name + '_decayTFMasking.eps')

        plt.figure()
        for blockI in range(1, 3):
            block = spreadDico.blocks[blockI]
            plt.subplot(2, 2, blockI)
            print block.mask.shape, block.mask.shape[0] / (block.scale / 2), block.scale / 2
            plt.imshow(
                np.reshape(block.mask, (block.mask.shape[0] / (block.scale / 2), block.scale / 2)),
                interpolation="nearest",
                aspect="auto",
            )
            plt.colorbar()
            plt.subplot(2, 2, blockI + 2)
            # print block.mask.shape, block.mask.shape[0] / (block.scale/2),
            # block.scale/2
            block.im_proj_matrix()
            plt.colorbar()
Beispiel #5
0
#mpl.rcParams['text.usetex'] = True

from PyMP import Signal, mp
from PyMP.mdct import Dico


sizes = [128, 1024, 8192]
n_atoms = 1000


abPath = os.path.abspath('../../data/')
sig = Signal(abPath + '/glocs.wav', mono=True, normalize=True)

# taking only the first musical phrase (3.5 seconds approximately)
sig.crop(0, 3.5 * sig.fs)
sig.pad(8192)

# add some minor noise to avoid null areas
sig.data += 0.0001 * np.random.randn(sig.length)

# create MDCT multiscale dictionary
dico = Dico(sizes)

# run the MP routine
approx, decay = mp.mp(sig, dico, 50, n_atoms)

# plotting the results
timeVec = np.arange(0, float(sig.length)) / sig.fs

plt.figure(figsize=(10, 6))
axOrig = plt.axes([0.05, 0.55, .4, .4])
Beispiel #6
0
import numpy as np
from PyMP import Signal, mp
from PyMP.mdct.dico import Dico, LODico
from PyMP.mdct.atom import Atom
print "Running MP, OMP and local versions on synthetic k-sparse"
scales = [16, 64, 256]
dico = Dico(scales)
M = len(scales)
L = 256 * 4
k = 0.2*L
# create a k-sparse signal
sp_vec = np.zeros(M*L,)
from PyMP.tools import mdct
random_indexes = np.arange(M*L)
np.random.shuffle(random_indexes)
random_weights = np.random.randn(M*L)
sp_vec[random_indexes[0:k]] = random_weights[0:k]
sparse_data = np.zeros(L,)
for m in range(M):
    sparse_data += mdct.imdct(sp_vec[m*L:(m+1)*L], scales[m])
signal_original = Signal(sparse_data, Fs=8000, mono=True, normalize=False)
signal_original.data += 0.01 * np.random.random(signal_original.length,)
n_atoms = k 
signal_original.pad(dico.get_pad())
app_2, dec2 = mp.greedy(signal_original, dico, 100,
                        n_atoms, debug=0, pad=False, update='locgp')
app_1, dec1 = mp.greedy(signal_original, dico, 100,
                        n_atoms, debug=0, pad=False, update='mp')
app_3, dec3 = mp.greedy(signal_original, dico, 100,
                        n_atoms, debug=0, pad=False, update='locomp')
Beispiel #7
0
# sig.plot()
# sig.write('newDestFile.wav')
# editing

print 'Before cropping Length of ', sig.length
sig.crop(0, 2048)
print 'After cropping Length of ', sig.length

sub_sig = sig[0:2048]
print sub_sig

new_sig = Signal(np.ones((8,)), 1)
new_sig.data
print "Padding"
new_sig.pad(4)
new_sig.data
print "De-Padding"
new_sig.depad(4)
new_sig.data


dico = Dico([128, 1024, 8192])
sig = Signal('../data/glocs.wav', mono=True)
app = approx.Approx(dico, [], sig)


app.add(atom.Atom(256, 1, 256, 10, 8000, 1))

app.compute_srr()
print app
Beispiel #8
0
"""

Tutorial provided as part of PyMP

M. Moussallam

"""
from PyMP.mdct import Dico, LODico
from PyMP.mdct.rand import SequenceDico
from PyMP import mp, mp_coder, Signal
signal = Signal('../data/ClocheB.wav', mono=True)  # Load Signal
signal.crop(0, 4.0 * signal.fs)     # Keep only 4 seconds
# atom of scales 8, 64 and 512 ms
scales = [(s * signal.fs / 1000) for s in (8, 64, 512)]
signal.pad(scales[-1])
# Dictionary for Standard MP
dico = Dico(scales)
# Launching decomposition, stops either at 20 dB of SRR or 2000 iterations
app, dec = mp.mp(signal, dico, 20, 2000, pad=False)

app.atom_number

snr, bitrate, quantized_app = mp_coder.simple_mdct_encoding(
    app, 8000, Q=14)
print (snr, bitrate)

print "With Q=5"
snr, bitrate, quantized_app = mp_coder.simple_mdct_encoding(
    app, 8000, Q=5)
print (snr, bitrate)