{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a custom spectral model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this basic example we are going to build a custom spectral model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the frequency grid:\n",
    "fmin = 0.1\n",
    "df = 0.5\n",
    "N = int((10.0 - fmin)/ df)\n",
    "myFrequencyGrid =  ot.RegularGrid(fmin, df, N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define the spectral function:\n",
    "def s(f):\n",
    "    if(f <= 5.0):\n",
    "        return 1.0\n",
    "    else:\n",
    "        x = f - 5.0\n",
    "        return m.exp(-2.0 * x * x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the collection of HermitianMatrix:\n",
    "myCollection = ot.HermitianMatrixCollection()\n",
    "for k in range(N):\n",
    "    frequency = myFrequencyGrid.getValue(k)\n",
    "    matrix = ot.HermitianMatrix(1)\n",
    "    matrix[0, 0] = s(frequency)\n",
    "    myCollection.add(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frequency=RegularGrid(start=0.1, step=0.5, n=19) dimension=1 DSPCollection=[[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (1,0) ]],[[ (0.980199,0) ]],[[ (0.486752,0) ]],[[ (0.0889216,0) ]],[[ (0.00597602,0) ]],[[ (0.000147748,0) ]],[[ (1.34381e-06,0) ]],[[ (4.49635e-09,0) ]],[[ (5.53461e-12,0) ]],[[ (2.50622e-15,0) ]]]#19\n"
     ]
    }
   ],
   "source": [
    "# Create the spectral model:\n",
    "mySpectralModel = ot.UserDefinedSpectralModel(myFrequencyGrid, myCollection)\n",
    "print(mySpectralModel)"
   ]
  }
 ],
 "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": 0
}
