An open source signal processing toolbox for ngspice (an open source circuit simulator)


Inspired partly by curiosity to learn about signal processing and controls and partly by Matlab simulink, which can simulate circuits as well.   (https://in.mathworks.com/help/physmod/sps/powersys/ug/building-and-simulating-a-simple-circuit.html) (where we can  directly include components like lpf and integrator as blocks). I thought it would be nice to have such blocks in open source for ngspice. (http://ngspice.sourceforge.net/)

 Installation

this tool box is built as a ngspice code model and can be compiled along with ngspice as described in the manual (http://ngspice.sourceforge.net/docs/ngspice-34-manual.pdf).   This tool is being developed in a separate fork of the main ngspice branch which can be cloned from here :  

  https://sourceforge.net/u/harshcht/ngspice/ci/master/tree/

Then build ngspice using the steps described in section 32.1.2 of the user manual.
 

Examples

the main example file (feedback_example.cir) is an example of a first order system (modeled by a simple RC circuit) with an integrator as a controller.  I will use this example to explain the general functioning of the toolbox.

The system being modeled is a simple first order system. -




The system is modeled using a simple RC circuit 




The addition block and the integrator block are taken from the code models themselves.

The example file is as follows :-

 `

*netlist
*v1 1 0 sin(0 1 1 0 0 270)
.model add adder(gain1 = 1, gain2 = -1)
*second
.model controller integrator(sampling_freq =10000, gain = 1,
                               offset = 0, adc_range = [-5,5],  
                            adc_resolution = 32, dac_resolution = 32,  
                            dac_range= [-10 10])
v1 in 0 PULSE(0 5 0 0 0 100 200 0)

*A1 1 2 4 add
*A2 2 3 controller

A3 in err out add
A4 err fb controller


r1 in OL 1
C1 0 OL 1

r2 fb out 1
c2 0 out 1



.control
tran 100us 20
.endc
`

An explanation of both the blocks (integrator and adder are given below ) :-
 

Integrator
This block is implemented in the form of a “digital” integrator, i.e it samples the input in at a given frequency digitizes it and then does the numerical processing. So this block would behave as if you had an micro-controller (or an FPGA) with an ADC and DAC of given resolution ,  sampling frequency and range and integrator was implemented digitally in these  for feedback. (I just thought this addition might be useful since most of the times controllers are implemented digitally. Feedback on how this can be effectively done and made easy to use are highly welcome).

Note :
*This is not an ideal implementation I will also be trying to add a feature if someone wants to simulate an ideal linear integrator using this.
 

Adder
This part is implemented as analog . It simply takes two inputs , multiplies them by the respective gain parameters and adds. (for subtraction one of the gains has to be negative as in the above example).

 
***Way forward :


I started this project just last week, so this is still in very early stages. I am planning to add more examples and more blocks (I will be trying to add an example for a buck converter and simulating motor control using this toolbox now as I'm not sure if such a RC circuit with feedback is actually used somewhere).

Any feedback in this direction is highly valued.

 

Results :- 

The results of the simulation are shown here :- 

abbreviations for the plot :- 

  • err - error
  • fb - feedback (output of integrator)
  • in - input signal
  • OL- open loop response
  • out - output of the closed loop system




 

Comments

Popular posts from this blog

Simulating a motor in ngspice

Counting pulses in Arduino (without interrupts or loops)

Embedded system simulations using tessim