{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "999f3698",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "2a4c035e",
            "cell_type": "markdown",
            "source": "In these examples, `remez` is used to design low-pass, high-pass,\nband-pass and band-stop filters.  The parameters that define each filter\nare the filter order, the band boundaries, the transition widths of the\nboundaries, the desired gains in each band, and the sampling frequency.\n\nWe'll use a sample frequency of 22050 Hz in all the examples.  In each\nexample, the desired gain in each band is either 0 (for a stop band)\nor 1 (for a pass band).\n\n`freqz` is used to compute the frequency response of each filter, and\nthe utility function ``plot_response`` defined below is used to plot\nthe response.\n",
            "metadata": {}
        },
        {
            "id": "e7c46399",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import signal\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "539cd86f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fs = 22050   # Sample rate, Hz",
            "outputs": []
        },
        {
            "id": "ca341071",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def plot_response(w, h, title):\n    \"Utility function to plot response functions\"\n    fig = plt.figure()\n    ax = fig.add_subplot(111)\n    ax.plot(w, 20*np.log10(np.abs(h)))\n    ax.set_ylim(-40, 5)\n    ax.grid(True)\n    ax.set_xlabel('Frequency (Hz)')\n    ax.set_ylabel('Gain (dB)')\n    ax.set_title(title)",
            "outputs": []
        },
        {
            "id": "5056874d",
            "cell_type": "markdown",
            "source": "The first example is a low-pass filter, with cutoff frequency 8 kHz.\nThe filter length is 325, and the transition width from pass to stop\nis 100 Hz.\n",
            "metadata": {}
        },
        {
            "id": "355ff343",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "cutoff = 8000.0    # Desired cutoff frequency, Hz\ntrans_width = 100  # Width of transition from pass to stop, Hz\nnumtaps = 325      # Size of the FIR filter.\ntaps = signal.remez(numtaps, [0, cutoff, cutoff + trans_width, 0.5*fs],\n                    [1, 0], fs=fs)\nw, h = signal.freqz(taps, [1], worN=2000, fs=fs)\nplot_response(w, h, \"Low-pass Filter\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "23fcd48b",
            "cell_type": "markdown",
            "source": "This example shows a high-pass filter:\n",
            "metadata": {}
        },
        {
            "id": "f6cae84e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "cutoff = 2000.0    # Desired cutoff frequency, Hz\ntrans_width = 250  # Width of transition from pass to stop, Hz\nnumtaps = 125      # Size of the FIR filter.\ntaps = signal.remez(numtaps, [0, cutoff - trans_width, cutoff, 0.5*fs],\n                    [0, 1], fs=fs)\nw, h = signal.freqz(taps, [1], worN=2000, fs=fs)\nplot_response(w, h, \"High-pass Filter\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "1a2d9dfb",
            "cell_type": "markdown",
            "source": "This example shows a band-pass filter with a pass-band from 2 kHz to\n5 kHz.  The transition width is 260 Hz and the length of the filter\nis 63, which is smaller than in the other examples:\n",
            "metadata": {}
        },
        {
            "id": "dfa596a2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "band = [2000, 5000]  # Desired pass band, Hz\ntrans_width = 260    # Width of transition from pass to stop, Hz\nnumtaps = 63         # Size of the FIR filter.\nedges = [0, band[0] - trans_width, band[0], band[1],\n         band[1] + trans_width, 0.5*fs]\ntaps = signal.remez(numtaps, edges, [0, 1, 0], fs=fs)\nw, h = signal.freqz(taps, [1], worN=2000, fs=fs)\nplot_response(w, h, \"Band-pass Filter\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "054f3268",
            "cell_type": "markdown",
            "source": "The low order leads to higher ripple and less steep transitions.\n\nThe next example shows a band-stop filter.\n",
            "metadata": {}
        },
        {
            "id": "3f5b6f1a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "band = [6000, 8000]  # Desired stop band, Hz\ntrans_width = 200    # Width of transition from pass to stop, Hz\nnumtaps = 175        # Size of the FIR filter.\nedges = [0, band[0] - trans_width, band[0], band[1],\n         band[1] + trans_width, 0.5*fs]\ntaps = signal.remez(numtaps, edges, [1, 0, 1], fs=fs)\nw, h = signal.freqz(taps, [1], worN=2000, fs=fs)\nplot_response(w, h, \"Band-stop Filter\")\nplt.show()",
            "outputs": []
        }
    ]
}