{"id":1384,"date":"2012-09-04T09:11:12","date_gmt":"2012-09-04T07:11:12","guid":{"rendered":"http:\/\/raftaman.net\/?p=1384"},"modified":"2021-05-15T11:46:39","modified_gmt":"2021-05-15T09:46:39","slug":"simulating-a-lowpass-filter-with-geda-and-ngspice","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=1384","title":{"rendered":"Simulating a lowpass filter with gEDA and ngspice"},"content":{"rendered":"<p>Getting started with <a href=\"http:\/\/ngspice.sourceforge.net\/\">ngspice<\/a> can be quite tricky. It&#8217;s a very powerful piece of software and although the <a href=\"http:\/\/ngspice.sourceforge.net\/docs.html\">user manual<\/a> is quite comprehensive, its complexity can easily scare off beginners.<br \/>\nHere&#8217;s a simple step-by-step tutorial on how to simulate a very simple electronic circuit. It should enable you to run your own simulations of circuits you designed.<\/p>\n<p><!--more--><\/p>\n<h2>Creating the circuit<\/h2>\n<p>The sample circuit is a lowpass RC filter. It&#8217;s simple, contains a reasonable amount of components and yet we can do different types of analysis. I assume, you already know how to use <a href=\"http:\/\/www.geda-project.org\/\">gschem<\/a>. If not, have a look at the <a href=\"http:\/\/wiki.geda-project.org\/\">gEDA wiki<\/a> for a list of tutorials.<\/p>\n<p>Start up <code>gschem<\/code> and draw the following circuit:<\/p>\n<div id=\"attachment_1386\" style=\"width: 517px\" class=\"wp-caption aligncenter\"><a data-rokbox data-rokbox-album=\"p1384\" data-rokbox-caption=\"Simple lowpass RC filter\" href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1386\" src=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.png\" alt=\"\" title=\"rcSIN\" width=\"507\" height=\"292\" class=\"aligncenter size-full wp-image-1386\" \/><\/a><p id=\"caption-attachment-1386\" class=\"wp-caption-text\">Simple lowpass RC filter<\/p><\/div>\n<p>Of course, you can name your buses differently. I chose <em>n0<\/em> and <em>n1<\/em> here. The bus named <strong><em>0<\/em> is mandatory<\/strong> as it specifies the reference (ground) net.<\/p>\n<p><em>V1<\/em> is an independent AC voltage source. It&#8217;s parameters are wave form (<em>sin<\/em>), voltage offset in Volts (<em>0[V]<\/em>), amplitude in Volts (<em>1[V]<\/em>) and frequency (<em>200kHz<\/em>).<\/p>\n<p>Save the schematic as <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.sch\">rcSIN.sch<\/a>.<\/p>\n<h2>Generating the netlist<\/h2>\n<p>SPICE doesn&#8217;t use gschem schematics as input but so-called netlists. To generate a netlist file from a schematic, <code>gnetlist<\/code> can be used:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ gnetlist -g spice -o rcSIN.net rcSIN.sch \r\nCommand line passed = gnetlist -g spice -o rcSIN.net rcSIN.sch \r\nLoading schematic &#x5B;rcSIN.sch]\r\n<\/pre>\n<p>The resulting <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.net\">rcSIN.net<\/a> should look like this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n* Spice netlister for gnetlist\r\nR1 n1 n0 10k\r\nC1 n1 0 1nF\r\nV1 n0 0 sin(0 1 200kHz)\r\n.END\r\n<\/pre>\n<p>The file is quite self-explanatory: Each line contains a single component, the first column of each line the component&#8217;s reference designator, the subsequent columns the netnames for each pin and finally the value attribute.<\/p>\n<h2>Running a transient simulation<\/h2>\n<p>SPICE can run different types of analysis, e.g.<\/p>\n<ul>\n<li>A <strong>transient<\/strong> analysis shows how the circuit behaves over time<\/li>\n<li>An <strong>AC<\/strong> analysis shows the behaviour of the circuit with varying frequency<\/li>\n<li>A <strong>parametrized<\/strong> analysis can be either a transient or an AC analysis. It varies one or more values of a given circuit, e.g. the resistance of a Resistor.<\/li>\n<\/ul>\n<p>Let&#8217;s run a transient analysis first. Start up <code>ngspice<\/code><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ ngspice \r\n******\r\n** ngspice-23 : Circuit level simulation program\r\n** The U. C. Berkeley CAD Group\r\n** Copyright 1985-1994, Regents of the University of California.\r\n** Please get your ngspice manual from http:\/\/ngspice.sourceforge.net\/docs.html\r\n** Please file your bug-reports at http:\/\/ngspice.sourceforge.net\/bugrep.html\r\n** Creation Date: Sat Jan 14 14:20:54 UTC 2012\r\n******\r\nngspice 230 -&gt; \r\n<\/pre>\n<p>and load the netlist file<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nngspice 230 -&gt; source rcSIN.net\r\n\r\nCircuit: * spice netlister for gnetlist\r\n\r\nngspice 231 -&gt; \r\n<\/pre>\n<p>Our voltage source runs at a frequency of \\(200kHz\\), which means our oscillating period is \\(200kHz^{-1} = 5{\\mu}s\\). Let&#8217;s say, we want to see how the input voltages behaves during the first 5 oscillations (i.e. \\(5 \\cdot 5{\\mu}s = 25{\\mu}s\\)) and we want to have a data point every \\(0.05{\\mu}s\\):<\/p>\n<pre class=\"brush: plain; highlight: [1]; title: ; notranslate\" title=\"\">\r\nngspice 231 -&gt; tran 0.05us 25us\r\nDoing analysis at TEMP = 27.000000 and TNOM = 27.000000\r\n\r\nWarning: v1: no DC value, transient time 0 value used\r\n\r\nInitial Transient Solution\r\n--------------------------\r\n\r\nNode                                   Voltage\r\n----                                   -------\r\nn1                                           0\r\nn0                                           0\r\nv1#branch                                    0\r\n\r\n%100.00\r\n\r\nNo. of Data Rows : 508\r\nngspice 232 -&gt; \r\n<\/pre>\n<p>We can now plot the results:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nngspice 232 -&gt; plot n0 n1\r\n<\/pre>\n<div id=\"attachment_1398\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a data-rokbox data-rokbox-album=\"p1384\" data-rokbox-caption=\"Output graph of the transient analysis\" href=\"wordpress\/wp-content\/uploads\/2012\/09\/transient.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1398\" src=\"wordpress\/wp-content\/uploads\/2012\/09\/transient-550x383.png\" alt=\"\" title=\"transient\" width=\"550\" height=\"383\" class=\"aligncenter size-medium wp-image-1398\" srcset=\"https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2012\/09\/transient-550x383.png 550w, https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2012\/09\/transient.png 636w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-1398\" class=\"wp-caption-text\">Output graph of the transient analysis<\/p><\/div>\n<h2>Running an AC simulation<\/h2>\n<p>You can easily see that the signal at the resistor <em>R1<\/em> is significantly smaller as the input voltage. Of course, you can adjust the values for <em>R1<\/em> and <em>C1<\/em> to archive even lower signal levels, but the signal level is also determined by the input frequency. So let&#8217;s run and AC analysis to see how the RC filter behaves when we alter the input frequency.<\/p>\n<p>Open up <code>gschem<\/code> again and change the input voltage to AC:<\/p>\n<div id=\"attachment_1408\" style=\"width: 517px\" class=\"wp-caption aligncenter\"><a data-rokbox data-rokbox-album=\"p1384\" data-rokbox-caption=\"Changed input voltage (AC)\" href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1408\" src=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.png\" alt=\"\" title=\"rcAC\" width=\"507\" height=\"292\" class=\"aligncenter size-full wp-image-1408\" \/><\/a><p id=\"caption-attachment-1408\" class=\"wp-caption-text\">Changed input voltage (AC)<\/p><\/div>\n<p>Save the schematic as <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.sch\">rcAC.sch<\/a> and generate a netlist file (<a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.net\">rcAC.net<\/a>)<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ gnetlist -g spice -o rcAC.net rcAC.sch \r\nCommand line passed = gnetlist -g spice -o rcAC.net rcAC.sch \r\nLoading schematic &#x5B;rcAC.sch]\r\n<\/pre>\n<p>We are now ready to run an AC analysis. For a detailed explanation of the ac command syntax, have a look at  <a href=\"http:\/\/www.seas.upenn.edu\/~jan\/spice\/spice.overview.html#AC%20Statement\">&#8220;SPICE &#8211; A Brief Tutorial&#8221;<\/a>. Don&#8217;t forget to <strong>load the <code>rcAC.net<\/code> netlist file<\/strong> before you run the analysis!<\/p>\n<pre class=\"brush: plain; highlight: [1,5,12]; title: ; notranslate\" title=\"\">\r\nngspice 236 -&gt; source rcAC.net\r\n\r\nCircuit: * spice netlister for gnetlist\r\n\r\nngspice 237 -&gt; ac lin 1000 0.1 250kHz\r\nDoing analysis at TEMP = 27.000000 and TNOM = 27.000000\r\n\r\nWarning: v1: has no value, DC 0 assumed\r\n\r\n\r\nNo. of Data Rows : 1000\r\nngspice 238 -&gt; plot n0 n1\r\nngspice 239 -&gt; \r\n<\/pre>\n<p>This command performs a linear frequency analysis (<em>lin<\/em>) from (almost) 0Hz to 250kHz and generates 1000 data points. To view the results, plot the input voltage and the voltage over <em>R1<\/em> versus the frequency:<\/p>\n<div id=\"attachment_1412\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a data-rokbox data-rokbox-album=\"p1384\" data-rokbox-caption=\"Frequency analysis\" href=\"wordpress\/wp-content\/uploads\/2012\/09\/ac.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1412\" src=\"wordpress\/wp-content\/uploads\/2012\/09\/ac-550x383.png\" alt=\"\" title=\"ac\" width=\"550\" height=\"383\" class=\"aligncenter size-medium wp-image-1412\" srcset=\"https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2012\/09\/ac-550x383.png 550w, https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2012\/09\/ac.png 636w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-1412\" class=\"wp-caption-text\">Frequency analysis<\/p><\/div>\n<p>Have a look at <a href=\"http:\/\/ashwith.wordpress.com\/2010\/09\/30\/ngspice-interactive-mode\/\">http:\/\/ashwith.wordpress.com\/2010\/09\/30\/ngspice-interactive-mode\/<\/a> for more ngspice examples.<\/p>\n<p>Schematic and netlist files:<br \/>\n<a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.sch\">rcSIN.sch<\/a> <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcSIN.net\">rcSIN.net<\/a> <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.net\">rcAC.sch<\/a> <a href=\"wordpress\/wp-content\/uploads\/2012\/09\/rcAC.net\">rcAC.net<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting started with ngspice can be quite tricky. It&#8217;s a very powerful piece of software and although the user manual is quite comprehensive, its complexity can easily scare off beginners. Here&#8217;s a simple step-by-step tutorial on how to simulate a very simple electronic circuit. It should enable you to run your own simulations of circuits you designed.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[54],"class_list":["post-1384","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-spice"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1384"}],"version-history":[{"count":37,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":2187,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1384\/revisions\/2187"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}