Modeling analog circuits in DSP?

Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.
It's easy to transcribe an analog circuit into SPICE and get a plot of the frequency response, impulse response, distortion or other useful measurements. What I would like to do is model a particular analog circuit in DSP for real time processing of audio.

Are there any textbooks or articles which describe this technique in tutorial fashion?

I see many web sites geared towards designing a filter, but none for analysis of an existing filter or translation of an existing circuit into a software DSP model.


Case in point: I'm looking at a discrete FM transmitter schematic based on transistors and passives. There is a 15 kHz low-pass and a 19 kHz notch filter that I would like to emulate exactly in software.

The 15 kHz LP is a series of 'pi' filters with shunt capacitors and series inductors. In total, there are 4 capacitors and 3 inductors (plus a series input resistor and a shunt output resistor). Quick searches online led me to determine that this is a 7th-order filter, presumably 42 dB/octave. Analog filter design calculators even produce very similar capacitor and inductor values when asked to create a 7th-order low pass, but the pattern isn't quite the same. I'm sure this is because I found a Chebyshev design calculator and the schematic I have was probably designed using a different method. Rather than continuing to search for new methods until I find a match, I'd rather just model each passive component in software and duplicate the exact filter, as is.

There is also a 19 kHz notch as a parallel combination of an inductor and capacitor in the signal path. I'd like to model that, too. SPICE doesn't give me DSP source, just graphs, and certainly not on real time audio inputs.

Looking back at my college texts, I've almost remembered how to calculate the frequency response of a random circuit. The resulting formula would reveal the poles and zeroes of the transfer function. At that point, I could probably use Direct Form I or II to translate the formula into code.

I'm just wondering if there might be an easier way to go directly from discrete components to DSP code, perhaps without solving algebra and calculus problems.
 
I would look up DSP techniques on the web or buy a good DSP book.
Look up Texas Instruments and Microchip DSP websites.
Thanks for the reply!

I have several excellent DSP books. I have also designed and released four commercial products based on Microchip and Texas Instruments DSP chips. I have the DSP side of things fairly well sorted.

What seems to be missing from the texts and the web is the translation of specific analog circuits into DSP code that accurately models what those circuits are doing.

Here are the techniques that I see covered quite well:

A) SPICE modeling of analog circuits, but not in real time and certainly not with live audio input
B) Tools for designing new analog circuits based on filter specifications
C) Tools for designing new DSP software based on filter specifications
D) Math tools for analysis of analog circuits, including predicting the frequency response or impulse response
E) FFT and other DSP tools for analysis of software plugins, showing frequency response or impulse response

As I said, what seems to be missing are tools for translating an analog circuit directly into DSP that can model or emulate them in real time. What seems to be missing is that analog circuits deal in actual voltages, whereas DSP generally normalizes everything to +/-1.0, modifying the constants to produce unity gain with every building block. When it comes to emulation, I would think that the DSP would need to simulate actual voltages so as to deal with potential nonlinearities, or at least so that standard capacitance values and standard resistor values can be plugged in rather than just using the "ideal" values. The nice thing about DSP is that you are not limited to standard values for passives, and thus you can get closer to your ideal frequency response, although there are limitations when you get close to Nyquist or when filter coefficient quantization causes the frequency response to shift away from the ideal. Although that flexibility of DSP is a great feature on its own, it's not useful for emulation, where the goal is to mimic the exact performance of some existing analog circuitry.

My hypothesis-in-progress is that I could use the mathematical tools to analyze a given analog circuit and come up with a matrix of simultaneous equations, then translate those equations into DSP code. The trick might be the optimization phase, where one of the simple Direct Form code patterns is preferred, i.e., FIR or IIR. But somehow this seems like going around my elbow to get to my ear.

I think it should be possible to have an almost object-oriented DSP system where each capacitor, inductor, resistor, transistor, diode, etc. can be plugged in and emulated. The math would be isolated to each component, possibly without the need to manually analyze the entire circuit. Basically, what I am looking for is real-time SPICE that can output source code to be compiled into an efficient DSP plug-in ... draw the circuit, press a button, compile the code. I'd be happy to find a technique that works even without the simplicity of pushing a button, but I'm not even close.
 
DSP filter algorithms are based on what's "easy" to do in DSPs, and fall into two categories: FIR and IIR. I think the second is "more analog-like" but neither is an exact copy of real-life analog filters. It's certainly possible to make such DSP filters approximate analog filters (or any arbitrary frequency response) to reasonable precision, and I might suggest you go in that direction.

If you're determined to do an exact simulation in the digital domain, you could start with LTspice - it won't run in real-time, but it can read and write .wav files as stimulus and response files in simulated circuits.

Exact circuits CAN be simulated by computers/DSPs in real time, but I don't know of any examples of it being done, as it appears to be much easier both in computational efficiency and code development to use FIR and IIR (and now that I think if it, FFT, and there may be others that don't come to mind) filters, which are digital-domain-specific techniques that have been developed over many decades to run on computers.

The 15 kHz LP is a series of 'pi' filters with shunt capacitors and series inductors. In total, there are 4 capacitors and 3 inductors (plus a series input resistor and a shunt output resistor). Quick searches online led me to determine that this is a 7th-order filter, presumably 42 dB/octave. Analog filter design calculators even produce very similar capacitor and inductor values when asked to create a 7th-order low pass, but the pattern isn't quite the same. I'm sure this is because I found a Chebyshev design calculator and the schematic I have was probably designed using a different method. Rather than continuing to search for new methods until I find a match, I'd rather just model each passive component in software and duplicate the exact filter, as is.
That's a rather high-order filter for analog, which as I understand becomes more and more sensitive to small changes in component values as the order goes up. This leads to an advantage in digital filtering for high-order filters in that the simulated component values are always "exact" and fixed, but I still think it would be easier to get the exact frequency response of that filter from a spice simulator and then make a digital filter using "traditional digital filter techniques" that matches that response.

And of course there's a lot to both analog filter design and DSP filters. Here's a free online DSP book that may help:
The Scientist and Engineer's Guide to Digital Signal Processing
 
And so now I see this post:
...
My hypothesis-in-progress is that I could use the mathematical tools to analyze a given analog circuit and come up with a matrix of simultaneous equations, then translate those equations into DSP code. The trick might be the optimization phase, where one of the simple Direct Form code patterns is preferred, i.e., FIR or IIR. But somehow this seems like going around my elbow to get to my ear.

I think it should be possible to have an almost object-oriented DSP system where each capacitor, inductor, resistor, transistor, diode, etc. can be plugged in and emulated. The math would be isolated to each component, possibly without the need to manually analyze the entire circuit. Basically, what I am looking for is real-time SPICE that can output source code to be compiled into an efficient DSP plug-in ... draw the circuit, press a button, compile the code. I'd be happy to find a technique that works even without the simplicity of pushing a button, but I'm not even close.
I agree this is possible, and as with everything else involved with computers, it's just "a simple matter of programming."

I recall about 30 years ago writing a basic program on an Apple /// to draw the frequency response of a 3-way speaker crossover. I used my AC circuit analysis knowledge to calculate the reactance of each capacitor and inductor at the frequency of interest, wrote the equations to do the complex number arithmetic, and plot the response at each driver on a log-log screen.

Of course this was in the frequency domain, and you want to do this in the time domain, which is completely different. You're looking to write discrete-time versions of equations such the second one here:
RLC circuit - Wikipedia, the free encyclopedia

A quick look with the right keywords finds there ARE such things on the Web, at least in their simplest forms. A Google search for discrete time capacitor simulation got me here:
Low-pass filter - Wikipedia, the free encyclopedia
The text goes through an algebraic simplification of the circuit to get to the code, but it's at least in the direction you want of code for real-time circuit simulation.
 
Last edited:
DSP filter algorithms are based on what's "easy" to do in DSPs, and fall into two categories: FIR and IIR. I think the second is "more analog-like" but neither is an exact copy of real-life analog filters. It's certainly possible to make such DSP filters approximate analog filters (or any arbitrary frequency response) to reasonable precision, and I might suggest you go in that direction.
I think the DSP algorithms are not really as far from analog as you might think.

Taking the simplest RC filter and looking at the formula for the Vo versus Vi, you end up with equations that look almost exactly like the DSP equivalent. In the digital realm you have "out = a * in + b * out" where you have to keep the old value of out around in a state variable. One interesting thing here is that a = 1 - x and b = x, where x is a transcendental function of the filter corner frequency and the sample rate. But really, choosing 'a' and 'b' such that a = 1 - b is merely a technique to get the gain of the filter as close to unity as possible. If you get rid of this normalization, and adjust for voltages versus arbitrary digital ranges, then what you have is the exact same mathematical formula in both the analog RC filter and the digital equivalent.

The biggest difference is that the normalization in the digital version hides any passive losses that might occur in the analog version, and thus any simulation would need to take this into account. Also, whenever there are semiconductors in the signal path, you'll want to know actual voltages instead of normalizing to +/-1.0

If you're determined to do an exact simulation in the digital domain, you could start with LTspice - it won't run in real-time, but it can read and write .wav files as stimulus and response files in simulated circuits.
That's a great suggestion. If I stimulate with an impulse, then I should be able to use the response file to create an FIR filter that implements the exact transfer function of the analog circuit in real time. Mathematically, this FIR should be identical to the analog circuit except for any truncation in the response.

That's a rather high-order filter for analog, which as I understand becomes more and more sensitive to small changes in component values as the order goes up. This leads to an advantage in digital filtering for high-order filters in that the simulated component values are always "exact" and fixed, but I still think it would be easier to get the exact frequency response of that filter from a spice simulator and then make a digital filter using "traditional digital filter techniques" that matches that response.
As I mentioned above, digital filters have a couple of unique disadvantages. First of all, as your filter corner frequency approaches the Nyquist frequency, the actual frequency response of the digital filter deviates from the ideal. There are even some digital filter design programs which account for this drift by calculating the error and adjusting for it.

Another problem is that fixed-point filter coefficients and sometimes even floating-point filter coefficients can cause the actual frequency response to deviate from the ideal due to quantization errors. This is particularly a problem with IIR filters. Then again, these two examples may actually be symptoms of the same fundamental problem.

So, I'm not trying to say that digital is 'worse' than analog; I'm merely pointing out that both can be very sensitive to certain border values. At the very least, digital filters are perfectly repeatable, even if their response is not perfect. To the degree that you can tweak out any imperfections in the digital filter response, these tweaks can be duplicated precisely everywhere they're needed.

One of the reasons I'm being particular is that the online analog filter design tools suggested slightly different capacitor values, and I'd like to emulate the exact response. Also, I noticed that where the analog circuit reused the same capacitor values in a symmetric pattern, the filter design site showed a different pattern. I'll want to duplicate the actual pattern.

And of course there's a lot to both analog filter design and DSP filters. Here's a free online DSP book that may help:
The Scientist and Engineer's Guide to Digital Signal Processing
Ha! Yes, I've read that free PDF book so many times that I ended up buying a hard copy! I'll scan it again, because there are a lot of hidden gems in there. It's certainly the top book on my stack (well, the top of one of the many stacks of books that I have). There is also a second book from this same author, and I am trying to decide whether to get it, too.
 
Google bilinear transform. The bilinear transform is used for precisely this purpose if I understand your question correctly.

You have to write the transfer function of the (linear time-invariant) filter in terms of the Laplace transform Ha(S) (which is comparatively straightforward) whence it can be transformed to a linear shift-invariant filter of transfer function Hd(Z)

There are facilities in Matlab to cope with this type of simulation IIRC, you might find that one of the open-source alternatives is useful.
 
The bilinear transform is used for precisely this purpose if I understand your question correctly.

You have to write the transfer function of the (linear time-invariant) filter in terms of the Laplace transform Ha(S) (which is comparatively straightforward) whence it can be transformed to a linear shift-invariant filter of transfer function Hd(Z)

There are facilities in Matlab to cope with this type of simulation IIRC, you might find that one of the open-source alternatives is useful.
Thanks!

I guess I'm going to have to review my college texts again. I don't even remember using Laplace transforms, but I'm sure we covered it and then I forgot it due to lack of application. In my day, computers could not process audio in real time, but we still studied impulse responses and convolution. Beautiful math, and at the time I never dreamed that it would be possible in real time.

I think you may be right that this is what I need. I just have to study those chapters in every text, and put some time into practicing the math.

By the way, when you say "open-source alternatives," do you mean alternatives to Matlab (which I don't have due to its expense), or merely alternatives to the bilinear transform facilities alone?
 
If I stimulate with an impulse, then I should be able to use the response file to create an FIR filter that implements the exact transfer function of the analog circuit in real time. Mathematically, this FIR should be identical to the analog circuit except for any truncation in the response.

I think that's a really nifty technique and I've been playing with it myself in MATLAB. It will work (assuming the filter length is sufficient) for a circuit which is LTI (like your FM transmitter filters, which might be elliptical btw) but you also mentioned transistors and diodes...:eek:
 
I was referring to Scilab or Octave which are alternatives to Matlab. I think you need the supporting environment to make use of the filters in a simulation without a lot of effort. I believe Octave is closest in terms of interoperability of code, but I haven't had the need to explore either.
Thanks again. I run across Matlab all the time in my research, and I'm mindful that I might need to someday purchase a license. Instead, I tend to write my own software every time I need such calculations, but that can be time-consuming compared to the Matlab environment.

I'll take a look at Octave and Scilab. Good to know about these alternatives, as I'd never heard of them before.
 
I like where this is going and have had similar ideas previously but lacked programming skills/time to implement.

Another open source alternative to matlab is Python with iPython and the SciPy/Numpy/matplotlib tools.

The SciPy library has some filter design tools that seem as though they may be handy, documentation are here: Module SciPy.signal.filter_design
It's entirely possible that Python might host a solution, but I would like to clarify that I am not looking for filter design tools. Filter design tools start with specifications and end up with code or circuits. I can find plenty of tools - including instant web tools online - that will design either an analog circuit or a digital filter that meets a given set of starting specifications.

What I'm looking for are tools to analyze an existing (already designed) circuit for which the original design specifications may not be available, but where the individual analog components are known (either via schematic or reverse-engineering the circuit from the PCB layout and probing around with a meter). That's not something you can do with a standard filter design tool.
 
MATLAB's filter design and analysis toolkit (fdatool) is bulletproof for this stuff. You select the type of filter, the parameters for it, and hit "generate" -- it'll spit out all the coefficients, already broken down into biquads (which is what's usually used for actually implementing filters on DSPs). The process is bulletproof.

You didn't specify the DSP platform you're planning -- is it embedded, or running on a computer? Getting a filter going in software on a computer is pretty trivial; getting it implemented on an embedded DSP processor is a little more convoluted and requires an embedded skillset that most people don't have (do you know what a debugger is and how to use it? How familiar are you with writing straight ANSI C and/or assembly?)
 
Trash

It's easy to transcribe an analog circuit into SPICE and get a plot of the frequency response, impulse response, distortion or other useful measurements. What I would like to do is model a particular analog circuit in DSP for real time processing of audio.

I today stumbled on the site about TRASH, a utility that allows tweaking and modeling of classic amps, distortions, delays and filters to get a specific flavor of sound.
In fact it feeds into a software DSP from iZotope (running on the PC/Mac).
And the software is expensive (at 200 USD) For that you can now 'spoil' any recording into your beloved sound style, based on e.g. amp modeling (including over-driven tube stages, just missing is 'rolling of tubes'), sweepable filters and delays. :rolleyes:

The direction is there.
Now the next step is have a module and feed that into a board from e.g. XMOS or MiniDSP.
MiniDSP has a utility to calculate frequencies, so though a lot of manual, it should be possible to insert many types of circuit.
albert :)
 
MATLAB's filter design and analysis toolkit (fdatool) is bulletproof for this stuff. You select the type of filter, the parameters for it, and hit "generate" -- it'll spit out all the coefficients, already broken down into biquads (which is what's usually used for actually implementing filters on DSPs). The process is bulletproof.

You didn't specify the DSP platform you're planning -- is it embedded, or running on a computer? Getting a filter going in software on a computer is pretty trivial; getting it implemented on an embedded DSP processor is a little more convoluted and requires an embedded skillset that most people don't have (do you know what a debugger is and how to use it? How familiar are you with writing straight ANSI C and/or assembly?)
I have designed around the TMS320 DSP, so I'm painfully aware. I might be tempted to switch to the SHARC DSP, or I might stick with what I know.

I'm still not sure that a filter design tool would help if all I have is a schematic, and not the type or parameters of the entire circuit. I am interested in modeling not just the filter, but the other gain stages of the circuit.

Obviously, I'm not making very much progress though, as I haven't worked on these ideas in years. Talk about a slow process...
 
Member
Joined 2007
Paid Member
as others stated you should use the Tustin/bilinear transform:
1) work out the S domain transfer function of your analog circuit. Use traditional circuit analysis techniques for this. If its complicated using a symbolic solver will help a lot like mupad or Maple.
2) blinear transform into z domain transfer function (note at this point your also setting your system sample rate): Bilinear transform - Wikipedia the example here shows how to do it for an RC filter.
3) factorize into biquads for your practical implementation of an IIR filter. (again symbolic solver helpful)
4) program biquad coefficients into DSP

the final filter will approximate the original transfer function within the limitations of the system sample rate.
 
Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.