# Analytic solution for non-flat filter design

I'm trying to write an accelerometer calibration script that uses filters to convert from volts into $$m/s^2$$. As accelerometers tend to have non-flat response curves, this means I have to design a rather complex filter. I'm not worried about phase, as I can just apply the filter twice in opposing directions to correct for any phase offsets (like matlab's filtfilt), so the focus is on designing a filter that approximates a user-provided magnitude curve.

Ideally, the user provides a calibration curve as input into an analytic algorithm to solve for the best fitting filter poles.

I'm aware MATLAB has a filter design function, but I don't know what the underlying algorithm is (if its an optimizer, or a closed form solution).

So my question is...

• Is there an analytic solution to my filter design problem? Or do I have to use optimisation scripts to get the best filter?

I'm not mentioning programming language here, as I want to understand the underlying math behind this.

• Okay... I think I answered my own question. What I was searching for is essentially linear-phase FIR filter design by least squares. This document seems to cover the theory behind it. Mar 26 '20 at 14:17
• Another easy simple method to do this is to populated a large FFT grid with your target amplitude, do an inverse FFT and then time window or gate the impulse response to the desired accuracy. Mar 26 '20 at 15:56
• @Hilmar, that sounds great! I'm guessing the benefit of the least squares approach is that I can force the design of a linear phase filter... but if I use the filtfilt approach to applying my filter, this would not be a concern for me, and it would result in a more accurately matched spectrum magnitude. If you post this as an answer (and i verify it works as expected), I'll accept it. Mar 27 '20 at 11:09

## 1 Answer

If you are not concerned on the phase and just want to approximate a magnitude response, then your first option should be the frequency sampling method implemented in Matlab/Octave fir2() function.

You would provide the frequency grid and corresponding frequency response magnitude at those frequencies.

As you have also mentioned, least-squares approach is another alternative. Indeed by using suitable weights, you can distribute the error according to your priority cirteria.

Magnitude approximation based on LMS adaptive system identification is also a possible option.

• Am i right in understanding frequency sampling is essentially what @Hilmar was suggesting in their comment? Or is there something a bit more intricate to it? Mar 27 '20 at 16:38
• Speaking of Matlab fir2() ;They should be essentially the same (possibly exact same things); both are based on inverse FFT of the specified magnitude response. You may check its documentation to see if there's anything intricate other than indicated in his comment. Mar 27 '20 at 17:16
• Reading into this more... The difference between this answers suggestion and Hilmar's comment above (just do an ifft of the desired response), is that the method proposed here (Matlab's fir2) applies a window function to reduce rippling in the frequency response after the ifft. Otherwise, the methods are the same. This answer goes into detail on how the windowing affects the filter. Apr 16 '20 at 11:54