Modeling analog circuits in DSP? - diyAudio
Go Back   Home > Forums > Design & Build > Software Tools

Software Tools SPICE, PCB CAD, speaker design and measurement software, calculators

Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools Search this Thread
Old 13th November 2011, 01:56 PM   #1
rsdio is offline rsdio  United States
diyAudio Member
 
Join Date: Feb 2008
Location: Seattle
Default Modeling analog circuits in DSP?

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.
  Reply With Quote
Old 16th November 2011, 10:31 PM   #2
diyAudio Member
 
nigelwright7557's Avatar
 
Join Date: Apr 2008
Location: Carlisle, England
I would look up DSP techniques on the web or buy a good DSP book.
Look up Texas Instruments and Microchip DSP websites.
  Reply With Quote
Old 16th November 2011, 11:01 PM   #3
rsdio is offline rsdio  United States
diyAudio Member
 
Join Date: Feb 2008
Location: Seattle
Quote:
Originally Posted by nigelwright7557 View Post
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.
  Reply With Quote
Old 16th November 2011, 11:08 PM   #4
diyAudio Member
 
nigelwright7557's Avatar
 
Join Date: Apr 2008
Location: Carlisle, England
Sounds a bit like designing FPGA's using a schematic rather than VHDL.
I found understanding schematics easy but the VHDL is a bit too abstract for me.

I guess your only way to forward is to reverse engineer the hardware and convert it to software.
  Reply With Quote
Old 16th November 2011, 11:24 PM   #5
benb is offline benb  United States
diyAudio Member
 
Join Date: Apr 2010
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.

Quote:
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
  Reply With Quote
Old 16th November 2011, 11:42 PM   #6
benb is offline benb  United States
diyAudio Member
 
Join Date: Apr 2010
And so now I see this post:
Quote:
Originally Posted by rsdio View 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 by benb; 16th November 2011 at 11:43 PM. Reason: deleted redundant link
  Reply With Quote
Old 17th November 2011, 12:05 AM   #7
rsdio is offline rsdio  United States
diyAudio Member
 
Join Date: Feb 2008
Location: Seattle
Quote:
Originally Posted by benb View Post
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

Quote:
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.

Quote:
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.

Quote:
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.
  Reply With Quote
Old 17th November 2011, 12:20 AM   #8
diyAudio Member
 
Join Date: Jun 2011
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.
  Reply With Quote
Old 17th November 2011, 12:42 AM   #9
rsdio is offline rsdio  United States
diyAudio Member
 
Join Date: Feb 2008
Location: Seattle
Quote:
Originally Posted by counter culture View Post
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?
  Reply With Quote
Old 17th November 2011, 01:03 AM   #10
diyAudio Member
 
Join Date: Jun 2011
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.
  Reply With Quote

Reply


Hide this!Advertise here!
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DSP and vacuum tubes (from Analog Devices) jackinnj Tubes / Valves 8 13th November 2011 03:03 PM
HDMI to analog, or multiple analog channel mixer TAYLOR1337 Digital Source 1 12th September 2011 09:17 PM
RS-232 cable for DSP-Amp Digisynthetic DSP-1400 Sabbelbacke Parts 0 9th May 2009 09:33 AM
Research on tubes and circuits simple circuits Cazcotty Tubes / Valves 6 16th June 2006 12:50 PM
85 powersupply circuits 58 charge circuits gev Power Supplies 0 31st July 2005 11:27 AM


New To Site? Need Help?

All times are GMT. The time now is 11:14 PM.


vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
Copyright 1999-2014 diyAudio

Content Relevant URLs by vBSEO 3.3.2