{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nSetting the correct `max_acc` value\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nSome of the methods in the <INSERTNAME> package estimate the instantaneous\nfrequency at sample-level resolution. Most methods will suffer from edge effects\nwhich cause the estimated instantaneous frequency to spike especially at the start and end of\n the sound or due to noise. \n\nThe typical way these spikes are dealt with is to calculate an aboslute frequency \naccelaration profile along the frequency profile. Any regions above a certain\nthreshold are considered anomalous, and an (sort of) extrapolation is attempted \nusing the nearest non-anomalous regions. \n\nAn example frequency profile\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nLet's create an example sound, and use the PWVD method to track the instantaneous \nfrequency over time. \n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np \nfrom itsfm.frequency_tracking import generate_pwvd_frequency_profile, frequency_spike_detection\nfrom itsfm.simulate_calls import make_fm_chirp\nimport matplotlib.pyplot as plt\nfrom itsfm.view_horseshoebat_call import plot_accelaration_profile, time_plot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's create a hyperbolic chirp, this is a nice example because the\nthe hyperbolic chirp shows a nice variation in frequeny velocity over time. \nThis means the accelaration varies from low-->high. But what is an 'acceptable'\nvalue of accelaration to allow. Let's inspect the accelaration profile itself\nto understand what accelaration values are 'normal' and which values correspond\nto the spikes caused by the edge effects and noise.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fs = 22100\nchirp = make_fm_chirp(500, 5000, 0.100, fs, 'logarithmic')\n\nraw_fp, frequency_index = generate_pwvd_frequency_profile(chirp,\n                                                              fs, percentile=99)\nplt.figure()\ntime_plot(raw_fp,fs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The spikes caused by edge effects are there here too- even without noise. Let's\ncheck out the typical accelaration profile of this sound, and pay special\nattention to the values towards the ends. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "acc_plot  = plot_accelaration_profile(raw_fp, fs)\nacc_plot.set_ylim(0,0.5) # show a limited y-axis, because the frequency spikes mess up the display"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Remember that the accelaration of the frequency is calcualted at a per-sample resolution and thus may not show \ntoo much variation -- but the profile still shows outliers! Looking at this\nplot we can see that a value $\\geq$ 0.1 kHz/ms $^{2}$ is likely to be an outlier. \n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we know a way to set sensible `max_acc` values for our own recordings - let's see \nhow this translates to outlier detection in the frequency profile:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spikey_regions, acc_profile = frequency_spike_detection(raw_fp, fs, max_acc=0.1)\n\nplt.figure()\na = plt.subplot(211)\ntime_plot(raw_fp, fs)\nplt.plot( np.argwhere(spikey_regions)/fs, raw_fp[spikey_regions], \n         '*', label='Anomalous spikes in frequency profile')\nplt.legend()\na.set_title('Detected spikes in frequency profile')\na.set_ylabel('Frequency, Hz')\na.set_xticks([])\nb = plt.subplot(212)\ntime_plot(acc_profile, fs)\nplt.plot( np.argwhere(spikey_regions)/fs, acc_profile[spikey_regions], '*')\nb.set_ylim(0,0.5)\nb.set_title('Frequency accelaration profile')\nb.set_ylabel('Frequency accelaration, $kHz/ms^{2}$')"
      ]
    }
  ],
  "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
}