{
 "cells": [
  {
   "cell_type": "raw",
   "id": "original-socket",
   "metadata": {},
   "source": [
    "Requirement: run notebook enf_extraction_example.ipynb before this\n",
    "Notebook compares original and reference ENF signal by cross correlation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "logical-columbus",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install scipy==1.7.1\n",
    "! pip install matplotlib==3.3.2\n",
    "! pip install numpy==1.20.2\n",
    "# different from enf_extraction_example.ipynb notebook because we need correlation_lags function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "native-control",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "organizational-section",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pyenf script produces zeros at the end because of window function\n",
    "audio = np.load('audio.npy')[:-3]\n",
    "audio_reference = np.load('audio_reference.npy')[:-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "serious-seller",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 76268 characters]\n",
      "text/plain": [
       "<Figure size 504x504 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "audio_values = [x[0] for x in audio]\n",
    "audio_reference_values = [x[0] for x in audio_reference]\n",
    "\n",
    "# added noise to prove working\n",
    "noise = np.random.normal(0, 0.01, len(audio_reference_values))\n",
    "audio_reference_values_with_noise = np.concatenate([audio_reference_values, audio_reference_values + noise])\n",
    "\n",
    "# cross correlation\n",
    "correlation = signal.correlate(audio_reference_values_with_noise, audio_values, \"full\")\n",
    "lags = signal.correlation_lags(len(audio_reference_values_with_noise), len(audio_values), \"full\")\n",
    "# normalization\n",
    "correlation /= np.max(correlation)\n",
    "\n",
    "fig, (ax_orig, ax_noise, ax_corr) = plt.subplots(3, 1, figsize=(7, 7))\n",
    "ax_orig.plot(audio_values)\n",
    "ax_orig.set_title('Extracted')\n",
    "ax_orig.set_xlabel('Sample Number')\n",
    "ax_noise.plot(audio_reference_values_with_noise)\n",
    "ax_noise.set_title('Grid frequency')\n",
    "ax_noise.set_xlabel('Sample Number')\n",
    "ax_corr.plot(lags, correlation)\n",
    "ax_corr.set_title('Cross-correlated signal')\n",
    "ax_corr.set_xlabel('Lag')\n",
    "ax_orig.margins(0, 0.1)\n",
    "ax_noise.margins(0, 0.1)\n",
    "ax_corr.margins(0, 0.1)\n",
    "fig.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Conda Python 3.7 (base)",
   "language": "python",
   "name": "python3.7"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
