{"id":16,"date":"2003-03-02T20:00:33","date_gmt":"2003-03-03T04:00:33","guid":{"rendered":"http:\/\/www.earlevel.com\/blog\/?p=16"},"modified":"2012-11-26T00:24:53","modified_gmt":"2012-11-26T08:24:53","slug":"the-digital-state-variable-filter","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2003\/03\/02\/the-digital-state-variable-filter\/","title":{"rendered":"The digital state variable filter"},"content":{"rendered":"<p>The digital state variable filter was described in Hal Chamberlin&#8217;s\u00a0<em>Musical Applications of Microprocessors. <\/em>Derived by straight-forward replacement of components from the analog state variable fiter with digital counterparts, the digital state variable is a popular synthesizer filter, as was its analog counterpart.<\/p>\n<p>The state variable filter has several advantages over biquads as a synthesizer filter. Lowpass, highpass, bandpass, and band reject are available simultaneously. Also, frequency and Q control are independent and their values calculated easily.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/DigitalAudio\/images\/StateVarBlock.gif\" alt=\"\" width=\"376\" height=\"124\" \/><\/p>\n<p>The frequency control coefficient, f, is defined as<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/DigitalAudio\/images\/StateVarK.gif\" alt=\"\" width=\"88\" height=\"35\" \/><\/p>\n<p>where Fs is the sample rate and Fc is the filter&#8217;s corner frequency you want to set. The q coefficient is defined as<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/DigitalAudio\/images\/StateVarQ.gif\" alt=\"\" width=\"36\" height=\"28\" \/><\/p>\n<p>where Q normally ranges from 0.5 to inifinity (where the filter oscillates).<\/p>\n<p>Like its analog counterpart, and biquads, the digital state variable has a cutoff slope of 12 dB\/octave.<\/p>\n<p>The main drawback of the digital state variable is that it becomes unstable at higher frequencies. It depends on the Q setting, but basically the upper bound of stability is about where f reaches 1, which is at one-sixth of the sample rate (8 kHz at 48 kHz). The only way around this is to oversample. A simple way to double the filter&#8217;s sample rate (and thereby double the filter&#8217;s frequency range) is to run the filter twice with the same input sample, and discard one output sample.<\/p>\n<h3>As a sine oscillator<\/h3>\n<p>The state variable makes a great low frequency sine wave oscillator. Just set the Q to infinity, and make sure it has an impulse to get it started. Simply preset the delays (set the &#8220;cosine&#8221; delay to 1, or other peak value, and the other to 0) and run, and it will oscillate forever without instability, with fixed point or floating point. Even better, it gives you two waves in quadrature\u2014simultaneous sine and cosine.<\/p>\n<p>Simplified to remove unecessary parts, the oscillator looks like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/DigitalAudio\/images\/StateVarLfoBlock.gif\" border=\"0\" alt=\"\" width=\"242\" height=\"97\" \/><\/p>\n<p>For low frequencies, we can reduce the calculation of the f coefficient equation to<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/DigitalAudio\/images\/StateVarK2.gif\" alt=\"\" width=\"63\" height=\"26\" \/><\/p>\n<p>Here&#8217;s an example in C to show how easy this oscillator is to use; first initialize the oscillator amplitude,\u00a0<em>amp,<\/em> to whatever amplitude you want (normally 1.0 for \u00b11.0 peak-to-peak output):<\/p>\n<p><span style=\"font-family: 'Courier New', Courier, mono;\">\/\/ initialize oscillator<br \/>\nsinZ = 0.0;<br \/>\ncosZ = amp;<\/span><\/p>\n<p>Then, for every new sample, compute the sine and cosine components and use them as needed:<\/p>\n<p><span style=\"font-family: 'Courier New', Courier, mono;\">\/\/ iterate oscillator<br \/>\nsinZ = sinZ + f * cosZ;<br \/>\ncosZ = cosZ &#8211; f * sinZ;<\/span><\/p>\n<p>The sine purity is excellent at low frequencies (becoming asymmetrical at high frequencies).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The digital state variable filter was described in Hal Chamberlin&#8217;s\u00a0Musical Applications of Microprocessors. Derived by straight-forward replacement of components from the analog state variable fiter with digital counterparts, the digital state variable is a popular synthesizer filter, as was its &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2003\/03\/02\/the-digital-state-variable-filter\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}