def generate_sinc_filterbank(f0, f1, J, N): # get the center frequencies freqs = get_scaled_freqs(f0, f1, J + 1) # make it with difference and make it a variable freqs = np.stack([freqs[:-1], freqs[1:]], 1) freqs[:, 1] -= freqs[:, 0] freqs = T.Variable(freqs, name='c_freq') # parametrize the frequencies f0 = T.abs(freqs[:, 0]) f1 = f0 + T.abs(freqs[:, 1]) # sampled the bandpass filters time = T.linspace(-N // 2, N // 2 - 1, N) time_matrix = time.reshape((-1, 1)) sincs = T.signal.sinc_bandpass(time_matrix, f0, f1) # apodize apod_filters = sincs * T.signal.hanning(N).reshape((-1, 1)) # normalize normed_filters = apod_filters / T.linalg.norm( apod_filters, 2, 0, keepdims=True) filters = T.transpose(T.expand_dims(normed_filters, 1), [2, 1, 0]) return filters, freqs
def generate_gaussian_filterbank(N, M, J, f0, f1, modes=1): # gaussian parameters freqs = get_scaled_freqs(f0, f1, J) freqs *= (J - 1) * 10 if modes > 1: other_modes = np.random.randint(0, J, J * (modes - 1)) freqs = np.concatenate([freqs, freqs[other_modes]]) # crate the average vectors mu_init = np.stack([freqs, 0.1 * np.random.randn(J * modes)], 1) mu = T.Variable(mu_init.astype('float32'), name='mu') # create the covariance matrix cor = T.Variable(0.01 * np.random.randn(J * modes).astype('float32'), name='cor') sigma_init = np.stack([freqs / 6, 1. + 0.01 * np.random.randn(J * modes)], 1) sigma = T.Variable(sigma_init.astype('float32'), name='sigma') # create the mixing coefficients mixing = T.Variable(np.ones((modes, 1, 1)).astype('float32')) # now apply our parametrization coeff = T.stop_gradient(T.sqrt((T.abs(sigma) + 0.1).prod(1))) * 0.95 Id = T.eye(2) cov = Id * T.expand_dims((T.abs(sigma)+0.1),1) +\ T.flip(Id, 0) * (T.tanh(cor) * coeff).reshape((-1, 1, 1)) cov_inv = T.linalg.inv(cov) # get the gaussian filters time = T.linspace(-5, 5, M) freq = T.linspace(0, J * 10, N) x, y = T.meshgrid(time, freq) grid = T.stack([y.flatten(), x.flatten()], 1) centered = grid - T.expand_dims(mu, 1) # asdf gaussian = T.exp(-(T.matmul(centered, cov_inv)**2).sum(-1)) norm = T.linalg.norm(gaussian, 2, 1, keepdims=True) gaussian_2d = T.abs(mixing) * T.reshape(gaussian / norm, (J, modes, N, M)) return gaussian_2d.sum(1, keepdims=True), mu, cor, sigma, mixing
import sys sys.path.insert(0, "../") import symjax as sj import symjax.tensor as T import matplotlib.pyplot as plt import matplotlib matplotlib.use('Agg') ###### 2D GAUSSIAN EXAMPLE t = T.linspace(-5, 5, 5) x, y = T.meshgrid(t, t) X = T.stack([x.flatten(), y.flatten()], 1) p = T.pdfs.multivariate_normal.pdf(X, T.zeros(2), T.eye(2)) p = p.reshape((5, 5)).round(2) print(p) # Tensor(Op=round_, shape=(5, 5), dtype=float32) # lazy evaluation (not compiled nor optimized) print(p.get()) # [[0. 0. 0. 0. 0. ] # [0. 0. 0.01 0. 0. ] # [0. 0.01 0.16 0.01 0. ] # [0. 0. 0.01 0. 0. ] # [0. 0. 0. 0. 0. ]] # create the function which internall compiles and optimizes
randn = T.random.randn(SHAPE) rand = T.random.rand(SHAPE) out = randn out2 = out.clone({randn: rand}) out3 = out2.clone({rand: 3}) get_vars = symjax.function(outputs=[out, out2]) for i in range(10): print(get_vars()) asdf # test shuffle matrix = T.linspace(0, 1, 16).reshape((4, 4)) smatrix = matrix[T.random.permutation(T.range(4))] get_shuffle = symjax.function(outputs=smatrix) for i in range(10): print(get_shuffle()) asdfsadf # test the random uniform SHAPE = (2, 2) z = T.Variable(np.random.randn(*SHAPE).astype("float32"), name="z") get_z = symjax.function(outputs=z) for i in range(10):
import symjax import symjax.tensor as T import matplotlib.pyplot as plt import numpy as np J = 5 Q = 4 scales = T.power(2, T.linspace(0.1, J - 1, J * Q)) scales = scales[:, None] print(scales.get()) wavelet = symjax.tensor.signal.complex_morlet(5 * scales, np.pi / scales) waveletw = symjax.tensor.signal.fourier_complex_morlet(5 * scales, np.pi / scales, wavelet.shape[-1]) waveletlp = symjax.tensor.signal.littewood_paley_normalization(waveletw, down=np.pi / scales[-1, 0]) wavelet = wavelet.get() waveletw = waveletw.get() waveletlp = waveletlp.get() plt.subplot(321) for i in range(J * Q): fr = np.real(np.fft.fft(np.fft.ifftshift(wavelet[i]))) fi = np.imag(np.fft.fft(np.fft.ifftshift(wavelet[i]))) plt.plot(i + fr, '--b') plt.plot(i + fi, '--r')