<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pylab inline\n",
    "import sk_dsp_comm.sigsys as ss\n",
    "import sk_dsp_comm.fir_design_helper as fir_d\n",
    "import sk_dsp_comm.iir_design_helper as iir_d\n",
    "import sk_dsp_comm.multirate_helper as mrh\n",
    "import scipy.signal as signal\n",
    "from IPython.display import Audio, display\n",
    "from IPython.display import Image, SVG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%config InlineBackend.figure_formats=['svg'] # SVG inline viewing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filter Design Using the Helper Modules\n",
    "\n",
    "The Scipy package *signal* assists with the design of many digital filter types. As an alternative, here we explore the use of the filter design modules found in `scikit-dsp-comm`\n",
    "(https://github.com/mwickert/scikit-dsp-comm).\n",
    "\n",
    "In this note we briefly explore the use of `sk_dsp_comm.fir_design_helper` and `sk_dsp_comm.iir_design_helper`. In the examples that follow we assume the import of these modules is made as follows:  \n",
    "\n",
    "```python\n",
    "import sk_dsp_comm.fir_design_helper as fir_d\n",
    "import sk_dsp_comm.iir_design_helper as iir_d\n",
    "```\n",
    "\n",
    "The functions in these modules provide an easier and more consistent interface for both finte impulse response (FIR) (linear phase) and infinite impulse response (IIR) classical designs. Functions inside these modules *wrap* `scipy.signal` functions and also incorporate new functionality.\n",
    "\n",
    "# Design From Amplitude Response Requirements\n",
    "With both `fir_design_helper` and `iir_design_helper` a design starts with amplitude response requirements, that is the filter passband critical frequencies, stopband critical frequencies, passband ripple, and stopband attenuation. The number of taps/coefficients (FIR case) or the filter order (IIR case) needed to meet these requirements is then determined and the filter coefficients are returned as an ndarray `b` for FIR, and for IIR both `b` and `a` arrays, and a second-order sections `sos` 2D array, with the rows containing the corresponding cascade of second-order sections toplogy for IIR filters.\n",
    "\n",
    "For the FIR case we have in the $z$-domain\n",
    "$$\n",
    "    H_\\text{FIR}(z) = \\sum_{k=0}^N b_k z^{-k}\n",
    "$$\n",
    "with ndarray `b` = $[b_0, b_1, \\ldots, b_N]$. For the IIR case we have in the $z$-domain\n",
    "$$\\begin{align}\n",
    "    H_\\text{IIR}(z) &= \\frac{\\sum_{k=0}^M b_k z^{-k}}{\\sum_{k=1}^N a_k z^{-k}} \\\\\n",
    "    &= \\prod_{k=0}^{N_s-1} \\frac{b_{k0} + b_{k1} z^{-1} + b_{k2} z^{-2}}{1 + a_{k1} z^{-1} + a_{k2} z^{-2}} = \\prod_{k=0}^{N_s-1} H_k(z)\n",
    "\\end{align}$$\n",
    "where $N_s = \\lfloor(N+1)/2\\rfloor$. For the `b/a` form the coefficients are arranged as\n",
    "```python\n",
    "b = [b0, b1, ..., bM-1], the numerator filter coefficients\n",
    "a = [a0, a1, ..., aN-1], the denominator filter ceofficients\n",
    "```\n",
    "For the `sos` form each row of the 2D `sos` array corresponds to the coefficients of $H_k(z)$, as follows:\n",
    "```python\n",
    "SOS_mat = [[b00, b01, b02, 1, a01, a02], #biquad 0\n",
    "           [b10, b11, b12, 1, a11, a12], #biquad 1\n",
    "            .\n",
    "            .\n",
    "           [bNs-10, bNs-11, bNs-12, 1, aNs-11, aNs-12]] #biquad Ns-1\n",
    "```\n",
    "\n",
    "# Linear Phase FIR Filter Design\n",
    "The primary focus of this module is adding the ability to design linear phase FIR filters from user friendly amplitude response requirements.\n",
    "\n",
    "Most digital filter design is motivated by the desire to approach an ideal filter. Recall an ideal filter will pass signals of a certain of frequencies and block others. For both analog and digital filters the designer can choose from a variety of approximation techniques. For digital filters the approximation techniques fall into the categories of IIR or FIR. In the design of FIR filters two popular techniques are truncating the ideal filter impulse response and applying a window, and optimum equiripple approximations [Oppenheim2010](https://www.amazon.com/Discrete-Time-Signal-Processing-3rd-Prentice-Hall/dp/0131988425/ref=sr_1_1?ie=UTF8&qid=1519940790&sr=8-1&keywords=oppenheim+discrete+time+signal+processing&dpID=51v48p99JjL&preST=_SX218_BO1,204,203,200_QL40_&dpSrc=srch). Frequency sampling based approaches are also popular, but will not be considered here, even though `scipy.signal` supports all three. Filter design generally begins with a specification of the desired frequency response. The filter frequency response may be stated in several ways, but amplitude response is the most common, e.g., state how $H_c(j\\Omega)$ or $H(e^{j\\omega}) = H(e^{j2\\pi f/f_s})$ should behave. A completed design consists of the number of coefficients (taps) required and the coefficients themselves (double precision float or `float64` in Numpy, and `float64_t` in C). Figure 1, below, shows amplitude response requirements in terms of filter gain and critical frequencies for lowpass, highpass, bandpass, and bandstop filters. The critical frequencies are given here in terms of analog requirements in Hz. The sampling frequency is assumed to be in Hz. The passband ripple and stopband attenuation values are in dB. Note in dB terms attenuation is the negative of gain, e.g., -60 of stopband gain is equivalent to 60 dB of stopband attenuation."
   ]<commit_msg>Adding spaces for proper latex parsing<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pylab inline\n",
    "import sk_dsp_comm.sigsys as ss\n",
    "import sk_dsp_comm.fir_design_helper as fir_d\n",
    "import sk_dsp_comm.iir_design_helper as iir_d\n",
    "import sk_dsp_comm.multirate_helper as mrh\n",
    "import scipy.signal as signal\n",
    "from IPython.display import Audio, display\n",
    "from IPython.display import Image, SVG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%config InlineBackend.figure_formats=['svg'] # SVG inline viewing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filter Design Using the Helper Modules\n",
    "\n",
    "The Scipy package *signal* assists with the design of many digital filter types. As an alternative, here we explore the use of the filter design modules found in `scikit-dsp-comm`\n",
    "(https://github.com/mwickert/scikit-dsp-comm).\n",
    "\n",
    "In this note we briefly explore the use of `sk_dsp_comm.fir_design_helper` and `sk_dsp_comm.iir_design_helper`. In the examples that follow we assume the import of these modules is made as follows:  \n",
    "\n",
    "```python\n",
    "import sk_dsp_comm.fir_design_helper as fir_d\n",
    "import sk_dsp_comm.iir_design_helper as iir_d\n",
    "```\n",
    "\n",
    "The functions in these modules provide an easier and more consistent interface for both finte impulse response (FIR) (linear phase) and infinite impulse response (IIR) classical designs. Functions inside these modules *wrap* `scipy.signal` functions and also incorporate new functionality.\n",
    "\n",
    "# Design From Amplitude Response Requirements\n",
    "With both `fir_design_helper` and `iir_design_helper` a design starts with amplitude response requirements, that is the filter passband critical frequencies, stopband critical frequencies, passband ripple, and stopband attenuation. The number of taps/coefficients (FIR case) or the filter order (IIR case) needed to meet these requirements is then determined and the filter coefficients are returned as an ndarray `b` for FIR, and for IIR both `b` and `a` arrays, and a second-order sections `sos` 2D array, with the rows containing the corresponding cascade of second-order sections toplogy for IIR filters.\n",
    "\n",
    "For the FIR case we have in the $z$-domain\n",
    "$$\n",
    "    H_\\text{FIR}(z) = \\sum_{k=0}^N b_k z^{-k}\n",
    "$$\n",
    "\n",
    "with ndarray `b` = $[b_0, b_1, \\ldots, b_N]$. For the IIR case we have in the $z$-domain\n",
    "\n",
    "$$\\begin{align}\n",
    "    H_\\text{IIR}(z) &= \\frac{\\sum_{k=0}^M b_k z^{-k}}{\\sum_{k=1}^N a_k z^{-k}} \\\\\n",
    "    &= \\prod_{k=0}^{N_s-1} \\frac{b_{k0} + b_{k1} z^{-1} + b_{k2} z^{-2}}{1 + a_{k1} z^{-1} + a_{k2} z^{-2}} = \\prod_{k=0}^{N_s-1} H_k(z)\n",
    "\\end{align}$$\n",
    "\n",
    "where $N_s = \\lfloor(N+1)/2\\rfloor$. For the `b/a` form the coefficients are arranged as\n",
    "```python\n",
    "b = [b0, b1, ..., bM-1], the numerator filter coefficients\n",
    "a = [a0, a1, ..., aN-1], the denominator filter ceofficients\n",
    "```\n",
    "For the `sos` form each row of the 2D `sos` array corresponds to the coefficients of $H_k(z)$, as follows:\n",
    "```python\n",
    "SOS_mat = [[b00, b01, b02, 1, a01, a02], #biquad 0\n",
    "           [b10, b11, b12, 1, a11, a12], #biquad 1\n",
    "            .\n",
    "            .\n",
    "           [bNs-10, bNs-11, bNs-12, 1, aNs-11, aNs-12]] #biquad Ns-1\n",
    "```\n",
    "\n",
    "# Linear Phase FIR Filter Design\n",
    "The primary focus of this module is adding the ability to design linear phase FIR filters from user friendly amplitude response requirements.\n",
    "\n",
    "Most digital filter design is motivated by the desire to approach an ideal filter. Recall an ideal filter will pass signals of a certain of frequencies and block others. For both analog and digital filters the designer can choose from a variety of approximation techniques. For digital filters the approximation techniques fall into the categories of IIR or FIR. In the design of FIR filters two popular techniques are truncating the ideal filter impulse response and applying a window, and optimum equiripple approximations [Oppenheim2010](https://www.amazon.com/Discrete-Time-Signal-Processing-3rd-Prentice-Hall/dp/0131988425/ref=sr_1_1?ie=UTF8&qid=1519940790&sr=8-1&keywords=oppenheim+discrete+time+signal+processing&dpID=51v48p99JjL&preST=_SX218_BO1,204,203,200_QL40_&dpSrc=srch). Frequency sampling based approaches are also popular, but will not be considered here, even though `scipy.signal` supports all three. Filter design generally begins with a specification of the desired frequency response. The filter frequency response may be stated in several ways, but amplitude response is the most common, e.g., state how $H_c(j\\Omega)$ or $H(e^{j\\omega}) = H(e^{j2\\pi f/f_s})$ should behave. A completed design consists of the number of coefficients (taps) required and the coefficients themselves (double precision float or `float64` in Numpy, and `float64_t` in C). Figure 1, below, shows amplitude response requirements in terms of filter gain and critical frequencies for lowpass, highpass, bandpass, and bandstop filters. The critical frequencies are given here in terms of analog requirements in Hz. The sampling frequency is assumed to be in Hz. The passband ripple and stopband attenuation values are in dB. Note in dB terms attenuation is the negative of gain, e.g., -60 of stopband gain is equivalent to 60 dB of stopband attenuation."
   ]
{'fraction_non_alphanumeric': 0.13064277637644603, 'fraction_numerical': 0.02191876141602157, 'mean_word_length': 3.636290322580645, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '133040', 'n_tokens_mistral': 3900, 'n_tokens_neox': 3675, 'n_words': 1356}