def build_generator(self, usymbs, psymbs, fs, time): usigs = [] for u in usymbs: usignal = self.signals[u] delta = time - usignal.data['time'] if delta > 0: usignal.data['tend'] += delta usignal.data['fs'] = fs usigs.append(signalgenerator(**usignal.data)()) def u(): i = 0 while i < time * fs: si = [next(usig) for usig in usigs] i += 1 yield numpy.array(si) psigs = [] for p in psymbs: psignal = self.signals[p] delta = time - psignal.data['time'] if delta > 0: psignal.data['tend'] += delta psignal.data['fs'] = fs psigs.append(signalgenerator(**psignal.data)()) def p(): i = 0 while i < time * fs: si = [next(sig) for sig in psigs] i += 1 yield numpy.array(si) return u, p
def signal_synthesis(): sigs = list() # test without parameters sigs.append(signalgenerator()) # test for all synthesis methods for w in sig.synthesis.names: kwargs = {'which': w} sigs.append(list(signalgenerator(**kwargs)())) return True
def signal_synthesis(): sigs = list() # test without parameters sigs.append(signalgenerator()) # test for all synthesis methods for w in sig.synthesis.names: kwargs = {'which': w} sigs.append(signalgenerator(**kwargs)) return True
def simulation_rlc_without_split(): rlc = core.__copy__() # Define the simulation parameters config = {'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'eps': 1e-16, # Global numerical tolerance 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} } simu = Simulation(rlc.to_method(), config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur*simu.config['fs'])) simu.process() if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
def simulation_rlc_without_split(): rlc = core.__deepcopy__() # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'gradient': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'numtol': 1e-16, # Global numerical tolerance 'path': None, # Path to the results folder 'progressbar': True, # Display a progress bar 'timer': True, # Display minimal timing infos 'language': 'python', # in {'python', 'c++'} 'cpp_build_and_run_script': None, # compile and exec binary 'eigen_path': None, # path to Eigen library } simu = PHSSimulation(rlc, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.fs) def sequ(): for el in u(): yield (el, ) simu.init(sequ=sequ(), nt=int(dur * simu.fs)) simu.process() return True
def plot_power_balance_rlc_with_split(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} } # retrieve the pyphs.Core of a linear RLC from the examples from pyphs.examples.rlc.rlc import core as rlc rlc_ = rlc.__copy__() rlc_.reduce_z() simu = Simulation(rlc_.to_method(), config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) sequ = u[:, np.newaxis] simu.init(u=sequ) simu.process() simu.data.plot_powerbal(mode='single', show=False) simu.data.plot_powerbal(mode='multi', show=False) if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
def plot_rlc_with_split(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'eps': 1e-16, # Global numerical tolerance 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} 'eigen': None, # path to Eigen library } # retrieve the pyphs.Core of a linear RLC from the examples from pyphs.examples.rlc.rlc import core as rlc print(rlc.w) print(rlc.z) simu = Simulation(rlc, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur * simu.config['fs'])) simu.process() print(simu.nums.method.w) print(simu.nums.method.z) dims = simu.nums.method.dims # plot u, y simu.data.plot([('u', i) for i in range(dims.y())] + [('y', i) for i in range(dims.y())], show=False) # plot w, z simu.data.plot([('w', 0), ('z', 0)], show=False) simu.data.plot([('dtx', i) for i in range(dims.x())] + [('dxH', i) for i in range(dims.x())], show=False) if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
def plot(self): import matplotlib.pyplot as plt self.update_data() data = self.data.copy() data.pop('time') sig = list(signalgenerator(**data)()) nt = len(sig) t = [n*self.data['fs']**-1 for n in range(nt)] plt.plot(t, sig) plt.title(self.label) plt.show()
def plot_rlc_with_split(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'gradient': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'numtol': 1e-16, # Global numerical tolerance 'path': None, # Path to the results folder 'progressbar': True, # Display a progress bar 'timer': True, # Display minimal timing infos 'language': 'python', # in {'python', 'c++'} 'cpp_build_and_run_script': None, # compile and exec binary 'eigen_path': None, # path to Eigen library } # retrieve the pyphs.PHSCore of a linear RLC from the examples from pyphs.examples.rlc.rlc import core as rlc print(rlc.w) print(rlc.z) simu = PHSSimulation(rlc, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.fs) def sequ(): for el in u(): yield (el, ) simu.init(sequ=sequ(), nt=int(dur * simu.fs)) simu.process() print(simu.nums.method.core.w) print(simu.nums.method.core.z) dims = simu.nums.method.core.dims # plot u, y simu.data.plot([('u', i) for i in range(dims.y())] + [('y', i) for i in range(dims.y())], show=False) # plot w, z simu.data.plot([('w', 0), ('z', 0)], show=False) simu.data.plot([('dtx', i) for i in range(dims.x())] + [('dxH', i) for i in range(dims.x())], show=False) return True
def signal_waves(): """ Write and read a random sample in wav format """ path = os.path.join(here, 'test.wav') s = signalgenerator() s /= np.max(np.abs(s)) fs = 1000 sig.waves.wavwrite(list(s), fs, path, normalize=False, timefades=0.) fss, ss = sig.waves.wavread(path) os.remove(path) ss /= max(abs(ss)) return max(abs(ss - s)) < 1e-4 and fss - fs < 1e-4
def signal_waves(): """ Write and read a random sample in wav format """ path = os.path.join(here, 'test.wav') s = np.array(list(signalgenerator()())) s /= np.max(np.abs(s)) fs = 1000 sig.waves.wavwrite(list(s), fs, path, normalize=False, timefades=0.) fss, ss = sig.waves.wavread(path) os.remove(path) ss /= max(abs(ss)) return max(abs(ss-s)) < 1e-4 and fss - fs < 1e-4
def plot_rlc_with_split(): # Define the simulation parameters config = {'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': False, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'eps': 1e-16, # Global numerical tolerance 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} } # retrieve the pyphs.Core of a linear RLC from the examples from pyphs.examples.rlc.rlc import core as rlc print(rlc.w) print(rlc.z) simu = Simulation(rlc.to_method(), config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur*simu.config['fs'])) simu.process() print(simu.nums.method.w) print(simu.nums.method.z) dims = simu.nums.method.dims # plot u, y simu.data.plot([('u', i) for i in range(dims.y())] + [('y', i) for i in range(dims.y())], show=False) # plot w, z simu.data.plot([('w', 0), ('z', 0)], show=False) simu.data.plot([('dtx', i) for i in range(dims.x())] + [('dxH', i) for i in range(dims.x())], show=False) if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
def dataH5File(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': True, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} } # retrieve the pyphs.Core of a nonlinear RLC from # the tutorial on Core from pyphs.tutorials.core import core as nlcore nlcore.reduce_z() simu = Simulation(nlcore.to_method(), config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) sequ = u[:, np.newaxis] simu.init(u=sequ) simu.process() simu.data.plot_powerbal(mode='single', show=False) simu.data.plot_powerbal(mode='multi', show=False) simu2 = Simulation(nlcore.to_method(), config=config, erase=False) start = int(simu.data.nt / 10.) stop = int(9 * simu.data.nt / 10.) step = 3 simu2.data.start = start simu2.data.stop = stop simu2.data.step = step test = len(range(start, stop, step)) == len(simu2.data['x', :, 0]) return test
def plot_power_balance_nlcore_with_split(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': True, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'eps': 1e-16, # Global numerical tolerance 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} 'eigen': None, # path to Eigen library } # retrieve the pyphs.Core of a nonlinear RLC from # the tutorial on Core from pyphs.tutorials.core import core as nlcore nlcore.reduce_z() simu = Simulation(nlcore, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur * simu.config['fs'])) simu.process() simu.data.plot_powerbal(mode='single', show=False) simu.data.plot_powerbal(mode='multi', show=False) if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
def plot_power_balance_rlc_with_split(): # Define the simulation parameters config = { 'fs': 48e3, # Sample rate 'gradient': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': True, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'numtol': 1e-16, # Global numerical tolerance 'path': None, # Path to the results folder 'progressbar': True, # Display a progress bar 'timer': True, # Display minimal timing infos 'language': 'python', # in {'python', 'c++'} 'cpp_build_and_run_script': None, # compile and exec binary 'eigen_path': None, # path to Eigen library } # retrieve the pyphs.PHSCore of a linear RLC from the examples from pyphs.examples.rlc.rlc import core as rlc rlc_ = rlc.__copy__() rlc_.build_R() simu = PHSSimulation(rlc_, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.fs) def sequ(): for el in u(): yield (el, ) simu.init(sequ=sequ(), nt=int(dur * simu.fs)) simu.process() simu.data.plot_powerbal(mode='single', show=False) simu.data.plot_powerbal(mode='multi', show=False) return True
def plot_power_balance_nlcore_with_split(): # Define the simulation parameters config = {'fs': 48e3, # Sample rate 'grad': 'discret', # in {'discret', 'theta', 'trapez'} 'theta': 0.5, # theta-scheme for the structure 'split': True, # apply core.split_linear() beforehand 'maxit': 10, # Max iteration for NL solvers 'eps': 1e-16, # Global numerical tolerance 'path': path, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': True, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} } # retrieve the pyphs.Core of a nonlinear RLC from # the tutorial on Core from pyphs.tutorials.core import core as nlcore nlcore.reduce_z() simu = Simulation(nlcore.to_method(), config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur*simu.config['fs'])) simu.process() simu.data.plot_powerbal(mode='single', show=False) simu.data.plot_powerbal(mode='multi', show=False) if not os.name.lower().startswith('nt'): shutil.rmtree(path) return True
graph = PHSGraph(netlist=netlist) core = graph.buildCore() if __name__ == '__main__': config = { 'fs': 48e3, 'split': True, 'pbar': True, 'timer': True, 'path': path } simu = PHSSimulation(core, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.fs) def sequ(): for el in u(): yield (el, ) simu.init(sequ=sequ(), nt=int(dur * simu.fs)) simu.process() simu.data.plot_powerbal(mode='multi') simu.data.plot([('u', 0), ('x', 1), ('x', 0), ('dtx', 0), ('dxH', 2), ('y', 0)])
} simu = core.to_simulation(config=config) # Signal parameters A = 100. # Signal amplitude (V) f0 = 100. # Signal frequency (Hz) nP = 5 # Sumber of periods (d.u.) tsig = 5/f0 # Signal duration (s) # signal generator vout = signalgenerator(which="sin", # Sinusoidal signal A=A, # Amplitude f0=f0, # Frequency tsig=tsig, # Number of time-steps fs=config['fs'], # Samplerate ramp_on=True, # Linear increase ) u = list(([el, ] for el in vout)) # Init simulation simu.init(u=u) # Process simulation simu.process() # In[26]:
} simu = core.to_simulation(config=config) # Signal parameters A = 100. # Signal amplitude (V) f0 = 100. # Signal frequency (Hz) nP = 5 # Sumber of periods (d.u.) tsig = 5/f0 # Signal duration (s) # signal generator vout = signalgenerator(which="sin", # Sinusoidal signal A=A, # Amplitude f0=f0, # Frequency tsig=tsig, # Number of time-steps fs=config['fs'], # Samplerate ramp_on=True, # Linear increase ) u = list(([el, ] for el in vout())) # Init simulation simu.init(u=u) # Process simulation simu.process() # In[26]:
'path': None, # Path to the results folder 'pbar': False, # Display a progress bar 'timer': False, # Display minimal timing infos 'lang': 'python', # in {'python', 'c++'} 'theano': False } # x0 = np.zeros(core.dims.x()) # x0[0] = 1 # x0_expr = list(map(sp.sympify, x0)) # inits = {'x': x0_expr} # simu = Simulation(core, config=config, inits=inits) simu = Simulation(core, config=config) dur = 0.01 u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs']) def sequ(): for el in u(): yield (el, ) simu.init(u=sequ(), nt=int(dur * simu.config['fs'])) # Run the simulation simu.process() # Plots simu.data.plot_powerbal(mode='single') simu.data.plot(['u', 'x', 'y']) # clean: delete folders 'data' and 'figures' shutil.rmtree(os.path.join(here, 'data'))
netlist = PHSNetlist(netlist_filename) graph = PHSGraph(netlist=netlist) core = graph.buildCore() if __name__ == '__main__': tsig = 0.01 fs = 48000. config = { 'fs': 48e3, 'progressbar': True, } simu = PHSSimulation(core, config) sig = signalgenerator(which='sin', f0=500., A=200., tsig=tsig, fs=fs) def sequ(): for u in sig(): yield np.array([ u, ]) nt = int(fs * tsig) simu.init(sequ=sequ(), nt=nt) simu.process() simu.data.plot_powerbal()