{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sound Field Synthesis\n",
    "\n",
    "Illustrates the usage of the SFS toolbox for the simulation of different sound field synthesis methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import sfs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simulation parameters\n",
    "number_of_secondary_sources = 56\n",
    "frequency = 680  # in Hz\n",
    "pw_angle = 30  # traveling direction of plane wave in degree\n",
    "xs = [-2, -1, 0]  # position of virtual point source in m\n",
    "\n",
    "grid = sfs.util.xyz_grid([-2, 2], [-2, 2], 0, spacing=0.02)\n",
    "omega = 2 * np.pi * frequency  # angular frequency\n",
    "npw = sfs.util.direction_vector(np.radians(pw_angle))  # normal vector of plane wave"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a helper function for synthesize and plot the sound field from the given driving signals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sound_field(d, selection, secondary_source, array, grid, tapering=True):\n",
    "    if tapering:\n",
    "        tapering_window = sfs.tapering.tukey(selection, alpha=0.3)\n",
    "    else:\n",
    "        tapering_window = sfs.tapering.none(selection)\n",
    "    p = sfs.fd.synthesize(d, tapering_window, array, secondary_source, grid=grid)\n",
    "    sfs.plot2d.amplitude(p, grid, xnorm=[0, 0, 0])\n",
    "    sfs.plot2d.loudspeakers(array.x, array.n, tapering_window)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Circular loudspeaker arrays\n",
    "\n",
    "In the following we show different sound field synthesis methods applied to a circular loudspeaker array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "radius = 1.5  # in m\n",
    "array = sfs.array.circular(number_of_secondary_sources, radius)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wave Field Synthesis (WFS)\n",
    "\n",
    "#### Plane wave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.wfs.plane_25d(omega, array.x, array.n, n=npw)\n",
    "sound_field(d, selection, secondary_source, array, grid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Point source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.wfs.point_25d(omega, array.x, array.n, xs)\n",
    "sound_field(d, selection, secondary_source, array, grid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Near-Field Compensated Higher Order Ambisonics (NFC-HOA)\n",
    "\n",
    "#### Plane wave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.nfchoa.plane_25d(omega, array.x, radius, n=npw)\n",
    "sound_field(d, selection, secondary_source, array, grid, tapering=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Point source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.nfchoa.point_25d(omega, array.x, radius, xs)\n",
    "sound_field(d, selection, secondary_source, array, grid, tapering=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Linear loudspeaker array\n",
    "\n",
    "In the following we show different sound field synthesis methods applied to a linear loudspeaker array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spacing = 0.07  # in m\n",
    "array = sfs.array.linear(number_of_secondary_sources, spacing,\n",
    "                         center=[0, -0.5, 0], orientation=[0, 1, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wave Field Synthesis (WFS)\n",
    "\n",
    "#### Plane wave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.wfs.plane_25d(omega, array.x, array.n, npw)\n",
    "sound_field(d, selection, secondary_source, array, grid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Point source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d, selection, secondary_source = sfs.fd.wfs.point_25d(omega, array.x, array.n, xs)\n",
    "sound_field(d, selection, secondary_source, array, grid)"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
