# 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. – RTbecard 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. – Hilmar 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. – RTbecard Mar 27 '20 at 11:09

• 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. – RTbecard Apr 16 '20 at 11:54