# Intro

My question is related to this one: Correcting phase response of a signal.

I need to design an FIR low-pass filter with real coefficients for data received from a sensor. An additional requirement is to try and equalize to phase distortion introduced by the sensor (we have measurements of it).

While I could in theory design an allpass in addition to the low pass, I prefer not to as I care about the overall delay more than the distortion.

However, I thought maybe I can design a single filter doing both low-pass and phase equalization. I will consider the solution suggested by Matt here, but first I thought of a simple solution which already builds on FIR which I designed and I'm happy with. Can't I simply:

1. Take the frequency response of the filter I have
2. Multiply the positive frequencies by the by the desired phase response, and negative ones by the conjugate.
3. Take the inverse transform (invfreqz) to find filter taps

# Questions

1. How does this approach compare to LS or complex PM design algorithms?
2. My intuition is that this approach (given the the LPF was designed using firls), will maintain its performance as a low pass, and will somewhat sacrifice the phase performance as it hasn't been optimized in any sense. Am I correct?
3. It sounds as if I'm getting here "something for free" and we all know there are no free lunches when it comes to engineering. What am I sacrificing compared to the original LPF?
4. I've stumbled upon some notions that such arbitrary phase FIRs aren't necessarily causal. Obviously the group delay of my filter will be altered (this is my goal by adding phase, not a consequence). How will the resulting filter be non-causal? I assume it it happens if the phase is large enough compared to the linear phase of the original filter? If so, assuming added phase is smaller than the $$N/2$$ delay of the FIR, I don't see an issue. Am I missing something?
5. What will happen to the temporal characteristics of the filter, such as ringing effects, overshoot and undershoots?

The only draw back I found so far is the since the resulting filter will not be symmetric (by design), I cannot save hardware taps by exploiting symmetry.

# Update

So I tied both lslevin, trimming the IFFT, and invfreqz (both with weights provided and without them, as the documentation states the algorithm used is different). Attached are frequency response and group delay plots of the results, both for a $$N=17$$ order filter and for a $$N=35$$ order filter.

Surprisingly, there are fundamental differences which I do not understand intuitively.

1. For some reason the magnitude response of the of the non-linear phase filter has better noise rejection than the linear phase, but only for the short filter. Obviously the standard LS design is optimal only as long as the problem is constrained to linear phase filter, so the better noise rejection for the non-linear phase filter is understandable, but why does it occur only for the shorter filter?
2. For the $$N=17$$ filter GD approximation is quite bad, but it is better with the unweighted invfreqz and IFFT. For the longer filter the weighted variant approaches the GD approximation performance of the other two within passband. Is there an intuitive/fundamental reason for this, or is it a coincidence?

$$N=17$$ plots  $$N=35$$ plots  • By "real FIR", I presume you mean real-valued coefficients. Does this real FIR also have to be realizable in real-time? If so, that means it's causal and that means it cannot have any impulse response occurring before the impulse input. That places constraints on the arbitrary phase response. Jul 9 at 17:33
• Indeed I meant real taps, and yes it must be also causal, but we can add an additional linear phase (delay). Jul 12 at 13:28

I agree with everything Hilmar wrote and want to add one more approach that is quite applicable to the OP’s objective:

Instead of using least squares (or any other approach) to meet a frequency response objective, use least squares with input (stimulus) and output (response) for the channel to be equalized. As long as the input is a proper sounding signal- meaning it has energy spread evenly over the bandwidth of interest (such as a frequency chirp or pseudo-random sequence) and the duration significantly exceeds the delay spread of the channel then the post-processing is quite straightforward (just a few lines of code in Matlab/Octave or Python. I detail the approach here including the code to use:

https://dsp.stackexchange.com/a/31326/21048

This readily provides the FIR filter coefficients which will equalize both the phase and amplitude distortion (linear distortions) of the channel (or the device the signal passes through). If the target frequency response for equalization was also determined from such measurements (such as a network analyzer or other channel probing technique), this technique would be more direct in operating on the original data to determine the equalizer needed.

• The suggestion to use an LMS equalizer is indeed interesting. I have two questions though. I'm not sure what is my "transmitted signal" in case as you suggested I have a frequency and phase response measurements taken on a network analyzer. Jul 12 at 5:37
• My second question has to do with the general applicability of the LMS to my use case. I already have a low pass filter designed since I intend to oversample and then decimate. Thus, I need a single filter doing both noise rejection (LPF) and equalization (as I'm constrained to a specific overall filter length). Thus, I wanted to modify the exiting filter. Can I use the LMS result to modify the existing filter? Jul 12 at 5:42
• @YairM In that case your "transmitted signal" (Tx) would be a frequency chirp-specifically the complex baseband equivalent of the frequency chirp as it passes through your bandpass equivalent signal. Yes if you have the Tx and Rx of the channel alone from the analyzer then you can determine the equalizer, filter that with a linear phase filter to emphasize the passband of interest, and then with the minimum representation of that required impulse response at your LPF sampling rate, you can convolve those coefficients to combine the result. Jul 12 at 7:02
• Although I suspect your network analyzer measurement is of very high SNR such that I doubt what I suggest would be any better than just taking the IFFT of the resulting complex frequency response, and then decimating that - I'll add some color to this in my answer above; but first can you confirm that you are sweeping an RF passband, for a signal that will be down-converted to complex baseband (where your low pass filter is)? Or do you really only have a real baseband? Jul 12 at 7:07
• I do not have a low pass pass equivalent which is modulated around some carrier. My signal has a BW of several hundreds of MHz and starts from DC. Thus I really have a real baseband signal. Jul 12 at 13:43

A few things to consider here

invfreqz will give you an IIR filter, not and FIR filter. The underlying algorithm is numerically challenging so the results can be all over the place. For example in audio processing you will often poles that are extremely close to $$z=1$$ and invfreqz can't handle this.

If you want an FIR you can simply specify your response (magnitude and phase) on conjugate symmetric FFT frequency grid and do an inverse FFT. Results will vary with the filter specification. You may see a lot of non-causality and pre- and post-ringing. It's typically best to start with a really long FFT and than see how much can be trimmed down to an acceptable length with acceptable accuracy.

The main downside of the FFT is that you have to specify the target at ALL frequencies, even for those you don't care about. For example, you need to specify the exact shape of the transition band, even if you don't care how exactly looks like.

A Least Squared Error approach will give you the best results within any given set of constraints simply because you get to define exactly what "best" means. By adjusting the frequency grid and weighting certain frequency ranges differently, you can optimize the filter performance for your specific application.

In the end it comes down to what works for your specific problem. You can start with the inverse FFT approach and if that's "good enough", you are done. A custom LS approach will give you better results, but it's also a lot more work.

EDIT: a note on FIR allpass filters

A true allpass filter, i.e. $$|H(z) = 1|$$ can only have zero/pole pairs that are inverse of each other. Since an FIR filter has all its poles at $$z = 0$$, the zeros of an FIR allpass must all be at $$z=\infty$$. It directly follows that any FIR allpass can only be a delay, i.e. $$H(z) = z^{-N}$$

• But invfreqz can return an FIR, you specify the order, nor problem to specify the order of the denominator as 0. Is there a reason not to do this? Given I use this, all stability issue become irrelevant. Jul 10 at 7:16
• Regarding your statement about optimality, what if the specified magnitude of the frequency response is already optimal in the LS sense? I designed a LPF of the required order using LS design, and now I want to alter the phase response by adding a frequency dependent phase. The reason I consider this approach to begin with, is that it will maintain the optimal magnitude of the response, will it not? Jul 10 at 7:20
• @YairM In general, the magnitude and phase responses of a FIR filter are interrelated and cannot be changed independently. You will get the best results if you design your filter in one shot using your actual desired response without the use of a "template filter." For a non-linear phase LS optimal filter, I would indeed suggest using lslevin as you've already found. (It's possible invfreqz would also work, I just don't have any experience with it.) Jul 11 at 12:53
• @YairM: that doesn't work since the only FIR allpass filters that exist are pure delays. If you need a phase correction that's different from a delay, you are out of luck with FIR. See my edited answer. You are indeed better off designing phase and magnitude in one go. Jul 12 at 15:17
• @Hilmar but I don’t want an all pass. I want a low pass to reject out-of-band noise (prior to decimation), and I want it with a non-linear phase, to have phase correction. I already have a linear phase LPF that I’m happy with, and I want phase equalization without additional taps, which is why I ruled out an extra low pass. Jul 12 at 22:46