ar1st0crat / NWaves

.NET DSP library with a lot of audio processing functions
MIT License
456 stars 71 forks source link

Convert impulse response to transfer function; estimate TF #13

Closed ToGoOrNotToGo closed 4 years ago

ToGoOrNotToGo commented 5 years ago

Is it possible to convert a filter impulse response into a transfer function? Can this be done with the class "TransferFunction"?

https://www.dsprelated.com/freebooks/filters/Transfer_Function_Analysis.html https://de.mathworks.com/help/signal/ug/filter-implementation-and-analysis.html

ar1st0crat commented 5 years ago

TF numerator is simply the impulse response (for FIR filters):

double[] impulseResponse;
//...

// IR -> TF
var tf = new TransferFunction(impulseResponse);

// TF -> IR
var kernel = tf.Numerator;  // filter 'kernel' is a synonym for IR

The impulse response of IIR filters is infinite (hence, the name).

If you are asking about the Partial Fraction Expansion method (used for doing the inverse Z-transform and finding analytic representation of impulse response), it's not implemented. Although there's a method DesignFilter.TfToSos decomposing TF into a collection of second-order sections TFs.

ToGoOrNotToGo commented 5 years ago

Yes I am looking for TF estimation of a given impulse response from an IIR filter by using Z-tranform. (and I start learning Z-Transform... oh and have to learn SOS... holy cow... ;-) )

ToGoOrNotToGo commented 5 years ago

Maybe a method like this is interesting for the TransferFunction class: https://www.mathworks.com/help/ident/ref/tfest.html

ar1st0crat commented 5 years ago

Impulse response of an IIR filter is infinite, so it can't be given in a finite array. Infinite impulse response can be derived only analytically from the TF / difference equations (so it's an inverse task).

For example, if we have TF 1 / (1-2z^-1) then the impulse response for all |z| > 2 is: 2^n u[n], n = -∞, ..., -2, -1, 0, 1, 2, ... ∞ where u[n] is the unit step function (i.e. infinite signal). DSP textbooks explain the math behind this.

Thus, you can have infinite impulse response only in analytic form like above and take Z-transform from it. I'm quite sure this is not what you need (most likely, you'll have just filter coefficients: non-recursive part of the difference equation is TF.Numerator and recursive part is TF.Denominator). But if I'm wrong then the symbolic math libraries can be used for analytical computations (like in this case - Z-transform series). This lib is not designed for these tasks.

ar1st0crat commented 5 years ago

Regarding tfest - maybe it'll be added to NWaves someday )). However, it solves another task: it finds the best estimate of the TF based on input/output in time domain or system behaviour in frequency domain.

ToGoOrNotToGo commented 5 years ago

There are more techniques calculation TF: "Impulse invariance" and "TF from bode plot"... But Z-transform seems to be the most interesting topic...

ar1st0crat commented 5 years ago

OK, this issue starts becoming like 'contemplations about everything' )). I gave the answer regarding the topic of the issue in my first answer. All in all, you raised three completely different tasks:

The first task is for symbolic math libraries like symPy. This lib is more practically oriented.

The second task is OK, although it's not related to the topic of the issue. I marked it as an 'enhancement'.

Regarding the third task, in this lib the so called BilinearTransform is implemented - it does the same thing and it's used in IIR filter design. I was also going to add MatchedTransform; it's similar.

There are more techniques calculation TF: "Impulse invariance" and "TF from bode plot"... But Z-transform seems to be the most interesting topic...

Actually, the entire TransferFunction class is about Z-transform. Z-transform is the mechanism for switching between time-domain difference equations (LTI filter coefficients) and Z-domain (in particular, frequency domain). Some of the tasks are practical and they can be done with NWaves (e.g., TF combinations, zeros\poles analysis, decomposing systems to SOS). However, this lib is not intended for calculus in general.

ToGoOrNotToGo commented 5 years ago

OK got it. Thank you very much! You are excellent! I will now learn "Transfer Function Analysis" first ...

ar1st0crat commented 5 years ago

No problem, you're welcome ) Also, maybe this repo https://github.com/ar1st0crat/DigitalSignalProcessing will be helpful. (There's a couple of old errors there, though. But I tried to make it quite simple and readable)

ToGoOrNotToGo commented 4 years ago

Can TransferFunction.StateSpace and TransferFunction.Zi be used for methods like Scilab's imrep2ss and frep2tf?

ar1st0crat commented 4 years ago

Currently, no. It's again related to MATLAB tfest function for TF estimation from frequency/impulse response, involving some optimization algorithm. It's not implemented yet...