def linear_SI_example(): ## Generate system A = np.array([[0.89, 0.], [0., 0.45]]) #(2,2) x<-x B = np.array([[0.3], [2.5]]) #(2,1) x<-u C = np.array([[0.7, 1.]]) #(1,2) y<-u D = np.array([[0.0]]) #(1,1) sys0 = deepSI.fit_systems.SS_linear(A=A, B=B, C=C, D=D) ## Generate experiment np.random.seed(3) sys_data_train = deepSI.System_data(u=np.random.normal(size=1000)) sys_data_test = deepSI.System_data(u=np.random.normal(size=1000)) ## apply experiment sys_data_train = sys0.apply_experiment(sys_data_train) sys_data_train.y += np.random.normal( scale=1e-2, size=1000) #add a bit of noise to the training set sys_data_test = sys0.apply_experiment(sys_data_test) #create linear fit systems fit_sys_SS = deepSI.fit_systems.SS_linear(nx=2) fit_sys_IO = deepSI.fit_systems.Sklearn_io_linear(na=4, nb=4) fit_sys_SS.fit(sys_data_train) fit_sys_IO.fit(sys_data_train) #use the fitted system sys_data_test_predict_SS = fit_sys_SS.apply_experiment(sys_data_test) sys_data_test_res_SS = sys_data_test - sys_data_test_predict_SS #this will subtract the y between the two data sets sys_data_test_predict_IO = fit_sys_IO.apply_experiment(sys_data_test) sys_data_test_res_IO = sys_data_test - sys_data_test_predict_IO #print NRMS values print( f'Linear SS simulation NRMS={sys_data_test_predict_SS.NRMS(sys_data_test)}' ) print( f'Linear IO simulation NRMS={sys_data_test_predict_IO.NRMS(sys_data_test)}' ) #plot results plt.subplot(2, 1, 1) sys_data_test.plot() sys_data_test_res_SS.plot() sys_data_test_res_IO.plot() plt.legend(['real', 'SS', 'IO']) plt.grid() plt.subplot(2, 1, 2) sys_data_test_res_SS.plot() sys_data_test_res_IO.plot() plt.ylim(-0.02, 0.02) plt.legend(['SS', 'IO']) plt.grid() plt.show()
def get_test_data(self, N=4000): from scipy import signal band = 200e3 order = 6 self.b, self.a = signal.butter(order, band, analog=False, fs=1 / self.dt) u0 = np.random.normal(scale=60, size=N) u = signal.lfilter(self.b, self.a, u0) exp = deepSI.System_data(u=u) return self.apply_experiment(exp)
class My_system(deepSI.System_ss): def __init__(self): super(My_system, self).__init__(nx=2) def f(self, x, u): return x[0] / (1.2 + x[1]**2) + x[1] * 0.4, x[1] / ( 1.2 + x[0]**2) + x[0] * 0.4 + u #some non-linear function def h(self, x): return x[0] + self.random.normal(scale=0.1, size=()) #add some noise if __name__ == '__main__': sys = My_system() train = deepSI.System_data(u=np.random.uniform(low=-2, high=2, size=10**4)) train = sys.apply_experiment(train) #fill train.y test = deepSI.System_data( u=np.random.uniform(low=-2, high=2, size=5 * 10**3)) test = sys.apply_experiment(test) #fill train.y fit_sys_ss_lin = deepSI.fit_systems.SS_linear(nx=2) fit_sys_ss_lin.fit(train, SS_f=20) test_lin = fit_sys_ss_lin.apply_experiment(test) plt.plot(test.y, label='real') plt.plot(test_lin.y - test.y, label='diff') plt.legend() plt.show() print(f'NRMS = {test_lin.NRMS(test):.2%}')
Y, X = np.meshgrid(Y, X) r = 0.75 A = np.clip((r**2 - X**2 - (Y - x[0])**2) / r**2, 0, 1) return A #return position if __name__ == '__main__': import cv2 import os from PIL import Image from matplotlib import pyplot as plt sys = Double_potential_well_video() train = sys.get_train_data() test = sys.get_test_data() exp = deepSI.System_data(u=2 * np.sin(np.arange(0, 100, sys.dt))) # exp.plot(show=True) sin_out = sys.apply_experiment(exp) def to_vid(sys_data, video_name='droplets-video.mp4'): nx, ny = sys_data.y.shape[1], sys_data.y.shape[ 2] #resolution of simulation nx_out, ny_out = nx * 10, ny * 10 #resolution of video produced height, width = nx_out, ny_out # height, width = 20, 100 video = cv2.VideoWriter(video_name, 0, 60, (width, height)) # video.write(to_img(D))