Skip to content

Instantly share code, notes, and snippets.

View 0xfe's full-sized avatar
💭
miles to go before i sleep

Mohit Cheppudira 0xfe

💭
miles to go before i sleep
View GitHub Profile
@0xfe
0xfe / fft_nn_plot_weights.py
Created March 2, 2020 18:48
Plot weights of FFT NN model
def plot_weights(index, weights=model.get_weights()[0], N=fft_size):
plt.figure(figsize=(9, 3))
plt.subplot(1, 2, 1)
plt.plot(weights[index][:N])
plt.plot(weights[index][N:], 'g-')
plt.subplot(1, 2, 2)
plt.plot(weights[fft_size+index][:N])
plt.plot(weights[fft_size+index][N:], 'g-')
plt.show()
@0xfe
0xfe / dft_nn.py
Last active March 2, 2020 18:37
Neural Network to learn the Discrete Fourier Transform
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
batch_size = 15000
fft_size = 128
xs = np.random.randn(batch_size, fft_size) + np.random.randn(batch_size, fft_size) * 1j
ys = np.fft.fft(xs, axis=1)
@0xfe
0xfe / peek_nn_spectrogram_weights_2.py
Created March 2, 2020 16:48
Peek into Spectrogram ANN weights (1D)
def plot_weights(index, weights=weights_sxx):
plt.figure(figsize=(15, 2))
plt.subplot(1, 3, 1)
plt.plot(weights[index][0])
plt.subplot(1, 3, 2)
plt.plot(weights[index][5])
plt.subplot(1, 3, 3)
plt.plot(weights[index][10])
plt.show()
@0xfe
0xfe / peek_nn_spectrogram_weights.py
Created March 2, 2020 16:42
Peek into NN layers
weights_sxx = model.get_weights()[0]
weights_sxx = np.reshape(weights_sxx, (size, rows * 2, cols))
weights_sxx = np.moveaxis(weights_sxx, (0,1,2), (2,1,0))
plt.figure(figsize=(12, 7))
plt.subplot(2, 2, 1)
plt.pcolormesh(weights_sxx[0], cmap="Accent")
plt.subplot(2, 2, 2)
plt.pcolormesh(weights_sxx[10], cmap="Accent")
plt.subplot(2, 2, 3)
@0xfe
0xfe / compare_scipy_vs_nn_log_scale.py
Created March 2, 2020 16:38
Compare SciPy vs ANN: log-scaled spectrograms
plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)
plt.pcolormesh(t, f, np.log(wave_sxx_graph))
plt.subplot(1, 2, 2)
plt.pcolormesh(t, f, np.log(nn_sxx_graph))
plt.show()
@0xfe
0xfe / spectrogram_scipy_vs_nn.py
Created March 2, 2020 16:37
Compare SciPy vs ANN in generating spectrograms
def make_wave():
# Create four evenly-spaced tones
waves = []
hz = 0
for i in range(0, size, 512):
hz = hz + 50
waves.append(np.sin(np.linspace(0, 2 * np.pi * hz, 512)))
return np.hstack(waves)
wave = make_wave()
@0xfe
0xfe / spectrogram_keras_model.py
Created March 2, 2020 16:31
Keras model for training spectrogram NN
import tensorflow as tf
size = 2048
N = 15000
(xs, ys, rows, cols) = build_training_data(N, size)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(rows * cols * 2, input_dim=size, use_bias=False)
])
@0xfe
0xfe / spectrogram_build_training_data.py
Created March 2, 2020 16:30
Generate training data for spectrogram
def build_training_data(batch_size, sample_size):
xs = np.random.randn(batch_size, sample_size)
f, t, ys = spectrogram(xs)
(num_rows, num_cols) = (ys.shape[1], ys.shape[2])
print(ys.shape)
ys = ys.reshape(batch_size, num_rows * num_cols)
Ys = np.hstack([ys.real, ys.imag])
return (xs, Ys, num_rows, num_cols)
@0xfe
0xfe / spectrogram.py
Created March 2, 2020 16:27
Return the spectrogram of a complex-valued signal
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
def spectrogram(xs):
return signal.spectrogram(xs, 256, window=('hann'), nperseg=256, nfft=256, noverlap=200, mode='complex')
@0xfe
0xfe / impulse-fft-inverse.py
Created February 28, 2020 11:42
Inverse FFT on Sine Wave
x = np.linspace(0, 2 * np.pi * hz, points * hz)
y = np.sin(x)
plt.figure(figsize=(10, 3))
plt.bar(np.arange(75), y[:75 * 10:10]) # sample every 10th element for bar graph
plt.show()
fft_y = fft.ifft(y.astype("complex64")) * 2
plt.figure(figsize=(10, 3))
plt.bar(np.arange(100), np.absolute(fft_y[:100]))
plt.show()