Exemple #1
0
    def synthesize(self, method=0, forceReSynthesis=True):
        """ function that will synthesise the approximant using the list of atoms
            this is mostly DEPRECATED"""
        if self.original_signal == None:
            _Logger.warning("No original Signal provided")
#            return None

        if (self.recomposed_signal == None) | forceReSynthesis:
            synthesizedSignal = np.zeros(self.length)

            if len(self.atoms) == 0:
                _Logger.info("No Atoms")
                return None

            # first method by inverse MDCT
            if method == 0:
                for mdctSize in self.dico.sizes:
                    mdctVec = np.zeros(self.length)
                    for atom in self.atoms:
                        if atom.length == mdctSize:
                            # bugFIx
                            n = atom.time_position + 1
                            frame = math.floor(
                                float(n) / float(atom.length / 2)) + 1
# mdctVec[frame*float(atom.length /2) + atom.frequencyBin] += atom.amplitude
                            mdctVec[frame * float(atom.length /
                                                  2) + atom.freq_bin] += atom.mdct_value
                    synthesizedSignal += imdct(mdctVec, mdctSize)
# synthesizedSignal += concatenate((zeros(mdctSize/4) , imdct(mdctVec ,
# mdctSize)) )[1:-mdctSize/4+1]

            # second method by recursive atom synthesis - NOT WORKING
            elif method == 1:
                for atom in self.atoms:
                    atom.synthesize_ifft()
                    synthesizedSignal[atom.time_position:
                                      atom.time_position + atom.length] += atom.waveform

            # HACK here to resynthesize using LOMP atoms
            elif method == 2:
                for atom in self.atoms:
                    atom.waveForm = atom.synthesize_ifft()
                    if (atom.proj_score is not None):
                        if (atom.proj_score < 0):
                            atom.waveform = (-math.sqrt(-atom.proj_score /
                                                        sum(atom.waveform ** 2))) * atom.waveform
                        else:
                            atom.waveform = (math.sqrt(atom.proj_score /
                                                       sum(atom.waveform ** 2))) * atom.waveform

                    synthesizedSignal[atom.time_position:
                                      atom.time_position + atom.length] += atom.waveform

            self.recomposed_signal = signals.Signal(synthesizedSignal, self.fs)
            # return self.recomposed_signal
        # other case: we just give the existing synthesized Signal.
        return self.recomposed_signal
Exemple #2
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')