AoE page 23 and 24

Mar 15, 2006 5 Replies

Okay so I am trying to make a c program which calculates an RC filters response to a bit stream (1 bit PWM audio playback). To determine the voltage on the output of the RC low pass filter after some given, fixed time interval (the bitrate), why cant I just use the equation on page



24 of AoE, the one at the top left (V = Vi(1-e^-t/RC) instead of the humungo one which I wont even try to type here but is directly to the right and is described as an 'inhomogeneous differential equation). Cant I just substitute the remaining voltage, i.e. the difference between the current voltage at the output and the max voltage input, like 5V (say the bitstream is 5V/0V) as the 'Vi' and then add it to the actual current voltage of the output?

You will need to split it into a charging and discharging case. In the charging case, you can use the difference, as you say, and in the discharging case Vi is just Vprev. I wrote some code to do what you're doing, but I ended up using PWM followed by a 2 stage RC filter to make my 1kHz sin wave.

Here's the relevant snip:

Vnew[0] = V * exp(-1./t1/Tc); Ve[0] = fabs(a - Vnew[0]); Vnew[1] = V + (Vmax - V) * (1. - exp(-1./t1/Tc)); Ve[1] = fabs(a - Vnew[1]); Vnew[2] = V; Ve[2] = fabs(a - Vnew[2]); /* then code selects min Ve and sets V=Vnew[x] */

Where Ve[x] is the error term for each case: discharging, charging and stay the same (assuming you can turn the output HiZ and the impedence of the following stage is very high, otherwise you might want to model Vnew[2] as a discharge with a higher RC).

Ben Jackson http://www.ben.com/

If you are satisfied that a first-order filter is good enough to be your reconstruction filter, then you can do this. And you'll find it's even simpler than it looks, because for fixed RC and sampling rate, all you have to do is multiply the current voltage difference by a constant and add it to the total.

-- Joe Legris

Given an RC, you can run a bit of code effectively at intervals of 't'...

Vin---------R-------+--------Vc | | C | | gnd

Then the math is just

Vc = Vc + (Vin-Vc) * t / (R*C)

Make sure the time step 't' is less than time constant R*C by some healthy factor. If your bit rate is much higher than R*C, which it probably is, a good choice for 't' is just 1/bitrate, namely the serial clock interval.

So, just run that equation once every serial bit, so that Vin is either 0 or +5 every pass.

That equation can obviously be used to calculate e. Just set R=1, C=1, t = 1/n, jam the input at +1 volt and run it n iterations.

v = 0

for x = 1 to n

v = v + (1-v)/n

next

The output voltage will then approximate 1-1/e. For n = 1000, you run the equation 1000 times and get damned close to 0.63212 volts out.

John

Yes- you can do that, but it would be helpful if you knew why this is a valid operation. At the boundary of each bit time KTs+ where K is the Kth bit, Ts is the bit time, and + indicates an infinitesimal time after KTs you have the simple RC with initial condition VC(KTs+) voltage on C which is also Vout(KTs). The equivalent circuit then becomes: View in a fixed-width font such as Courier.

. . . . . . R . Vin>----/\\/\\-----+--> Vout . | . ===C . | . + | . --- Vc(KTs+) . - . | . | . | . --- . . . . where the ideal voltage source accounts for the initial . . condition on C. . . . Vc(t-KTs) = (Vin-Vc(KTs+))(1-exp(-(t-KTs)/RC)) . . . . Vout(t-KTs)= Vc(t-KTs) + Vc(KTs+) = Vc(t-KTs) + Vout(KTs+) . . . . . Vout(t-KTs)= (Vin-Vout(KTs+))(1-exp(-(t-KTs)/RC)) . . . + Vout(KTs+) . . . . or defining t'= t-KTs as time into new bit interval . . . . . Vout(t')= (Vin-Vout(0))(1-exp(- t'/RC)) + Vout(0) . . . Discretizing Vout and Vin at time KTs this becomes: . . . . Vout(K+1)= (Vin(K)-Vout(K))*(1-exp(-Ts/RC)) + Vout(K) . . . Since 1-exp(-Ts/RC) is a constant, say F, this reduces . . . to Vout(K+1)= (Vin(K)-Vout(K))* F + Vout(K) . . . an exact expression for the Vout sequence. .

The problem i have found all too often is that what you are calling 1 bit PWM is not. The usual bitstream is the equivalent to the output of a CVSD digitizer, which is nothing of the sort. Start by reading up on mu-law and A-law compressed audio.

JosephKK Gegen dummheit kampfen die Gotter Selbst, vergebens. --Schiller

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required