-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPyStartle.py
More file actions
executable file
·1534 lines (1381 loc) · 67 KB
/
PyStartle.py
File metadata and controls
executable file
·1534 lines (1381 loc) · 67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
"""
Acoustic Startle Program
Python version
This program generates sound in one channel (L), followed by a startle stimulus
(noise burst) in the other channel (R).
The conditioning sound can be one of:
tone pip before the startle
tone with gap before the startle
bandpass noise burst
bandpass noise burst with gap before the startle
Output hardware is either an National Instruments DAC card or a system sound card
If the NI DAC is available, TDT system 3 hardware is assumed as well for the
attenuators (PA5) and an RP2.1 to input the startle response.
Second channel of RP2.1 is collected as well. Use this for a microphone input
to monitor sound in the chamber.
Requires: startle2.rco, generated by RPvds (from TDT)
Python 2.5
PyQt4, Qt Designer (for Gui)
scipy, pylab, numpy, matplotlib
pyaudio
stack is optional
Works with Enthought distribution on Mac OS X and Windows.
November, 2008. Paul B. Manis, Ph.D.
UNC Chapel Hill
Supported by NIH Grant DC000425-22
"""
################################################################################
import sys, re, os
import datetime, time
from time import strftime
import struct, ctypes
from PyQt4 import Qt, QtCore, QtGui
from sets import *
from pylab import *
import scipy, numpy
import scipy.signal
import numpy
from numpy.fft import fft
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
# non-standard stuff:
import stack
import pyaudio
from random import sample
# our gui:
from PyStartle_gui import Ui_MainWindow
################################################################################
# the first thing we must do is find out what hardware is available and what
# system we are on.
################################################################################
print "PyStartle: Checking Hardware and OS"
try:
if os.name is not 'nt':
assert 0 # force use of pyaudio if not on windows xp/nt.
print "OS is Windows (NT or XP)"
# get the drivers and the activeX control (win32com)
from nidaq import NIDAQ as n
import nidaq
import win32com.client
print "Attemtp to Assert num devs > 0:",
assert(len(n.listDevices()) > 0)
print " OK"
print "devices: %s" % n.listDevices()
hwerr = 0
print "getDevice:",
dev0 = n.getDevice('Dev2')
print " ", dev0
print "\nAnalog Channels:",
# print " AI: ", dev0.listAIChannels()
print " AO: ", dev0.listAOChannels() # check output only
# active x connection to attenuators
# note - variables set at this scope level are global to source file
PA5 = win32com.client.Dispatch("PA5.x")
a=PA5.ConnectPA5("USB", 1)
if a > 0:
print "Connected to PA5 Attenuator 1"
else:
print "Failed to connect to PA5 Attenuator 1"
hwerr = 1
PA5.SetAtten(120.0)
a = PA5.ConnectPA5("USB", 2)
if a > 0:
print "Connected to PA5 Attenuator 2"
else:
print "Failed to connect to PA5 Attenuator 2"
hwerr = 1
PA5.SetAtten(120.0)
RP21 = win32com.client.Dispatch("RPco.x") # connect to RP2.1
a = RP21.ConnectRP2("USB", 1)
if a > 0:
print "RP2.1 Connect is good: %d" % (a)
else:
print "Failed to connect to PA5 Attenuator 1"
hwerr = 1
RP21.ClearCOF()
samp_cof_flag = 2 # 2 is for 24.4 kHz
samp_flist = [6103.5256125, 12210.703125, 24414.0625, 48828.125,
97656.25, 195312.5]
if samp_cof_flag > 5:
samp_cof_flag = 5
a = RP21.LoadCOFsf("C:\pyStartle\startle2.rco", samp_cof_flag)
if a > 0:
print "Connected to TDT RP2.1 and startle2.rco is loaded"
else:
print "Error loading startle2.rco?, error = %d" % (a)
hwerr = 1
def_sampleRate = 100000
my_hardware = 'nidaq'
if hwerr == 1:
print "?? Error connecting to hardware"
exit()
except:
print "OS/hardware only supports standard sound card audio"
my_hardware = 'pyaudio'
def_sampleRate = 44100
REF_ES_dB = 86.0; # calibration info - Assumes 10 dB padding with attenuator.
REF_ES_volt = 2.0; # output in volts to get refdb
REF_MAG_dB = 100.0; # right speaker is mag... different scaling.
print "PyStartle is running with output hardware: %s" % (my_hardware)
################################################################################
# One class for the program: PyStartle
################################################################################
class PyStartle(QtGui.QMainWindow):
def __init__(self):
""" In the constructor get the application
started byconstructing a basic QApplication with
its __init__ method, then adding our slot/signal connections
and finally starting
the exec_loop. """""
QtGui.QDialog.__init__(self)
self.AutoSave = True
self.maxptsplot = 10000
self.SF = 44100 # for ni board or audio board output
self.RPSF = 12000
self.ch1 = []
self.ch2 = []
self.response_tb = []# response time base
self.stim_tb = []# stimuluation time base (not implemented yet..)
self.PostDuration = 0.35 # seconds after startle ends to record response
self.PPGo = False
self.PP_Notch_F1 = 12000.0 # set defaults for the notch - not in gui yet
self.PP_Notch_F2 = 14000.0
self.fileDate = ''
self.Description = "Acoustic Startle Parameters"
self.CurrentTab = 0 # set a default current tab - left most entry
self.stack1 = stack.Stack() # init the stack mode used for holding MOUSE event data
self.stack2 = stack.Stack() # init the stack mode used for holding MOUSE event data
# We pass None since it's the top-level widget, we could in fact leave
# that one out, but this way it's easier to add more dialogs or widgets.
self.ui = Ui_MainWindow() # this is the ONE THING
self.ui.setupUi(self)
self.connect(self.ui.QuitButton,QtCore.SIGNAL("clicked()"),
self.slotQuit)
self.connect(self.ui.actionQuit,QtCore.SIGNAL("clicked()"),
self.slotQuit)
self.connect(self.ui.actionOpen,QtCore.SIGNAL("clicked()"),
self.Analysis_Read)
self.connect(self.ui.CloseDataWindows,QtCore.SIGNAL("clicked()"),
self.slotCloseDataWindows)
self.connect(self.ui.ToneTest,QtCore.SIGNAL("clicked()"),
self.ToneTest)
self.connect(self.ui.NoiseTest,QtCore.SIGNAL("clicked()"),
self.NoiseTest)
self.connect(self.ui.PrePulse_Run,QtCore.SIGNAL("clicked()"),
self.PrePulseStart)
self.connect(self.ui.PrePulse_Stop,QtCore.SIGNAL("clicked()"),
self.PrePulseStop)
self.connect(self.ui.Save_Params,QtCore.SIGNAL("clicked()"),
self.writeini)
self.connect(self.ui.Load_Params,QtCore.SIGNAL("clicked()"), self.readini)
self.connect(self.ui.Write_Data,QtCore.SIGNAL("clicked()"),
self.Write_Data)
self.connect(self.ui.Analysis_Read,QtCore.SIGNAL("clicked()"),
self.Analysis_Read)
self.connect(self.ui.Analysis_Test,QtCore.SIGNAL("clicked()"),
self.Analysis_Test)
self.connect(self.ui.Analysis_Analyze,QtCore.SIGNAL("clicked()"),
self.Analyze_Data)
self.TrialTimer=QtCore.QTimer() # get a Q timer
self.connect(self.TrialTimer, QtCore.SIGNAL("timeout()"), self.NextTrial);
# timer calls NextTrial when timed out
self.readini("pystartle.ini") # read the initialization file if it is there.
self.setMainWindow('default')
self.statusBar().showMessage("No File" )
self.Status('Welcome to PyStartle V0.8')
################################################################################
# utility routines for Gui:
# close the windows and exit
#
def slotQuit(self):
try:
if my_hardware == 'nidaq':
RP21.Halt() # make sure the RP21 is stopped.
finally:
pass
self.slotCloseDataWindows() # should close the matplotlib windows...
QtCore.QCoreApplication.quit()
#
# just close the data plot windows (matplotlib windows)
#
def slotCloseDataWindows(self):
for i in range(1,5):
try:
plt.close(i)
except AttributeError:
pass
def getCurrentTab(self):
self.CurrentTab = self.ui.AcquisitionTabs.currentIndex()
return(self.CurrentTab)
def setCurrentTab(self, tab = 0):
self.ui.AcquisitionTabs.setCurrentIndex(tab)
# update status window
#
#
def Status(self, text, clear = 0):
self.ui.Status_Window.insertItem(0, '[' +
datetime.datetime.now().ctime() + '] ' + text)
item = self.ui.Status_Window.item(0) # get top item object
self.ui.Status_Window.setCurrentItem(item)
self.ui.Status_Window.update() # force an update with every line
def setMainWindow(self, text):
self.setWindowTitle("PyStartle [%s]" % (text))
# figure title for matplotlib window...
def putTitle(self, infotext):
pa, fname = os.path.split(self.fileName)
titletext = 'File: %s R:[' % (fname)
for i in self.reclist:
titletext = titletext + '%d ' % (i)
titletext = titletext + '] B:[ '
for i in self.blocklist:
titletext = titletext + '%d ' % (i)
titletext = titletext + '] ' + infotext
gcf().text(0.5, 0.95, titletext, horizontalalignment='center',
fontproperties=FontProperties(size=12))
#
# Handle mouse events in matlab windows.
# onclick1 does "figure1" events - the raw data traces
# onclick2 does "figure2" events.- the analyzed data
def onclick1(self, event):
self.ui.lcdXNumber.display(event.xdata)
self.ui.lcdYNumber.display(event.ydata)
# print event.xdata
self.stack1.push((event.xdata, event.ydata))
self.Status( "stack1: %d items (%8.3f %8.3f)" % (self.stack1.num_items(),
event.xdata, event.ydata) )
# print '1: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata)
def onclick2(self, event):
self.ui.lcdXNumber.display(event.xdata)
self.ui.lcdYNumber.display(event.ydata)
self.stack2.push((event.xdata, event.ydata))
self.Status( "stack2: %d items" % (self.stack2.num_items()) )
# print '2: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata)
################################################################################
################################################################################
# Read the gui data into our local parameters
################################################################################
def readParameters(self):
#
self.AutoSave = self.ui.AutoSave.isChecked()
# from the Levels and Durations tab:
self.CN_Level = self.ui.Condition_Level.value()
self.CN_Dur = self.ui.Condition_Dur.value()
self.CN_Var = self.ui.Condition_Var.value()
self.PP_Level = self.ui.PrePulse_Level.value()
self.PP_OffLevel = self.ui.PrePulse_Off_Level.value()
self.PP_Dur = self.ui.PrePulse_Dur.value()
self.PS_Dur = self.ui.PreStartle_Dur.value()
self.ST_Dur = self.ui.Startle_Dur.value()
self.ST_Level = self.ui.Startle_Level.value()
self.StimEnable = self.ui.Stimulus_Enable.isChecked()
self.WavePlot = self.ui.Waveform_PlotFlag.isChecked()
self.ShowSpectrum = self.ui.OnlineSpectrum_Flag.isChecked()
self.OnLineAnalysis = self.ui.OnlineAnalysis_Flag.isChecked()
# from the Waveforms tab:
self.PP_Freq = self.ui.PrePulse_Freq.value()
self.PP_HP = self.ui.PrePulse_HP.value()
self.PP_LP = self.ui.PrePulse_LP.value()
self.PP_Mode = self.ui.Waveform_PrePulse.currentIndex()
self.CN_Mode = self.ui.Waveform_Conditioning.currentIndex()
self.PP_GapFlag = self.ui.PrePulse_GapFlag.isChecked()
self.PP_Notch_F1 = self.ui.PrePulse_Notch_F1.value()
self.PP_Notch_F2 = self.ui.PrePulse_Notch_F2.value()
self.PP_MultiFreq = str(self.ui.PrePulse_MultiFreq.text())
# from the Timing and Trials tab:
self.ITI_Var = self.ui.PrePulse_ITI_Var.value()
self.ITI = self.ui.PrePulse_ITI.value()
self.Trials = self.ui.PrePulse_Trials.value()
self.NHabTrials = self.ui.PrePulse_NHabTrials.value()
# from the analysis tab:
self.readAnalysisTab()
def readAnalysisTab(self): # we call this elsewhere, - define for convenience
self.Analysis_Start = self.ui.Analysis_Start.value()
self.Analysis_End = self.ui.Analysis_Duration.value()
self.Analysis_HPF = self.ui.Analysis_HPF.value()
self.Analysis_LPF = self.ui.Analysis_LPF.value()
def ToneTest(self):
self.readParameters()
w = self.StimulusMaker(mode='tone', freq = (self.PP_Freq, 0),
duration = self.PP_Dur, plotSignal = True)
def NoiseTest(self):
self.readParameters()
w = self.StimulusMaker(mode = 'bpnoise', freq=(self.PP_HP, self.PP_LP),
duration=self.PP_Dur, plotSignal = True)
################################################################################
#
# PrePulseRun controls the stimulus presentation and timing.
# It is the main event loop during stimulation/acquisition.
#
# note : we use QTimer for the timing. One instance is generated with the
# main init routine above. We then start this and run it as a separate thread
# Allows gui interaction during data acquisition/stimulation and ability to
# stop the presentation cleanly.
################################################################################
def PrePulseStart(self):
if self.PPGo:
print "already running"
return;
self.Status ("Starting Run")
#
# open and build the file
#
dt = strftime('%Y%m%d%H%M')
self.fn = dt + "_Startle.txt"
self.readParameters() # get the parameters for stimulation
self.TrialCounter = 0
itil = self.ITI + self.ITI_Var*(rand(1, self.Trials+self.NHabTrials)-0.5)
self.ITI_List = itil.reshape(max(shape(itil)))
stimd = self.CN_Dur + self.CN_Var*(rand(1, self.Trials+self.NHabTrials)-0.5)
self.Dur_List = stimd.reshape(max(shape(stimd)))
self.Gap_List = int(self.Trials+self.NHabTrials)*[False]
list = int(self.Trials/2)*[False, True]
s=sample(list, int(self.Trials))
self.Gap_List[int(self.NHabTrials):] = s
if self.AutoSave:
self.writeDataFileHeader(self.fn) # wait to write header until we have all the values.
self.Gap_StartleMagnitude = zeros(self.Trials)
self.Gap_Counter = 0
self.noGap_StartleMagnitude = zeros(self.Trials)
self.noGap_Counter = 0
self.PPGo = True
self.TrialTimer.setSingleShot(True)
self.TrialTimer.start(10) # start right away
# catch the stop button press
def PrePulseStop(self):
self.setAttens() # attenuators down
self.HwOff() # turn hardware off
self.PPGo=False # signal the prepulse while loop that we are stopping
self.statusBar().showMessage("Stimulus/Acquisition Events stopped")
# callback routine to stop timer when thread times out.
def NextTrial(self):
self.TrialTimer.stop()
if self.TrialCounter <= self.Trials and self.PPGo:
self.statusBar().showMessage("Rep: %d of %d" % (self.TrialCounter+1,
self.Trials+self.NHabTrials))
DoneTime = self.ITI_List[self.TrialCounter] # get this before we start stimulus so stim time is included
self.TrialTimer.start(int(1000*DoneTime))
self.Stim_Dur = self.Dur_List[self.TrialCounter] # randomize the durations a bit too
self.runOnePP()
if self.WavePlot == True:
self.plotSignal(self.wave_outL, self.wave_outR,
float(1000.0)/float(def_sampleRate))
self.Status('sent signal')
if self.AutoSave:
self.AppendData(self.fn)
self.Status('appended data')
self.TrialCounter = self.TrialCounter + 1
else:
self.PPGo = False
self.statusBar().showMessage("Test Complete")
################################################################################
# runOnePP - "run one prepulse" trial.
# Generate one stimulus set based on the choice. Builds both channels.
# Presents the stimuli if the flag is set.
################################################################################
def runOnePP(self):
# print "runOnePP at elapsed time = %9.3f" % time.time()
# the modes parse as follows (same modes apply for CN/PS, and for PP)
# 0 is silence
# 1 is tone
# 2 is bandpass noise
# 3 is notch noise (not implemented yet)
# 4 is multi tones (not implemented yet)
# 5 is AM tones (not implemented yet)
# 6 is AM Noise (not implemented yet)
#
# The conditioning (CN) and the prepulse (PP) can be any of the above
# the pre-startle (post prepulse) is always the same as the conditioning.
# The conditioning stimulus always runs the whole duration (including through the end of the startle)
# If the conditioning stimulus is not the same as the prepulse, then the conditiioning
# will be interrupted by a gap during the prepulse period, and the prepulse will be calculated,
# shaped, and added during the prepulse period.
#
if self.CN_Mode == 0:
cnmode = 'silence'
cnfreq = (self.PP_Freq, 0) # anything will do
if self.CN_Mode == 1 or self.CN_Mode == 4 or self.CN_Mode == 5:
cnmode = 'tone'
cnfreq = (self.PP_Freq, 0)
if self.CN_Mode == 2 or self.CN_Mode == 6:
cnmode = 'bpnoise'
cnfreq = (self.PP_HP, self.PP_LP)
if self.CN_Mode == 3:
cnmode = 'notchnoise' # Note: notch is embedded into a bandpass noise
cnfreq = (self.PP_HP, self.PP_LP, self.PP_Notch_F1, self.PP_Notch_F2)
# generat the conditioning stimulus and the post-prepulse stimulus
print cnmode
self.wave_outL = self.StimulusMaker(mode = cnmode, duration = (self.Stim_Dur+self.PP_Dur+self.PS_Dur+self.ST_Dur),
freq = cnfreq, samplefreq = def_sampleRate, delay=0, level = self.CN_Level)
# now tailor the conditioning stimulus
# this is regulated by the current Gap_List value
w_pp = [] # default with no prepulse
if self.Gap_List[self.TrialCounter]: # only make a prepulse if it is set
if self.PP_Mode == 0 or self.PP_GapFlag: # insert a gap
self.wave_outL = self.insertGap(self.wave_outL, delay = self.Stim_Dur,
duration = self.PP_Dur, samplefreq = def_sampleRate) # inserts the gap
if self.PP_Mode == 1 or self.PP_Mode ==4 or self.PP_Mode == 5: # now insert a tone
w_pp = self.StimulusMaker(mode = 'tone', duration = self.PP_Dur, freq = (self.PP_Freq, 0),
delay=self.Stim_Dur, samplefreq = def_sampleRate, level = self.PP_Level)
w_pp = append(w_pp, numpy.zeros(len(self.wave_outL)-len(w_pp))) # pad
if self.PP_Mode == 2 or self.PP_Mode == 6: # 2 is bandpass noise
w_pp = self.StimulusMaker(mode = 'bpnoise', duration = self.PP_Dur, freq = (self.PP_HP, self.PP_LP),
delay=self.Stim_Dur, samplefreq = def_sampleRate, level = self.PP_Level)
w_pp = append(w_pp, numpy.zeros(len(self.wave_outL)-len(w_pp))) # pad
if self.PP_Mode == 3: # 3 Notched noise
w_pp = self.StimulusMaker(mode = 'notchnoise', duration = self.Stim_Dur,
freq = (self.PP_HP, self.PP_LP, self.Notch_F1, self.Notch_F2),
samplefreq = def_sampleRate, delay=self.Stim_Dur,
level = self.PP_Level)
w_pp = append(w_pp, numpy.zeros(len(self.wave_outL)-len(w_pp))) # pad
if len(w_pp) > 0:
self.wave_outL = self.wave_outL + w_pp
# generate the startle sound. Note that it overlaps the end of the conditioning sound...
self.wave_outR = self.StimulusMaker(mode = 'bpnoise', delay = (self.Stim_Dur+self.PP_Dur+self.PS_Dur),
duration = self.ST_Dur, samplefreq=def_sampleRate,
freq = (1000.0, 32000.0), level = self.ST_Level,
channel = 1)
lenL = len(self.wave_outL)
lenR = len(self.wave_outR)
if lenR > lenL:
self.wave_outL = append(self.wave_outL, numpy.zeros(lenR-lenL))
if lenL > lenR:
self.wave_outR = append(self.wave_outR, numpy.zeros(lenL-lenR))
if self.StimEnable == True:
self.playSound(self.wave_outL, self.wave_outR, def_sampleRate)
################################################################################
# STIMULUS GENERATION ROUTINES
#
# transcribed from Matlab. P. Manis, Nov. 28-December 1 2008.
################################################################################
def StimulusMaker(self, mode = 'tone', amp = 1, freq = (1000, 3000, 4000), delay = 0, duration = 2000,
rf = 2.5, phase0 = 0, samplefreq = 44100, ipi = 20, np = 1, alternate = 1, level = 70,
playSignal = False, plotSignal= False, channel = 0):
# generate a tsound (tone, bb noise, bpnoise) pip with amplitude (V), frequency (Hz) (or frequencies, using a tuple)
# delay (msec), duration (msec).
# if no rf (risefall) time is given, cosine^2 shaping with 5 msec ramp duration is applied.
# if no phase is given, phase starts on 0, with positive slope.
# level is in dB SPL as given by the reference calibration data above...
#
clock = 1000.0/samplefreq # calculate the sample clock rate - msec (khz)
uclock = 1000.*clock # microsecond clock
phi = 2*pi*phase0/360.0 # convert phase from degrees to radians...
Fs = 1000/clock
phi = 0 # actually, always 0 phase for start
w = []
fil = self.rfShape(0, duration, clock, rf) # make the shape filter with 0 delay
jd = int(floor(delay/clock)) # beginning of signal buildup (delay time)
if jd < 0:
jd = 0
jpts = arange(0,len(fil))
signal = numpy.zeros(len(jpts))
siglen = len(signal)
if mode =='tone':
for i in range(0, len(freq)):
signal = signal + fil*amp*sin(2*pi*freq[i]*jpts/Fs)
self.Status("Generated Tone at %7.1fHz" % (freq[i]))
if mode == 'bbnoise':
signal = signal + fil*amp*normal(0,1,siglen)
self.Status("BroadBand Noise " )
if mode == 'bpnoise':
tsignal = fil*amp*normal(0,1,siglen)
# use freq[0] and freq[1] to set bandpass on the noise
# print "freqs: HP: %6.1f LP: %6.1f" % (freq[0], freq[1])
wp = [float(freq[0])/samplefreq*2, float(freq[1])/samplefreq*2]
ws = [0.75*float(freq[0])/samplefreq*2, 1.25*float(freq[1])/samplefreq*2]
filter_b,filter_a=scipy.signal.iirdesign(wp, ws,
gpass=1.0,
gstop=60.0,
ftype="ellip")
self.Status("BandPass Noise %7.1f-%7.1f" % (freq[0], freq[1]))
signal=scipy.signal.lfilter(filter_b, filter_a, tsignal)
if mode == 'notchnoise':
return array(signal)
if mode == 'multitones':
return array(signal)
if mode == 'silence':
return array(signal)
# now build the waveform from the components
w = numpy.zeros(ceil(ipi*(np-1)/clock)+jd+siglen)
sign = numpy.ones(np)
if alternate == True:
sign[range(1,np,2)] = -1
id = int(floor(ipi/clock))
for i in range(0, np): # for each pulse in the waveform
j0 = jd + i*id # compute start time
w[range(j0,j0+siglen)] = sign[i]*signal
w = w*self.dbconvert(spl = level, chan = channel) # aftera all the shaping ane scaling, we convert to generate a signal of w dB
if playSignal == True:
self.playSound(w, w, samplefreq)
if plotSignal == True:
self.plotSignal(w, w, clock)
return array(w)
#
# Rise-fall shaping of a waveform. This routine generates an envelope with
# 1 as the signal max, and 0 as the baseline (off), with cosine^2 shaping of
# duration rf starting at delay (msec). The duration of the signal includes the
# rise and fall, so the duration of the signal at full amplitude is dur - 2*rf.
#
def rfShape(self, delay=0, duration=100, clock=44100, rf=2.5):
jd = int(floor(delay/clock)) # beginning of signal buildup (delay time)
if jd < 0:
jd = 0
je = int(floor((delay+duration)/clock)) # end of signal decay (duration + delay)
#
# build sin^2 filter from 0 to 90deg for shaping the waveform
#
nf = int(floor(rf/clock)) # number of points in the filter
fo = 1.0/(4.0*rf) # filter "frequency" in kHz - the 4 is because we use only 90deg for the rf component
pts = arange(jd,jd+nf)
fil = numpy.zeros(je)
fil[range(jd,jd+nf)] = sin(2*pi*fo*pts*clock)**2 # filter
fil[range(jd+nf,je-nf)] = 1
pts = range(je-nf,je)
kpts = range(jd+nf,jd,-1)
fil[pts] = fil[kpts]
return(fil)
#
# insertGap takes a waveform and inserts a shaped gap into it.
# currently, gap is all the way off, i.e., 0 intensity.
# a future change is to include relative gap level (-dB from current waveform)
#
def insertGap(self, wave, delay = 20, duration = 20, rf = 2.5, samplefreq = def_sampleRate):
clock = 1000.0/samplefreq # calculate the sample clock rate - msec (khz)
fil = self.rfShape(delay, duration, clock, rf) # make the shape filter with 0 delay
lenf = len(fil)
lenw = len(wave)
if lenw > lenf:
fil = append(fil, numpy.zeros(lenw-lenf))
if lenf > lenw:
fil = append(fil, numpy.zeros(lenf-lenw))
return(wave*(1.0-fil))
#
# compute voltage from reference dB level
# db = 20 * log10 (Vsignal/Vref)
#
def dbconvert(self, spl = 0, chan = 0):
ref = REF_ES_dB
if chan == 1:
ref = REF_MAG_dB
zeroref = REF_ES_volt/(10**(ref/20.0));
sf = zeroref*10**(spl/20.0); # actually, the voltage needed to get spl out...
print "scale = %f for %f dB" % (sf, spl)
return (sf) # return a scale factor to multiply by a waveform normalized to 1
################################################################################
# hardware interactions:
#
# set the attenuators on the PA5.
# If no args are given, set to max attenuation
def setAttens(self, attenl = 120, attenr = 120):
if my_hardware == 'nidaq':
PA5.ConnectPA5("USB", 1)
PA5.SetAtten(attenl)
PA5.ConnectPA5("USB", 2)
PA5.SetAtten(attenr)
#
# playSound sends the sound out to an audio device. In the absence of NI card
# and TDT system, it will use the system audio device (sound card, etc)
# The waveform is played in stereo.
#
def playSound(self, wavel, waver, samplefreq = 44100):
if my_hardware == 'pyaudio':
self.audio = pyaudio.PyAudio()
chunk = 1024
FORMAT = pyaudio.paFloat32
CHANNELS = 2
RATE = samplefreq
self.RPSF = samplefreq
self.SF = self.RPSF
self.stream = self.audio.open(format = FORMAT,
channels = CHANNELS,
rate = int(RATE),
output = True,
input = True,
frames_per_buffer = chunk)
# play stream
wave = zeros(2*len(wavel))
if len(wavel) != len(waver):
print "waves not matched in length: %d vs. %d (L,R)" % (len(wavel), len(waver))
return
(waver, clipr) = self.clip(waver, 20.0)
(wavel, clipl) = self.clip(wavel, 20.0)
wave[0::2] = waver
wave[1::2] = wavel # order chosen so matches entymotic earphones on my macbookpro.
postdur = int(float(self.PostDuration*self.SF))
rwave = self.read_array(len(wavel)+postdur, CHANNELS)
self.write_array(wave)
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
self.ch1 = rwave[0::2]
self.ch2 = rwave[1::2]
if my_hardware == 'nidaq':
self.task = dev0.createTask() # creat a task for the NI 6731 board.
self.task.CreateAOVoltageChan("/Dev2/ao0", "ao0", -10., 10.,
nidaq.Val_Volts, None)
self.task.CreateAOVoltageChan("/Dev2/ao1", "ao1", -10., 10.,
nidaq.Val_Volts, None) # use 2 channels
wlen = 2*len(wavel)
self.task.CfgSampClkTiming(None, samplefreq, nidaq.Val_Rising,
nidaq.Val_FiniteSamps, len(wavel))
# DAQmxCfgDigEdgeStartTrig (taskHandle, "PFI0", DAQmx_Val_Rising);
self.task.SetStartTrigType(nidaq.Val_DigEdge)
self.task.CfgDigEdgeStartTrig('PFI0', nidaq.Val_Rising)
daqwave = numpy.zeros(wlen)
(wavel, clipl) = self.clip(wavel, 10.0)
(waver, clipr) = self.clip(waver, 10.0)
daqwave[0:len(wavel)] = wavel
daqwave[len(wavel):] = waver # concatenate channels (using "groupbychannel" in writeanalogf64)
dur = wlen/float(samplefreq)
self.task.write(daqwave)
# now take in some acquisition...
a = RP21.ClearCOF()
if a <= 0:
print "problem with RP21"
return
samp_cof_flag = 2 # 2 is for 24.4 kHz
samp_flist = [6103.5256125, 12210.703125, 24414.0625, 48828.125,
97656.25, 195312.5]
if samp_cof_flag > 5:
samp_cof_flag = 5
a = RP21.LoadCOFsf("C:\pyStartle\startle2.rco", samp_cof_flag)
if a > 0:
print "Connected to TDT RP2.1 and startle2.rco is loaded"
else:
print "Error loading startle2.rco?, error = %d" % (a)
hwerr = 1
return
self.RPSF = RP21.GetSFreq()
self.SF = self.RPSF
Ndata = ceil(0.5*(dur+self.PostDuration)*self.RPSF)
RP21.SetTagVal('REC_Size', Ndata) # old version using serbuf -- with
# new version using SerialBuf, can't set data size - it is fixed.
# however, old version could not read the data size tag value, so
# could not determine when buffer was full/acquisition was done.
self.setAttens(10.0,10.0) # set equal, but not at minimum...
self.task.start() # start the NI AO task
a=RP21.Run() # start the RP2.1 processor...
a=RP21.SoftTrg(1) # and trigger it. RP2.1 will in turn start the ni card
while not self.task.isTaskDone(): # wait for AO to finish?
if not self.PPGo: # while waiting, check for stop.
RP21.Halt()
self.task.stop()
return
self.task.stop() # done, so stop the output.
self.setAttens() # attenuators down (there is noise otherwise)
# read the data...
curindex1=RP21.GetTagVal('Index1')
curindex2=RP21.GetTagVal('Index2')
while(curindex1 < Ndata or curindex2 < Ndata): # wait for input data to be sampled
if not self.PPGo: # while waiting, check for stop.
RP21.Halt()
return
curindex1=RP21.GetTagVal('Index1')
curindex2=RP21.GetTagVal('Index2')
self.task.stop()
self.ch2=RP21.ReadTagV('Data_out2', 0, Ndata)
# ch2 = ch2 - mean(ch2[1:int(Ndata/20)]) # baseline: first 5% of trace
self.ch1=RP21.ReadTagV('Data_out1', 0, Ndata)
RP21.Halt()
def HwOff(self): # turn the hardware off if you can.
if my_hardware == 'pyaudio':
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
if my_hardware == 'nidaq':
self.task.stop()
self.setAttens()
RP21.Halt()
# clip data to max value (+/-) to avoid problems with daqs
def clip(self, data, maxval):
clip = 0
u = where(data >= maxval)
ul = list(transpose(u).flat)
if len(ul) > 0:
data[ul] = maxval
clip = 1 # set a flag in case we want to know
v = where(data <= (-maxval))
vl = list(transpose(u).flat)
if len(vl) > 0:
data[vl] = -maxval
clip = 1
return (data, clip)
# print 'Data acquired ok, %d points' % (curindex1)
################################################################################
# the following was taken from #http://hlzr.net/docs/pyaudio.html
# it is used for reading and writing to the system audio devie
#
def write_array(self, data):
"""
Outputs a numpy array to the audio port, using PyAudio.
"""
# Make Buffer
buffer_size = struct.calcsize('@f') * len(data)
output_buffer = ctypes.create_string_buffer(buffer_size)
# Fill Up Buffer
#struct needs @fffff, one f for each float
format = '@' + 'f'*len(data)
struct.pack_into(format, output_buffer, 0, *data)
# Shove contents of buffer out audio port
self.stream.write(output_buffer)
def read_array(self, size, channels=1):
input_str_buffer = self.stream.read(size)
input_float_buffer = struct.unpack('@' + 'f'*size*channels, input_str_buffer)
return numpy.array(input_float_buffer)
################################################################################
#
# plot the signal and it's power spectrum
#
def plotSignal(self, wL, wR, clock):
npts = len(wL)
t = clock*arange(0,npts)/1000
self.datafig = plt.figure(1)
skip = int(npts/self.maxptsplot)
if skip < 1:
skip = 1
plt.clf()
plt.axes([0.07, 0.7, 0.55, 0.27]) # top subplot has stimulus waveforms.
plt.plot(t[0::skip], wL[0::skip], 'b-')
plt.hold(True)
plt.plot(t[0::skip], wR[0::skip], 'r-')
# spectrum of signal
if self.ShowSpectrum:
plt.axes([0.7, 0.7, 0.27, 0.27]) # top right subplot has power spectrum of Left signal
(spectrum, freqAzero) = self.pSpectrum(wL, clock)
plt.plot(freqAzero, 1000*spectrum)
# response
plt.axes([0.07, 0.37, 0.55, 0.12])
ds = shape(self.ch1)
self.response_tb=float(1.0/self.SF)*arange(0,len(self.ch1))
plt.plot(self.response_tb[0::skip], self.ch1[0::skip], 'g-')
plt.axes([0.07, 0.49, 0.55, 0.12])
plt.plot(self.response_tb[0::skip], self.ch2[0::skip], 'r-')
# spectrum of the response
self.SpecAxes = plt.axes([0.7, 0.37, 0.27, 0.27])
self.SignalAxes = plt.axes([0.07, 0.05, 0.4, 0.27])
self.ResponseAxes = plt.axes([0.57, 0.05, 0.4, 0.27])
tdelay = self.Stim_Dur + self.PP_Dur + self.PS_Dur
# analyze the response signal
self.Response_Analysis(timebase= self.response_tb, signal = self.ch1,
rate = self.RPSF, delay=tdelay, SpecAxes = self.SpecAxes,
SignalAxes = self.SignalAxes,
ResponseAxes = self.ResponseAxes,
ntrials = self.Trials,
trialcounter = self.TrialCounter,
gaplist = self.Gap_List)
if self.TrialCounter > 0:
plt.figtext(0.82, 0.20, "Rd: %7.3f" % (dprime))
self.ui.Rd_Dial.setValue(int(100*dprime))
plt.show()
plt.draw()
def getSelectionIndices(self, x, xstart, xend):
astart = where(x >= xstart)
aend = where (x <= xend)
s0 = Set(transpose(astart).flat)
s1 = Set(transpose(aend).flat)
xpts = list(s1.intersection(s0))
return (xpts)
# compute the power spectrum.
# simple, no windowing etc...
def pSpectrum(self, data, clock):
npts = len(data)
# we should window the data here
padw = append(data, zeros(npts))
npts = len(padw)
spfft = fft(padw)
nUniquePts = ceil((npts+1)/2.0)
spfft = spfft[0:nUniquePts]
spectrum = abs(spfft)
spectrum = spectrum / float(npts) # scale by the number of points so that
# the magnitude does not depend on the length
# of the signal or on its sampling frequency
spectrum = spectrum**2 # square it to get the power
spmax = amax(spectrum)
spectrum = spectrum + 1e-12*spmax
# multiply by two (see technical document for details)
# odd nfft excludes Nyquist point
if npts % 2 > 0: # we've got odd number of points fft
spectrum[1:len(spectrum)] = spectrum[1:len(spectrum)] * 2
else:
spectrum[1:len(spectrum) -1] = spectrum[1:len(spectrum) - 1] * 2 # we've got even number of points fft
freqAzero = arange(0, nUniquePts, 1.0) * ((1/clock) / npts)
# print "min spec: %f\n" % (amin(spectrum))
return(spectrum, freqAzero)
# filter signal with elliptical filter
def SignalFilter(self, signal, LPF, HPF, samplefreq):
# print "sfreq: %f LPF: %f HPF: %f" % (samplefreq, LPF, HPF)
flpf = float(LPF)
fhpf = float(HPF)
sf = float(samplefreq)
sf2 = sf/2
wp = [fhpf/sf2, flpf/sf2]
ws = [0.5*fhpf/sf2, 2*flpf/sf2]
print "signalfilter: samplef: %f wp: %f, %f ws: %f, %f lpf: %f hpf: %f" % (
sf, wp[0], wp[1], ws[0], ws[1], flpf, fhpf)
filter_b,filter_a=scipy.signal.iirdesign(wp, ws,
gpass=1.0,
gstop=60.0,
ftype="ellip")
w=scipy.signal.lfilter(filter_b, filter_a, signal) # filter the incoming signal
# print "sig: %f-%f w: %f-%f" % (min(signal), max(signal), min(w), max(w))
return(w)
def Write_Data(self):
self.writeDataFileHeader('test.dat')
def writeDataFileHeader(self, filename):
# make a dictionary of all the parameters
filedict = {}
filedict_gap = {}
filedict_iti = {}
filedict['CN_Level'] = self.CN_Level
filedict['CN_Dur'] = self.CN_Dur
filedict['CN_Var'] = self.CN_Var
filedict['PP_Level'] = self.PP_Level
filedict['PP_OffLevel'] = self.PP_OffLevel
filedict['PP_Dur'] = self.PP_Dur
filedict['PS_Dur'] = self.PS_Dur
filedict['ST_Dur'] = self.ST_Dur
filedict['ST_Level'] = self.ST_Level
filedict['StimEnable'] = self.StimEnable
filedict['WavePlot'] = self.WavePlot
filedict_gap['GapList'] = self.Gap_List # save the sequencing information
# from the Waveforms tab:
filedict['PP_Freq'] = self.PP_Freq
filedict['PP_HP'] = self.PP_HP
filedict['PP_LP'] = self.PP_LP
filedict['PP_Mode'] = self.PP_Mode
filedict['CN_Mode'] = self.CN_Mode
filedict['PP_Notch_F1'] = self.PP_Notch_F1
filedict['PP_Notch_F2'] = self.PP_Notch_F2
filedict['PP_MultiFreq'] = self.PP_MultiFreq
filedict['PP_GapFlag'] = self.PP_GapFlag
# from the Timing and Trials tab:
filedict['ITI_Var'] = self.ITI_Var
filedict['ITI'] = self.ITI
filedict['Trials'] = self.Trials
filedict['NHabTrials'] = self.NHabTrials
# analysis parameters
filedict['Analysis_Start'] = self.Analysis_Start
filedict['Analysis_End'] = self.Analysis_End
filedict['Analysis_HPF'] = self.Analysis_HPF
filedict['Analysis_LPF'] = self.Analysis_LPF
print "Writing File: %s" % (filename)
hdat = open(filename, 'w')
hdat.write("%s \n" % (filedict))
hdat.write("%s \n" % (filedict_gap)) # write in separate lines
hdat.close()
def AppendData(self, filename):
hdat = open(filename, 'a')
datainfo = {}
datainfo['Points'] = len(self.response_tb)
datainfo['SampleRate'] = self.SF
datainfo['GapMode'] = self.Gap_List[self.TrialCounter]
datainfo['ITI'] = self.ITI_List[self.TrialCounter]
datainfo['CNDur'] = self.Dur_List[self.TrialCounter]
hdat.write("%s \n" % (datainfo))
for i in range(0, len(self.response_tb)):
hdat.write("%f %f %f\n" % (self.response_tb[i], 1000*self.ch1[i],