Subversion Repositories f9daq

Rev

Rev 240 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
240 f9daq 1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "metadata": {
6
    "deletable": true,
7
    "editable": true
8
   },
9
   "source": [
10
    "# On trigger signal acquisition\n",
11
    "\n",
12
    "## Description\n",
13
    "\n",
14
    "This example shows how to acquire 16k samples of signal on fast analog inputs.\n",
15
    "Signal will be acquired when the internal trigger condition is meet.\n",
16
    "Time length of the acquired signal depends on the time scale of a buffer\n",
17
    "that can be set with a decimation factor.\n",
18
    "\n",
19
    "TODO: describe some calculations\n",
20
    "\n",
21
    "## Required hardware\n",
22
    "\n",
23
    "- Red Pitaya\n",
24
    "- Signal (function) generator\n",
25
    "\n",
26
    "![wiring diagram](img/generate_continous_signal_on_fast_analog_output.png)"
27
   ]
28
  },
29
  {
30
   "cell_type": "markdown",
31
   "metadata": {
32
    "deletable": true,
33
    "editable": true
34
   },
35
   "source": [
36
    "The `rp` object is an instance of the `redpitaya` class.\n",
37
    "When the object is initialized, the FPGA bitstream is loaded and\n",
38
    "memory mapped FPGA registers are mapped into software.\n",
39
    "Repeating FPGA bitstream loading will cause all registers to reset,\n",
40
    "while mapping the memory space multiple times will cause segmentation faults.\n",
41
    "So untill this issues are handled by the driver\n",
42
    "a `redpitaya` instance should be created only once."
43
   ]
44
  },
45
  {
46
   "cell_type": "code",
47
   "execution_count": null,
48
   "metadata": {
49
    "collapsed": false,
50
    "deletable": true,
51
    "editable": true
52
   },
53
   "outputs": [],
54
   "source": [
55
    "from redpitaya import redpitaya\n",
56
    "rp = redpitaya()"
57
   ]
58
  },
59
  {
60
   "cell_type": "code",
61
   "execution_count": null,
62
   "metadata": {
63
    "collapsed": false,
64
    "deletable": true,
65
    "editable": true
66
   },
67
   "outputs": [],
68
   "source": [
69
    "# generator configuration\n",
70
    "#rp.GenReset()\n",
71
    "#rp.GenFreq(0, 100000.0)\n",
72
    "#rp.GenAmp(0, 1.0)\n",
73
    "#rp.GenWaveform(0, rp.WAVEFORM_SINE)\n",
74
    "#rp.GenOutEnable(0)"
75
   ]
76
  },
77
  {
78
   "cell_type": "code",
79
   "execution_count": null,
80
   "metadata": {
81
    "collapsed": false,
82
    "deletable": true,
83
    "editable": true
84
   },
85
   "outputs": [],
86
   "source": [
87
    "# acquisition configuration\n",
88
    "size = rp.AcqGetBufSize()\n",
89
    "print(size)\n",
90
    "rp.AcqReset()\n",
91
    "rp.AcqSetDecimationFactor(1)\n",
92
    "rp.AcqSetTriggerLevel(0, -0.05)\n",
93
    "#rp.AcqSetPostTriggerDelay(size//2) # place trigger in the middle of the buffer\n",
94
    "#rp.AcqSetPostTriggerDelay(size//2)\n",
95
    "rp.AcqSetPostTriggerDelay(0)\n",
96
    "\n",
97
    "size = 1024\n",
98
    "channels = (0,1)\n",
99
    "\n",
100
    "# start acquisition process\n",
101
    "rp.AcqStart()\n",
102
    "# set trigger source to start acquisition\n",
103
    "rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_PE)\n",
104
    "# wait in a loop for trigger state to chage from TRIG_STATE_WAITING\n",
105
    "while rp.AcqGetTriggerSrc():\n",
106
    "    pass\n",
107
    "print('triggered')\n",
108
    "\n",
109
    "# read data from FPGA FIFO into memory and display it\n",
110
    "buff = [rp.AcqGetOldestDataV(ch, size) for ch in channels];"
111
   ]
112
  },
113
  {
114
   "cell_type": "code",
115
   "execution_count": null,
116
   "metadata": {
117
    "collapsed": false,
118
    "deletable": true,
119
    "editable": true
120
   },
121
   "outputs": [],
122
   "source": [
123
    "import time\n",
124
    "import numpy as np\n",
125
    "\n",
126
    "from bokeh.io import push_notebook, show, output_notebook\n",
127
    "from bokeh.models import HoverTool, Range1d\n",
128
    "from bokeh.plotting import figure\n",
129
    "from bokeh.plotting import show, output_file, vplot\n",
130
    "from bokeh.resources import INLINE \n",
131
    "from bokeh.models import Column\n",
132
    "output_notebook(resources=INLINE)"
133
   ]
134
  },
135
  {
136
   "cell_type": "code",
137
   "execution_count": null,
138
   "metadata": {
139
    "collapsed": false,
140
    "deletable": true,
141
    "editable": true
142
   },
143
   "outputs": [],
144
   "source": [
145
    "def hfill(histogram, datum, weight=1):\n",
146
    "  for idx, b in enumerate(histogram[1]):\n",
147
    "    if idx > 0:\n",
148
    "        if (datum < b and datum >= histogram[1][0]) or (datum <= b and idx == len(histogram[1]) - 1):\n",
149
    "            histogram[0][idx - 1] += int(weight)\n",
150
    "            break\n",
151
    "            \n",
152
    "def h1d(title, titlex, titley, nbins, xmin,xmax):\n",
153
    "    h = np.histogram([], bins=nbins, range= (xmin,xmax))\n",
154
    "    p = figure(title=title,tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
155
    "    p.quad(top=h[0], bottom=0, left=h[1][:-1], right=h[1][1:])\n",
156
    "    p.xaxis.axis_label = 'p.h.(ADC)'\n",
157
    "    p.yaxis.axis_label = 'N'\n",
158
    "    return [h,p]"
159
   ]
160
  },
161
  {
162
   "cell_type": "code",
163
   "execution_count": null,
164
   "metadata": {
165
    "collapsed": false,
166
    "deletable": true,
167
    "editable": true,
168
    "scrolled": false
169
   },
170
   "outputs": [],
171
   "source": [
172
    "hadc =  np.histogram([], bins=40, range= (0,0.5))\n",
173
    "p1 = figure(title=\"ADC distribution\",tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
174
    "p1.quad(top=hadc[0], bottom=0, left=hadc[1][:-1], right=hadc[1][1:],  fill_color=\"#036564\", line_color=\"#033649\")\n",
175
    "p1.xaxis.axis_label = 'p.h.(ADC)'\n",
176
    "p1.yaxis.axis_label = 'N'\n",
177
    "\n",
178
    "\n",
179
    "htdc =  np.histogram([], bins=100, range= (0,size))\n",
180
    "p2 = figure(title=\"TDC distribution\",tools=\"save\", background_fill_color=\"#E8DDCB\")\n",
181
    "p2.quad(top=htdc[0], bottom=0, left=htdc[1][:-1], right=htdc[1][1:],  fill_color=\"#036564\", line_color=\"#033649\")\n",
182
    "p2.xaxis.axis_label = 'p.h.(ADC)'\n",
183
    "p2.yaxis.axis_label = 'N'\n",
184
    "\n",
185
    "#print(hist)\n",
186
    "#for event in [ 1,2,3,4,4,5,6,32,4,10]:\n",
187
    "#   hfill(hist,event)\n",
188
    "\n",
189
    "#adcplot = show(p1, notebook_handle=True)\n",
190
    "N = size\n",
191
    "x = np.arange(N) / rp.FS\n",
192
    "\n",
193
    "colors = ('red', 'blue')\n",
194
    "hover = HoverTool(mode = 'vline', tooltips=[(\"T\", \"@x\"), (\"V\", \"@y\")])\n",
195
    "tools = \"pan,wheel_zoom,box_zoom,reset,crosshair,save\"\n",
196
    "p = figure(plot_height=500, plot_width=900, title=\"oscilloscope\", toolbar_location=\"above\", tools=(tools, hover))\n",
197
    "p.xaxis.axis_label='time [s]'\n",
198
    "p.yaxis.axis_label='voltage [V]'\n",
199
    "p.y_range=Range1d(-0.1, 0.1)\n",
200
    "r = [p.line(x, buff[i], line_width=1, line_alpha=0.7, color=colors[i]) for i in channels]\n",
201
    "\n",
202
    "target = show( Column(p, p1, p2), notebook_handle=True)"
203
   ]
204
  },
205
  {
206
   "cell_type": "code",
207
   "execution_count": null,
208
   "metadata": {
209
    "collapsed": false,
210
    "deletable": true,
211
    "editable": true
212
   },
213
   "outputs": [],
214
   "source": [
215
    "size = 1024\n",
216
    "channels = (0,1)\n",
217
    "\n",
218
    "rp.AcqReset()\n",
219
    "rp.AcqSetDecimationFactor(1)\n",
220
    "threshold = -0.05\n",
221
    "rp.AcqSetTriggerLevel(0, threshold)\n",
222
    "rp.AcqSetPostTriggerDelay(size-80)\n",
223
    "\n",
224
    "neve = 0\n",
225
    "while True:\n",
226
    "    rp.AcqStart()\n",
227
    "    rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_NE)  \n",
228
    "    while (rp.AcqSetTriggerSrc(rp.TRIG_SRC_CHA_NE)): pass\n",
229
    "    buff = [rp.AcqGetLatestDataV(ch, size) for ch in channels];\n",
230
    "\n",
231
    "    adcdata = np.absolute(np.amin(buff[0]))\n",
232
    "    signal  = np.array(buff[0])\n",
233
    "    tdcdata = np.where(signal < threshold)[0] # get item\n",
234
    "#    print (adcdata, tdcdata)\n",
235
    "    hfill(hadc,adcdata)\n",
236
    "    \n",
237
    "    for a in tdcdata:\n",
238
    "      if a - tdcdata[0] > 5:\n",
239
    "         hfill(htdc,a-tdcdata[0])\n",
240
    "         break   \n",
241
    "    neve+=1\n",
242
    "    for i in channels:\n",
243
    "        r[i].data_source.data['y'] = buff[i]\n",
244
    "# push updates to the plot continuously using the handle (intererrupt the notebook kernel to stop)\n",
245
    "    if (neve%10 == 0):\n",
246
    "      push_notebook(handle=target)\n",
247
    "#    time.sleep(0.05)"
248
   ]
249
  }
250
 ],
251
 "metadata": {
252
  "kernelspec": {
253
   "display_name": "Python 3",
254
   "language": "python",
255
   "name": "python3"
256
  },
257
  "language_info": {
258
   "codemirror_mode": {
259
    "name": "ipython",
260
    "version": 3
261
   },
262
   "file_extension": ".py",
263
   "mimetype": "text/x-python",
264
   "name": "python",
265
   "nbconvert_exporter": "python",
266
   "pygments_lexer": "ipython3",
267
   "version": "3.5.2"
268
  }
269
 },
270
 "nbformat": 4,
271
 "nbformat_minor": 2
272
}