def generate_data(system): # Training set A = 1 noises, noise_responses = [], [] for _ in range(NOISE_NUM): noise = gaussian_white_noise(A, NOISE_LEN, NOISE_LEN) noises.append(noise) noise_responses.append(system(noise)) training_set = DataSet(noises, noise_responses, memory_depth=NN_MEM_DEPTH, intensity=A) # Validation set fs = 20000 freq_pool = [[8000], [1200, 7690], [560, 1400, 8000], [3200, 4550, 6710, 8190], [1100, 3200, 5210, 7019, 7200]] duration = 2 noise_power = 1e-3 val_signals, val_responses = [], [] for freqs in freq_pool: val_signal = multi_tone(freqs, fs, duration, noise_power=noise_power) val_signals.append(val_signal) val_responses.append(system(val_signal)) validation_set = DataSet(val_signals, val_responses, memory_depth=NN_MEM_DEPTH) # Test set test_signal = multi_tone(TEST_FREQS, fs, duration, noise_power=noise_power) test_response = system(test_signal) test_set = DataSet(test_signal, test_response, memory_depth=NN_MEM_DEPTH) return training_set, validation_set, test_set
def psudo_import_data(): from signals.generator import multi_tone from systems import Volterra # Generate input signal fs, duration = 2000, 100 freqs, vrms = [500, 800], [2, 1] phases = [0, np.pi] x = multi_tone(freqs, fs, duration, vrms=vrms, phases=phases, noise_power=1e-3) assert isinstance(x, Signal) # Generate targets black_box = Volterra() black_box.set_kernel((0, ), 1) black_box.set_kernel((5, ), 0.4) black_box.set_kernel((1, 4), 0.1) black_box.set_kernel((2, 3), 0.1) y = black_box(x) assert isinstance(y, Signal) return x, y
def define_and_plot(*args, **kwargs): from signals.generator import multi_tone from signals.utils import Figure, Subplot # Initiate model model = Volterra() model.set_kernel((0, ), 1.0) model.set_kernel((2, ), 0.0) model.set_kernel((0, 0), 0.0) model.set_kernel((2, 2), 0.0) model.set_kernel((1, 0, 0), 0.2) model.set_kernel((2, 2, 2), 0.0) # Generate multi tone signal freqs = [160, 220] signal = multi_tone(freqs, 1000, 2, noise_power=1e-3) response = model(signal) # Plot title = 'Volterra Response, Input freqs = {}'.format(freqs) fig = Figure(title) fig.add(Subplot.PowerSpectrum(signal, prefix='Input Signal')) prefix = 'System Response' fig.add(Subplot.PowerSpectrum(response, prefix=prefix)) fig.plot()
def separate_test(*args, **kwargs): from signals.generator import multi_tone from signals.utils import Figure, Subplot # Initiate model model = Volterra(degree=3, memory_depth=3) model.set_kernel((0, ), 1.0) model.set_kernel((1, ), 0.2) model.set_kernel((2, ), 0.0) model.set_kernel((0, 0), 0.0) model.set_kernel((1, 0), 0.0) model.set_kernel((1, 1), 0.0) model.set_kernel((2, 0), 0.1) model.set_kernel((2, 1), 0.0) model.set_kernel((2, 2), 0.1) model.set_kernel((0, 0, 0), 0.0) model.set_kernel((1, 0, 0), 0.0) model.set_kernel((1, 1, 0), 0.0) model.set_kernel((1, 1, 1), 0.001) model.set_kernel((2, 0, 0), 0.0) model.set_kernel((2, 1, 0), 0.0) model.set_kernel((2, 2, 0), 0.002) model.set_kernel((2, 2, 1), 0.0) model.set_kernel((2, 2, 2), 0.0) # Generate multi tone signal freqs = [160, 220] signal = multi_tone(freqs, 1000, 2, noise_power=1e-3) response = model(signal) max_order = 2 yn = model.separate_interp(signal, max_order=max_order, verbose=True) truth = model.separate(signal, max_order=max_order) print('>> rms(response) = {:.4f}'.format( np.linalg.norm(response) / signal.size)) for n in range(len(yn)): delta = np.linalg.norm(yn[n] - truth[n]) / signal.size print(':: Delta_{} = {:.4f}'.format(n + 1, delta)) # Separate Kernel # Plot bshow = True if bshow: for n in range(len(yn)): fig = Figure('Volterra Separation order - {}'.format(n + 1)) fig.add(Subplot.PowerSpectrum(response, prefix='response')) fig.add( Subplot.PowerSpectrum(truth[n], prefix='truth order - {}'.format(n + 1))) fig.add( Subplot.PowerSpectrum(yn[n], prefix='pred order - {}'.format(n + 1))) fig.plot()
def generate_data(): system = Volterra() # order 1 system.set_kernel((0, ), 1.0) system.set_kernel((10, ), 0.2) system.set_kernel((20, ), 0.03) # order 2 system.set_kernel((0, 0), 0.03) system.set_kernel((10, 20), 0.01) # order 3 system.set_kernel((10, 20, 10), 0.001) system.set_kernel((0, 20, 10), 0.0005) # order 4 system.set_kernel((0, 20, 10, 30), 0.0001) system.set_kernel((0, 10, 10, 10), 0.0001) # order 5 system.set_kernel((0, 10, 20, 10, 30), 0.00001) system.set_kernel((0, 10, 20, 20, 30), 0.00001) # order 6 system.set_kernel((0, 10, 20, 20, 30, 20), 0.000001) system.set_kernel((0, 10, 20, 20, 30, 30), 0.000001) # order 7 system.set_kernel((0, 10, 20, 20, 30, 30, 20), 0.0000001) system.set_kernel((0, 10, 20, 20, 30, 30, 10), 0.0000001) # order 8 system.set_kernel((0, 10, 20, 20, 30, 30, 10, 10), 0.0000001) system.set_kernel((0, 10, 20, 20, 30, 30, 10, 0), 0.0000001) # order 9 system.set_kernel((0, 10, 20, 20, 30, 30, 10, 0, 10), 0.0000001) system.set_kernel((0, 10, 20, 20, 30, 30, 10, 0, 10), 0.0000001) sig_length = 100000 intensity = 1 fs = 20000 noise = gaussian_white_noise(intensity, sig_length, fs) system_output_tra = system(noise) tra_features = noise[:80000] tra_targets = system_output_tra[:80000] val_features = noise[80000:] val_targets = system_output_tra[80000:] freqs = [5000, 6000] mulit_tone_signal = multi_tone(freqs, fs, 3) systemoutput_test = system(mulit_tone_signal) train_set = DataSet(tra_features, tra_targets, memory_depth=30) val_set = DataSet(val_features, val_targets, memory_depth=30) test_set = DataSet(mulit_tone_signal, systemoutput_test, memory_depth=30) return train_set, val_set, test_set
def load_svn1997(depth): A = 1 L = 1000 * 100 fs = 1000 x = gaussian_white_noise(1, L, fs) y = sys97(x) train_data = DataSet(x, y, intensity=A, memory_depth=depth) val_x = multi_tone([100, 260], fs, 1, noise_power=1e-4) val_y = sys97(val_x) val_data = DataSet(val_x, val_y, memory_depth=depth) return train_data, val_data
def define_and_plot(*args, **kwargs): from signals.generator import multi_tone from signals.utils import Figure, Subplot # Initiate model model = Volterra(degree=3, memory_depth=3) model.set_kernel((0, ), 1.0) model.set_kernel((1, ), 0.3) model.set_kernel((2, ), 0.0) model.set_kernel((0, 0), 0.0) model.set_kernel((1, 0), 0.0) model.set_kernel((1, 1), 0.0) model.set_kernel((2, 0), 0.1) model.set_kernel((2, 1), 0.0) model.set_kernel((2, 2), 0.0) model.set_kernel((0, 0, 0), 0.0) model.set_kernel((1, 0, 0), 0.0) model.set_kernel((1, 1, 0), 0.0) model.set_kernel((1, 1, 1), 0.0) model.set_kernel((2, 0, 0), 0.0) model.set_kernel((2, 1, 0), 0.0) model.set_kernel((2, 2, 0), 0.0) model.set_kernel((2, 2, 1), 0.0) model.set_kernel((2, 2, 2), 0.0) # Generate multi tone signal freqs = [160, 220] signal = multi_tone(freqs, 1000, 2, noise_power=1e-3) order = 2 response = model.inference(signal, order) delta = np.linalg.norm(signal - response) / signal.size print('>> Delta = {:.4f}'.format(delta)) # Plot title = 'Volterra Response, Input freqs = {}'.format(freqs) fig = Figure(title) fig.add(Subplot.PowerSpectrum(signal, prefix='Input Signal')) prefix = 'System Response{}'.format( ' Order-{}'.format(order) if not order is None else '') response = model(signal) - model.inference(signal, 2) fig.add(Subplot.PowerSpectrum(response, prefix=prefix)) fig.plot()
noise_responses = [] for i in range(num): noise = gaussian_white_noise(A, N, N) noise_response = system(noise) noises.append(noise) noise_responses.append(noise_response) train_set = DataSet(noises, noise_responses, memory_depth=3, intensity=A) # endregion : Generate data sets # Prepare test signal freqs = [120, 310] fs = 1000 duration = 2 vrms = [1, 0.7] noise_power = 1e-3 signal = multi_tone(freqs, fs, duration, vrms, noise_power=noise_power) system_output = system(signal) val_set = DataSet(signal, system_output, memory_depth=3) # Wiener degree = 3 memory_depth = 3 wiener = Wiener(degree, memory_depth) # wiener.cross_correlation(noises[0], noise_responses[0], A) wiener.identify(train_set, val_set) # MLP mlp = model_lib.mlp_00(memory_depth, mlp_mark) if FLAGS.train:
plt.xlabel(xlabel) if ylabel is not None: plt.ylabel(ylabel) if legends is not None: plt.legend(legends) plt.grid(grid) # endregion : Private Methods """For some reason, do not delete this line""" if __name__ == '__main__': from signals.generator import multi_tone fs = 2000 duration = 1 freqs = [500, 800] vrms = [2, 1] phases = [0, np.pi] signal = multi_tone(freqs, fs, duration, vrms=vrms, phases=phases, noise_power=1e-2) fig = Figure(signal.info_string) fig.add(Subplot.TimeDomainPlot(signal, prefix='Input signal')) fig.add(Subplot.AmplitudeSpectrum(signal, db=True)) fig.plot()