def stft(wav, n_fft=1024, overlap=4, dt=tf.int32, absp=False):
assert (wav.shape[0] > n_fft)
X = tf.placeholder(dtype=dt,shape=wav.shape)
X = tf.cast(X,tf.float32)
hop = n_fft / overlap
## prepare constant variable
Pi = tf.constant(np.pi, dtype=tf.float32)
W = tf.constant(scipy.hanning(n_fft), dtype=tf.float32)
S = tf.pack([tf.fft(tf.cast(tf.multiply(W,X[i:i+n_fft]),\
tf.complex64)) for i in range(1, wav.shape[0] - n_fft, hop)])
abs_S = tf.complex_abs(S)
sess = tf.Session()
if absp:
return sess.run(abs_S, feed_dict={X:wav})
else:
return sess.run(S, feed_dict={X:wav})
python类fft()的实例源码
compact_bilinear_pooling.py 文件源码
项目:tensorflow_compact_bilinear_pooling
作者: ronghanghu
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def _fft(bottom, sequential, compute_size):
if sequential:
return sequential_batch_fft(bottom, compute_size)
else:
return tf.fft(bottom)
def FFT(z):
return tf.fft(z)
def fft_circ_conv1d(X, keys, batch_size, num_copies, num_keys=None, conj=False):
if conj:
keys = HolographicMemory.conj_real_by_complex(keys)
# Get our original shapes
xshp = X.get_shape().as_list()
kshp = keys.get_shape().as_list()
kshp[0] = num_keys if num_keys is not None else kshp[0]
kshp[1] = xshp[1] if kshp[1] is None else kshp[1]
print 'X : ', xshp, ' | keys : ', kshp, ' | batch_size = ', batch_size
# duplicate out input data by the ratio: number_keys / batch_size
# eg: |input| = [2, 784] ; |keys| = 3*[2, 784] ; (3 is the num_copies)
# |new_input| = 6/2 |input| = [input; input; input]
#
# At test: |memories| = [3, 784] ; |keys| = 3*[n, 784] ;
# |new_input| = 3n / 3 = n [where n is the number of desired parallel retrievals]
num_dupes = kshp[0] / batch_size
print 'num dupes = ', num_dupes
xcplx = HolographicMemory.split_to_complex(tf.tile(X, [num_dupes, 1]) \
if num_dupes > 1 else X)
xshp = xcplx.get_shape().as_list()
kcplx = HolographicMemory.split_to_complex(keys, kshp)
# Convolve & re-cast to a real valued function
unsplit_func = HolographicMemory.unsplit_from_complex_ri if not conj \
else HolographicMemory.unsplit_from_complex_ir
#fft_mul = HolographicMemory.bound(tf.mul(tf.fft(xcplx), tf.fft(kcplx)))
fft_mul = tf.mul(tf.fft(xcplx), tf.fft(kcplx))
conv = unsplit_func(tf.ifft(fft_mul))
print 'full conv = ', conv.get_shape().as_list()
batch_iter = min(batch_size, xshp[0]) if xshp[0] is not None else batch_size
print 'batch = ', batch_size, ' | num_copies = ', num_copies, '| num_keys = ', num_keys, \
'| xshp[0] = ', xshp[0], ' | len(keys) = ', kshp[0], ' | batch iter = ', batch_iter
conv_concat = [tf.expand_dims(tf.reduce_mean(conv[begin:end], 0), 0)
for begin, end in zip(range(0, kshp[0], batch_iter),
range(batch_iter, kshp[0]+1, batch_iter))]
print 'conv concat = ', len(conv_concat), ' x ', conv_concat[0].get_shape().as_list()
# return a single concatenated tensor:
# C = [c0; c1; ...]
C = tf.concat(0, conv_concat)
return C
#C = tf_mean_std_normalize(C)
#return C / tf.maximum(tf.reduce_max(C), 1e-20)
#return tf.nn.sigmoid(C)
#return tf_mean_std_normalize(C)
def create_network():
dp = tflearn.data_preprocessing.DataPreprocessing()
dp.add_featurewise_zero_center()
dp.add_featurewise_stdnorm()
#dp.add_samplewise_zero_center()
#dp.add_samplewise_stdnorm()
network = tflearn.input_data(shape=[None, chunk_size])#, data_preprocessing=dp)
# input is a real signal
network = tf.complex(network, 0.0)
# fft the input
input_fft = tf.fft(network)
input_orig_fft = input_fft
input_fft = tf.stack([tf.real(input_fft), tf.imag(input_fft)], axis=2)
fft_size = int(input_fft.shape[1])
network = input_fft
print("fft shape: " + str(input_fft.get_shape()))
omg = fft_size
nn_reg = None
mask = network
mask = tflearn.layers.fully_connected(mask, omg*2, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
mask = tflearn.layers.fully_connected(mask, omg, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
mask = tflearn.layers.fully_connected(mask, omg/2, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
#mask = tflearn.layers.fully_connected(mask, omg/4, activation="tanh")
mask = tflearn.reshape(mask, [-1, 1, omg/2])
mask = tflearn.layers.recurrent.lstm(mask, omg/4)
mask = tflearn.layers.fully_connected(mask, omg/2, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
mask = tflearn.layers.fully_connected(mask, omg, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
mask = tflearn.layers.fully_connected(mask, omg*2, activation="tanh", regularizer=nn_reg)
mask = tflearn.layers.normalization.batch_normalization(mask)
mask = tflearn.layers.fully_connected(mask, omg, activation="sigmoid", regularizer=nn_reg)
real = tf.multiply(tf.real(input_orig_fft), mask)
imag = tf.multiply(tf.imag(input_orig_fft), mask)
network = tf.real(tf.ifft(tf.complex(real, imag)))
print("final shape: " + str(network.get_shape()))
network = tflearn.regression(network, optimizer="adam", learning_rate=learning_rate, loss="mean_square")
return network