@@ -0,0 +1,2019 @@
+%22%22%22%0AUtilizes a neural net to predict depth (specifically, distance%0Afrom wall) given audio data (inspired by bats' echolocation)%0A%0Adata by Nathan Sprague %0Adeveloped by Nhung Hoang %0A%0AMay 18, 2017%0A%22%22%22%0Afrom keras.layers import Conv2D, Dense%0Afrom keras.layers.core import Flatten%0Afrom keras.models import Sequential%0Aimport matplotlib.pyplot as plt%0Aimport numpy as np %0Afrom scipy import io, signal #io.wavfile, signal.spectrogram%0Afrom sys import argv%0A%0Adef main():%0A%09if len(argv) != 2:%0A%09%09print %22%5Cnusage: depth_detect.py npz_file%5Cn%22%0A%09%09return%0A%09%0A%09data = np.load(argv%5B1%5D)%0A%09audio = data%5B'audio'%5D%0A%09depth = data%5B'depth'%5D%0A%0A%09shrunk = depth%5B:, 230:250, 310:330%5D%0A%09shrunk_reshaped = np.reshape(shrunk, (1159,-1))%0A%09target_set = np.max(shrunk_reshaped,axis=1)%0A%0A%09'''%0A%09fs = 10e3%0A%09N = 1e5%0A%09amp = 2 * np.sqrt(2)%0A%09noise_power = 0.01 * fs / 2%0A%09time = np.arange(N) / float(fs)%0A%09mod = 500*np.cos(2*np.pi*0.25*time)%0A%09carrier = amp * np.sin(2*np.pi*3e3*time + mod)%0A%09noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)%0A%09noise *= np.exp(-time/5)%0A%09x = carrier + noise%0A%09'''%0A%0A%09# Turn audio data into spectrograms %0A%09training_set = np.empty((1159, 129, 11)) # shape of 1159 spectrograms%0A%09for i in range(audio.shape%5B0%5D):%0A%09%09freq, time, spectro = signal.spectrogram(audio%5Bi,:%5D)%0A%09%09training_set%5Bi%5D = spectro%0A%09'''%0A%09plt.pcolormesh(time, freq, spectro)%0A%09plt.ylabel('Frequency %5BHz%5D')%0A%09plt.xlabel('Time %5Bsec%5D')%0A%09plt.show()%0A%09'''%0A%0A%09# Using these sets for now%0A%09in_train = training_set%5B:928%5D%0A%09out_train = target_set%5B:928%5D%0A%09in_test = training_set%5B928:%5D%0A%09out_test = target_set%5B928:%5D%0A%0A%09# Build neural net %0A%09net = Sequential()%0A%09INPUT_SHAPE = (in_train.shape%5B1%5D, in_train.shape%5B2%5D, 3)%0A%09net.add(Conv2D(3, (4, 4), strides=(1,1), activation='relu', input_shape=INPUT_SHAPE))%0A%09net.add(Flatten())%0A%09net.add(Dense(1, activation='relu'))%0A%09net.compile(optimizer='sgd', loss='mean_squared_error')%0A%09net.fit(in_train, out_train, validation_split=0.2)%0A%09loss = net.evaluate(in_test, out_test)%0A%09print %22%5Cnloss: %22, loss%0A%0A%09print %22%5Cn%22,%22-%22*30,%22%5Cn%22,%22 %22*12,%22DONE%5Cn%22,%22-%22*30%0A%09%0Amain()%0A

Add initial depth detection program