Sunday, April 22, 2012

Analysis tools – part 2

I use Mathematica as my swiss army knife for math, data analysis and modeling.

Mathematica can read and write many common file formats, so it’s easy for example to import an audio file to run a Fourier analysis or to compute some coefficients and export them for inclusion in some C/C++ code.

One of its strengths is the ability to perform symbolic computations, although for complex problems it’s often necessary to fall back to numerical methods.

Even more than for solving problems, I love to use it to study data and possible models and to get a better intuitive understanding of known results.
This screenshot shows some calculations I did to help me understand the effect of the feedback delay in a possible digital implementation of a resonant digital filter:

Mathematica

It’s possible to write programs in Mathematica, but they are typically slow. For serious number crunching, over time I have built a small collection of C/C++ routines for generic function optimization. I’ve used them for digital filter design, and I plan to use them for fine tuning the shape of the nonlinear distortion in the CEM filter emulation.

SPICE is an electronic circuit simulator. It’s very useful to understand how analog synth components work. One of the goals of virtual analog synth simulation is to recreate the waveforms generated by the original analog electronics. SPICE can take the schematics and derive by numeric computation the behavior of the circuits. Usually the main characteristics of the actual circuit output will be present also in the digital simulation, and it’s possible to study the signals by inserting probes at various stages along the signal path. SPICE can also show how the circuit’s behavior varies with temperature.

Here is a very simple example circuit of the output stage of the Matrix 1000:

OutputCircuit

I drew it because when I first looked at a recording of a square wave, it was obvious from the graph that the horizontal segments of the square wave are not horizontal at all (the vertical lines aren’t straight, either, because in the Matrix 1000 it’s not possible to completely disable the low pass filter and because of other effects of the analog circuitry). The horizontal lines have the shape of exponential decay functions, suggesting the presence of a high pass filter:

square1

I was curious to understand how much of the high pass filtering was due to the DC-removal filter on the synth output versus other circuits like the oscillator itself, electronics inside the CEM chip and DC-removal in the audio card). The SPICE simulation of the output stage shows a filter cutoff frequency of about 3 Hz (actual value depends on the input impedance of the audio interface connected to the synth output). The following graph shows the frequency response (red for magnitude, green for phase) of the output stage:

OutputResponse

In theory it should be possible to create a virtual analog emulation based on the original circuit and simulating its behavior numerically like SPICE does. However, there are also some major issues with this approach:

  • The simulation is expensive in terms of CPU usage. Generous oversampling would also be necessary to limit aliasing.
  • The result is only as good as the model used for each electronic component. A look at the graphs on actual datasheets shows that the typical math formulas used in standard models are a good approximation, but typically not a 100% match. It’s possible to get closer by creating more complex models containing many subparts, but this further increases the complexity. For example, one could model all the components that are inside an opamp rather than using simplified formulas, but there may be over one hundred basic components in it, often with unknown specs, and in turn each transistor, capacitor and resistor might need modeling of its non-ideal real-world behavior.
  • The model has hundreds of variables, and an error in just one of them can result in inaccuracies.
  • Real circuits don’t have all components perfectly matched like in SPICE circuits due to component tolerances and variable temperatures. Good design of the original and thermal-matched components limit the impact of these imperfections, but it is known that some of these effects actually contribute to the warmth of analog sound.
  • If the virtual analog implementation contains a very precise representation of the original analog circuit, some could argue that it partially makes use of the intellectual property of the original.

In the case of the CEM 3396 chip that generates each Matrix voice, as far as I know there are no schematics for what’s inside the chip, so SPICE simulation is not feasible. However, I still plan to look at other discrete designs from those years that are probably similar to understand their main characteristics. For example, I don’t currently know why the distortion in the CEM filter is asymmetrical (this is visible in the shape and spectrum of the self-oscillation waveform) and I hope that SPICE experiments can provide me an insight.