def simulate(self): self.netlist.to_spice('optimize.sp') ngspyce.source('optimize.sp') ngspyce.ac(**self.sim_params) fs = np.abs(ngspyce.vector('frequency')) vs = ngspyce.vector('vout') return fs, vs
def sim(): tb = testbench(common_emitter_amplifer()) circuit = Builder(tb).compile() circuit.to_spice('common_emitter_amplifier.sp') ng.source('common_emitter_amplifier.sp') ng.cmd('tran 50us 10ms') print('\n'.join(ng.vector_names())) time, tp1 = map(ng.vector, ['time', 'V(VOUT)']) plt.plot(time, tp1, label='VOUT') plt.legend() plt.show()
import subprocess import os import sys from matplotlib.colors import SymLogNorm #include store.py import sys sys.path.append('/home/arthurdent/covidinator/electronics/') import store import ngspyce source_file = 'oscillator.cir' ngspyce.source(source_file) def run_sim(varactor_voltage): ngspyce.cmd(f'alterparam varactor_bias_voltage = {varactor_voltage}') ngspyce.cmd('reset') # ngspyce.cmd('stop after 10000') step_ps = 1 #not always obeyed - ngspice sets its own timestep. sim_duration = 100000 n_steps = sim_duration / step_ps # ngspyce.cmd(" ") ngspyce.cmd(f'tran {step_ps}p {sim_duration}ps uic') timesteps = ngspyce.vector('time') v_collector = ngspyce.vector('v(E1)')
""" Plot output voltages of an operational amplifier quadrature oscillator """ import ngspyce import numpy as np from matplotlib import pyplot as plt # Load netlist ngspyce.source('quad.net') # Simulate 10 ms ngspyce.cmd('tran 12n 10m 1n') # Read results time, vsin, vcos = map(ngspyce.vector, ['time', 'Vsin', 'Vcos']) # And plot them plt.plot(time * 1e3, vcos, label='Vcos') plt.plot(time * 1e3, vsin, label='Vsin') plt.title('Quadrature op-amp oscillator output voltage') plt.xlabel('Time [ms]') plt.ylabel('Voltage [V]') plt.savefig('quad.png') plt.show()
""" Plot the output characteristics for the BC337 NPN transistor """ import ngspyce import numpy as np from matplotlib import pyplot as plt # Load netlist ngspyce.source('npn.net') # Sweep both base and collector current ngspyce.cmd('dc vcc 0 2 .05 vbb .7 1.2 .1') # Load simulation results into numpy arrays vb, vc, Ivcc = map(ngspyce.vector, ['Vb', 'Vc', 'I(Vcc)']) # Correct the sign for collector current ic = -Ivcc plt.figure() # Plot one line per base voltage series = np.unique(vb) for _vb in series: plt.plot(vc[vb == _vb], ic[vb == _vb], '-', label='Vb = {:.1f}'.format(_vb)) plt.legend() plt.title('Output characteristics for BC337')
write_outfile = '-p' in sys.argv or '--pdf' in sys.argv do_plot_thd = '-t' in sys.argv or '--thd' in sys.argv do_plot_bode = '-b' in sys.argv or '--bode' in sys.argv do_plot_bode_resonance = '-r' in sys.argv or '--bode-resonance' in sys.argv TWO_STAGE_AMP = '-2' in sys.argv or '--two-stage' in sys.argv PREGAIN = 21 if ('-i' in sys.argv or '--instrumentation-amplifier' in sys.argv) else 1 infile = [arg for arg in sys.argv[1:] if arg[0] != '-'][0] thd_outfile = os.path.splitext(infile)[0] + "_thd" + ("_DRAFT" if DRAFT_MODE else "") + ".pdf" bode_outfile = os.path.splitext(infile)[0] + "_bode" + ("_DRAFT" if DRAFT_MODE else "") + ".pdf" bode2_outfile = os.path.splitext(infile)[0] + "_bode_reso" + ( "_DRAFT" if DRAFT_MODE else "") + ".pdf" ns.source(infile) plt.rcParams.update({'font.size': 8}) if do_plot_thd: plot_thd_grid(thd_outfile if write_outfile else None) if do_plot_bode: plot_bode(bode_outfile if write_outfile else None) if do_plot_bode_resonance: plot_bode2(bode2_outfile if write_outfile else None) if not write_outfile: plt.show() exit(0)
""" Plot the frequency response of an RC low-pass filter """ import ngspyce from matplotlib import pyplot as plt import numpy as np # Read netlist ngspyce.source('lowpass.net') # Calculate small-signal transfer function between 1 kHz and 10 MHz, with 5 # points per decade ngspyce.ac(mode='dec', npoints=7, fstart=1e3, fstop=10e6) # Read results freq = np.abs(ngspyce.vector('frequency')) vout = ngspyce.vector('vout') # And plot them fig, (ax1, ax2) = plt.subplots(2, sharex=True) fig.suptitle('Frequency response of an RC low-pass filter') ax1.semilogx(freq, 20 * np.log10(np.abs(vout))) ax1.set_ylabel('Magnitude [dB]') ax1.grid(True, which='both') ax2.semilogx(freq, np.angle(vout, True)) ax2.set_xlabel('Frequency [Hz]') ax2.set_ylabel('Phase [degrees]') ax2.grid(True, which='both')
""" Plot output voltages of an operational amplifier quadrature oscillator """ import ngspyce import numpy as np from matplotlib import pyplot as plt # Load netlist ngspyce.source('quad.net') # Simulate 10 ms ngspyce.cmd('tran 12n 10m 1n') # Read results time, vsin, vcos = map(ngspyce.vector, ['time', 'Vsin', 'Vcos']) # And plot them plt.plot(time*1e3, vcos, label='Vcos') plt.plot(time*1e3, vsin, label='Vsin') plt.title('Quadrature op-amp oscillator output voltage') plt.xlabel('Time [ms]') plt.ylabel('Voltage [V]') plt.savefig('quad.png') plt.show()
def test_source(self): ns.source(os.path.join(os.path.dirname(__file__), '../examples/npn/npn.net')) self.assertEqual(ns.model_parameters(model='QBC337AP')['bf'], 175)
""" Plot the frequency response of an RC low-pass filter """ import ngspyce from matplotlib import pyplot as plt import numpy as np # Read netlist ngspyce.source('lowpass.net') # Calculate small-signal transfer function between 1 kHz and 10 MHz, with 5 # points per decade ngspyce.ac(mode='dec', npoints=7, fstart=1e3, fstop=10e6) # Read results freq = np.abs(ngspyce.vector('frequency')) vout = ngspyce.vector('vout') # And plot them fig, (ax1, ax2) = plt.subplots(2, sharex=True) fig.suptitle('Frequency response of an RC low-pass filter') ax1.semilogx(freq, 20*np.log10(np.abs(vout))) ax1.set_ylabel('Magnitude [dB]') ax1.grid(True, which='both') ax2.semilogx(freq, np.angle(vout, True)) ax2.set_xlabel('Frequency [Hz]') ax2.set_ylabel('Phase [degrees]') ax2.grid(True, which='both')
""" Plot the output characteristics for the BC337 NPN transistor """ import ngspyce import numpy as np from matplotlib import pyplot as plt # Load netlist ngspyce.source('npn.net') # Sweep both base and collector current ngspyce.cmd('dc vcc 0 2 .02 vbb .7 1.2 .1') # Load simulation results into numpy arrays vb, vc, Ivcc = map(ngspyce.vector, ['Vb', 'Vc', 'I(Vcc)']) # Correct the sign for collector current ic = -Ivcc plt.figure() # Plot one line per base voltage series = np.unique(vb) for _vb in series: plt.plot(vc[vb == _vb], ic[vb == _vb], '-', label='Vb = {:.1f}'.format(_vb)) plt.legend(loc='center right') plt.title('Output characteristics for BC337') plt.xlabel('Collector-emitter voltage [V]') plt.ylabel('Collector current [A]')