DSP: Unexpected LowPass/HighPass Filter response in Matlab - Page 1
Forum section: electronics / general

author: BurnedResistor
author_rank: Regular Contributor
timestamp: March 06, 2017, 08:18:07 pm
content: 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_realizationhttps://en.wikipedia.org/wiki/High-pass_filter#Discrete-time_realizationLOW 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

author: b_force
author_rank: Super Contributor
timestamp: March 07, 2017, 07:19:59 am
content: 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.

author: snarkysparky
author_rank: Frequent Contributor
timestamp: March 07, 2017, 02:47:56 pm
content: try evaluating more points in the frequency responses.

author: mark03
author_rank: Frequent Contributor
timestamp: March 07, 2017, 06:06:34 pm
content: 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 withbutter,freqz,zplane,impzto get a feel for simple IIR filters like the ones you show. Refer to the help page forfilterto understand how Matlab defines the numerator and denominator coefficients.FIR filters are a whole 'nother area, in some ways simpler to understand.

author: Benta
author_rank: Super Contributor
timestamp: March 07, 2017, 08:27:30 pm
content: 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.

author: hamster_nz
author_rank: Super Contributor
timestamp: March 08, 2017, 02:58:37 am
content: 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 CsEdit: 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