<?xml version="1.0" encoding="UTF-8"?>

<!--
 *
 * This help file was generated from invfreqz.sci using help_from_sci().
 *
 -->

<refentry version="5.0-subset Scilab" xml:id="invfreqz" xml:lang="en"
          xmlns="http://docbook.org/ns/docbook"
          xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg"
          xmlns:ns3="http://www.w3.org/1999/xhtml"
          xmlns:mml="http://www.w3.org/1998/Math/MathML"
          xmlns:scilab="http://www.scilab.org"
          xmlns:db="http://docbook.org/ns/docbook">

  <refnamediv>
    <refname>invfreqz</refname>
    <refpurpose>Fit filter B(z)/A(z) to the complex frequency response H at frequency points F.</refpurpose>
  </refnamediv>


<refsynopsisdiv>
   <title>Syntax</title>
   <synopsis>
   [B, A] = invfreqz(H, F, nB, nA)
   [B, A] = invfreqz(H, F, nB, nA, W)
   [B, A] = invfreqz(H, F, nB, nA, W, iter, tol, 'trace')
   
   </synopsis>
</refsynopsisdiv>

<refsection>
   <title>Parameters</title>
   <variablelist>
        <varlistentry>
            <term>H:</term>
            <listitem><para> Desired complex frequency response.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>F:</term>
            <listitem><para> Normalized frequency (0 to π) (must be the same length as H).</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>nA:</term>
            <listitem><para> Order of the denominator polynomial A.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>nB:</term>
            <listitem><para> Order of the numerator polynomial B.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>W:</term>
            <listitem><para> (optional) Vector of weights (must be the same length as F). Default is uniform weighting.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>iter:</term>
            <listitem><para> (optional) Number of iterations for refinement. Default is no iterations.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>tol:</term>
            <listitem><para> (optional) Tolerance for iterations. Default is no tolerance.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>'trace':</term>
            <listitem><para> (optional) Enables tracing of the iterative process.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>B:</term>
            <listitem><para> Coefficients of the numerator polynomial.</para></listitem>
        </varlistentry>
        <varlistentry>
            <term>A:</term>
            <listitem><para> Coefficients of the denominator polynomial.</para></listitem>
        </varlistentry>
   </variablelist>
</refsection>

<refsection>
   <title>Description</title>
   <para>
The `invfreqz` function fits a rational transfer function B(z)/A(z) to the desired complex frequency response `H` at frequency points `F`.
The orders of the numerator and denominator polynomials are specified by `nB` and `nA`, respectively. Optionally, the fit-errors can be
weighted using the weights `W`. The function supports iterative refinement of the solution, specified by `iter` and `tol`.
   </para>
   <para>
Note: The core implementation of this function relies on `invfreq.sci`.
   </para>
   <para>
</para>
</refsection>

<refsection>
   <title>Examples</title>
   <programlisting role="example"><![CDATA[
order = 9  // order of test filter
// going to 10 or above leads to numerical instabilities and large errors
fc = 1/2   // sampling rate / 4
n = 128    // frequency grid size
[B0, A0] = butter(order, fc)
[H0, w] = freqz(B0, A0, n);
Nn = (rand(size(w,1),size(w,2),'normal')+%i*rand(size(w,1),size(w,2),'normal'))/sqrt(2);
[Bh, Ah, Sig0] = invfreqz(H0, w, order, order);
[Hh, wh] = freqz(Bh, Ah, n);
[BLS, ALS, SigLS] = invfreqz(H0+1e-5*Nn, w, order, order, [], [], [], [], "method", "LS");
HLS = freqz(BLS, ALS, n);
[BTLS, ATLS, SigTLS] = invfreqz(H0+1e-5*Nn, w, order, order, [], [], [], [], "method", "TLS");
HTLS = freqz(BTLS, ATLS, n);
[BMLS, AMLS, SigMLS] = invfreqz(H0+1e-5*Nn, w, order, order, [], [], [], [], "method", "QR");
HMLS = freqz(BMLS, AMLS, n);
plot(w,[abs(H0) abs(Hh)])
xlabel("Frequency (rad/sample)");
ylabel("Magnitude");
legend('Original','Measured');
err = norm(H0-Hh)
disp(sprintf('L2 norm of frequency response error = %f',err));
   ]]></programlisting>
</refsection>

<refsection>
   <title>Authors</title>
   <simplelist type="vert">
   <member>FOSSEE Team</member>
   <member>toolbox@scilab.in</member>
   </simplelist>
</refsection>

<refsection>
   <title>Bibliography</title>
   <para>- J. O. Smith, "Techniques for Digital Filter Design and System Identification with Application to the Violin, Ph.D. Dissertation, Elec. Eng. Dept., Stanford University, June 1983.</para>
   <para>- https://ccrma.stanford.edu/~jos/filters/FFT_Based_Equation_Error_Method.html</para>
</refsection>

<refsection role="see also">
   <title>See also</title>
   <simplelist type="inline">
   <member><link linkend="invfreq">invfreq</link></member>
   </simplelist>
</refsection>
</refentry>
