You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
music-synthesizer-for-android/lab/Zero delay the easy way.ipynb

1107 lines
243 KiB

{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Zero delay the easy way"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Raph Levien \n",
"25 Jan 2014\n",
"\n",
"So-called \"zero delay\" filters have become popular in synthesizer and audio applications, due to their\n",
"accurate emulation of analog filter designs and efficient implementation. The techniques used\n",
"to create zero delay filters are considered standard in the circuit emulation field, but even so the\n",
"idea of creating a zero delay filter from scratch can be intimidating to those just wanting to design\n",
"a decent audio filter.\n",
"\n",
"This notebook presents a very easy design methodology, using cookbook tools. There are references to\n",
"the literature for those wanting to dig deeper into the theory. None of these techniques are new, but\n",
"it is my hope that presenting them in this form will make them more accessible.\n",
"\n",
"In broad outline, the approach recommended here is:\n",
"\n",
"* Choose an analog filter \"prototype\".\n",
"\n",
"* Write down the filter response in state space representation.\n",
"\n",
"* Choose a discretization method. Bilinear transform is most popular.\n",
"\n",
"* Discretize (convert from continuous time to discrete time).\n",
"\n",
"* Implement the filter in discrete state space representation.\n",
"\n",
"The resulting filter will be a reasonably (but not perfectly) accurate emulation of the analog filter.\n",
"It will share the same stability properties as the prototype, as well as similar behavior under\n",
"modulation.\n",
"\n",
"It is presented here as an IPython notebook, so you can easily read it as a static document, but you\n",
"can also load it into your IPython environment, play with the tools, and adapt the code. Have fun!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline\n",
"from scipy import signal\n",
"from scipy import linalg"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Choose a prototype filter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I'll assume you already have a good idea which analog filter you want to simulate. We're going to concentrate on the _linear_\n",
"response, but one of the great advantages of the state space approach is that it can be extended to nonlinearities without\n",
"too much trouble.\n",
"\n",
"One of the most important factors is the _order_ of the filter, which corresponds directly to the dimensionality of the state\n",
"vector. This technique can handle arbitraty order filters, but direct state space evaluation is definitely most efficient for\n",
"smaller orders (2 or 4 especially). If you have a very high order filter, consider decomposing it into a series of smaller\n",
"filters. There are tools for doing this in transfer function space; it's particularly easy if you know where your poles and\n",
"zeros are. (Search for tf2sos for more information)\n",
"\n",
"We'll use three filter designs as running examples, all of which are popular as filters in analog synthesizers: a simple\n",
"one-pole lowpass (also known as the \"RC lowpass\"), the classic two-pole \"state variable filter\", and the 4-pole Moog resonant lowpass\n",
"filter."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"One-pole lowpass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is also known as the RC filter, as it can be trivially implemented with one resistor and one capacitor (generally in audio\n",
"applications you'd want an opamp to buffer the output so it's available at low impedance, but that's not central to the\n",
"response of the filter).\n",
"\n",
"Being only one pole, this filter is super easy to analyze. Its impulse response is just exponential decay.\n",
"\n",
"(TODO: images for circuit diagrams for these filters)"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"State variable filter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See Figure 3 in https://ccrma.stanford.edu/~jos/svf/svf.pdf for a circuit diagram. This is given in highly idealized form,\n",
"with integrators drawn as integrators rather than expanded out as opamps. However, this is the same as the classic state\n",
"variable filter, for example, the one presented in Don Lancaster's Active Filter Cookbook, which has been the basis of many\n",
"synth builds.\n"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Moog 4-pole ladder filter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Moog 4-pole filter is of course one of the classic designs. There's quite a bit of literature on it\n",
"(see http://www.timstinchcombe.co.uk/index.php?pge=papers).\n",
"\n",
"A good simple description of the filter is given in https://ccrma.stanford.edu/~stilti/papers/moogvcf.pdf (Analyzing the\n",
"Moog VCF with Considerations for Digital Implementation, by Tom Stilson and Julious Smith). This paper presents a very\n",
"simple analog model of the linear response: it is simply 4 one pole lowpass filters in series, all tuned to the same corner\n",
"frequency, with a feedback path which controls resonance. Ignore the discretization in this paper, however; we'll do much\n",
"better."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"State space representation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you've chosen your filter, get it into state space representation. This will be four matrices $(A, B, C, D)$ that together\n",
"represent the differential equations of the filter. The general equations are:\n",
"\n",
"$$ \\begin{align}\n",
"\\mathbf{\\dot{x}}(t) = & A\\mathbf{x}(t) + B\\mathbf{u}(t) \\\\\n",
"\\mathbf{y}(t) = & C\\mathbf{x}(t) + D\\mathbf{u}(t)\n",
"\\end{align} $$\n",
"\n",
"Here, $\\mathbf{u}(t)$ represents the input, $\\mathbf{x}(t)$ represents the state, and $\\mathbf{y}(t)$\n",
"represents the output. Such systems are known as \"single input\" or \"multiple input\" depending on the\n",
"size of $\\mathbf{u}$, and \"single output\" or \"multiple output\" depending on the size of $\\mathbf{y}$.\n",
"In this presentation, we'll concentrate on the \"single input, single output\" case."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"One-pole lowpass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because it's just one pole, the state can be a scalar rather than a vector, so we don't even need to write this in matrix form.\n",
"The differential equation is:\n",
"\n",
"$ \\dot{x}(t) = -\\alpha x(t) + u(t) $\n",
"\n",
"Variables are as in standard state variable representation. Lowpass is $y(t) = x(t)$, and highpass is $y(t) = u(t) - x(t)$.\n",
"\n",
"Here, $\\alpha$ controls the corner frequency. It's easiest just to normalize it to 1. The state space reprentation could hardly\n",
"be simpler:\n",
"\n",
"$$ \\begin{align}\n",
"A & = -1 \\\\\n",
"B & = 1 \\\\\n",
"C & = 1 \\\\\n",
"D & = 0\n",
"\\end{align} $$\n",
"\n",
"For highpass, $C = -1$ and $D = 1$ instead."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def rc_lp_system():\n",
" return array([[-1]]), array([1]), array([1]), 0\n",
"\n",
"def rc_hp_system():\n",
" return array([[-1]]), array([1]), array([-1]), 1\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"State variable filter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The jos paper gives the state variable representation (here we switch the order of the state variables so they\n",
"respresent left-to-right flow, and to emphasize the similarity to the other filters).\n",
"\n",
"$$ \\begin{align}\n",
"A & = \\left[\\begin{array}{cc} -k & -1 \\\\ 1 & 0 \\\\ \\end{array}\\right] \\\\\n",
"B & = \\left[\\begin{array}{c} 1 \\\\ 0 \\\\ \\end{array}\\right] \\\\\n",
"C & = [ \\begin{array}{cc} 0 & 1 \\end{array} ] \\\\\n",
"D & = 0\n",
"\\end{align} $$\n",
"\n",
"Here, $k$ controls resonance. It is equal to $1/Q$, and ranges from 2 for no resonance to 0 for undamped resonance,\n",
"i.e. self-oscillation. For a resonance control that varies from 0 to 1, simply set $k = 2 - 2 \\cdot \\mathit{resonance}$.\n",
"\n",
"For bandpass, $C = [ 1 \\: 0 ]$ instead, and for highpass,\n",
"$C = [ {-k} \\: {-1} ]$ and $D=1$. In fact, all \"Audio EQ Cookbook\" responses can easily be done with a state variable\n",
"filter, making it an appealing alternative to canonical form biquads. See [Second order sections in matrix form](Second%20order%20sections%20in%20matrix%20form.ipynb) notebook for all of these recipes. Also see [Solving the continuous SVF equations using trapezoidal \n",
"integration and equivalent currents](http://www.cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf) for a different\n",
"derivation."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def svf_lp_system(res):\n",
" k = 2 - 2 * res\n",
" A = array([[-k, -1], [1, 0]])\n",
" B = array([1, 0])\n",
" C = array([0, 1])\n",
" D = 0\n",
" return A, B, C, D\n",
"\n",
"def svf_hp_system(res):\n",
" k = 2 - 2 * res\n",
" A = array([[-k, -1], [1, 0]])\n",
" B = array([1, 0])\n",
" C = array([-k, -1])\n",
" D = 1\n",
" return A, B, C, D"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Moog filter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Moog filter also has a simple state-space representation. It is easy to build the total system by stitching\n",
"together four one-pole lowpass filters and adding a feedback path.\n",
"\n",
"$$ \\begin{align}\n",
"A & = \\left[\\begin{array}{cc} -1 & 0 & 0 & -k \\\\\n",
"1 & -1 & 0 & 0 \\\\\n",
"0 & 1 & -1 & 0 \\\\\n",
"0 & 0 & 1 & -1 \\\\\n",
"\\end{array}\\right] \\\\\n",
"B & = \\left[\\begin{array}{c} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\\\ \\end{array}\\right] \\\\\n",
"C & = [ \\begin{array}{cc} 0 & 0 & 0 & 1 \\end{array} ] \\\\\n",
"D & = 0\n",
"\\end{align} $$\n",
"\n",
"Here, k varies from 0 (no resonance) to 4 (self oscillation)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def moog_system(res):\n",
" k = 4 * res\n",
" A = array([[-1, 0, 0, -k], [1, -1, 0, 0], [0, 1, -1, 0], [0, 0, 1, -1]])\n",
" B = array([1, 0, 0, 0])\n",
" C = array([0, 0, 0, 1])\n",
" D = 0\n",
" return A, B, C, D"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Response of continuous time filter in state space representation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's possible to accurately compute the frequency and phase response of a state space filter, without for\n",
"example having to expand out an impulse response and compute a fourier transform.\n",
"\n",
"The transfer function of a filter in (continuous) state space formulation is simple:\n",
"\n",
"$$ H(s) = D + C(sI-A)^{-1}B $$\n",
"\n",
"The Wikipedia article on State space representation isn't a bad source for this: http://en.wikipedia.org/wiki/State_space_representation\n",
"\n",
"We can use it to plot out the frequency response of some of the filters we defined above. (Note: if you're interested in\n",
"phase response, just plot the angle(response) instead of abs(response))."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def transfer(sys, s):\n",
" A, B, C, D = sys\n",
" return D + dot(C, dot(inv(s * identity(len(A)) - A), B))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"xs = logspace(-3, 0, num=200)\n",
"def db(x):\n",
" return 20 * log10(abs(x))\n",
"def freqz_ss_continuous(sys, f):\n",
" return array([transfer(sys, 1j * x / f) for x in xs])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The one-pole lowpass and highpass filters have the expected response, with both at -3dB at the corner frequency,\n",
"and 6 dB/octave rolloff."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = sqrt(1e-3)\n",
"semilogx(xs, db(freqz_ss_continuous(rc_lp_system(), f)))\n",
"semilogx(xs, db(freqz_ss_continuous(rc_hp_system(), f)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt8z3X/x/HHDtaQQiEZyTbHNKNyChvGNrbm2Myhoih1\nIVeuqF+hLlKKq5Mojcip0gzZnGpyyOEK5XDFZMuMJedTzLbP749PuXI57rvvd5/v4Xm/3b4323ef\nfT7P9clrb+/P++BlGIaBiIh4BG+rA4iISPFR0RcR8SAq+iIiHkRFX0TEg6joi4h4EBV9EREP4rCi\nn5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dSNyF02+XmJmZSUxMDNu2bbv0wl5ejBw58uLnYWFhhIWFOTKKuKEzuWd4LPkxfjnxC190+4Kq\nt1a1OtIlli2DPn1g2DBzRq8215LCSktLIy0t7eLno0ePdr6if/DgQSpXrgzAxIkT2bRpE7Nnz770\nwmrpSxFlHs8kbm4cDe5owOSOk/H39bc60hX98gt07Wpu1pKYCCVLWp1IXJlTtvT79OnD1q1b8fLy\n4u6772bKlClUqnRp/6qKvhTFqsxVxM+P5/nmzzO48WCn35/299/h8cdh925YsMAc8SNiC6cs+jd0\nYRV9sdGkTZMYvWo0szrPcvhwTHsyDHPZhvffh6QkuP9+qxOJK1LRF4+RX5DP0KVDWb53OYt6LCKw\nfKDVkWyyYAE88QS88w706GF1GnE12jlLPMKZ3DMkfJnA6dzTrOu3jrL+Za2OZLO4OLj7bnjoIdi1\nC0aO1ANeKT5acE2cXs7pHFpN
"text": [
"<matplotlib.figure.Figure at 0x108a38bd0>"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we plot the response of the Moog filter model, and also see the expected response."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in range(5):\n",
" sys = moog_system(0.2 * i)\n",
" semilogx(xs, db(freqz_ss_continuous(sys, f)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEFCAYAAADjUZCuAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlYVnX+//HnvQACgoIrgooKiiimaahliiUpWY6Taeqk\nllmN5ljZZvWb0iY1+04zkxWt2mKbOaWoGWomOtW4pNOiaOKON+AGbiyynd8fN6IEGtvNfQOvx3Wd\n65z7c+77nLccOa/7LJ+DyTAMAxERqffMzi5ARERcgwJBREQABYKIiBRRIIiICKBAEBGRIgoEEREB\nHBgIycnJDBw4kC5dutC1a1fmz58PQHp6OtHR0XTs2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qpXXXZBAd+dPs3ajAxWnDzJuYICRjRtRvdTzUhZ58uqL03s/rmABzoepZ8t\nGb+wBnSa34GGXRsC9iO0TUc28c72d/hi9xfc0eUOHurzEGFNwy6u5NgxGDvWfmX844+hZUvO5ufT\nd/t2HggM5P5mAWztspXQV0PxH+xfkz9ZkVIUCFK98vMv9mq7tJPCuXOX78CQmWm/BzUjw77jv3R8\n7pz9nlN/f3vHhIAAe+eEgICL061a2QOgjDt8LnU8N5fNZ86w6cwZ/nvmDFvOniXC25toPz9i/P2J\n9PXFXBQauUdzSX0nlcPzbZxp3pDlnq35ZFdjBt5g4s474dZb7T23Lzh67iixW2N5/YfXubH9jTwX\n9RyhTULtMwsK7EcICxdCXBz07Mm+7Gyu3b6dJV260OW7AvY+tJdeP/fC4llHeutJrVR3A2HlSvuL\nsroCX5iu6vzqXFZl11VYeLEr
"text": [
"<matplotlib.figure.Figure at 0x108a95b10>"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Choose a discretization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By far, the most popular discretization method in audio applications is the bilinear transform. A second, valid choice is\n",
"step invariance. These two are the most popular discretization methods, though others exist, as they both preserve the\n",
"stability properties of the original filter.\n",
"\n",
"A very simple discretization is forward Euler. You'll see this quite a bit in older papers (particularly if they seem to\n",
"be struggling with how to accommodate the delay-free feedback path), but it's basically rubbish. It works okay at very low\n",
"frequencies, but can easily transform a stable filter into an unstable one (as happens in the discretized state variable\n",
"filter), or totally lose the resonance properties (as in the Moog). It turns out to be totally fine for the one-pole lowpass,\n",
"so you'll see it there, and as a consequence works for the Moog filter in the zero resonance case, because that's just\n",
"four of those strung together.\n",
"\n",
"Both bilinear transform and step invariant lose accuracy, but in different ways. The bilinear transform has exactly the same\n",
"_frequency_ response as the analog prototype, but with frequencies warped, so that infinite frequency in the analog domain\n",
"maps to the Nyquist frequency. By contrast, step invariance has exactly the same _impulse_ response assuming that the input\n",
"is a step exactly one sample period wide (also known as zero order hold), but since that signal is not band-limited, it\n",
"means that the frequency response is aliased. Note that it's just the response that's aliased; there are no frequencies in\n",
"the output that are not present in the input.\n",
"\n",
"A good general criterion for choosing is: does the frequency response have a sharp lowpass characteristic? If so, then\n",
"with step invariance, the aliased components will be small, and primarily affect only the near-Nyquist frequencies. Otherwise,\n",
"for example for a high-pass response, bilinear transform is a better choice. We'll show this with graphs after implementing\n",
"the discretization techniques."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Bilinear transform"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we need to pre-warp the frequency response, so that the response of the digital filter matches that of the analog filter\n",
"at the cutoff frequency. Here, frequency $f$ is as a fraction of the sampling rate, so that 1/2 is the Nyquist frequency.\n",
"\n",
"$ g = tan( \\pi f ) $\n",
"\n",
"Then, the discretization is as given by [Comparing digital implementation\n",
"via the bilinear and step-invariant transformations](http://myweb.polyu.edu.hk/~magzhang/Research/ZC04_auto.pdf), Guofeng Zhang and Tongwen Chen.\n",
"\n",
"$$ \\begin{align}\n",
"A_d & = (\\mathbf{I} - gA)^{-1} (\\mathbf{I} + gA) \\\\\n",
"B_d & = 2g(\\mathbf{I} - gA)^{-1} B \\\\\n",
"C_d & = (\\mathbf{I} - gA)^{-1} C \\\\\n",
"D_d & = g C (\\mathbf{I} - gA)^{-1} B + D\n",
"\\end{align} $$\n",
"\n",
"Here, the $(\\mathbf{I} - gA)^{-1}$ term represents solving the entire set of equations simultaneously, and includes the zero-delay\n",
"feedback term. To me, it's much easier just to throw the system into a matrix and solve it by matrix inverse in this way,\n",
"rather than solving all the individual equations by hand. Your mileage may vary."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def bilinear(sys, f):\n",
" A, B, C, D = sys\n",
" g = tan(pi * f)\n",
" im = inv(identity(len(A)) - g * A)\n",
" Ad = dot(im, identity(len(A)) + g * A)\n",
" Bd = 2 * dot(im, g * B)\n",
" Cd = dot(C, im)\n",
" Dd = D + dot(C, dot(im, g * B))\n",
" return Ad, Bd, Cd, Dd"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some historical notes. To my knowledge, the first publication of a proper bilinear transform of the analog Moog filter\n",
"was: http://quod.lib.umich.edu/cgi/p/pod/dod-idx/preserving-the-structure-of-the-moog-vcf-in-the-digital.pdf, Preserving the Structure\n",
"of the Moog VCF in the Digital Domain, Federico Fontana.\n",
"\n",
"The earlier publications by Stilson and Smith, then Huovilainen, contained defective discretizations that attempted to deal with\n",
"the delay free loop some other technique than simply solving the equations.\n",
"\n",
"These days, however, the proper bilinear transform is standard. In synthesizer circles, the technique goes by the name\n",
"\"zero delay filters\" and also \"topology preserving transform\". An important milestone in popularizing this technique was\n",
"the publication of http://www.native-instruments.com/fileadmin/ni_media/downloads/pdf/VAFilterDesign_1.0.3.pdf, The Art of\n",
"VA Filter Design, by Vadim Zavalishin.\n",
"\n",
"Meanwhile, outside the published literature, there is an excellent discussion of this topic in the KVR DSP Forum thread,\n",
"http://www.kvraudio.com/forum/viewtopic.php?f=33&t=349859, Cheap non-linear zero-delay filters."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Step-invariant transform"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The step-invariant transform represents the exact response under the assumption that the input is a sequence of steps\n",
"each one sample period in width (also known as zero order hold).\n",
"There are a few ways to compute it; here is the one I find\n",
"easiest.\n",
"\n",
"$ A' = \\left[\\begin{array}{cc} 0 & 0 \\\\ B & A \\end{array}\\right] $\n",
"\n",
"Then,\n",
"\n",
"$ \\left[\\begin{array}{cc} \\_ & \\_ \\\\ B_d & A_d \\end{array}\\right] = \\exp(2 \\pi f A') $\n",
"\n",
"Here, $\\exp(A)$ represents the matrix exponential, and the underscores respresent throwing away that part of the\n",
"resulting matrix.\n",
"\n",
"And\n",
"\n",
"$$ \\begin{align}\n",
"C_d & = C \\\\\n",
"D_d & = D\n",
"\\end{align} $$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def step_invariant(sys, f):\n",
" A, B, C, D = sys\n",
" A_ = concatenate([[zeros(len(A) + 1)], concatenate([[B], A.T]).T])\n",
" eA = linalg.expm(2 * pi * f * A_)\n",
" Ad = eA[1:, 1:]\n",
" Bd = eA[1:, 0]\n",
" return Ad, Bd, C, D"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Matrix exponential is a cookbook technique (implemented as linalg.expm in NumPy). It is defined in terms of the straightforward extension of the Taylor series for\n",
"ordinary exp(x) generalized to matrices:\n",
"\n",
"$$ exp(A) = \\sum\\limits_{k=0}^{\\infty} \\frac{A^k}{k!} $$\n",
"\n",
"If you're implementing it by yourself, for small matrices, the \"scaling and squaring\"\n",
"technique works best. See http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.156.9316, The scaling and squaring method for the matrix exponential revisited, Nicholas Higham. Here's a simple implementation that computes the first n1 terms of the Taylor's series expansion\n",
"for exp, followed by n2 repeated squarings."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# should give the same results as linalg.expm for well-behaved systems\n",
"def my_expm(A, n1 = 8, n2 = 8):\n",
" A1 = A / (1 << n2)\n",
" A2 = np.identity(len(A))\n",
" A3 = np.identity(len(A))\n",
" for i in range(1, n1):\n",
" A3 = dot(A1, A3) / i\n",
" A2 = A2 + A3\n",
" for i in range(n2):\n",
" A2 = dot(A2, A2)\n",
" return A2"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Implementing state space filters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Actually running a filter given in discrete time state space formulation is pretty straightforward. Mathematically, it is\n",
"given as:\n",
"\n",
"$$ \\begin{align}\n",
"\\mathbf{x}(n + 1) = & A\\mathbf{x}(n) + B\\mathbf{u}(n) \\\\\n",
"\\mathbf{y}(n) = & C\\mathbf{x}(n) + D\\mathbf{u}(n)\n",
"\\end{align} $$\n",
"\n",
"But it's probably just as easy to show the code:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def runss(sys, inp):\n",
" A, B, C, D = sys\n",
" x = zeros(len(A))\n",
" result = []\n",
" for u in inp:\n",
" result.append(dot(C, x) + D * u)\n",
" x = dot(A, x) + B * u\n",
" return result"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the impulse response for the bilinear and step-invariant transformations of the Moog filter are pretty similar,\n",
"but off in phase by a half sample or so."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sys = moog_system(0.8)\n",
"impulse = zeros(100)\n",
"impulse[10] = 1\n",
"plot(runss(bilinear(sys, 0.1), impulse))\n",
"plot(runss(step_invariant(sys, 0.1), impulse))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEACAYAAAC3adEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd4VWW6t++dRkIK6T2QQBICCAEFERVFaYKCiqgoIiKj\nDMoZz6gzls+CzhnBM+N4FKzYmNEBZFSKYigqNkCQIiUQAiSQ3kMqqev742WFbEjZZe0kwHNfVy7Z\n2e9a68ky2b/11NekaZqGIAiCIJzGqbMNEARBELoWIgyCIAiCGSIMgiAIghkiDIIgCIIZIgyCIAiC\nGSIMgiAIghl2C0NSUhIJCQnExcXx8ssvn/P+oUOHGDFiBO7u7rzyyitm70VHRzNo0CCGDBnC5Zdf\nbq8pgiAIggG42HNwQ0MD8+bNY9OmTURERDBs2DAmT55Mv379mtYEBASwaNEiVq1adc7xJpOJzZs3\n4+/vb48ZgiAIgoHY5TFs376d2NhYoqOjcXV1Zdq0aaxevdpsTVBQEEO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5ycsAjoO2KJB29MsnX3ZeS6hKH2pWSAj0isgn3Djf7vi4MwyOHszNnp0NtEATh/ESE\nwU6ys8HNt+38go7uMfi6+zp8/HVKCrgFnisMffooYYj0iSSrPMuhNgiCcH4iwmAnOTlg8mq7IklH\nFwY/dz+HCkNjI6SmQqNn6x5DpE8kWWUiDIIgnIsIgx3oXc/1bpZ5DPpYDF93X0pOOS6UlJGhthIt\nqmldGMK8wsirzKO+sd5hdgiCcH4iwmAHhYXQoweU1lrnMTg6lKTPSMquOFcYoqJOi1mtK4HdA8mr\nyHOYHYIgnJ+IMNhBUw9DO13P
"text": [
"<matplotlib.figure.Figure at 0x108a803d0>"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Matrix-based implementations in state space can be very, very fast. The \"raising\" trick of [Implementation of recursive\n",
"digital filters into vector SIMD DSP architectures](https://mns.ifn.et.tu-dresden.de/Lists/nPublications/Attachments/360/Robelly_P_ICASSP_04.pdf) by Robelly et al for an especially useful technique for implementing second\n",
"order filters on SIMD hardware tuned for 4xfloat vectors.\n",
"\n",
"See [Second order sections in matrix form](Second%20order%20sections%20in%20matrix%20form.ipynb) notebook for more details\n",
"on matrix implementation, including favorable comparisons of roundoff error compared to canonical direct form IIR implementations."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Response of state space filters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's possible to accurately compute the frequency and phase response of a state space filter, without for\n",
"example having to expand out an impulse response and compute a fourier transform. Of course, it doesn't\n",
"hurt to plot this Fourier transform of an impulse anyway, if for no other reason than a sanity check:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"impulse = concatenate([[1], zeros(255)])\n",
"w, h = signal.freqz(runss(bilinear(sys, 0.1), impulse))\n",
"semilogx(w, db(h))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEFCAYAAADjUZCuAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHPxJREFUeJzt3X1Y1GW+x/E3CqZW5no0H2ZITEBEMT0m6ilz2sCH3Dim\nyUH3lB3z9GCtuuWa/bEb7qbo7na8ssLNVkstk6xVssu4JHP09KC4dMrVaRONWhhEaxUfykRlzh8/\nRBBEGGbm95uZz+u65prhnpnffLwvnC/3ff8eIjwejwcREQl7rcwOICIi1qCCICIigAqCiIhUU0EQ\nERFABUFERKqpIIiICODHglBSUsLtt99Ov3796N+/P0uXLgXg6NGjpKamEh8fz6hRo6ioqPBXBBER\naYYIfx2HUF5eTnl5OQMHDuTUqVMMHjyYjRs38sorr9C5c2fmzp3L4sWLOXbsGIsWLfJHBBERaQa/\njRC6devGwIEDAbjmmmvo27cvbrebd955h6lTpwIwdepUNm7c6K8IIiLSDH4bIdT29ddfM3LkSPbu\n3csNN9zAsWPHAPB4PHTq1KnmZxERMY/fF5VPnTrFxIkTee6557j22mvrPBcREUFERIS/I4iISBNE\n+nPjZ8+eZeLEidx7772MHz8egK5du1JeXk63bt04dOgQ119/fb33xcbGcvDgQX9GExEJOb179+bA\ngQNev99vIwSPx8MDDzxAYmIis2fPrmlPS0tj1apVAKxataqmUNR28OBBPB6PX29PP/203997pdc1\n9vzlnru0vaHXNeU1VunP5rzP1/3pTd9ZuS8D1Z/NaQ+X/gzE//Wm9F9L/5D22wjho48+4rXXXmPA\ngAEMGjQIgKysLObNm0d6ejorVqwgJiaGN998018RGuVwOPz+3iu9rrHnL/fcpe0Nva4l/zZvefuZ\nzXmfr/uzKW3B1JfNfa+3/dmc9nDpz0D8X2+ozdf9GZBF5eaKiIjAgrGCVmZmJpmZmWbHCAnqS99S\nf/pWS787daRyGDDjr7JQpb70LfWntWiEICISIjRCEBERn1BBEBERQAVBRESqqSCIiAiggiAiItVU\nEEREBFBBEBGRaioIIiICqCCIiEg1FQQREQFUEEREpJoKgoiIACoIIiJSTQVBREQ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EkbMyfbxys9tFHxim6xVq8Lgj5+fmNPv/qq6+yefNmtm7dWtNms9koKSmp+bm0tBSbzdbg\n+zMzM2seOxwOHA6Ht1FFxCI6dDAWmGfMgMJC46SC4j2n04nT6fTZ9vxyYFpeXh5PPPEE27dvp3Pn\nzjXtLpeLKVOmUFBQgNvtJiUlhQMHDtQbJejANJHQ5fHAHXfA+PEwc6bZaUKLJY9UjouLo7Kykk6d\nOgEwfPhwsquvlrFw4UJWrlxJZGQkzz33HKNHj64fSgVBJKR98QXcdptxtbfu3c1OEzosWRBaSgVB\nJPTNm2ccsPbaa2YnCR0qCCISlL7/HhITYdUq0BKhb+hcRiISlK6+GpYsgUcfNS5JKuZTQRAR09x9\nN/TsCc89Z3YSAU0ZiYjJDhyAYcOMC+nUOm5VvKApIxEJarGxxrTRL39pdhLRCEFETHf6NPTvD8uW\nwahRZqcJXhohiEjQa9cOli6Fxx6DM2fMThO+VBBExBLGjTN2Q/3DH8xOEr40ZSQilvHNNzB4MOze\nDb16mZ0m+GjKSERCRs+eMHs2zJljdpLwpBGCiFjKjz8aU0fLl0NKitlpgotGCCISUtq2NY5gnjlT\n12AONBUEEbGctDSIjoYXXzQ7SXjRlJGIWJLLZZz0bv9+6NjR7DTBQVNGIhKSEhONkcKiRWYnCR8a\nIYiIZbndMGAAfP65znPUFLoe
"text": [
"<matplotlib.figure.Figure at 0x108a92b50>"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The transfer function of a filter in (discrete time) state space formulation is simple, and very similar to\n",
"the continuous time version:\n",
"\n",
"$$ H(z) = D + C(zI-A)^{-1}B $$\n",
"\n",
"So similar, in fact, we can reuse the existing transfer() function for both continuous and discrete time.\n",
"\n",
"See https://ccrma.stanford.edu/~jos/filters/Transfer_Function_State_Space.html for the derivation and more discussion."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll plot the bilinear and step-invariant transforms for the Moog filter, against the analog prototype,\n",
"for a single frequency. Both are quite close, although the frequency warping of the bilinear version causes\n",
"its rolloff to accelerate at high frequencies."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def freqz_ss(sys):\n",
" return array([transfer(sys, exp(1j * pi * x)) for x in xs])\n",
"\n",
"axis([1e-2, 1, -60, 10])\n",
"f = 0.05\n",
"sysd = bilinear(sys, f)\n",
"semilogx(xs, db(freqz_ss(sysd)))\n",
"sysd = step_invariant(sys, f)\n",
"semilogx(xs, db(freqz_ss(sysd)))\n",
"\n",
"# Analog prototype\n",
"semilogx(xs, db(freqz_ss_continuous(sys, 2 * f)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVPX+x/HXwAyIiiuKV1AxwRA119TMiq4SLtelzcRW\nzbxlZrmldTPxd1UsrSzN5WouWbmUGXZL0lSyVdyXUMEdQdRyX1lmfn9MUV5NEZg5s7yfjwcP4cyZ\n8317O/fjl8+c8z0mm81mQ0REvIKP0QFERMR5VPRFRLyIir6IiBdR0RcR8SIq+iIiXkRFX0TEixS7\n6Pfu3Zvg4GAaNmxYsO348ePExMRQt25d7rnnHk6ePFncYUREpAQUu+j36tWLpKSky7aNGzeOmJgY\n0tLSaNu2LePGjSvuMCIiUgJMJXFz1v79++ncuTPbtm0DIDIykm+++Ybg4GCys7OJjo5m586dxQ4r\nIiLF45Ce/pEjRwgODgYgODiYI0eOOGIYERG5QQ7/INdkMmEymRw9jIiIFILZEQf9va1TrVo1Dh8+\nTNWqVa/YJzw8nD179jhieBERj1WnTh12795d5Pc7ZKbfpUsX5s6dC8DcuXPp1q3bFfvs2bMHm83m\nEV8jR470iDGLe8yivv9G3lfYfa+3X3Ffd5cvo/4ennJ+GnFuXm+f4k6Wi1304+LiaN26Nbt27aJG\njRrMnj2b4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Hk8cz5afz87kZH7+9DNsm36k5sHd1D96itKBFnKr38TPzbuSO+4J2nVpwc0Oelyhq7tp8DSa9GrI\nsc1rqdK4pdFxxIMkJyeTnJxc
"text": [
"<matplotlib.figure.Figure at 0x108ac8510>"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The bilinear transform shows the characteristic warping of frequency response, including a zero at the\n",
"Nyquist frequency."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"axis([1e-2, 1, -60, 10])\n",
"for i in range(5):\n",
" f = 0.4 * .5 ** i\n",
" sysd = bilinear(sys, f)\n",
" semilogx(xs, db(freqz_ss(sysd)))\n",
" semilogx(xs, db(freqz_ss_continuous(sys, 2 * f)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd4FNX6xz+zLb2RThJKGgnSi4iCBBAQFOEqIqDYwGtv\n14bXAjbA8rN3BETFAteColJUQpXeSUgnpPe62WyZOb8/FhAkkE1dkPk8zzwze9p8lyzvnHnPOe+R\nhBACFRUVFZULAo2zBaioqKiotB+q0VdRUVG5gFCNvoqKisoFhGr0VVRUVC4gVKOvoqKicgGhGn0V\nFRWVC4gWG/3bb7+d4OBgevbseSKtvLycUaNGERsby+jRo6msrGzpbVRUVFRUWoEWG/3bbruNVatW\nnZI2f/58Ro0aRWpqKiNHjmT+/PktvY2KioqKSisgtcbirCNHjjB+/HgOHDgAQFxcHOvXryc4OJjC\nwkISEhI4fPhwi8WqqKioqLSMNvHpFxUVERwcDEBwcDBFRUVtcRsVFRUVlSbS5gO5kiQhSVJb30ZF\nRUVFxQF0bdHocbdOSEgIBQUFBAUFnVYmOjqajIyMtri9ioqKyj+WqKgo0tPTm12/TXr611xzDUuW\nLAFgyZIl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9NewsPDycvL68tbqXS\nhqT+tARsNgZM+Y9D5fUubiT3DCXl63MrfrgQ8PXXntx6q83hOhMnurN9u18bqmoehYVfIO2+lLDp\ncQ6VDxjYF2GF6vwDbaysaVRYrfxSVvbXrJ2zuHYA8PBg9NGj56Rff+GehczoO+OMY15/N/rBNWNY\nnbHaaQ/iNjH6ZxvwUzl/KHtn
"text": [
"<matplotlib.figure.Figure at 0x108acba50>"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this particular filter response, the step invariant transform is more accurate."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"axis([1e-2, 1, -60, 10])\n",
"for i in range(5):\n",
" f = 0.4 * .5 ** i\n",
" sysd = step_invariant(sys, f)\n",
" semilogx(xs, db(freqz_ss(sysd)))\n",
" semilogx(xs, db(freqz_ss_continuous(sys, 2 * f)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd8FNXax38zu5tNb6Q3AqmE0DsIBAxditIErKACKuCV\nq8LrvQqKgOhVQIqINAtIEykiUkM19JqQXkjv2STbd+Z5/1hAkZLsZpPJynz5zGdmz5zyG5I8e+Y5\nzzmHISKCiIiIiMhjASu0ABERERGRxkM0+iIiIiKPEaLRFxEREXmMEI2+iIiIyGOEaPRFREREHiNE\noy8iIiLyGFFvoz958mR4e3ujTZs2d9PKy8sxYMAAhIeHY+DAgaisrKxvMyIiIiIiFqDeRv/ll1/G\ngQMH7klbvHgxBgwYgJSUFDz55JNYvHhxfZsREREREbEAjCUmZ2VlZWH48OG4fv06ACAyMhLHjx+H\nt7c3CgsLERMTg6SkpHqLFRERERGpHw3i0y8qKoK3tzcAwNvbG0VFRQ3RjIiIiIiIiTT4QC7DMGAY\npqGbER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bbtDS8\nsmcPmNGja83r3K8fOubk4HhlZSMoM40T2SdgL7NHZ7/OWL8emDz5/uEJGxsgNhY4cuS2X1/A0M0G\nMfp5eXkIDAy8+zkgIAB5VjSrTsRIwp5vwXI82o+bWaf8MrkdEtv4ImXrqgZWZhpEwN693njjDds6\nlxk50g5nz7o0oCrzyDq+FrKU3nBpE1Cn/F5PdAbpCNUlTeuLuEKvx36FApM8PQF7+9oLPPEEBp46\nhYOFhQ0vzkTWXV6HKR2mQKlk
"text": [
"<matplotlib.figure.Figure at 0x108aaef10>"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For highpass filters, the bilinear transform performs fairly well, albeit with noticeable effects\n",
"from frequency warping at higher corner frequencies."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sys = svf_hp_system(0.5)\n",
"axis([1e-2, 1, -60, 10])\n",
"for i in range(5):\n",
" f = 0.4 * .5 ** i\n",
" sysd = bilinear(sys, f)\n",
" semilogx(xs, db(freqz_ss(sysd)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd4FNXbhu/dNAiEDgkQIEgLoSYhgFiIBUUUVDooKkVs\n2LAA+lNBDaCfXQSRroIUaYqAIBhaAoHQEyABAqQnBNLLZnfP98cBBATFFGZncu7rmmtmJ7szT5Kd\nZ86855z3NQkhBAqFQqGoEJi1FqBQKBSKm4cyfYVCoahAKNNXKBSKCoQyfYVCoahAKNNXKBSKCoQy\nfYVCoahAlNr0R4wYgaenJ+3atbu079y5c/To0YOWLVty3333kZmZWdrTKBQKhaIMKLXpDx8+nPXr\n11+xb+rUqfTo0YOYmBjuuecepk6dWtrTKBQKhaIMMJXF5KxTp07Ru3dvDh06BICvry9btmzB09OT\n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t6Ou956/3mrj4E2GC2kBtE7S9\neAATCASmi58T0O/CPqbLn+cBeRSTwjngnHwvgkYm+BzgODAO9iCX0lIupt+wYUPi4+MvvY6Pj8fb\n2/tv75s4ceKl7eDgYIKDg8tDjqKcyArPIuGzBM5vPo/nY560W9uOqu2qai3rP3OuuJhvk5KYlpiI\nn7s7zzdowMN16uBqdqwnE5PZhFMlJ/ine2l4OPzfFIiIgNGj4bsZcI1rTw/sTd7LmLVjKLAW8MFd\nH/Bgiwf/1qAU9gtWLPgr5CG4zKGv2n/Vz4UQ/33/ZfuEEBe6NQQC2U9jt8t+GmEX8h4m5Do3T/Bo\nXwgJEXTsCHa7HXuxFaxWhNUK
"text": [
"<matplotlib.figure.Figure at 0x108a7ec50>"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, step-invariant is not a good choice for high-pass filter responses, as expected."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"axis([1e-2, 1, -60, 10])\n",
"for i in range(5):\n",
" f = 0.4 * .5 ** i\n",
" sysd = step_invariant(sys, f)\n",
" semilogx(xs, db(freqz_ss(sysd)))\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEFCAYAAAAPCDf9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcFPX/wPHXLoeI94kKKgooYmjg1fGrqDTtULs0tcM8\n0zIz+5Z2a988ysrK1NQEzazU1LD6enVQmSLeKYeAoCKHgCD3tbuf3x+jKIoHsLCL+34+HvPYYZid\n+awu75l5z2feH51SSiGEEMIm6C3dACGEEDVHgr4QQtgQCfpCCGFDJOgLIYQNkaAvhBA2RIK+EELY\nkCoH/dGjR+Pi4oKvr2/psoyMDPr160enTp247777OHv2bFV3I4QQwgyqHPRHjRrFli1byiybO3cu\n/fr1Izo6mnvvvZe5c+dWdT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S517PzxeYTOSbTBRcdEVyflmOoZjj2SlkFudT36kZOjsnCkwmSpTC+dwVj6NeTx2d\nrsy847nfXWne8dz6dfR6SvJ17NyhY+9uHTf76rjvXh0tm+mw15Wd7HSXLyvze7hs2fnU28WvuvJ+\nvmiZ7pL3lLe+ua6grDbob9myhSlTpmA0Ghk7dizTpk0ru2MJ+je0kjMlZGzNIGNLBpm/ZoIOGv1f\nIxr0aEC9rvVw7uqMUzsndPqauYF4LSaDiVPpBXwXm8T/4tNoUqBngH0jbqMezYrsMOYYMeYYMeRo\nB47S+fPLsy/MA+UeEOzq2ZVeieidL7kqKW/ZxVcvTnr0dfToHHXoHXXofvwB/cx3oEULLdj3719j\nN2OvRCnFDxE/MHXbVPp17McH
"text": [
"<matplotlib.figure.Figure at 0x108a9c910>"
]
}
],
"prompt_number": 19
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"A bit of simplification"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's certainly possible to just use matrix primitives to design the filter. However, for modulating the filter parameters\n",
"at a high rate, it might make sense to simplify the math symbolically. For the state variable filter, the following\n",
"code goes straight to the bilinear transformed state space representation, using a by-hand symbolic solution to the matrix\n",
"inverse:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def svf_lp(f, res):\n",
" g = tan(pi * f)\n",
" k = 2 - 2 * res\n",
" a1 = 1/(1 + g * (g + k))\n",
" a2 = g * a1\n",
" a3 = g * a2\n",
" A = array([[2*a1 - 1, -2*a2],\n",
" [2*a2, 1 - 2*a3]])\n",
" B = array([2 * a2, 2 * a3])\n",
" C = array([a2, 1 - a3])\n",
" D = a3\n",
" return A, B, C, D"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we can check that this really does compute the same system."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = 0.1\n",
"res = 0.2\n",
"print svf_lp(f, res)\n",
"print bilinear(svf_lp_system(res), f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(array([[ 0.23043279, -0.39979185],\n",
" [ 0.39979185, 0.87009975]]), array([ 0.39979185, 0.12990025]), array([ 0.19989592, 0.93504988]), 0.064950123180475744)\n",
"(array([[ 0.23043279, -0.39979185],\n",
" [ 0.39979185, 0.87009975]]), array([ 0.39979185, 0.12990025]), array([ 0.19989592, 0.93504988]), 0.064950123180475744)\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simplified code for the rest of the [Audio EQ cookbook](http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt) designs are also at the [Second order sections in matrix form](Second%20order%20sections%20in%20matrix%20form.ipynb) notebook.\n",
"\n",
"Similarly, for the Moog filter, it's not too difficult to invert the matrix symbolically. Both Vadim's book and\n",
"the KVR thread on cheap zero delay filters contain these derivations.\n",
"\n",
"TODO: adapt code and put here."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"A note on nonlinearity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Real analog filters have nonlinearities, and true \"virtual analog\" simulations attempt to model those as\n",
"well as the purely linear components.\n",
"Antti's paper is excellent at presenting differential equations representing\n",
"an only slightly idealized version of the Moog filter.\n",
"\n",
"Really emulating the non-linearity is beyond the scope of this cookbook, but I will note that keeping the\n",
"state space variables matching physical quantities (voltages across capacitors, etc.) is a huge help. There\n",
"are a number of ways of simply adding the nonlinearity back in. These are covered in more detail in the\n",
"\"cheap zero delay\" KVR thread and Vadim's book."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Conclusion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have shown how to design digital IIR filters in a state space framework, starting from analog prototypes\n",
"and giving some choices regarding discretization. All these techniques are cookbook and don't require solving\n",
"systems of equations by hand, or doing symbolic manipulation.\n",
"\n",
"These techniques work on a wide range of source filters and allow both easy, fast implementation and analysis, for example plotting the frequency response as a precise function of frequency, not requiring Fourier transforms\n",
"of impulse responses.\n",
"\n",
"In addition, the literature is rich in techniques for state space systems. As an example, the poles of the resulting\n",
"filter are exactly the eigenvalues of the $A$ matrix. In analog form, state space matrices can be composed arbitrarily,\n",
"including with feedback paths, making the basic technique powerful for circuit design and analysis.\n",
"\n",
"It is my hope that these matrix formulations will make the process of designing digital filters less mysterious,\n",
"and accessible to a broader audience, much in the way that Dan Lancaster's Active Filter Cookbook popularized analog\n",
"filter designs, and no doubt contributed to a great many home-built designs.\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}