Author Topic: DSP: Unexpected LowPass/HighPass Filter response in Matlab  (Read 1798 times)

0 Members and 1 Guest are viewing this topic.

Offline BurnedResistorTopic starter

  • Regular Contributor
  • *
  • Posts: 192
  • Country: at
DSP: Unexpected LowPass/HighPass Filter response in Matlab
« on: March 06, 2017, 08:18:07 pm »
Hello all,

I was researching discrete digital filters, and though I would start with the simple low and high pass filter:

https://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization
https://en.wikipedia.org/wiki/High-pass_filter#Discrete-time_realization

LOW PASS:



where the coeffecient a is chosen given the wanted cutoff frequency (f_c) and sampling period (d_T)



HIGH PASS:



where the coeffecient a is chosen given the wanted cutoff frequency (f_c) and sampling period (d_T):




Note that the coeffecient for a given frequency and sampling period of the highpass filter is one minus the coeffecient for the lowpass filter:



I wanted to test the frequency response of the filter, to see if I would get the characteristic response curve of such a filter. So I wrote the following Matlab Script, which evaluates both the filters for a cutoff frequency of 100Hz over a frequency span of 1 to 20000Hz:

While the frequency response of the LowPass and HighPass Filter are the general shape of what I would expect, something seems to be off:
Because I defined both with the same cutoff frequency, I would expect that if I added the responses back together, I would get 0 attenuation across all frequencies. This is However not the case (See attached graph)



I am confused as if this is an error in my code, or an error with my understanding of the filters. Could it have something to do with the initial value choosen in my filter implementation? Could somebody help me out?

Thank you!
Philipp Schilk



« Last Edit: March 07, 2017, 01:56:09 pm by BurnedResistor »
 

Offline b_force

  • Super Contributor
  • ***
  • Posts: 1381
  • Country: 00
    • One World Concepts
Re: DSP: Unexpected LowPass/HighPass Filter response in Matlab
« Reply #1 on: March 07, 2017, 07:19:59 am »
Maybe you should go back to your textbooks, because this summing is exactly according the theory.
Change the Q-factor to 0.5 and you will see that the response will be flat.

Offline snarkysparky

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
Re: DSP: Unexpected LowPass/HighPass Filter response in Matlab
« Reply #2 on: March 07, 2017, 02:47:56 pm »
try evaluating more points in the frequency responses.
 

Offline mark03

  • Frequent Contributor
  • **
  • Posts: 708
  • Country: us
Re: DSP: Unexpected LowPass/HighPass Filter response in Matlab
« Reply #3 on: March 07, 2017, 06:06:34 pm »
I think you have some typos in your equations, but the wikipedia page looks ok and your code probably is too, based on the plots.

Interesting.  I've never seen digital HP/LP filters derived in this way before.  Normally you would use something called the bilinear transform when mapping a continuous-time filter (Laplace) to a discrete-time difference equation (z).  Maybe that wikipedia derivation is equivalent--I didn't look too closely.

In any case, you probably know that practical digital-filter design rarely (if ever) proceeds from an analog-circuit model--even for simple 1- or 2-pole filters.  We live in the z-transform domain from the very start, speaking of poles and zeros and their positions relative to the unit circle (rather than the left-half-plane).  Since you have Matlab, you can play around with butter, freqz, zplane, impz to get a feel for simple IIR filters like the ones you show.  Refer to the help page for filter to understand how Matlab defines the numerator and denominator coefficients.

FIR filters are a whole 'nother area, in some ways simpler to understand.
 

Offline Benta

  • Super Contributor
  • ***
  • Posts: 5840
  • Country: de
Re: DSP: Unexpected LowPass/HighPass Filter response in Matlab
« Reply #4 on: March 07, 2017, 08:27:30 pm »
Even though you are doing this numerically, you are still using classic transfer functions, such a Bessel, Butterworth etc.

This means you'll have to take phase into account as well. Just adding the signal magnitudes of the two filters will not give the correct result.
The -3 dB point is where amplitude has dropped to 1/sqrt(2) (~0.707) and phase is at 45 degrees. If you multiply the cosine of the phase to your amplitude, the summing will be correct.

As mark03 says, FIR filters are a completely different story.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: DSP: Unexpected LowPass/HighPass Filter response in Matlab
« Reply #5 on: March 08, 2017, 02:58:37 am »
Way out there idea (but works, or so I have been told)

If you apply the same IIR filter both forwards in time and filter a second time backwards in time, you can get zero phase shift. That will then let you simply subtract it from the original signal and calculate what has been filtered out.

The cunning part is to work out how long "infinite" is in the context of your filter - for example, if it can be proven that at time 'n' the maximum difference that sample n-32 makes is less than 1 bit, then you could do the following....

Process overlapping blocks of 320 samples (32+256+32) in the forward direction, giving the last 288  samples (256+32) that have insignificant errors, but do have phase delays from the IIR filter.

Then process those three 288 samples in the reverse direction, ending up with the middle 256 samples that have insignificant errors, twice as much filtering, but zero phase delays.

You can't do that in electronics with Ls and Cs  ;)

Edit: Image attached of applying the IIR output(n) = input(n)*0.3+output(n-1)*0.7 twice - one trace is applying it forward in time twice, the other is applying it forward and then backwards (or if you like, the second time is calculating output(n) = input(n)*0.3+output(n-1)*0.7 ). Note that the latter trace is symmetric and is not skewed
« Last Edit: March 08, 2017, 03:40:38 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf