DSPRelated.com
Forums

DC component

Started by xargon July 21, 2005
Hi everyone,

I am totally new to DSP. Coming from a software background, I have
recently tried to pick up some DSP concepts.

I was going through the literature and it is very frustrating because
most of the literature on this topic do not take the time to explain
basic concepts.

Anyways, I was going through a part about a DC blocker and it said that
the DC blocker removes the DC component of a signal. Now, what I
understood from the diagram is that the DC component is just the
offset, right? So basically this component shifts the signal vertically
and normalizes it so that the first sample  corresponds to 0. Am I
correct?

Also, the difference equation of the DC blocker was given as:

y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no
explanation given as to why).

However, if it is just the offset than the equation should eb simply:

y(n) = x(n) - x(0)

Someone please help me understand these concepts.

Thanks,
P

"xargon" <pankajdaga@excite.com> wrote in message
news:1121979286.089588.321670@g47g2000cwa.googlegroups.com...
> Hi everyone, > > I am totally new to DSP. Coming from a software background, I have > recently tried to pick up some DSP concepts. > > I was going through the literature and it is very frustrating because > most of the literature on this topic do not take the time to explain > basic concepts. > > Anyways, I was going through a part about a DC blocker and it said that > the DC blocker removes the DC component of a signal. Now, what I > understood from the diagram is that the DC component is just the > offset, right? So basically this component shifts the signal vertically > and normalizes it so that the first sample corresponds to 0. Am I > correct? > > Also, the difference equation of the DC blocker was given as: > > y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no > explanation given as to why). > > However, if it is just the offset than the equation should eb simply: > > y(n) = x(n) - x(0) > > Someone please help me understand these concepts. > > Thanks, > P >
If you know what the dc level is then just subtract it but suppose you don't or it varies very slowly (I know this means it isnt dc any more but never mind). Shytot
"xargon" <pankajdaga@excite.com> wrote in message 
news:1121979286.089588.321670@g47g2000cwa.googlegroups.com...
> Hi everyone, > > I am totally new to DSP. Coming from a software background, I have > recently tried to pick up some DSP concepts. > > I was going through the literature and it is very frustrating because > most of the literature on this topic do not take the time to explain > basic concepts. > > Anyways, I was going through a part about a DC blocker and it said that > the DC blocker removes the DC component of a signal. Now, what I > understood from the diagram is that the DC component is just the > offset, right? So basically this component shifts the signal vertically > and normalizes it so that the first sample corresponds to 0. Am I > correct?
****No. The "DC" part is the average of the samples. So, removing it will normalize the samples to have zero mean and not necessarily zero first sample value. ***In order to be practical about it, you want the DC part to the the average of a lotta samples but not an infinite number - or it will take forever to compute the output. In a streaming system you don't know the values yet to come so you have to rely on some number of past values.
> > Also, the difference equation of the DC blocker was given as: > > y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no > explanation given as to why). > > However, if it is just the offset than the equation should eb simply: > > y(n) = x(n) - x(0) >
Well there are a couple of problems with this I believe. The first reason is given above so you wouldn't have x(0) in there. What you need in place of x(0) is the average of x(0),x(1),....,x(n) And, you may be doing streaming processing instead of block processing so that n keeps changing indefinitely. The streaming process could be finite impulse response (FIR) or infinite impulse response (IIR). The formula below is IIR (meaning that all input values have some component in the output). Let's assume that the input signal is 1 + sin(t). Then the desired output would be sin(t). y(n) = x(n) - x( n - 1) + Ry(n - 1) y(1) = x(1) - x(0) y(2) = x(2) - x(1) + R[x(1) - x(0)] = x(2)- x(1)+Rx(1) -Rx(0) y(3) = x(3) - x(2) + R[x(2)- x(1) + Rx(1) - Rx(0)] = x(3) - x(2)+Rx(2) + R^2*x(1) - R^2*x(0) y(4) = x(4) - x(3) + R[x(3) - x(2)+Rx(2) + R^2*x(1) - R^2*x(0)] = x(4) - x(3) + R*x(3) - x(2)+R^2x(2) + R^3*x(1) - R^3*x(0)] Examination of y(.) shows there is a sum and difference of succeeding past values of x. Now, assume that x(0)=x(1)=x(2)..... Then y(4) = x(.) - x(.) + R*x(.) - x(.)+R^2x(.) + R^3*x(.) - R^3*x(.)] = 0 + R*x(.) - R^2*x(.) + 0 If R is nearly equal to 1.0 then R^2 is nearly 1.0 and the middle terms sum to nearly zero - which is what we want: zero result for a constant input. Another way to analyze it is to note that the filter has a zero at z=1 which is DC - so, in the steady state situation, DC is removed. It also has a pole at -R and the gain at most frequencies is 1.0. See dspguru tricks. Fred
On 21 Jul 2005 13:54:46 -0700, "xargon" <pankajdaga@excite.com> wrote:

>Hi everyone, > >I am totally new to DSP. Coming from a software background, I have >recently tried to pick up some DSP concepts. > >I was going through the literature and it is very frustrating because >most of the literature on this topic do not take the time to explain >basic concepts. > >Anyways, I was going through a part about a DC blocker and it said that >the DC blocker removes the DC component of a signal. Now, what I >understood from the diagram is that the DC component is just the >offset, right?
Yes.
>So basically this component shifts the signal vertically >and normalizes it so that the first sample corresponds to 0. Am I >correct?
No, it shifts the signal vertically so that the average of all samples corresponds to zero. Actually, that's just an approximation of what most DC blockers do, but they get close enough for practical purposes. If you know much about electronics, it's analogous to a capacitor in series with the signal, then from the other side of the capacitor, a resistor to ground, with the output taken across the resistor.
> >Also, the difference equation of the DC blocker was given as: > >y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no >explanation given as to why).
Aww, I dunno if I can explain it even though I've written code to do it, but one thing this is called is a "leaky integrator" and the 0.995 is a constant that determines how fast it leaks. Google for "leaky integrator" and see if you can understand some of the links. Write a program that does this calculation, and print out the values at each sample point and see if you can get a feel for what the equation is doing.
>However, if it is just the offset than the equation should eb simply: > >y(n) = x(n) - x(0) > >Someone please help me understand these concepts. > >Thanks, >P
----- http://www.mindspring.com/~benbradley
Take z-transform of difference equation and look at it in the frequency
domain; the filter does block DC and passes all other frequencies.

y(n) = x(n) - x(n-1) + R y(n-1)
y(n) - R y(n-1) = x(n) - x(n-1)

Y(z) - R z^-1 Y(z) = X(z) - z^-1 X(z)
Y(z) [ 1 - R z^-1 ] = X(z) [ 1 - z^-1 ]

H(z)  = Y(z) / X(z)  =  [ 1 - z^-1 ] / [ 1 - R z^-1 ]


Matlab code:

R = 0.995;
b = [ 1  -1 ];  %  numerator coefficients  of H(z)
a = [ 1  -R ];  % denominator coefficients  of H(z)
[H, w] = freqz(b,a,512);  % for info, type "help freqz"
plot(w,abs(H));
zoom on;

note1:  frequency axis goes from  [ 0 to pi ] radians/sample,
(important:  digital frequency omega "w" is in radians/sample, not
radians/sec); with pi corresponding to the highest frequency at Fs/2
(i.e., half the sampling rate).

F  = analog frequency (cycles/sec)
f  = digital frequency (cycles/sample)
Fs = sampling frequency (samples/sec)
w  = digital frequency (radians/sample)

w = 2pi f = 2pi (F/Fs) ==>  F = (w Fs)/(2pi)

As a check on "note1", if w = pi, therefore F = (pi Fs)/ (2pi) = Fs/2 =
Fmax.  No doubt this was a tangent from your original question, but it
is important and may be confusing to someone just getting into DSP.

I find looking at the difference equation in words helpful:
y(n) = x(n) - x(n-1) + R y(n-1)

The current output, y(n), is the difference between the current and
past inputs, plus the past output scaled by R.

Remove 99 (but leave 7) to reply
"xargon" <pankajdaga@excite.com> wrote in message 
news:1121979286.089588.321670@g47g2000cwa.googlegroups.com...
> Hi everyone, > > I am totally new to DSP. Coming from a software background, I have > recently tried to pick up some DSP concepts. > > I was going through the literature and it is very frustrating because > most of the literature on this topic do not take the time to explain > basic concepts.
Finding the right references can be difficult. You might try the text "Understanding Digital Signal Processing" by Rick Lyons. It is written to be accessible to the beginner, rather than the graduate student.
> Anyways, I was going through a part about a DC blocker and it said that > the DC blocker removes the DC component of a signal. Now, what I > understood from the diagram is that the DC component is just the > offset, right? So basically this component shifts the signal vertically > and normalizes it so that the first sample corresponds to 0. Am I > correct? > > Also, the difference equation of the DC blocker was given as: > > y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no > explanation given as to why).
This type of DC blocker (technically an IIR filte) completely removes DC but also progressively attenuates low frequency signal near DC. The value of R determines where this low-frequency roll-off starts to take place. You want to choose R such that your lowest frequency signal of interest is not significantly attenuated.
> However, if it is just the offset than the equation should eb simply: > > y(n) = x(n) - x(0)
As someone else already mentioned, you are not trying to make the first sample be zero, but the average of all samples be zero. In other words y(n) = x(n) - avg(x(n)), where avg is the average of all samples from 0..n. The DC blocker above is an approximation of that appropriate for working with continuous (streaming or real-time) data. However, if you are working with a file of known finite length, an alternative way to do a DC blocker is to directly find the average of all samples and subtract it from each sample. (If your file is extremely large, this may be computationally expensive.)
Hi Fred,

Thanks for the reply. So, as I understand it is a DC component is a
signal of 0 frequency. So, basically a shift in the waveform by a
constant.

However, say there is a sinewave that fluctautes between +50 and -50.
And there is a DC component of +25. So, now the sine wave fluctuates
between +75 and -25. What I cannot understand is that I only see a sine
wave. How does the filter know that there is a DC part associated with
it?

Hello again,

I also have a question about the filter itself: The filter is defined
as
y(n) = x(n) - x( n - 1) + Ry(n - 1)

Now, if we feed it a signal like 1 + Sin(t) where the Sine wave is
entered around 0 Hz. We should get the output as Sin(t).

Now, in a streaming data as everyone mentioned we rely on previous
values. So, the initial output of the filter would be wrong. Am I
correct?

The first input 1 + Sin(0) will remain unchanged, for example. The
filter will output the original value instead of removing the DC
offset. As more and more values will be available to the filter, it
will adjust itself better.

xargon wrote:

   ...

> y(n) = x(n) - x( n - 1) + Ry(n - 1) where R was chosen to be 0.995 (no > explanation given as to why).
What that difference equation means in words is Set the present output equal to the present input less the previous input, less R times the previous output. When the signal changes slowly, x(n) - x(n-1) will be very small. However, when the signal is near 1/2 the sampling frequency, it can be nearly +/-2. It is in effect a digital differentiator, compensating the integration. There are better DC blockers -- see the discussion about removing errors at http://www.dspguru.com/comp.dsp/tricks/alg/dc_block.htm -- and also about how the parts fit together. The choice of .995 is a bit arbitrary. The closer to unity, the lower the highest affected, but the more danger that numerical issues will create instability by making the effective value greater than unity. With a great enough word length, .999 works. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
"xargon" <pankajdaga@excite.com> wrote in message 
news:1122023955.943684.70780@o13g2000cwo.googlegroups.com...
> Hello again, > > I also have a question about the filter itself: The filter is defined > as > y(n) = x(n) - x( n - 1) + Ry(n - 1) > > Now, if we feed it a signal like 1 + Sin(t) where the Sine wave is > entered around 0 Hz. We should get the output as Sin(t). > > Now, in a streaming data as everyone mentioned we rely on previous > values. So, the initial output of the filter would be wrong. Am I > correct?
Yes. The filter doesn't have any history to work with.
> The first input 1 + Sin(0) will remain unchanged, for example. The > filter will output the original value instead of removing the DC > offset. As more and more values will be available to the filter, it > will adjust itself better.
Exactly right. That brings up another point about the choice of R--the smaller it is, the faster the filter will converge to the correct value. But the tradeoff is that it will also attenuate low frequencies ("near DC") more. So you choose it based on your lowest frequency of interest. This tradeoff was made clear to me in a recent audio design. With audio, you normally have a capacitor in series with the audio inputs/outputs to block any DC that may be received/generated by your design. But you need to pass signals down to at least 20 Hz with negligible attenuation, and usually 10 Hz is even better for the pro-quality equipment. So we stuck in the largest capacitor we had on the outputs. It worked great, passing signals down even to a few Hz, but we found it took some 5-10 seconds after power-up for initial transients to sufficiently settle out, which caused an audible thump for us. So the capacitor was reduced (equivalent to a smaller R) to solve this.