DSPRelated.com
Forums

Measuring Lag of a Black-Box Filter

Started by axlq July 20, 2004
I'm curious how one goes about measuring the lag in the output of
an arbitrary lowpass filter.  That is, I want to measure the lag
in terms of sample count or a function of cutoff wavelength.  For
example, in the case of a simple 1-pole IIR filter having a 3 dB
cutoff wavelength of pi*n :

 a0 = 2/(n+1)
 b1 = 1 - a0
 y[0] = a0*x[0] + b1*y[1]

This will have a lag of (n-1)/2, a linear relationship with the
cutoff wavelength.

Now, if all I knew about the filter was that it's causal and
non-adaptive, but I didn't know the underlying algorithm for the
filter, and all I could control are the inputs (x and n), how
would I determine the lag of the filter, from its output y?

One way I came up with is to use a ramp function as the input data;
that is, the input series x[] has the values 0, 1, 2, 3, 4, 5...
etc. increasing indefinitely.  Then the lag would simply be the
difference between the current input and the current output.  This
actually works for the 1-pole IIR filter above, giving a result of
(n-1)/2 for the lag.

Is this method valid for more complicated filters, such as a
2-pole Butterworth?  In doing this experiment I find that the lag
calculated this way is nonlinear -- it's almost a line, actually
looks like a function that slowly oscillates around, and approaches,
a line that's a function of the cutoff wavelength n.

I apologize if this is question is clueless -- DSP isn't my area
of expertise, I've only recently taken an interest in it, and I've
been lurking in this group for a while.  Should anyone think this is
a homework assignment, let it be known that I'm not in school, and
haven't been for 12 years.

I'm trying to develop a 1-dimensional adaptive low-lag tracking
filter, and one of the early steps in this process is to understand
what "lag" actually is, and how to measure it, for non-adaptive
filters.  To me, "lag" is simply the amount the filter output has
to be shifted backward in time to overlay the input data, such that
there are an equal number of data points above and below the filter
output.

Thanks.

-A
axlq wrote:

> I'm curious how one goes about measuring the lag in the output of > an arbitrary lowpass filter. That is, I want to measure the lag > in terms of sample count or a function of cutoff wavelength. For > example, in the case of a simple 1-pole IIR filter having a 3 dB > cutoff wavelength of pi*n : > > a0 = 2/(n+1) > b1 = 1 - a0 > y[0] = a0*x[0] + b1*y[1] > > This will have a lag of (n-1)/2, a linear relationship with the > cutoff wavelength.
I suppose this will be true for some definition "lag". In general, the time delay of a narrow-band signal passing through an IIR filter depends on its center frequency.
> Now, if all I knew about the filter was that it's causal and > non-adaptive, but I didn't know the underlying algorithm for the > filter, and all I could control are the inputs (x and n), how > would I determine the lag of the filter, from its output y?
You can measure the filter's amplitude and phase responses with frequency or its step or impulse response in time. Either will be enough to characterize it. Then you will be able to compute its lag -- however you define it.
> One way I came up with is to use a ramp function as the input data; > that is, the input series x[] has the values 0, 1, 2, 3, 4, 5... > etc. increasing indefinitely. Then the lag would simply be the > difference between the current input and the current output. This > actually works for the 1-pole IIR filter above, giving a result of > (n-1)/2 for the lag.
Different signals will give different results. ... Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
axlq wrote:

> I'm curious how one goes about measuring the lag in the output of > an arbitrary lowpass filter. That is, I want to measure the lag > in terms of sample count or a function of cutoff wavelength. For > example, in the case of a simple 1-pole IIR filter having a 3 dB > cutoff wavelength of pi*n : > > a0 = 2/(n+1) > b1 = 1 - a0 > y[0] = a0*x[0] + b1*y[1] > > This will have a lag of (n-1)/2, a linear relationship with the > cutoff wavelength. > > Now, if all I knew about the filter was that it's causal and > non-adaptive, but I didn't know the underlying algorithm for the > filter, and all I could control are the inputs (x and n), how > would I determine the lag of the filter, from its output y? > > One way I came up with is to use a ramp function as the input data; > that is, the input series x[] has the values 0, 1, 2, 3, 4, 5... > etc. increasing indefinitely. Then the lag would simply be the > difference between the current input and the current output. This > actually works for the 1-pole IIR filter above, giving a result of > (n-1)/2 for the lag. > > Is this method valid for more complicated filters, such as a > 2-pole Butterworth? In doing this experiment I find that the lag > calculated this way is nonlinear -- it's almost a line, actually > looks like a function that slowly oscillates around, and approaches, > a line that's a function of the cutoff wavelength n. > > I apologize if this is question is clueless -- DSP isn't my area > of expertise, I've only recently taken an interest in it, and I've > been lurking in this group for a while. Should anyone think this is > a homework assignment, let it be known that I'm not in school, and > haven't been for 12 years. > > I'm trying to develop a 1-dimensional adaptive low-lag tracking > filter, and one of the early steps in this process is to understand > what "lag" actually is, and how to measure it, for non-adaptive > filters. To me, "lag" is simply the amount the filter output has > to be shifted backward in time to overlay the input data, such that > there are an equal number of data points above and below the filter > output. > > Thanks. > > -A
Unfortunately you're not guaranteed that the lag (sometimes called "group delay") of a filter will be constant with frequency. If you want a filter with constant lag you need one with an impulse response that's symmetrical around some point -- this is why FIR filters are popular in communications design, because you don't care much about the overall group delay but you _really_ want it to be constant over your band of interest. On the other hand, an IIR filter will usually give you a smaller phase shift at critical frequencies for the amount of attenuation that you get, so they're very good to use in control systems where lowering the delay is preferable to having it all match up. Furthermore for specific problems like tracking a ramp you can design a system that will have zero error as long as the input is of the correct type, but which may have a greater error for other inputs. In the ramp tracking case a given system that's controlled by a type-II controller (i.e. two integrators in the forward path) will have zero DC error tracking a ramp, but when presented with a step input it will have greater overshoot than a type-I controller. Your definition of lag is good, but your real problem is going to be defining the conditions under which you measure it, and recognizing that for an IIR system (or other non-symmetrical impulse-response systems) changing your input signal will change your measured lag. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
In article <10fqm4u9e007ja3@corp.supernews.com>,
Tim Wescott  <tim@wescottnospamdesign.com> wrote:
>axlq wrote: >> I'm trying to develop a 1-dimensional adaptive low-lag tracking >> filter, and one of the early steps in this process is to understand >> what "lag" actually is, and how to measure it, for non-adaptive >> filters. To me, "lag" is simply the amount the filter output has >> to be shifted backward in time to overlay the input data, such that >> there are an equal number of data points above and below the filter >> output. > >Unfortunately you're not guaranteed that the lag (sometimes called >"group delay") of a filter will be constant with frequency. If >you want a filter with constant lag you need one with an impulse >response that's symmetrical around some point -- this is why FIR >filters are popular in communications design, because you don't >care much about the overall group delay but you _really_ want it to >be constant over your band of interest.
I see. So if I have some arbitrary waveform as an input signal (say, the sound made by an orchestra playing Beethoven's 5th), then there's really no way I can calculate "lag" for an IIR filter such that I can shift the output of the filter back in time so that it overlays the input signal more or less symmetrically, as I describe above? In other words, an FIR filter would be better suited? My purpose in needing the lag is because I eventually want to use the filter's output to de-trend the input signal, for the purpose of characterizing the noise in the signal. If I know how much to shift the filter output in time, then simply subtracting the shifted output from the input gives me de-trended input. Then I can measure statistics of the input signal's "noise" (what's left over after detrending). Now, I'm slapping my forehead. Until I just wrote the paragraph above, I didn't realize that I'm describing a HIGH pass filter. Sometimes it helps just to describe a problem to someone.... :-)
>Furthermore for specific problems like tracking a ramp you can >design a system that will have zero error as long as the input is >of the correct type, but which may have a greater error for other >inputs.
Yes, that's the trouble. If my input signal consists of a series of connected ramps going up and down at different random intervals and speeds, a zero-error non-adaptive filter will suffer from overshoot. That is, in fact, the signal I'm contending with, with noise added. So I'm fiddling around trying to see if I can come up with an adaptive tracking filter that will smooth out the noise and yet maintain the high-frequency content of the signal only when it changes direction, to reproduce a fairly clean series of ramps.
>Your definition of lag is good, but your real problem is going to be >defining the conditions under which you measure it, and recognizing that >for an IIR system (or other non-symmetrical impulse-response systems) >changing your input signal will change your measured lag.
Yeah... I guess an FIR filter is the way to go then. In my naivete as a neophyte DSP learner, I invented an interesting FIR filter, cascading three simple moving averages (SMAs) so that the nulls in the frequency response of each moving average cancel out the sidelobes in the other two. It doesn't work well at short cutoff wavelengths due to the need to round the filter length "n" to an integer, but I guess I can fix that by increasing the number of samples per data point. I was hoping to have something faster though. Thanks for the reply. I had ignored FIR filters until now, due to my familiarity with them being limited to simple moving averages, which suffer from requiring an integer filter length, as well as slower calculation time. I'll look into them now. -Alex
In article <40fd57a9$0$5635$61fed72c@news.rcn.com>,
Jerry Avins  <jya@ieee.org> wrote:
>axlq wrote: >> a0 = 2/(n+1) >> b1 = 1 - a0 >> y[0] = a0*x[0] + b1*y[1] >> >> This will have a lag of (n-1)/2, a linear relationship with the >> cutoff wavelength. > >I suppose this will be true for some definition "lag". In general, the >time delay of a narrow-band signal passing through an IIR filter depends >on its center frequency.
Yes, thanks, I realize that now. Looks like I might have to go with an FIR filter for my needs. -Alex
axlq wrote:

-- snip --
> > Yes, that's the trouble. If my input signal consists of a series > of connected ramps going up and down at different random intervals > and speeds, a zero-error non-adaptive filter will suffer from > overshoot. That is, in fact, the signal I'm contending with, with > noise added. So I'm fiddling around trying to see if I can come up > with an adaptive tracking filter that will smooth out the noise and > yet maintain the high-frequency content of the signal only when it > changes direction, to reproduce a fairly clean series of ramps. >
-- snip --
> -Alex
If this really is your purpose you may be better off detecting the existence and parameters of each ramp, and rebuilding the signal from that. It'll be hard to wrap an "adaptive filter" definition around, but you'll be making maximal use of your problem structure to get a good solution. Of course you're going to have trouble if the length of a ramp is fully random because you won't be able to tell the difference between a short ramp and noise, or if you could you would have a hard time localizing it. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
"axlq" <axlq@spamcop.net> wrote in message
news:cdjhr8$9vd$1@blue.rahul.net...
> > I'm curious how one goes about measuring the lag in the output of > an arbitrary lowpass filter. That is, I want to measure the lag > in terms of sample count or a function of cutoff wavelength. For > example, in the case of a simple 1-pole IIR filter having a 3 dB > cutoff wavelength of pi*n : > > a0 = 2/(n+1) > b1 = 1 - a0 > y[0] = a0*x[0] + b1*y[1] > > This will have a lag of (n-1)/2, a linear relationship with the > cutoff wavelength. > > Now, if all I knew about the filter was that it's causal and > non-adaptive, but I didn't know the underlying algorithm for the > filter, and all I could control are the inputs (x and n), how > would I determine the lag of the filter, from its output y? > > One way I came up with is to use a ramp function as the input data; > that is, the input series x[] has the values 0, 1, 2, 3, 4, 5... > etc. increasing indefinitely. Then the lag would simply be the > difference between the current input and the current output. This > actually works for the 1-pole IIR filter above, giving a result of > (n-1)/2 for the lag. > > Is this method valid for more complicated filters, such as a > 2-pole Butterworth? In doing this experiment I find that the lag > calculated this way is nonlinear -- it's almost a line, actually > looks like a function that slowly oscillates around, and approaches, > a line that's a function of the cutoff wavelength n. > > I apologize if this is question is clueless -- DSP isn't my area > of expertise, I've only recently taken an interest in it, and I've > been lurking in this group for a while. Should anyone think this is > a homework assignment, let it be known that I'm not in school, and > haven't been for 12 years. > > I'm trying to develop a 1-dimensional adaptive low-lag tracking > filter, and one of the early steps in this process is to understand > what "lag" actually is, and how to measure it, for non-adaptive > filters. To me, "lag" is simply the amount the filter output has > to be shifted backward in time to overlay the input data, such that > there are an equal number of data points above and below the filter > output.
Stock price analysis? You can construct an adaptive filter that adapts so that sinusoidal components are "predicted". Whether it serves your purpose is another matter. A predictor can be done as follows: Single input that is to be predicted. Pass the input through a delay and use the delayed version as the input to an adapted filter in a noise canceller context. Add/subtract the output of the filter from the direct input signal. The sum/difference is the signal that drives the filter adaptation. The output of the filter then must be a replica of a predicted version of the signal. Now pass the input signal into an identical filter (without the delay at the input) and the output of the filter will be a predicted result. Great in concept. But how good in practice? But I digress.... Two ways to determine lag: Input a step and call the lag the time it requires to reach 67% of final output. Input a tone step at any frequency you choose and call the lag the time it requires to reach 67% of the final output envelope. Also called the "rise time". You can modify the measure to be 80%, 90%, 95%, whatever. Do note that all measures are at a single frequency more or less. Fred
In article <edmdndE9JpxJQmDdRVn-pg@centurytel.net>,
Fred Marshall <fmarshallx@remove_the_x.acm.org> wrote:
>> I'm curious how one goes about measuring the lag in the output of >> an arbitrary lowpass filter. That is, I want to measure the lag > >Stock price analysis?
No, but it undoubtedly has financial applications. I was inspired by this very low-lag smoothing filter I saw that manages to smooth data with low lag and at the same time exhibit no overshoot, called "JMA" which you can see at http://www.jurikres.com/catalog/ms_ama.htm and also some benchmark reports at http://www.jurikres.com/down/why_jma.pdf This, as far as I can tell, doesn't work as a frequency filter, but by analyzing the noise distribution and adjusting the "speed" of the filter.
>You can construct an adaptive filter that adapts so that sinusoidal >components are "predicted". Whether it serves your purpose is another >matter.
I'm not really interested in prediction. Especially for financial data, I don't believe any extraction of sinewave components is valid. You can do that on a pure random walk, and get an output that "looks" meaningful, but it's not really meaningful.
>But I digress.... >Two ways to determine lag: >Input a step and call the lag the time it requires to reach 67% of final >output. >Input a tone step at any frequency you choose and call the lag the time it >requires to reach 67% of the final output envelope. >Also called the "rise time". >You can modify the measure to be 80%, 90%, 95%, whatever. >Do note that all measures are at a single frequency more or less.
Thanks. I think I'll have to go with an FIR filter though, rather than IIR. -Alex
"axlq" <axlq@spamcop.net> wrote in message
news:cdkoa0$me4$1@blue.rahul.net...
> In article <edmdndE9JpxJQmDdRVn-pg@centurytel.net>, > Fred Marshall <fmarshallx@remove_the_x.acm.org> wrote: > >> I'm curious how one goes about measuring the lag in the output of > >> an arbitrary lowpass filter. That is, I want to measure the lag > > > >Stock price analysis? > > No, but it undoubtedly has financial applications. I was inspired > by this very low-lag smoothing filter I saw that manages to smooth > data with low lag and at the same time exhibit no overshoot, called > "JMA" which you can see at http://www.jurikres.com/catalog/ms_ama.htm > and also some benchmark reports at > http://www.jurikres.com/down/why_jma.pdf > > This, as far as I can tell, doesn't work as a frequency filter, but > by analyzing the noise distribution and adjusting the "speed" of the > filter. > > >You can construct an adaptive filter that adapts so that sinusoidal > >components are "predicted". Whether it serves your purpose is another > >matter. > > I'm not really interested in prediction. Especially for financial > data, I don't believe any extraction of sinewave components is > valid. You can do that on a pure random walk, and get an output > that "looks" meaningful, but it's not really meaningful. > > >But I digress.... > >Two ways to determine lag: > >Input a step and call the lag the time it requires to reach 67% of final > >output. > >Input a tone step at any frequency you choose and call the lag the time
it
> >requires to reach 67% of the final output envelope. > >Also called the "rise time". > >You can modify the measure to be 80%, 90%, 95%, whatever. > >Do note that all measures are at a single frequency more or less. > > Thanks. I think I'll have to go with an FIR filter though, rather than > IIR.
FIR / IIR same thing as far as these step response measures go. If you use a symmetrical FIR filter then the frequency dependence of the delay goes away in the steady state - it is constant delay. That is, if you apply a stable signal to the filter, then the delay or lag will be the same for all frequencies. This means that a waveform will have no delay change as a function of frequency - just amplitude changes according to the filter response at each frequency. (Sinusoid phase will vary linearly with frequency to match the constant delay). For a general IIR filter, constant delay isn't the case - although some can approximate it. If you intend to use these filters in transient cases then I don't know how meaningful the steady state measures would be. Thus, the step response measures may be more meaningful to you. So, note that the shorter the FIR filter, the shorter the delay. For a symmetrical FIR filter, the steady state delay is 1/2 the length of the filter - always. For a given frequency response, an IIR filter will be "shorter" (have fewer coefficients) but that doesn't equate to short lag in the same way. The JMA results are very interesting but I don't know how to comprehend what they're doing. If you had such a filter, you could measure the single unit sample response and get the transient response. The plot of "A Perfect Noise Reduction Filter" is a cartoon of what you'd love to have but can't get. It simply sets the stage - it's not a real filter by any means. Just in case that wasn't clear. The next plot of "JMA Jurik 1998" looks much like a filter that has these characteristics: 1) The filter definitely has a lowpass characteristic because all of the "noise" is removed. One must assume that they didn't simply place a transmission zero at the fundamental frequency of the periodic noise in this plot. 2) The filter otherwise has been made to have minimum rise time subject to some overshoot criterion. Filters of this type are well known. There have been numerous studies of signals that are a) bandlimited (in frequency) and b) have minimum rise time (in time). Signal or filter impulse response... pretty much the same thing. See "The prolate filter: An ideal lowpass filter with optimum step response" J. Franklin Inst. Feb 1972. So, if you take an ideal unit sample response function and approximate it with the unit sample response of a real filter then you will be doing the best you can do. In general, the wider the frequency response, the shorter the rise time. So, selecting the lowpass cutoff frequency is a critical matter for what you're trying to do. Higher lowpass cutoff, faster rise and more noise. Lower lowpass cutoff, slower rise and less noise. The wider the transition going from pass to stop band, the faster the rise time. So, this becomes a secondary adjustment that's possible. Shaping the transition has an affect on the overshoot in time. Fred