def loss_fft(y_true, y_pred): y_true_complex = tf.cast(y_true, dtype=tf.complex64) y_pred_complex = tf.cast(y_pred, dtype=tf.complex64) fft_true = K.abs(tf.fft(y_true_complex)) fft_pred = K.abs(tf.fft(y_pred_complex)) loss = K.mean(K.square(fft_true - fft_pred)) return loss
def _get_w(self, last_w, raw_output, memory): with tf.variable_scope('get_w'): k, beta, g, s, gamma = tf.split( raw_output, [self.M, self.N, 1, self.N, self.N], axis=1) # memory_row_norm = tf.reduce_sum(tf.abs(memory), axis=1) memory = tf.nn.l2_normalize(memory, axis=1) k = tf.nn.l2_normalize(k, axis=1) product = tf.squeeze(tf.matmul(tf.expand_dims(k, 1), memory)) # omit division of `k` since it is canceled after softmax anyway beta = tf.nn.softplus(beta, name='beta') # w_c = tf.nn.softmax(beta * product / memory_row_norm, name='w_c') w_c = tf.nn.softmax(beta * product, name='w_c') g = tf.sigmoid(g, name='gate_parameter') w_g = tf.add(g * w_c, (1 - g) * last_w, name='w_g') w_g = tf.cast(w_g, tf.complex64) # s = tf.cast(tf.nn.softmax(s), tf.complex64) s = tf.cast(tf.tanh(s, name='shift'), tf.complex64) w_tild = tf.real(tf.ifft(tf.fft(w_g) * tf.fft(s)), name='w_tild') gamma = tf.add(tf.nn.softplus(gamma), 1, name='gamma') # w = tf.pow(w_tild, gamma) # w = w / tf.reduce_sum(w) # FIXME: tf.log yields lots of NaN here!! # w = tf.nn.softmax(gamma * tf.log(tf.nn.softmax(w_tild))) w = tf.nn.softmax(gamma * tf.log(tf.nn.softplus(w_tild)), name='new_weight') # w = tf.nn.softmax(gamma * tf.log(tf.nn.sigmoid(w_tild))) # w = tf.nn.softmax(gamma * w_tild) return w
def call(self, x): p1 = self.count_sketch(x[:, :img_dim], self.output_dim, self.h1, self.s1) p2 = self.count_sketch(x[:, img_dim:], self.output_dim, self.h2, self.s2) pc1 = tf.complex(p1, tf.zeros_like(p1)) pc2 = tf.complex(p2, tf.zeros_like(p2)) conved = tf.ifft(tf.fft(pc1) * tf.fft(pc2)) return tf.real(conved)
def tensor_product(self, P, ch1, Q, ch2): P_hat = tf.fft(tf.complex(P, tf.zeros(tf.shape(P), dtype=tf.float32))) Q_hat = tf.fft(tf.complex(Q, tf.zeros(tf.shape(Q), dtype=tf.float32))) p_hat_list = [tf.squeeze(p) for p in tf.split(P_hat, self.k, axis=-1)] q_hat_list = [tf.squeeze(q) for q in tf.split(Q_hat, self.k, axis=-1)] if ch1 == 't' and ch2 == 't': S_hat = tf.concat([ tf.expand_dims(tf.matmul(tf.transpose(p_hat), tf.transpose(q_hat)), axis=-1) for (p_hat, q_hat) in zip(p_hat_list, q_hat_list) ], axis=-1) elif ch1 == 't': S_hat = tf.concat([ tf.expand_dims(tf.matmul(tf.transpose(p_hat), q_hat), axis=-1) for (p_hat, q_hat) in zip(p_hat_list, q_hat_list) ], axis=-1) elif ch2 == 't': S_hat = tf.concat([ tf.expand_dims(tf.matmul(p_hat, tf.transpose(q_hat)), axis=-1) for (p_hat, q_hat) in zip(p_hat_list, q_hat_list) ], axis=-1) else: S_hat = tf.concat([ tf.expand_dims(tf.matmul(p_hat, q_hat), axis=-1) for (p_hat, q_hat) in zip(p_hat_list, q_hat_list) ], axis=-1) return tf.real(tf.ifft(S_hat))
def TF_TSNUFFT_Run3(H, W, InImage, SNc, paddings, nTraj, nTSC, nCh, sp_R, sp_I, TSBFX): # SNx=tf.reshape(SNx,[SNx.shape[0],SNx.shape[1],1]) # InImage=tf.reshape(InImage,[InImage.shape[0],InImage.shape[1],1]) Step1 = tf.multiply(InImage, SNc) Step1 = tf.reshape(Step1, [H, W, nCh * nTSC]) Padded = tf.pad(Step1, paddings, "CONSTANT") Step2 = tf.transpose(tf.fft( tf.transpose(tf.fft(tf.transpose(Padded, perm=[2, 0, 1])), perm=[0, 2, 1])), perm=[1, 2, 0]) # Step2=tf.fft(tf.transpose(tf.fft(Padded),perm=[1,0])) Col = tf.reshape(Step2, [-1, nTSC * nCh]) ColR = tf.real(Col) ColI = tf.imag(Col) RR = tf.sparse_tensor_dense_matmul(sp_R, ColR) RI = tf.sparse_tensor_dense_matmul(sp_R, ColI) IR = tf.sparse_tensor_dense_matmul(sp_I, ColR) II = tf.sparse_tensor_dense_matmul(sp_I, ColI) R = RR - II I = RI + IR C = tf.complex(R, I) # pdb.set_trace() # CX=np.reshape(C,(nTraj,nTSC,nCh)) CX = tf.reshape(C, [nTraj, nTSC, nCh]) WithTSB = CX * TSBFX WithTSBR = tf.reduce_sum(WithTSB, axis=1) return WithTSBR
def cconv(x, y): x_fft_ = tf.fft(tf.complex(x,0.0)) #e2_fft_ = tf.fft(tf.complex(tf.nn.l2_normalize(self.e2, axis=2),0.0)) y_fft_ = tf.fft(tf.complex(y,0.0)) x_fft = x_fft_ #+ tf.complex(tf.to_float(tf.equal(x_fft_, 0.)),0.)*no_zeros y_fft = y_fft_ #+ tf.complex(tf.to_float(tf.equal(y_fft_, 0.)),0.)*no_zeros return tf.cast(tf.real(tf.ifft(tf.multiply(tf.conj(x_fft),\ y_fft))),dtype=tf.float32)
def circular_cross_correlation(x, y): """Periodic correlation, implemented using the FFT. x and y must be of the same length. """ return tf.real( tf.ifft( tf.multiply(tf.conj(tf.fft(tf.cast(x, tf.complex64))), tf.fft(tf.cast(y, tf.complex64)))))
def circular_corr(a, b, name=''): name = get_name(name, 'circular_corr') with tf.name_scope(name): a_fft = tf.conj(tf.fft(tf.complex(a, 0.0))) b_fft = tf.fft(tf.complex(b, 0.0)) ifft = tf.ifft(a_fft * b_fft) res = tf.cast(tf.real(ifft), 'float32') return res
def fft_loss(y_target, y_predicted): y_target_complex = tf.cast(y_target, dtype=tf.complex64) y_predicted_complex = tf.cast(y_predicted, dtype=tf.complex64) loss = tf.square( tf.abs(tf.fft(y_target_complex)) - tf.abs(tf.fft(y_predicted_complex))) return loss
def triple_linear_pool(x1, x2, x3, output_size): p1 = count_sketch(x1, output_size) p2 = count_sketch(x2, output_size) p3 = count_sketch(x3, output_size) pc1 = tf.complex(p1, tf.zeros_like(p1)) pc2 = tf.complex(p2, tf.zeros_like(p2)) pc3 = tf.complex(p3, tf.zeros_like(p3)) conved = tf.ifft(tf.fft(pc1) * tf.fft(pc2) * tf.fft(pc3)) return tf.real(conved)
def spectral_loss(feature1, ref): f1_trans = tf.transpose(feature1, (0, 2, 1)) f2_trans = tf.transpose(ref, (0, 2, 1)) m1 = tf.abs(tf.fft(tf.cast(f1_trans, tf.complex64))) m2 = tf.abs(tf.fft(tf.cast(f2_trans, tf.complex64))) loss = tf.reduce_mean(tf.square(m1 - m2)) return loss
def loss_fft_filter(y_true, y_pred): filter_low = 2 filter_high = 20 y_true_complex = tf.cast(y_true, dtype=tf.complex64) y_pred_complex = tf.cast(y_pred, dtype=tf.complex64) fft_true = K.abs(tf.fft(y_true_complex)) fft_pred = K.abs(tf.fft(y_pred_complex)) abs_tensor = K.square(fft_true[filter_low:filter_high] - fft_pred[filter_low:filter_high]) loss = K.mean(abs_tensor) return loss
def call(self, input): v1=input[0] v2=input[1] sketch_v1 = self.get_sketch_matrix(self.h_s[0], self.h_s[1],v1,self.d) sketch_v2 = self.get_sketch_matrix(self.h_s[2], self.h_s[3],v2,self.d) fft_1, fft_2 = tf.fft(sketch_v1), tf.fft(sketch_v2) fft_product = multiply([fft_1, fft_2]) inv_fft = tf.ifft(fft_product) sgn_sqrt = multiply([tf.real(tf.sign(inv_fft)), tf.sqrt(tf.abs(inv_fft))]) l2_norm = tf.keras.backend.l2_normalize(sgn_sqrt) return l2_norm
def emg_on_fft(y_true, y_pred): delta = tf.constant(5.0) y_true_complex = tf.cast(y_true, dtype=tf.complex64) y_pred_complex = tf.cast(y_pred, dtype=tf.complex64) fft_true = tf.real(tf.fft(y_true_complex)) fft_pred = tf.real(tf.fft(y_pred_complex)) mask = tf.greater( fft_true, delta) # boolean tensor, mask[i] = True iff fft[x] > delta fft_true = tf.boolean_mask(fft_true, mask) fft_pred = tf.boolean_mask(fft_pred, mask) loss = K.mean(K.square(fft_true - fft_pred)) return loss
def TF_NUFT(A, SN, Kd, P): # A is data, e.g. of size H,W,nMaps # SN should be from Fessler, .* Channel maps; so finally H,W,nMaps # Kd is the final size for the overFT, e.g. H*2,W*2 # P is a sparse matrix of nTraj x H*W ; <101x16320 sparse matrix of type '<class 'numpy.complex128'>' with 2525 stored elements in Compressed Sparse Column format> # MData=scipy.io.loadmat('/media/a/f38a5baa-d293-4a00-9f21-ea97f318f647/home/a/gUM/ForTFNUFT.mat') # A=MData['A'] # SN=MData['SN'] # Kd=MData['Kd'] # P=MData['P'] # NUbyFS3=MData['NUbyFS3'].T ToPad = [Kd[0, 0] - A.shape[0], Kd[0, 1] - A.shape[1]] paddings = tf.constant([[0, ToPad[0]], [0, ToPad[1]], [0, 0]]) # paddings = tf.constant([[0, 68], [0, 60]]) nMaps = 2 # A.shape[1] Idx = scipy.sparse.find(P) I2 = np.vstack([Idx[0], Idx[1]]).T sp_R = tf.SparseTensor(I2, tf.cast(np.real(Idx[2]), tf.float32), [101, 16320]) sp_I = tf.SparseTensor(I2, tf.cast(np.imag(Idx[2]), tf.float32), [101, 16320]) SNx = tf.constant(tf.cast(SN, tf.complex64)) Ax = tf.constant(tf.cast(A, tf.complex64)) SNx = tf.reshape(SNx, [SNx.shape[0], SNx.shape[1], 1]) Step1 = tf.multiply(Ax, SNx) Padded = tf.pad(Step1, paddings, "CONSTANT") Step2 = tf.transpose(tf.fft( tf.transpose(tf.fft(tf.transpose(Padded, perm=[2, 0, 1])), perm=[0, 2, 1])), perm=[1, 2, 0]) # Step2=tf.fft(tf.transpose(tf.fft(Padded),perm=[1,0])) Col = tf.reshape(Step2, [-1, nMaps]) ColR = tf.real(Col) ColI = tf.imag(Col) RR = tf.sparse_tensor_dense_matmul(sp_R, ColR) RI = tf.sparse_tensor_dense_matmul(sp_R, ColI) IR = tf.sparse_tensor_dense_matmul(sp_I, ColR) II = tf.sparse_tensor_dense_matmul(sp_I, ColI) R = RR - II I = RI + IR C = tf.complex(R, I) return C
def cir_corre(self, a, b): """Function performs circular correlation. Args: a (Tensor): Input Tensor. b (Tensor): Input Tensor. Returns: Tensor: Output Tensor after performing circular correlation. """ a = tf.cast(a, tf.complex64) b = tf.cast(b, tf.complex64) return tf.real(tf.ifft(tf.conj(tf.fft(a)) * tf.fft(b)))
def partial_circulant_tf(inputs, filters, indices, sign_pattern): n = np.prod(inputs.get_shape().as_list()[1:]) bs = inputs.get_shape().as_list()[0] input_reshape = tf.reshape(inputs, (-1, n)) input_sign = tf.multiply(input_reshape, sign_pattern) zeros_input = tf.zeros_like(input_sign) zeros_filter = tf.zeros_like(filters) complex_input = tf.complex(input_sign, zeros_input) complex_filter = tf.complex(filters, zeros_filter) output_fft = tf.multiply(tf.fft(complex_input), tf.fft(complex_filter)) output_ifft = tf.ifft(output_fft) output = tf.real(output_ifft) return tf.gather(output, indices, axis=1)
def call(self, tensors_list): bottom1, bottom2 = tensors_list # Step 1: Flatten the input tensors and count sketch bottom1_flat = tf.reshape(bottom1, [-1, self.input1_dim]) bottom2_flat = tf.reshape(bottom2, [-1, self.input2_dim]) # sketch1 = bottom1 * sparse_sketch_matrix # sketch2 = bottom2 * sparse_sketch_matrix # But tensorflow only supports left multiplying a sparse matrix, so: # sketch1 = (sparse_sketch_matrix.T * bottom1.T).T # sketch2 = (sparse_sketch_matrix.T * bottom2.T).T sparse_sketch_matrix1 = self._generate_sketch_matrix( self.rand_h_1, self.rand_s_1, self.output_dim) sparse_sketch_matrix2 = self._generate_sketch_matrix( self.rand_h_2, self.rand_s_2, self.output_dim) sketch1 = tf.transpose( tf.sparse_tensor_dense_matmul(sparse_sketch_matrix1, bottom1_flat, adjoint_a=True, adjoint_b=True)) sketch2 = tf.transpose( tf.sparse_tensor_dense_matmul(sparse_sketch_matrix2, bottom2_flat, adjoint_a=True, adjoint_b=True)) # Step 2: FFT fft1 = tf.fft(tf.complex(real=sketch1, imag=tf.zeros_like(sketch1))) fft2 = tf.fft(tf.complex(real=sketch2, imag=tf.zeros_like(sketch2))) # Step 3: Elementwise product fft_product = tf.multiply(fft1, fft2) # Step 4: Inverse FFT and reshape back # Compute output shape dynamically: [batch_size, height, width, output_dim] cbp_flat = tf.real(tf.ifft(fft_product)) output_shape = tf.add(tf.multiply(tf.shape(bottom1), [1, 1, 1, 0]), [0, 0, 0, self.output_dim]) cbp = tf.reshape(cbp_flat, output_shape) # set static shape for the output cbp.set_shape(bottom1.get_shape().as_list()[:-1] + [self.output_dim]) # print(cbp.get_shape) # Step 5: Sum pool over spatial dimensions, if specified cbp = tf.reduce_sum(cbp, reduction_indices=[1, 2]) # print(cbp.get_shape()) return cbp
def fft_of_input(nets, pad_factor, rFFT): """ Computs the fft of the signal and adds appropriate padding Parameters ---------- nets : dictionary dictionary containing parts of the cochleagram graph. 'subbands' are used for the hilbert transform pad_factor : int how much padding to add to the signal. Follows conventions of pycochleagram (ie pad of 2 doubles the signal length) rFFT : Boolean If true, cochleagram graph is constructed using rFFT wherever possible Returns ------- nets : dictionary updated dictionary containing parts of the cochleagram graph with the rFFT of the input """ # fft of the input if not rFFT: if pad_factor is not None: nets['input_signal_complex'] = tf.concat([nets['input_signal_complex'], tf.zeros([nets['input_signal_complex'].get_shape()[0], nets['input_signal_complex'].get_shape()[1]*(pad_factor-1)], dtype=tf.complex64)], axis=1) nets['fft_input'] = tf.fft(nets['input_signal_complex'],name='fft_of_input') else: nets['fft_input'] = tf.spectral.rfft(nets['input_real'],name='fft_of_input') # Since the DFT of a real signal is Hermitian-symmetric, RFFT only returns the fft_length / 2 + 1 unique components of the FFT: the zero-frequency term, followed by the fft_length / 2 positive-frequency terms. nets['fft_input'] = tf.expand_dims(nets['fft_input'], 1, name='exd_fft_of_input') return nets
def spectrogram(x, frame_length, nfft=1024): ''' Spectrogram of non-overlapping window ''' with tf.name_scope('Spectrogram'): shape = tf.shape(x) b = shape[0] D = frame_length t = shape[1] // D x = tf.reshape(x, [b, t, D]) window = tf.contrib.signal.hann_window(frame_length) window = tf.expand_dims(window, 0) window = tf.expand_dims(window, 0) # [1, 1, L] x = x * window pad = tf.zeros([b, t, nfft - D]) x = tf.concat([x, pad], -1) x = tf.cast(x, tf.complex64) X = tf.fft(x) # TF's API doesn't do padding automatically yet X = tf.log(tf.abs(X) + 1e-2) X = X[:, :, :nfft // 2 + 1] X = tf.transpose(X, [0, 2, 1]) X = tf.reverse(X, [1]) X = tf.expand_dims(X, -1) X = (X - tf.reduce_min(X)) / (tf.reduce_max(X) - tf.reduce_min(X)) X = gray2jet(X) tf.summary.image('spectrogram', X) return X
def fftc(im, name="fftc", do_orthonorm=True): """Centered FFT on second to last dimension.""" with tf.name_scope(name): im_out = im if do_orthonorm: fftscale = tf.sqrt(1.0 * im_out.get_shape().as_list()[-2]) else: fftscale = 1.0 fftscale = tf.cast(fftscale, dtype=tf.complex64) if len(im.get_shape()) == 4: im_out = tf.transpose(im_out, [0, 3, 1, 2]) im_out = fftshift(im_out, axis=3) else: im_out = tf.transpose(im_out, [2, 0, 1]) im_out = fftshift(im_out, axis=2) with tf.device('/gpu:0'): im_out = tf.fft(im_out) / fftscale if len(im.get_shape()) == 4: im_out = fftshift(im_out, axis=3) im_out = tf.transpose(im_out, [0, 2, 3, 1]) else: im_out = fftshift(im_out, axis=2) im_out = tf.transpose(im_out, [1, 2, 0]) return im_out
def forward_proj_domain(self, sinogram): """ the projection domain of the model for processing sinograms Parameters ---------- sinogram : ndarray The projection data used for processing and reconstruction Returns ------- ndarray the sinograms after processing """ self.sinogram_cosine = tf.multiply(sinogram, self.cosine_weight) self.weighted_sinogram_fft = tf.fft( tf.cast(self.sinogram_cosine, dtype=tf.complex64)) self.filtered_sinogram_fft = tf.multiply( self.weighted_sinogram_fft, tf.cast(self.recon_filter, dtype=tf.complex64)) self.filtered_sinogram = tf.real(tf.ifft(self.filtered_sinogram_fft)) return self.filtered_sinogram
def bandPass(signal, low_cut, high_cut, sample_length, sample_rate): ''' band pass filter args: signal = input signal low_cut = filtering bandwidth Hz (lower bound) high_cut = filtering bandwidth Hz (upper bound) sample_length = total signal length (number of samples) sample_rate = sampling rate of the input signal return: filtered.real = filtered signal ''' ratio = int(sample_length / sample_rate) with tf.Graph().as_default(): signal = tf.Variable(signal, dtype=tf.complex64) fft = tf.fft(signal) with tf.Session() as sess: tf.variables_initializer([signal]).run() result = sess.run(fft) for i in range(high_cut * ratio, len(result) - (high_cut * ratio) + 1): result[i] = 0 for i in range(0, (low_cut * ratio) + 1): result[i] = 0 for i in range(len(result) - (low_cut * ratio), len(result)): result[i] = 0 ifft = tf.ifft(result) with tf.Session() as sess: filtered = sess.run(ifft) return filtered.real
def test_plot_fbins(ts_data,Fs=250): L=len(ts_data[0]) print(L) pphz=L/(Fs) #number of points per 1Hz binsize_hz=((fbin_max-fbin_min)/fbin_steps) #in Hz binsize_pnts=binsize_hz*pphz #This is the mapping from FFT points returned to the frequency bins of interest bins = np.array([max(0,np.rint(pphz*(fbin_min - binsize_hz/2)))]) #fft points we don't care about bins = np.append( bins, [np.rint(binsize_pnts) for x in range(fbin_steps)] ) #actual frequency bins of interest bins = np.append( bins, (L/2) - np.sum(bins)) #remaining points on one side of the fft bins = np.append( bins, (L/2) ) #other half of fft features = list() for i in ts_data: m_fft = tf.fft(i) print(m_fft) print(bins) m_fft = tf.split(m_fft, bins.astype(int)) features.append(m_fft) p=sess.run(features) for j in range(len(p)): dic1 = plt.figure() plt.plot(fbins,[np.abs(np.sum(p[j][i][:]))/len(p[j][i][:]) for i in range(len(p[j])-1)]) dic2 = plt.figure() plt.plot(np.linspace(0,125,L/2),np.flip(np.abs(p[j][-1]),0)) dic3 = plt.figure() plt.bar(range(len(fbins)),[np.abs(np.sum(p[j][i][:]))/len(p[j][i][:]) for i in range(len(p[j])-1)]) plt.xticks(range(len(fbins)), fbins) return p
def model(self, sinogram): """ main model for the FDK algorithm Parameters ---------- sinogram : ndarray The projection data used for reconstruction Returns ------- ndarray the reconstructed CT data """ sinogram_cos = tf.multiply(sinogram, self.cosine_weight) weighted_sino_fft = tf.fft(tf.cast(sinogram_cos, dtype=tf.complex64)) filtered_sinogram_fft = tf.multiply( weighted_sino_fft, tf.cast(self.filter, dtype=tf.complex64)) filtered_sinogram = tf.real(tf.ifft(filtered_sinogram_fft)) reconstruction = cone_backprojection3d(filtered_sinogram, self.geometry, hardware_interp=True) return reconstruction
def forward_proj_domain(self, sinogram): """ the projection domain of the model for processing sinograms Parameters ---------- sinogram : ndarray The projection data used for processing and reconstruction Returns ------- ndarray the sinograms after processing """ # U-Net added in the projection domain #################################################################### sinogram = tf.expand_dims(sinogram, 3) sinogram = self.unet_model(sinogram) sinogram = tf.squeeze(sinogram, axis=3) #################################################################### self.sinogram_cosine = tf.multiply(sinogram, self.cosine_weight) self.weighted_sinogram_fft = tf.fft( tf.cast(self.sinogram_cosine, dtype=tf.complex64)) self.filtered_sinogram_fft = tf.multiply( self.weighted_sinogram_fft, tf.cast(self.recon_filter, dtype=tf.complex64)) self.filtered_sinogram = tf.real(tf.ifft(self.filtered_sinogram_fft)) return self.filtered_sinogram
def hilbert(xr): ''' Implements the hilbert transform, a mapping from C to R. Args: xr: The input sequence. Returns: xc: A complex sequence of the same length. ''' with tf.variable_scope('hilbert_transform'): n = tf.Tensor.get_shape(xr).as_list()[0] # Run the fft on the columns no the rows. x = tf.transpose(tf.fft(tf.transpose(xr))) h = np.zeros([n]) if n > 0 and 2*np.fix(n/2) == n: # even and nonempty h[0:int(n/2+1)] = 1 h[1:int(n/2)] = 2 elif n > 0: # odd and nonempty h[0] = 1 h[1:int((n+1)/2)] = 2 tf_h = tf.constant(h, name='h', dtype=tf.float32) if len(x.shape) == 2: hs = np.stack([h]*x.shape[-1], -1) reps = tf.Tensor.get_shape(x).as_list()[-1] hs = tf.stack([tf_h]*reps, -1) elif len(x.shape) == 1: hs = tf_h else: raise NotImplementedError tf_hc = tf.complex(hs, tf.zeros_like(hs)) xc = x*tf_hc return tf.transpose(tf.ifft(tf.transpose(xc)))
def deconv(comb_loss, loss_a, loss_b): x = tf.cast(comb_loss[:, 0], dtype=tf.complex64) y = tf.cast(comb_loss[:, 1], dtype=tf.complex64) yfft = tf.fft(y) ay = tf.cast(loss_a[:, 1], dtype=tf.complex64) by = tf.cast(loss_b[:, 1], dtype=tf.complex64) ayfft = tf.fft(ay) byfft = tf.fft(by) ayfftest = yfft / byfft ayest = tf.abs(tf.ifft(ayfftest)) aest = tf.cast(tf.abs(tf.ifft(ayfft)), dtype=tf.float64) best = tf.cast(tf.abs(tf.ifft(byfft)), dtype=tf.float64) return best
def encode_fft(audio, size=512, steps=512, step_offset=0): return_values = [] with tf.variable_scope("encode_fft", reuse=encode_fft_reuse): input = tf.placeholder(tf.float32, [size], "input") output = tf.fft(tf.cast(input, tf.complex64)) output = output[:int(size / 2)] output = output[:int(source_size)] sess = tf.get_default_session() for i in range(steps): offset = step_offset + i * size feed_audio = audio[offset:offset + size, 0] values = sess.run({"output": output}, feed_dict={input: feed_audio}) output_values = values["output"] output_values = np.expand_dims(output_values, axis=0) return_values.append(output_values) global encode_fft_reuse encode_fft_reuse = True return np.concatenate(return_values, axis=0)
def fftc(im, data_format='channels_last', orthonorm=True, transpose=False, name='fftc'): """Centered FFT on last non-channel dimension.""" with tf.name_scope(name): im_out = im if data_format == 'channels_last': permute_orig = np.arange(len(im.shape)) permute = permute_orig.copy() permute[-2] = permute_orig[-1] permute[-1] = permute_orig[-2] im_out = tf.transpose(im_out, permute) if orthonorm: fftscale = tf.sqrt(tf.cast(im_out.shape[-1], tf.float32)) else: fftscale = 1.0 fftscale = tf.cast(fftscale, dtype=tf.complex64) im_out = fftshift(im_out, axis=-1) if transpose: im_out = tf.ifft(im_out) * fftscale else: im_out = tf.fft(im_out) / fftscale im_out = fftshift(im_out, axis=-1) if data_format == 'channels_last': im_out = tf.transpose(im_out, permute) return im_out
def op(self): xf = tensorflow.fft(self.x) x2 = xf * tensorflow.conj(xf) xt = tensorflow.ifft(x2) xr = 10*tensorflow.log( tensorflow.abs( xt[:,0:self.aclen] ) ) if self.avg: N = tensorflow.shape(xr)[0] idx = tensorflow.cast(tensorflow.range(0,N), tensorflow.float32) s = tensorflow.reshape( self.alpha * tensorflow.pow( (1-self.alpha), idx ), [N,1] ) self.u = tensorflow.pow( (1-self.alpha), tensorflow.cast(N,tensorflow.float32) )*self.u + tensorflow.reduce_sum(s*xr, 0) return self.u else: return xr
def auto_correlation( x, axis=-1, max_lags=None, center=True, normalize=True, name="auto_correlation"): """Auto correlation along one axis. Given a `1-D` wide sense stationary (WSS) sequence `X`, the auto correlation `RXX` may be defined as (with `E` expectation and `Conj` complex conjugate) ``` RXX[m] := E{ W[m] Conj(W[0]) } = E{ W[0] Conj(W[-m]) }, W[n] := (X[n] - MU) / S, MU := E{ X[0] }, S**2 := E{ (X[0] - MU) Conj(X[0] - MU) }. ``` This function takes the viewpoint that `x` is (along one axis) a finite sub-sequence of a realization of (WSS) `X`, and then uses `x` to produce an estimate of `RXX[m]` as follows: After extending `x` from length `L` to `inf` by zero padding, the auto correlation estimate `rxx[m]` is computed for `m = 0, 1, ..., max_lags` as ``` rxx[m] := (L - m)**-1 sum_n w[n + m] Conj(w[n]), w[n] := (x[n] - mu) / s, mu := L**-1 sum_n x[n], s**2 := L**-1 sum_n (x[n] - mu) Conj(x[n] - mu) ``` The error in this estimate is proportional to `1 / sqrt(len(x) - m)`, so users often set `max_lags` small enough so that the entire output is meaningful. Note that since `mu` is an imperfect estimate of `E{ X[0] }`, and we divide by `len(x) - m` rather than `len(x) - m - 1`, our estimate of auto correlation contains a slight bias, which goes to zero as `len(x) - m --> infinity`. Args: x: `float32` or `complex64` `Tensor`. axis: Python `int`. The axis number along which to compute correlation. Other dimensions index different batch members. max_lags: Positive `int` tensor. The maximum value of `m` to consider (in equation above). If `max_lags >= x.shape[axis]`, we effectively re-set `max_lags` to `x.shape[axis] - 1`. center: Python `bool`. If `False`, do not subtract the mean estimate `mu` from `x[n]` when forming `w[n]`. normalize: Python `bool`. If `False`, do not divide by the variance estimate `s**2` when forming `w[n]`. name: `String` name to prepend to created ops. Returns: `rxx`: `Tensor` of same `dtype` as `x`. `rxx.shape[i] = x.shape[i]` for `i != axis`, and `rxx.shape[axis] = max_lags + 1`. Raises: TypeError: If `x` is not a supported type. """ # Implementation details: # Extend length N / 2 1-D array x to length N by zero padding onto the end. # Then, set # F[x]_k := sum_n x_n exp{-i 2 pi k n / N }. # It is not hard to see that # F[x]_k Conj(F[x]_k) = F[R]_k, where # R_m := sum_n x_n Conj(x_{(n - m) mod N}). # One can also check that R_m / (N / 2 - m) is an unbiased estimate of RXX[m]. # Since F[x] is the DFT of x, this leads us to a zero-padding and FFT/IFFT # based version of estimating RXX. # Note that this is a special case of the Wiener-Khinchin Theorem. with tf.name_scope(name, values=[x]): x = tf.convert_to_tensor(x, name="x") # Rotate dimensions of x in order to put axis at the rightmost dim. # FFT op requires this. rank = util.prefer_static_rank(x) if axis < 0: axis = rank + axis shift = rank - 1 - axis # Suppose x.shape[axis] = T, so there are T "time" steps. # ==> x_rotated.shape = B + [T], # where B is x_rotated's batch shape. x_rotated = util.rotate_transpose(x, shift) if center: x_rotated -= tf.reduce_mean(x_rotated, axis=-1, keepdims=True) # x_len = N / 2 from above explanation. The length of x along axis. # Get a value for x_len that works in all cases. x_len = util.prefer_static_shape(x_rotated)[-1] # TODO(langmore) Investigate whether this zero padding helps or hurts. At # the moment is is necessary so that all FFT implementations work. # Zero pad to the next power of 2 greater than 2 * x_len, which equals # 2**(ceil(Log_2(2 * x_len))). Note: Log_2(X) = Log_e(X) / Log_e(2). x_len_float64 = tf.cast(x_len, np.float64) target_length = tf.pow( np.float64(2.), tf.ceil(tf.log(x_len_float64 * 2) / np.log(2.))) pad_length = tf.cast(target_length - x_len_float64, np.int32) # We should have: # x_rotated_pad.shape = x_rotated.shape[:-1] + [T + pad_length] # = B + [T + pad_length] x_rotated_pad = util.pad(x_rotated, axis=-1, back=True, count=pad_length) dtype = x.dtype if not dtype.is_complex: if not dtype.is_floating: raise TypeError("Argument x must have either float or complex dtype" " found: {}".format(dtype)) x_rotated_pad = tf.complex(x_rotated_pad, dtype.real_dtype.as_numpy_dtype(0.)) # Autocorrelation is IFFT of power-spectral density (up to some scaling). fft_x_rotated_pad = tf.fft(x_rotated_pad) spectral_density = fft_x_rotated_pad * tf.conj(fft_x_rotated_pad) # shifted_product is R[m] from above detailed explanation. # It is the inner product sum_n X[n] * Conj(X[n - m]). shifted_product = tf.ifft(spectral_density) # Cast back to real-valued if x was real to begin with. shifted_product = tf.cast(shifted_product, dtype) # Figure out if we can deduce the final static shape, and set max_lags. # Use x_rotated as a reference, because it has the time dimension in the far # right, and was created before we performed all sorts of crazy shape # manipulations. know_static_shape = True if not x_rotated.shape.is_fully_defined(): know_static_shape = False if max_lags is None: max_lags = x_len - 1 else: max_lags = tf.convert_to_tensor(max_lags, name="max_lags") max_lags_ = tensor_util.constant_value(max_lags) if max_lags_ is None or not know_static_shape: know_static_shape = False max_lags = tf.minimum(x_len - 1, max_lags) else: max_lags = min(x_len - 1, max_lags_) # Chop off the padding. # We allow users to provide a huge max_lags, but cut it off here. # shifted_product_chopped.shape = x_rotated.shape[:-1] + [max_lags] shifted_product_chopped = shifted_product[..., :max_lags + 1] # If possible, set shape. if know_static_shape: chopped_shape = x_rotated.shape.as_list() chopped_shape[-1] = min(x_len, max_lags + 1) shifted_product_chopped.set_shape(chopped_shape) # Recall R[m] is a sum of N / 2 - m nonzero terms x[n] Conj(x[n - m]). The # other terms were zeros arising only due to zero padding. # `denominator = (N / 2 - m)` (defined below) is the proper term to # divide by by to make this an unbiased estimate of the expectation # E[X[n] Conj(X[n - m])]. x_len = tf.cast(x_len, dtype.real_dtype) max_lags = tf.cast(max_lags, dtype.real_dtype) denominator = x_len - tf.range(0., max_lags + 1.) denominator = tf.cast(denominator, dtype) shifted_product_rotated = shifted_product_chopped / denominator if normalize: shifted_product_rotated /= shifted_product_rotated[..., :1] # Transpose dimensions back to those of x. return util.rotate_transpose(shifted_product_rotated, -shift)
def _cconv(self, a, b): return tf.ifft(tf.fft(a) * tf.fft(b)).real
def _ccorr(self, a, b): a = tf.cast(a, tf.complex64) b = tf.cast(b, tf.complex64) return tf.real(tf.ifft(tf.conj(tf.fft(a)) * tf.fft(b)))