Equalisation/Crossover - Best way to fit a curve?

No, no, I meant the actual source-code for this function. Never mind, I am able to see the code from the following path:

...\MATLAB\<version>\toolbox\signal\signal\invfreqs.m

I'm attaching the file here with a TXT entension, for anyone interested in seeing it. Open with Wordpad to see the source code.
 

Attachments

  • Thank You
Reactions: Dave Zan
gberchin:

Sir, in my opinion, there are two things to consider:

1) The MATLAB source code needs to be "knocked down" before actually understanding / implementing it. However, with your method, the OP has the inventor himself to guide him through the process of implementing the method. It is also time-tested and mathematically documented and since it has also been published, I assume it was peer reviewed as well.

2) I was able to see Mathworks' copyright somewhere within invfreqs().m, so I guess it will have to be re-written by the OP, before use. However, you've offered to unconditionally share your method (and related documentation) for the OP's benefit, which I believe makes things much much easier (copyright-wise) when compared to the Mathworks route.

Nevertheless, the usage of the continuous frequency s-domain is one solid advantage that the Mathworks' method might have. I think we'll wait and see what the OP decides.
 
1) The MATLAB source code needs to be "knocked down" before actually understanding / implementing it. However, your method is already time-tested and mathematically documented and since it has also been published, I assume it was peer reviewed as well. More importantly the OP has the inventor himself to guide him through the process of implementing the method.

2) I was able to see Mathworks' copyright somewhere within invfreqs().m, so I guess it would need to be re-written by the OP, before use. However, you've offered to unconditionally share your method (and related documentation) for the OP's benefit, which I believe makes things much much easier when compared to the Mathworks' method.

Nevertheless, the usage of the continuous frequency s-domain is one solid advantage that the Mathworks' method might have. I think we'll wait and see what the OP decides.

1) The Matlab help page references two documents that they used to develop the invfreqs() function. I would recommend that one refer to those, rather than reverse-engineering the Matlab function.

2) FDLS has been freely available for decades, but it's not well-known. I help however I can, but it's not something to which I devote a lot of time any more.

As for s-domain vs. z-domain; if the equalization is to be done in DSP, then starting with a z-domain transfer function will probably make the job easier.
 
  • Like
Reactions: mondogenerator
The Matlab help page references two documents that they used to develop the invfreqs() function. I would recommend that one refer to those, rather than reverse-engineering the Matlab function.
I purposely did not use the term "reverse-engineering", as I never meant hackers' disassembly of the function, but only an analysis to help one understand (learn) what is done. Of course, the mathematics behind the function is more easily understood from the citations.

Besides, I also believe the programmers at Mathworks to be more skilled than many of us and therefore consider it to be a good idea to visit the source code (to see if any extra "tricks" have been used), in addition to reading the mathematical explanation in the citations.
 
A question for any truly advanced level mavens.
When we decide to fit a dataset with a line there are well defined algorithms like Least Squares fit in statistics.
All clear cut and reproducible.
When I want to, say, equalise a speaker then I have to best fit a curve with Poles and Zeros.
In parametric equaliser nomenclature that's centre frequencies and Qs, lo-pass and hi-pass, shelves and so on.
Similarly for a cross over, there may be a need to tailor the electrical response so the acoustic response behaves correctly.
Rather than just trial and error "there's a peak there so let's drop a parametric notch on that", has anyone seen any work on how to optimise the process?
The statistics literature has many ways to fit splines and the like but not quite suitable for this AFAIK.
It seems there should be some work on this but perhaps it's too nerdy for DIY?

David
A question for any truly advanced level mavens.
When we decide to fit a dataset with a line there are well defined algorithms like Least Squares fit in statistics.
All clear cut and reproducible.
When I want to, say, equalise a speaker then I have to best fit a curve with Poles and Zeros.
In parametric equaliser nomenclature that's centre frequencies and Qs, lo-pass and hi-pass, shelves and so on.
Similarly for a cross over, there may be a need to tailor the electrical response so the acoustic response behaves correctly.
Rather than just trial and error "there's a peak there so let's drop a parametric notch on that", has anyone seen any work on how to optimise the process?
The statistics literature has many ways to fit splines and the like but not quite suitable for this AFAIK.
It seems there should be some work on this but perhaps it's too nerdy for DIY?

David
Comprehensive review by
Vesa Välimäki and Joshua D. Reiss, All About Audio Equalization: Solutions and Frontiers, Appl. Sci. 2016, 6(5), 129.

Also look for German Ramos papers in AES journal/conference proceedings on the subject.
 
  • Thank You
Reactions: Dave Zan
There is a Laplace version...It doesn't work nearly as well as the z-domain version.
Do you have an explanation why the Laplace version is worse?
Both versions fall completely apart if there is excessive phase delay in the measurements.
That doesn't surprise me, kind of to be expected .
How about ... "invfreqs"...(S-domain)...Then use a "bilinear" (or another like it) to get the Z-domain correction filter.
Thanks, I was unaware of this...
The MATLAB documentation for "invfreqs" has a short explanation...and a couple of citations:

https://in.mathworks.com/help/signal/ref/invfreqs.html
and unaware there was an explanation and references, I am more interested in the explanation than a particular solution, at the moment.
My experience with invfreqs() has been hit-or-miss. Still, it's a viable candidate, worthy of at least a try.
What problems have you seen? Do they illuminate any issues?
Thanks, mostly review but there are some useful details.
One of their references is the same as in the Mathworks "Invfreq", a paper by Levi. that looks to be the classic for the subject.
But it's in a journal from 1959 (!) and not easy to find. After 60 years I had hoped for public domain.

Best wishes
David
 
Do you have an explanation why the Laplace version is worse?

No. Once I found that it did not solve my problem, I moved on.

What problems have you seen? Do they illuminate any issues?

Exactly what one would expect -- sometimes it worked well, sometimes it didn't. It was not my intent to analyze the algorithm, but to solve my problem. So again, once I found that it did not, I moved on.
 
Another candidate: Oppenheim & Schafer mention a technique by Steiglitz in section 5.3.1 of their "Digital Signal Processing" book.

K. Steiglitz, "Computer-Aided Design of Recursive Digital Filters", IEEE Transactions on Audio and Electroacoustics, Vol. AU-18, June 1970.

I've never tried this method.
 
Last edited:
One of their references is the same as in the Mathworks "Invfreq", a paper by Levi. that looks to be the classic for the subject.
But it's in a journal from 1959 (!) and not easy to find. After 60 years I had hoped for public domain.
Take it, here it is. Thank you.
 

Attachments

Thanks, mostly review but there are some useful details.
One of their references is the same as in the Mathworks "Invfreq", a paper by Levi. that looks to be the classic for the subject.
But it's in a journal from 1959 (!) and not easy to find. After 60 years I had hoped for public domain.

I glanced over the entire discussion, as far as I understood, you are generally interested in the problem of a complex function approximation (nonlinear real approximation problem). Along with the mentioned by gberchin Steiglitz method, there also is Chebyshev method for complex function approximation problem. You can find its description in T. W. Parks and C. S. Burrus, Digital Filter Design, John Wiley & Sons, New York, 1987 (chapter 4) (Fortran code for complex approximation is presented).
 
Last edited:
Maybe, but the MATLAB function combines the methods in both (cited) papers and seems to use Levi's method only to arrive at an educated guess for the next one, which I think, is what it finally implements.

Dave Zan:
Please note that the MATLAB function is iterative, assuming you're looking for direct, non-iterative methods.
 
Yes, that would flatten the response, but at the expense of phase shift (and group delay), which are increased.


No idea, Sir !!


Q-factor is a fairly standard term defined as resonant frequency by -3dB (skirt) bandwidth. Some people like RBJ have defined their own gain and Q-factor, that may be converted to the standard "EE's Q-factor", with explanations as given at the following link.

https://www.andyc.diy-audio-engineering.org/parametric-eq-parameters/index.html

Please note that the MATLAB code in post#20 is missing the gain correction (only), as it was accidentally left out while being copied from a much larger piece of code. Anyone planning to use the same please add the following line just before the %PARAEQ comment (thanks to fluid):

Code:
A=sqrt(A);

The definition of Q-factor for a shelf filter is also sometimes problematic, as some people are used to the quantity called "Slope", which equals unity when Q-factor = 0.7071 for a shelf gain of 1.
Not problematic at all, provided some knowledge is held by the user.
I.e. the simple rule that 0.707 Q is flat, 0dB gain, in all cases.
Remember the simple rule and its easy
 
  • Thank You
Reactions: newvirus2008
I.e. the simple rule that 0.707 Q is flat, 0dB gain, in all cases.
Remember the simple rule and its easy

Sorry, that was supposed to be "shelf slope of 1" that got messed up during editing. But how about "a simple rule" for a gain & slope of 2 (each) ? I think that would really help. 😉

Not problematic at all, provided some knowledge is held by the user.

The "problem" lies in that some processing units ask the user for the Q-factor while some others the slope, and the relation between these quantities is just too "complicated" for most people to remember:

CodeCogsEqn (1).gif


And, for anyone using RBJ's definition of shelving, A = sqrt(A) and the equation becomes:

CodeCogsEqn.gif


Neither seems to be friendly from my point of view. I recently wrote some programmes for my "DIY Dolby Box", where I decided to ditch the quantity called slope and use Q-factor instead.
 
  • Like
Reactions: mondogenerator
Sorry, that was supposed to be "shelf slope of 1" that got messed up during editing. But how about "a simple rule" for a gain & slope of 2 (each) ? I think that would really help. 😉



The "problem" lies in that some processing units ask the user for the Q-factor while some others the slope, and the relation between these quantities is just too "complicated" for most people to remember:

View attachment 1078357

And, for anyone using RBJ's definition of shelving, A = sqrt(A) and the equation becomes:

View attachment 1078359

Neither seems to be friendly from my point of view. I recently wrote some programmes for my "DIY Dolby Box", where I decided to ditch the quantity called slope and use Q-factor instead.
I'd agree with that.
Slope factor is just a term that confuses the situation.
A gain of 2, 6dB, should be straightforward to figure. Shouldn't it?
As for automating the iteration, that's a different story perhaps, and not my forte.
Mind you, Boxsim does this auto correction; albeit with a different proviso - minimise deviation from a flat acoustic summing of drivers - so curve fitting, to an extent, it's just that the curve, can't be changed.
 
...as far as I understood, you are...interested in the problem of a complex function approximation (nonlinear real approximation problem).
Yes, I am a data analyst and statistician and this overlaps with my audio hobby.
In statistics the data is always real, and I have never seen any work on how to fit complex data.
So it interests me as a maths problem as well as its audio applications.
... there also is Chebyshev method for complex function approximation problem.
Thanks, I will try to track this down.
...seems to indicate that Levi only uses the magnitude error...
I think he includes the phase error in the error term and then tries to minimise that. But I could be mistaken, it's a bit cryptic.
Maybe, but the MATLAB function combines the methods in both (cited) papers and seems to use Levi's method only to arrive...

Dave Zan:
Please note that the MATLAB function is iterative...
I think you have perhaps misinterpreted here.
It seems the Matlab function calculates the poles and zeroes of the function directly and that allows calculation of the inverse.
But if the function has non minimum phase (RHP zeroes) then the inverse will not be stable.
That's just an inconvenient reality.
So the iterative step is only required in this case, to try to find an approximation such that the inverse remains stable.
As you say, it calculates Levy's solution curve fit then uses it as the start of the iterative approximation, but this doesn't invalidate the fact that the curve fit can be directly calculated.
I could, of course, be mistaken here too.

Best wishes
David
 
Last edited:
I think you have perhaps misinterpreted here....
Quite possibly, but the following lines seem to suggest that the iterative procedure is executed whenever more than three arguments (besides h and w) are entered by the user, for example:
invfreqs(h, w, n, m, [], iter, tol);

Code:
gaussFlag = length(varargin)>3;  % run Gauss-Newton algorithm or not?
......
if ~gaussFlag,return,end

% Now for the iterative minimization

if isempty(maxiter), maxiter = 30; end
if isempty(tol)
    tol=0.01;
end
.....


It seems the Matlab function .... allows calculation of the inverse. But if the function has non minimum phase...That's just an inconvenient reality.
If the idea behind all this is equalisation, then it may be better to deal with zeroes in the following manner and avoid instability, as we've seen earlier in this thread. The poles (being on the LHS) may either be mirrored or cancelled, as one deems appropriate. The only drawback would be the resulting increase in phase lag and group delay.

RHP zero => Mirror to LHP pole (flat magnitude, double phase)
LHP zero => Cancel using LHP pole (flat magnitude, zero phase)

Nevertheless, Mr. Berchin's z-plane method's still there. The MATLAB code is also quite old anyway, having been written in '86, with revisions in '88 and '92.
 
Last edited:
...the iterative procedure is executed whenever more than three...
That's the mechanism to invoke iteration but I think the rationale is as I said.
If the idea behind all this is equalisation, then it may be better to deal with zeroes...
RHP zero => Mirror to LHP pole...
Yes, that's my kind of approach, simple and obvious has an appeal!
However, the room correction systems don't usually try this, AFAIK.
Some of them just leave RHP zeroes alone I believe.
If I understand correctly, that "Invfreqs" tries to find a pole zero pattern that is close to the specified response but with a stable inverse, then that is new to me.
Thanks for your contribution of the documentation for this.

Best wishes
David