Beispiel #1
0
def dgt(x, hop_size, n_fft):
    """Compute the DGT of a real signal with a gauss window."""
    assert len(x.shape) == 1
    assert np.mod(len(x), hop_size) == 0
    assert np.mod(n_fft,
                  2) == 0, "The number of stft channels needs to be even"
    assert np.mod(len(x), n_fft) == 0
    g_analysis = _analysis_window(x, hop_size, n_fft)
    return dgtreal(x.astype(np.float64), g_analysis, hop_size, n_fft)[0]
Beispiel #2
0
 def dgt(self, x, hop_size=None, stft_channels=None):
     """Compute the DGT of a real signal with a gauss window."""
     if hop_size is None:
         hop_size = self.hop_size
     if stft_channels is None:
         stft_channels = self.stft_channels
     assert (len(x.shape) == 1)
     assert (np.mod(len(x), hop_size) == 0)
     assert (np.mod(stft_channels, 2) == 0), 'The number of stft channels needs to be even'
     assert (np.mod(len(x), stft_channels) == 0)
     g_analysis = self._analysis_window(x)
     return dgtreal(x.astype(np.float64), g_analysis, hop_size, stft_channels)[0]
Beispiel #3
0
 def forward(self, data):
     """
     Forward transform
     data -- input data (Numpy array)
     """
         
     if self.transform == 'dgtreal':
         out,_,_ = dgtreal(data, self.analysis_window, \
                           self.time_step, self.num_freqs)
         return TimeFreq(self, out, data.size)
     elif self.transform == 'dgt':
         out,_,_ = dgt(data, self.analysis_window, \
                           self.time_step, self.num_freqs)
         return TimeFreq(self, out, data.size)         
     # elif test expression:
     else: 
         logging.warning("Unknown transform")
         return None
Beispiel #4
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]
        def test01(signal,
                   fft_window_length,
                   fft_hop_size,
                   window,
                   rtolgrad=1e-8,
                   atolgrad=1e-9):
            dgt = ltfatpy.dgt(signal, window, fft_hop_size,
                              fft_window_length)[0]
            dgtmag = np.abs(dgt)
            dgtmask = np.ones_like(dgtmag)
            dgtmask[dgtmag < 1e-4] = 0

            dgtreal = ltfatpy.dgtreal(signal, window, fft_hop_size,
                                      fft_window_length)[0]
            dgtrealmag = np.abs(dgtreal)
            dgtrealmask = np.ones_like(dgtrealmag)
            dgtrealmask[dgtrealmag < 1e-4] = 0

            np.testing.assert_allclose(dgt[:self.fft_window_length // 2 + 1],
                                       dgtreal,
                                       rtol=1e-14,
                                       atol=1e-14)

            tgrad, fgrad = ltfatpy.gabphasegrad('phase', np.angle(dgt),
                                                fft_hop_size,
                                                fft_window_length) * dgtmask
            tgradreal, fgradreal = ltfatpy.gabphasegrad(
                'phase', np.angle(dgtreal), fft_hop_size,
                fft_window_length) * dgtrealmask
            np.testing.assert_allclose(tgrad[:self.fft_window_length // 2 + 1],
                                       tgradreal,
                                       rtol=rtolgrad,
                                       atol=atolgrad)
            np.testing.assert_allclose(fgrad[:self.fft_window_length // 2 + 1],
                                       fgradreal,
                                       rtol=rtolgrad,
                                       atol=atolgrad)
Beispiel #6
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
Beispiel #7
0
 def oneSidedStft(self, signal, windowLength, hopSize):
     gs = {'name': 'gauss', 'M': windowLength}
     return ltfatpy.dgtreal(signal, gs, hopSize, windowLength)[0]