{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Comparison of Instantaneous Frequency and Group Delay\n\nInstantaneous frequency and group delay are very closely related. The former is\nthe frequency of a signal at a given instant, and the latter is the time delay\nof frequency components. As this example shows, they coincide with each other\nfor a given signal when the time bandwidth product of the signal is\nsufficiently high.\n\nFigure 2.5 from the tutorial.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom tftb.generators import amgauss, fmlin\nfrom tftb.processing import loctime, locfreq, inst_freq, group_delay\n\ntime_instants = np.arange(2, 256)\nsig1 = amgauss(256, 128, 90) * fmlin(256)[0]\ntm, T1 = loctime(sig1)\nfm, B1 = locfreq(sig1)\nifr1 = inst_freq(sig1, time_instants)[0]\nf1 = np.linspace(0, 0.5 - 1.0 / 256, 256)\ngd1 = group_delay(sig1, f1)\n\nplt.subplot(211)\nplt.plot(time_instants, ifr1, '*', label='inst_freq')\nplt.plot(gd1, f1, '-', label='group delay')\nplt.xlim(0, 256)\nplt.grid(True)\nplt.legend()\nplt.title(\"Time-Bandwidth product: {0}\".format(T1 * B1))\nplt.xlabel('Time')\nplt.ylabel('Normalized Frequency')\n\n\nsig2 = amgauss(256, 128, 30) * fmlin(256, 0.2, 0.4)[0]\ntm, T2 = loctime(sig2)\nfm, B2 = locfreq(sig2)\nifr2 = inst_freq(sig2, time_instants)[0]\nf2 = np.linspace(0.02, 0.4, 256)\ngd2 = group_delay(sig2, f2)\n\n\nplt.subplot(212)\nplt.plot(time_instants, ifr2, '*', label='inst_freq')\nplt.plot(gd2, f2, '-', label='group delay')\nplt.ylim(0.2, 0.4)\nplt.xlim(0, 256)\nplt.grid(True)\nplt.legend()\nplt.title(\"Time-Bandwidth product: {0}\".format(T2 * B2))\nplt.xlabel('Time')\nplt.ylabel('Normalized Frequency')\n\nplt.subplots_adjust(hspace=0.5)\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}