Subversion Repositories f9daq

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# On trigger signal acquisition\n",
    "\n",
    "## Description\n",
    "\n",
    "This example shows how to acquire 16k samples of signal on fast analog inputs.\n",
    "Signal will be acquired when the internal trigger condition is meet.\n",
    "Time length of the acquired signal depends on the time scale of a buffer\n",
    "that can be set with a decimation factor.\n",
    "\n",
    "TODO: describe some calculations\n",
    "\n",
    "## Required hardware\n",
    "\n",
    "- Red Pitaya\n",
    "- Signal (function) generator\n",
    "\n",
    "![wiring diagram](img/generate_continous_signal_on_fast_analog_output.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The `rp` object is an instance of the `redpitaya` class.\n",
    "When the object is initialized, the FPGA bitstream is loaded and\n",
    "memory mapped FPGA registers are mapped into software.\n",
    "Repeating FPGA bitstream loading will cause all registers to reset,\n",
    "while mapping the memory space multiple times will cause segmentation faults.\n",
    "So untill this issues are handled by the driver\n",
    "a `redpitaya` instance should be created only once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from redpitaya import redpitaya\n",
    "rp = redpitaya()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# generator configuration\n",
    "#rp.GenReset()\n",
    "#rp.GenFreq(0, 100000.0)\n",
    "#rp.GenAmp(0, 1.0)\n",
    "#rp.GenWaveform(0, rp.WAVEFORM_SINE)\n",
    "#rp.GenOutEnable(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# acquisition configuration\n",
    "size = rp.AcqGetBufSize()\n",
    "print(size)\n",
    "rp.AcqReset()\n",
    "rp.AcqSetDecimationFactor(1)\n",
    "rp.AcqSetTriggerLevel(0, -0.05)\n",
    "#rp.AcqSetPostTriggerDelay(size//2) # place trigger in the middle of the buffer\n",
    "#rp.AcqSetPostTriggerDelay(size//2)\n",
    "rp.AcqSetPostTriggerDelay(0)\n",
    "\n",
    "size = 1024\n",
    "channels = (0,1)\n",
    "\n",
    "# start acquisition process\n",
    "rp.AcqStart()\n",
    "# set trigger source to start acquisition\n",
    "rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_PE)\n",
    "# wait in a loop for trigger state to chage from TRIG_STATE_WAITING\n",
    "while rp.AcqGetTriggerSrc():\n",
    "    pass\n",
    "print('triggered')\n",
    "\n",
    "# read data from FPGA FIFO into memory and display it\n",
    "buff = [rp.AcqGetOldestDataV(ch, size) for ch in channels];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "\n",
    "from bokeh.io import push_notebook, show, output_notebook\n",
    "from bokeh.models import HoverTool, Range1d\n",
    "from bokeh.plotting import figure\n",
    "from bokeh.plotting import show, output_file, vplot\n",
    "from bokeh.resources import INLINE \n",
    "from bokeh.models import Column\n",
    "output_notebook(resources=INLINE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def hfill(histogram, datum, weight=1):\n",
    "  for idx, b in enumerate(histogram[1]):\n",
    "    if idx > 0:\n",
    "        if (datum < b and datum >= histogram[1][0]) or (datum <= b and idx == len(histogram[1]) - 1):\n",
    "            histogram[0][idx - 1] += int(weight)\n",
    "            break\n",
    "            \n",
    "def h1d(title, titlex, titley, nbins, xmin,xmax):\n",
    "    h = np.histogram([], bins=nbins, range= (xmin,xmax))\n",
    "    p = figure(title=title,tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
    "    p.quad(top=h[0], bottom=0, left=h[1][:-1], right=h[1][1:])\n",
    "    p.xaxis.axis_label = 'p.h.(ADC)'\n",
    "    p.yaxis.axis_label = 'N'\n",
    "    return [h,p]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "hadc =  np.histogram([], bins=40, range= (0,0.5))\n",
    "p1 = figure(title=\"ADC distribution\",tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
    "p1.quad(top=hadc[0], bottom=0, left=hadc[1][:-1], right=hadc[1][1:],  fill_color=\"#036564\", line_color=\"#033649\")\n",
    "p1.xaxis.axis_label = 'p.h.(ADC)'\n",
    "p1.yaxis.axis_label = 'N'\n",
    "\n",
    "\n",
    "htdc =  np.histogram([], bins=100, range= (0,size))\n",
    "p2 = figure(title=\"TDC distribution\",tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
    "p2.quad(top=htdc[0], bottom=0, left=htdc[1][:-1], right=htdc[1][1:],  fill_color=\"#036564\", line_color=\"#033649\")\n",
    "p2.xaxis.axis_label = 'p.h.(ADC)'\n",
    "p2.yaxis.axis_label = 'N'\n",
    "\n",
    "#print(hist)\n",
    "#for event in [ 1,2,3,4,4,5,6,32,4,10]:\n",
    "#   hfill(hist,event)\n",
    "\n",
    "#adcplot = show(p1, notebook_handle=True)\n",
    "N = size\n",
    "x = np.arange(N) / rp.FS\n",
    "\n",
    "colors = ('red', 'blue')\n",
    "hover = HoverTool(mode = 'vline', tooltips=[(\"T\", \"@x\"), (\"V\", \"@y\")])\n",
    "tools = \"pan,wheel_zoom,box_zoom,reset,crosshair,save\"\n",
    "p = figure(plot_height=500, plot_width=900, title=\"oscilloscope\", toolbar_location=\"above\", tools=(tools, hover))\n",
    "p.xaxis.axis_label='time [s]'\n",
    "p.yaxis.axis_label='voltage [V]'\n",
    "p.y_range=Range1d(-0.1, 0.1)\n",
    "r = [p.line(x, buff[i], line_width=1, line_alpha=0.7, color=colors[i]) for i in channels]\n",
    "\n",
    "target = show( Column(p, p1, p2), notebook_handle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "size = 1024\n",
    "channels = (0,1)\n",
    "\n",
    "rp.AcqReset()\n",
    "rp.AcqSetDecimationFactor(1)\n",
    "threshold = -0.05\n",
    "rp.AcqSetTriggerLevel(0, threshold)\n",
    "rp.AcqSetPostTriggerDelay(size-80)\n",
    "\n",
    "neve = 0\n",
    "while True:\n",
    "    rp.AcqStart()\n",
    "    rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_NE)  \n",
    "    while (rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_NE)): pass\n",
    "    buff = [rp.AcqGetLatestDataV(ch, size) for ch in channels];\n",
    "\n",
    "    adcdata = np.absolute(np.amin(buff[0]))\n",
    "    signal  = np.array(buff[0])\n",
    "    tdcdata = np.where(signal < threshold)[0] # get item\n",
    "#    print (adcdata, tdcdata)\n",
    "    hfill(hadc,adcdata)\n",
    "    \n",
    "    for a in tdcdata:\n",
    "      if a - tdcdata[0] > 5:\n",
    "         hfill(htdc,a-tdcdata[0])\n",
    "         break   \n",
    "    neve+=1\n",
    "    for i in channels:\n",
    "        r[i].data_source.data['y'] = buff[i]\n",
    "# push updates to the plot continuously using the handle (intererrupt the notebook kernel to stop)\n",
    "    if (neve%10 == 0):\n",
    "      push_notebook(handle=target)\n",
    "#    time.sleep(0.05)"
   ]
  }
 ],
 "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
}