Пример #1
0
def ifft(z):
	B      = z.shape[0]//2
	L      = z.shape[1]
	C      = TT.as_tensor_variable(np.asarray([[[1,-1]]], dtype=T.config.floatX))
	Zr, Zi = TTF.rfft(z[:B], norm="ortho"), TTF.rfft(z[B:]*-1, norm="ortho")
	isOdd  = TT.eq(L%2, 1)
	Zr     = TI.ifelse(isOdd, TT.concatenate([Zr, C*Zr[:,1:  ][:,::-1]], axis=1),
	                          TT.concatenate([Zr, C*Zr[:,1:-1][:,::-1]], axis=1))
	Zi     = TI.ifelse(isOdd, TT.concatenate([Zi, C*Zi[:,1:  ][:,::-1]], axis=1),
	                          TT.concatenate([Zi, C*Zi[:,1:-1][:,::-1]], axis=1))
	Zi     = (C*Zi)[:,:,::-1]  # Zi * i
	Z      = Zr+Zi
	return TT.concatenate([Z[:,:,0], Z[:,:,1]*-1], axis=0)
Пример #2
0
    def test_params(self):
        inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        with pytest.raises(ValueError):
            fft.rfft(inputs, norm=123)

        inputs_val = np.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        with pytest.raises(ValueError):
            fft.irfft(inputs, norm=123)
        with pytest.raises(ValueError):
            fft.irfft(inputs, is_odd=123)
Пример #3
0
    def test_norm_rfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        # Unitary normalization
        rfft = fft.rfft(inputs, norm="ortho")
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = np.asarray(res_rfft[:, :, :, 0]) + 1j * np.asarray(
            res_rfft[:, :, :, 1]
        )

        rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))

        utt.assert_allclose(rfft_ref / N, res_rfft_comp, atol=1e-4, rtol=1e-4)

        # No normalization
        rfft = fft.rfft(inputs, norm="no_norm")
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = np.asarray(res_rfft[:, :, :, 0]) + 1j * np.asarray(
            res_rfft[:, :, :, 1]
        )

        utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)

        # Inverse FFT inputs
        inputs_val = np.random.random((1, N, N // 2 + 1, 2)).astype(
            theano.config.floatX
        )
        inputs = theano.shared(inputs_val)
        inputs_ref = inputs_val[..., 0] + 1j * inputs_val[..., 1]

        # Unitary normalization inverse FFT
        irfft = fft.irfft(inputs, norm="ortho")
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()

        irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))

        utt.assert_allclose(irfft_ref * N, res_irfft, atol=1e-4, rtol=1e-4)

        # No normalization inverse FFT
        irfft = fft.irfft(inputs, norm="no_norm")
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()

        utt.assert_allclose(irfft_ref * N ** 2, res_irfft, atol=1e-4, rtol=1e-4)
Пример #4
0
    def test_irfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        rfft = fft.rfft(inputs)
        f_rfft = theano.function([], rfft)
        res_fft = f_rfft()

        m = rfft.type()
        irfft = fft.irfft(m)
        f_irfft = theano.function([m], irfft)
        res_irfft = f_irfft(res_fft)

        utt.assert_allclose(inputs_val, np.asarray(res_irfft))

        inputs_val = np.random.random(
            (1, N, N, 2)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        irfft = fft.irfft(inputs)
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()
        inputs_ref = inputs_val[..., 0] + inputs_val[..., 1] * 1j

        irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))

        utt.assert_allclose(irfft_ref, res_irfft, atol=1e-4, rtol=1e-4)
Пример #5
0
def Tfft2old(a):
	""" assumes len(a.shape)==2, does not assert this """
	#A = np.zeros_like(a,dtype=np.complex_)
	s = a.shape[1]
	S = s//2+1
	aa = T.stack([a],axis=0)
	AA = Tfft.rfft(aa)
	B = AA[...,0] + 1.j * AA[...,1] # Theano rfft stores complex values in separate array 
	#return AA[0,...,0]
	#A[:,:S] = B 
	# copy left half to right half
	#A[:,S:] = np.conj(np.fliplr(A[:,1:S-1]))
	#below no worky
	C = B[0,...]
	CC = C[:,1:S-1]
	return  T.concatenate([C,T.conj(CC[:,::-1])],axis=1)
	A = T.zeros_like(a)
	#Alookup = theano.shared(A)
	Afront = A[:,:S]
	Aback = A[:,S:]
	A = T.set_subtensor(Afront,B)
	A = T.set_subtensor(Aback,T.conj(Tfftshift(A))[:,S:])
	#A[:,:S] = B
	#A[:,S:] = T.conj(npfft.fftshift(A))[:,S:]
	return A
Пример #6
0
    def test_1Drfft(self):
        inputs_val = np.random.random((1, N)).astype(theano.config.floatX)

        x = T.matrix('x')
        rfft = fft.rfft(x)
        f_rfft = theano.function([x], rfft)
        res_rfft = f_rfft(inputs_val)
        res_rfft_comp = (np.asarray(res_rfft[:, :, 0]) +
                         1j * np.asarray(res_rfft[:, :, 1]))

        rfft_ref = np.fft.rfft(inputs_val, axis=1)

        utt.assert_allclose(rfft_ref, res_rfft_comp)

        m = rfft.type()
        print(m.ndim)
        irfft = fft.irfft(m)
        f_irfft = theano.function([m], irfft)
        res_irfft = f_irfft(res_rfft)

        utt.assert_allclose(inputs_val, np.asarray(res_irfft))

        # The numerical gradient of the FFT is sensitive, must set large
        # enough epsilon to get good accuracy.
        eps = 1e-1

        def f_rfft(inp):
            return fft.rfft(inp)
        inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
        utt.verify_grad(f_rfft, [inputs_val], eps=eps)

        def f_irfft(inp):
            return fft.irfft(inp)
        inputs_val = np.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
        utt.verify_grad(f_irfft, [inputs_val], eps=eps)
Пример #7
0
    def test_1Drfft(self):
        inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX)

        x = T.matrix('x')
        rfft = fft.rfft(x)
        f_rfft = theano.function([x], rfft)
        res_rfft = f_rfft(inputs_val)
        res_rfft_comp = (numpy.asarray(res_rfft[:, :, 0]) +
                         1j * numpy.asarray(res_rfft[:, :, 1]))

        rfft_ref = numpy.fft.rfft(inputs_val, axis=1)

        utt.assert_allclose(rfft_ref, res_rfft_comp)

        m = rfft.type()
        print(m.ndim)
        irfft = fft.irfft(m)
        f_irfft = theano.function([m], irfft)
        res_irfft = f_irfft(res_rfft)

        utt.assert_allclose(inputs_val, numpy.asarray(res_irfft))

        # The numerical gradient of the FFT is sensitive, must set large
        # enough epsilon to get good accuracy.
        eps = 1e-1

        def f_rfft(inp):
            return fft.rfft(inp)
        inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX)
        utt.verify_grad(f_rfft, [inputs_val], eps=eps)

        def f_irfft(inp):
            return fft.irfft(inp)
        inputs_val = numpy.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
        utt.verify_grad(f_irfft, [inputs_val], eps=eps)
Пример #8
0
    def test_irfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        rfft = fft.rfft(inputs)
        f_rfft = theano.function([], rfft)
        res_fft = f_rfft()

        m = rfft.type()
        irfft = fft.irfft(m)
        f_irfft = theano.function([m], irfft)
        res_irfft = f_irfft(res_fft)

        utt.assert_allclose(inputs_val, np.asarray(res_irfft))

        inputs_val = np.random.random((1, N, N, 2)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        irfft = fft.irfft(inputs)
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()
        inputs_ref = inputs_val[..., 0] + inputs_val[..., 1] * 1j

        irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))

        utt.assert_allclose(irfft_ref, res_irfft, atol=1e-4, rtol=1e-4)
Пример #9
0
def theano_fft(x):

    x_win = x

    # zero-pad
    frame = T.zeros((x.shape[0], NFFT))
    frame = T.set_subtensor(frame[:, :x.shape[1]], x_win)

    # apply FFT
    x = fft.rfft(frame, norm='ortho')

    # get first half of spectrum
    x = x[:, :fbins]
    # squared magnitude
    x = x[:, :, 0]**2 + x[:, :, 1]**2

    # floor (prevents log from going to -Inf)
    x = T.maximum(x, 1e-9)  # -90dB

    # map to log domain where 0dB -> 1 and -90dB -> -1
    x = (20.0 / 90.0) * T.log10(x) + 1.0

    # scale to weigh errors
    x = 0.1 * x

    return x
Пример #10
0
    def test_norm_rfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        # Unitary normalization
        rfft = fft.rfft(inputs, norm='ortho')
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
                         1j * np.asarray(res_rfft[:, :, :, 1]))

        rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))

        utt.assert_allclose(rfft_ref / N, res_rfft_comp, atol=1e-4, rtol=1e-4)

        # No normalization
        rfft = fft.rfft(inputs, norm='no_norm')
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
                         1j * np.asarray(res_rfft[:, :, :, 1]))

        utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)

        # Inverse FFT inputs
        inputs_val = np.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)
        inputs_ref = inputs_val[..., 0] + 1j * inputs_val[..., 1]

        # Unitary normalization inverse FFT
        irfft = fft.irfft(inputs, norm='ortho')
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()

        irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))

        utt.assert_allclose(irfft_ref * N, res_irfft, atol=1e-4, rtol=1e-4)

        # No normalization inverse FFT
        irfft = fft.irfft(inputs, norm='no_norm')
        f_irfft = theano.function([], irfft)
        res_irfft = f_irfft()

        utt.assert_allclose(irfft_ref * N**2, res_irfft, atol=1e-4, rtol=1e-4)
Пример #11
0
def y_fd(Tobs, f0, fdot, fddot, phi0, nhat, cos_iota, psi, heterodyne_bin, N):
    fhet = heterodyne_bin / Tobs

    ts = linspace(0, Tobs, N + 1)[:-1]

    ys_re, ys_im = y_slow(ts,
                          f0,
                          fdot,
                          fddot,
                          phi0,
                          nhat,
                          cos_iota,
                          psi,
                          fhet=fhet)

    ys_re_rfft = ttf.rfft(tt.reshape(ys_re, (-1, N)))
    ys_im_rfft = ttf.rfft(tt.reshape(ys_im, (-1, N)))

    NN = N // 2 + 1

    ys_re_rfft = tt.reshape(ys_re_rfft, (3, 3, NN, 2))
    ys_im_rfft = tt.reshape(ys_im_rfft, (3, 3, NN, 2))

    y_fd_re = tt.zeros((3, 3, N))
    y_fd_im = tt.zeros((3, 3, N))

    y_fd_re = tt.set_subtensor(y_fd_re[:, :, :NN],
                               ys_re_rfft[:, :, :, 0] - ys_im_rfft[:, :, :, 1])
    y_fd_im = tt.set_subtensor(y_fd_im[:, :, :NN],
                               ys_re_rfft[:, :, :, 1] + ys_im_rfft[:, :, :, 0])

    y_fd_re = tt.set_subtensor(
        y_fd_re[:, :, NN:],
        ys_re_rfft[:, :, -2:0:-1, 0] + ys_im_rfft[:, :, -2:0:-1, 1])
    y_fd_im = tt.set_subtensor(
        y_fd_im[:, :, NN:],
        -ys_re_rfft[:, :, -2:0:-1, 1] + ys_im_rfft[:, :, -2:0:-1, 0])

    return 0.5 * (ts[1] - ts[0]) * y_fd_re, 0.5 * (ts[1] - ts[0]) * y_fd_im
Пример #12
0
    def test_rfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        rfft = fft.rfft(inputs)
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
                         1j * np.asarray(res_rfft[:, :, :, 1]))

        rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))

        utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)
Пример #13
0
    def test_rfft(self):
        inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
        inputs = theano.shared(inputs_val)

        rfft = fft.rfft(inputs)
        f_rfft = theano.function([], rfft)
        res_rfft = f_rfft()
        res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
                         1j * np.asarray(res_rfft[:, :, :, 1]))

        rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))

        utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)
Пример #14
0
def Tfft2(a):
	""" assumes len(a.shape)==2, does not assert this """
	#A = np.zeros_like(a,dtype=np.complex_)
	s = a.shape[0]
	S = s//2+1
	#aa = T.stack([a],axis=0)
	aa = a.reshape((1,a.shape[0],a.shape[1]))
	AA = Tfft.rfft(aa)
	B = AA[...,0] + 1.j * AA[...,1] # Theano rfft stores complex values in separate array 
	# get first output
	C = B[0,...]
	CC = C[:,1:S-1]
	A = T.zeros_like(a)
	Afront = A[:,:S]
	A = T.set_subtensor(Afront,C)
	Aback = A[:,S:]
	A = T.set_subtensor(Aback,T.conj(Tfftshift(A))[:,S:])
	return A
Пример #15
0
    def call(self, x, mask=None):
        for fr_idx in range(self.n_frame):
            X_frame_power = K.sum(K.square(
                fft.rfft(self.fft_window *
                         x[:, :, fr_idx * self.n_hop:fr_idx * self.n_hop +
                           self.n_fft])),
                                  axis=3,
                                  keepdims=True)
            if fr_idx == 0:
                output = X_frame_power
            else:
                output = T.concatenate([output, X_frame_power], axis=3)

        if self.power_stft != 2.0:
            output = K.pow(output, self.power_stft / 2.0)
        if self.return_decibel_stft:
            output = backend_keras.amplitude_to_decibel(output)
        return output
Пример #16
0
def theano_fft(x):

    # window with analysis window
    x_win = win * x

    # zero-pad
    frame = T.zeros((x.shape[0], NFFT))
    frame = T.set_subtensor(frame[:, :x.shape[1]], x_win)

    # apply FFT
    x = fft.rfft(frame, norm='ortho')

    # get first half of spectrum
    x = x[:, :fbins]
    # squared magnitude
    x = x[:, :, 0]**2 + x[:, :, 1]**2

    # floor (prevents log from going to -Inf)
    x = T.maximum(x, 1e-9)

    return x
Пример #17
0
 def f_rfft(inp):
     return fft.rfft(inp)
Пример #18
0
 def f_rfft(inp):
     return fft.rfft(inp, norm='ortho')
Пример #19
0
def fft(x, norm=None):
    '''Fast fourier transform:
       Compute an n-point fft of frames along given axis.
    '''
    return rfft(x, norm=norm)
Пример #20
0
 def f_rfft(inp):
     return fft.rfft(inp)
Пример #21
0
 def f_rfft(inp):
     return fft.rfft(inp, norm='ortho')
Пример #22
0
def fft(x, norm=None):
    '''Fast fourier transform:
       Compute an n-point fft of frames along given axis.
    '''
    return rfft(x, norm=norm)
Пример #23
0
import numpy as np
import theano
import theano.tensor as T
from theano.tensor import fft
import theano.sandbox.fourier as tsf
import Image, ImageFilter
import matplotlib.pyplot as plt
im = Image.open('./pic/f.png')


x = T.matrix('x', dtype='float64')


rfft = fft.rfft(x)
y=rfft.type()
ifft = fft.irfft(y)
f_rfft = theano.function([x],rfft)
f_ifft = theano.function([y],ifft)


out = f_rfft(im)
rec=f_ifft(out)
plt.imshow(out[:,:,0], 'gray')

plt.imshow(rec, 'gray')
plt.show()
#c_out = np.asarray(out[0, :, 0] + 1j*out[0, :, 1])
#abs_out = abs(c_out)

Пример #24
0
 def f_rfft(inp):
     return fft.rfft(inp, norm="ortho")