Пример #1
0
def denoise_gabor_real(signal, alpha, beta, window, LW=None, th_method='soft', plot=0,ret='visu',sigma=1,facT = 1):
    # signal denoising through shrinkage of gabor coefficients
    # alpha, beta: time and frequency step
    # window     : either (bsplines, level) or (gauss, variance)
    # LW         : length of the window in terms of samples
    # ret        : Threshold type either 'visu' for the universal threshold
    #			              or 'facT' for a the given threshold
    # sigma      : estimated noise standard deviation


    L      = len(signal)

    # Gabor Frame
    a, M, Lg, N, Ngood = gabimagepars(L, int(L/alpha), int(L/beta))
    if LW is None:
        LW = Lg
    if window[0] == 'bsplines':
        gs = gabwin(mySpline(window[1], LW, 0)[-1], a, M, LW)[0]
    else:
        gs = gabwin({'name': ('gauss'), 'tfr': window[1]}, a, M, LW)[0]

    ga = gabdual(gs, a, M)

    # denoising
    c_noisy = dgtreal(signal, ga, a, M)[0]

    if ret =='visu':
        US  = sigma * np.linalg.norm(ga) * np.sqrt(2*np.log(M*N))
        ES  = error_gabor(signal, c_noisy, gs, a, M, th_method, US)
    else:
        TH = facT
        ES = error_gabor(signal, c_noisy, gs, a, M, th_method, TH)

    # plots
    if plot:
        plt.clf()
        plt.plot( signal, alpha=0.3)
        plt.plot( ES[-1], linestyle='--', alpha=2)

    return ES[-1]
Пример #2
0
def get_stft_operators(win_type='hann',
                       win_len=16,
                       hop=8,
                       nb_bins=32,
                       sig_len=128,
                       phase_conv='freqinv'):
    w = gabwin(g={
        'name': win_type,
        'M': win_len
    }, a=hop, M=nb_bins, L=sig_len)[0]
    wd = gabdual(w, a=hop, M=nb_bins, L=sig_len)
    direct_stft = \
        lambda x:dgt(x, g=w, a=hop, M=nb_bins, L=sig_len, pt=phase_conv)[0]
    adjoint_stft = \
        lambda x:idgt(x, g=w, a=hop, Ls=sig_len, pt=phase_conv)[0]
    pseudoinverse_stft = \
        lambda x:idgt(x, g=wd, a=hop, Ls=sig_len, pt=phase_conv)[0]
    return direct_stft, adjoint_stft, pseudoinverse_stft
Пример #3
0
    def reduced_kernel(self, delay=0):
        """
        Returns a reduced form of the reproducing kernel, with
        optionally a (possibly fractional) time shift.
        The kernel is reduced to its components at freq=0.
        The anticipated relative error due to this reduction 
        is also returned.
        """
        # assert delay < dualwin.size

        synth_window,_ = gabwin(self.synthesis_window, \
                                self.time_step, \
                                self.num_freqs, \
                                512)
        window = numpy.pad(numpy.fft.fftshift(synth_window), (512))
        tfmap = self.forward(utils.delayseq(window, \
                                      delay))
        # normalized_map = numpy.fft.fftshift(tfmap.data, axes=0)/numpy.linalg.norm(tfmap.data)
        kernel = numpy.vstack((tfmap.data[2:0:-1,7:18],tfmap.data[0:3,7:18]))
        
        return kernel, window
Пример #4
0
def sim_gabor_real(signal, alpha, beta, window, LW=None, SNR=1, plot=0, ret='visu', facT = 1, Seed=1):
    # simulation gabor denoising of a signal
    # alpha, beta: time and frequency step
    # window     : either (bsplines, level) or (gauss, variance)
    # LW         : length of the window in terms of samples
    # SNR        : Signal-to-noise ratio
    # ret        : Threshold type either 'best' for the best threshold
    #	                         'visu' for the universal threshold
    #			      or 'facT' for a the given threshold

    # Seed setzen
    np.random.seed(Seed)

    # noise
    L      = len(signal)
    sigma  = np.sqrt(np.mean(signal ** 2) / SNR)
    noise  = np.random.normal(0, sigma, size=L)

    # Gabor Frame
    a, M, Lg, N, Ngood = gabimagepars(L, int(L/alpha), int(L/beta))
    if LW is None:
        LW = Lg
    if window[0] == 'bsplines':
        gs = gabwin(mySpline(window[1],LW,0)[-1], a, M, LW)[0]
    else:
        gs = gabwin({'name': ('gauss'), 'tfr': window[1]}, a, M, LW)[0]

    ga = gabdual(gs, a, M)

    # denoising
    c_noisy = dgtreal(signal + noise, ga, a, M)[0]


    if ret =='best':
        c_noisy_max = c_noisy.real.max()
        print 'c_noisy_max', c_noisy_max

        minimizer_kwargs = {"method": "L-BFGS-B", "bounds": ((0, c_noisy_max),)}
        BS = basinhopping(lambda y: error_gabor(signal, c_noisy, gs, a, M, 'soft', y)[0], 0, minimizer_kwargs=minimizer_kwargs, niter=100, stepsize=c_noisy_max/10)
        BH = basinhopping(lambda y: error_gabor(signal, c_noisy, gs, a, M, 'hard', y)[0], 0, minimizer_kwargs=minimizer_kwargs, niter=100, stepsize=c_noisy_max/10)
        # BS = minimize_scalar(lambda y: error_gabor(signal, c_noisy, gs, a, M, 'soft', y, c_noisy_max)[0], bracket = (0, c_noisy_max))
        # BH = minimize_scalar(lambda y: error_gabor(signal, c_noisy, gs, a, M, 'hard', y, c_noisy_max)[0], bracket = (0, c_noisy_max)) 
        res = BS.x, BH.x, BS.fun, BH.fun
    elif ret =='visu':
        print 'ga:', np.linalg.norm(ga),'gs:', np.linalg.norm(gs), M, N
        US  = sigma * np.linalg.norm(ga) * np.sqrt(2*np.log(M*N))
        ES  = error_gabor(signal, c_noisy, gs, a, M, 'soft', US)
        EH  = error_gabor(signal, c_noisy, gs, a, M, 'hard', US)
        res =  US, ES[0], EH[0]
    else:
        TH = facT
        ES = error_gabor(signal, c_noisy, gs, a, M, 'soft', TH)
        EH = error_gabor(signal, c_noisy, gs, a, M, 'hard', TH)
        res = TH, ES[0], EH[0]

    # plots
    if plot:
        plt.clf()
        plt.plot(signal, alpha=0.3)
        plt.plot(ES[-1], linestyle='--', alpha=2)



    return (Seed, sigma, alpha, beta, window, LW) + res