LockIn7280 Ruido

De Bajas Temperaturas
Ir a la navegaciónIr a la búsqueda
 

#!/usr/bin/env python
########################################################################
########################################################################
#####Programa para adquirir el espectro 
#####de ruido con el Lock-In SIGNAL RICOVERY 7280 
###MIDE RUIDO TOTAL.
###LOCK-IN: NOISE MEASUREMENT: OFF
########################################################Imports de Python
########################################################Imports de Python
import serial 
import time 
import sys
import os
import Gnuplot
import numpy
from math import *
###########################################################PARAMETROS DE MEDICION
###########################################################PARAMETROS DE MEDICION
#-------------FRECUENCIA
freq_start = 0.1    #(Hz)
freq_end = 100E3      #(Hz)
freq_pts = 300     #puntos total
tc_prm = 100E-3     #time constant
nmeds = 5 #Meds / punto + sort + prom
filenm="ruido_#.dat"
#---------------------------
###########################################################FUNCIONES PYTHON LOCK IN
###########################################################FUNCIONES PYTHON LOCK IN
############# Escribe TC 5S, Fc = 0.032 Hz
def TC5S():
	global tc
	tc = 5
	print 'TC 5s'
	s.write('TC20r\n')
	time.sleep(0.1)
############# Escribe TC 2S, Fc = 0.08 Hz
def TC2S():
	global tc
	tc = 2
	print 'TC 2s'
	s.write('TC19\r\n')
	time.sleep(0.1)
############# Escribe TC 1S, Fc = 0.16 Hz
def TC1S():
	global tc
	tc=1
	print 'TC 1s'
	s.write('TC18\r\n')
	time.sleep(0.1)
############# Escribe TC 500ms , Fc = 0.32 Hz
def TC500MS():
	global tc
	tc=0.5
	print 'TC 500ms'
	s.write('TC17\r\n')
	time.sleep(0.1)
############# Escribe TC 200ms , Fc = 0.8 Hz
def TC200MS():
  	global tc
	tc=0.2
	print 'TC 200ms'
	s.write('TC16\r\n')
	time.sleep(0.1)
############# Escribe TC 100ms , Fc = 1.6 Hz
def TC100MS():
  	global tc
	tc=0.1
	print 'TC 100ms'
	s.write('TC15\r\n')
	time.sleep(0.1)
############# Escribe TC 50ms , Fc = 3.2 Hz
def TC50MS():
  	global tc
	tc=0.05
	print 'TC 50ms'
	s.write('TC14\r\n')
	time.sleep(0.1)
############# Escribe TC 20ms , Fc = 8 Hz
def TC20MS():
  	global tc
	tc=0.02
	print 'TC 20ms'
	s.write('TC13\r\n')
	time.sleep(0.1)
############# Escribe TC 10ms , Fc = 16 Hz
def TC10MS():
	global tc
	tc=0.01
	print 'TC 10ms'
	s.write('TC12\r\n')
	time.sleep(0.1)
############# Escribe TC 5ms , Fc = 32 Hz
def TC5MS():
	global tc
	tc=5E-3
	print 'TC 4ms'
	s.write('TC11\r\n')    
	time.sleep(0.1)
############# Escribe TC 2ms , Fc = 80 Hz
def TC2MS():
	global tc
	tc=2E-3
	print 'TC 2ms'
	s.write('TC10\r\n')  
	time.sleep(0.1)
############# Escribe TC 1ms, Fc = 160 Hz
def TC1MS():
	global tc
	tc=1E-3
	print 'TC 1ms'
	s.write('TC9\r\n')
	time.sleep(0.1)
############# Escribe TC 500uS, Fc = 320 Hz
def TC500US():
	global tc
	tc=500E-6
	print 'TC 500us'
	s.write('TC8\r\n')
	time.sleep(0.1)
############# Escribe TC 200uS, Fc = 800 Hz
def TC200US():
  	global tc
	tc=200E-6
	print 'TC 200us'
	s.write('TC7\r\n')  
	time.sleep(0.1)
############# Escribe TC 100uS, Fc = 1600 Hz
def TC100US():
  	global tc
	tc=100E-6
	print 'TC 100us'
	s.write('TC6\r\n')
	time.sleep(0.1)
############# Escribe TC 50uS, Fc = 3200 Hz
def TC50US():
  	global tc
	tc=50E-6
	print 'TC 50us'
	s.write('TC5\r\n')
	time.sleep(0.1)
############# Escribe TC 20uS, Fc = 8000 Hz
def TC20US():
  	global tc
	tc=20E-6
	print 'TC 20us'
	s.write('TC4\r\n')
	time.sleep(0.1)
############# Escribe TC 10uS, Fc = 16000 Hz
def TC10US():
  	global tc
	tc=10E-6
	print 'TC 10us'
	s.write('TC3\r\n')
	time.sleep(0.1)
###################### Set TC con freq
###########If para setear el TC correcto
def SET_TC(tcl):
      global tc
      if( (tcl>=0.5) and tc!=0.5):
	TC500MS()
	tc=0.5
      if( (tcl>=0.2 and tcl<0.5)):
	TC200MS()
	tc=0.2
      if( (tcl>=0.1 and tcl<0.2) ):
	TC100MS()
	tc=0.1
      elif( (tcl>=0.05 and tcl<0.1) ):
	TC50MS()
	tc=0.05
      elif( (tcl>=0.02 and tcl<0.05) ):
	TC20MS()
	tc=0.02
      elif( (tcl>=0.01 and tcl<0.02) ):
	TC10MS()
	tc=0.01
      elif( (tcl>=5E-3 and tcl<0.01) ):
	TC5MS()
	tc=5E-3
      elif( (tcl>=2E-3 and tcl<5E-3) ):
	TC2MS()
	tc=2E-3
      elif( (tcl>=1E-3 and tcl<2E-3) ):
	TC1MS()
	tc=1E-3
      elif( (tcl>=500E-6 and tcl<1E-3) ):
	TC500US()
	tc=500E-6
      elif( (tcl>=200E-6 and tcl<500E-6) ):
	TC200US()
	tc=200E-6
      elif( (tcl>=100E-6 and tcl<200E-6) ):
	TC100US()
	tc=100E-6
      elif( (tcl>=50E-6 and tcl<100E-6) ):
	TC50US()
	tc=50E-6
      elif(tcl<=50E-6 and tcl!=50E-6):
	TC50US()
	tc=50E-6
#############BUSCA USB LOCK IN
def Get_ttyUSB_List(): 
    dirList=os.listdir("/dev")
    result=list()

    for fname in dirList:
        if(fname.startswith("ttyUSB")):
            result.append("/dev/" + fname)

    return result
###########ACTIVAR MODO NOISE ON
def NMOD():
	s.write('NOISEMODE1\r') #Modo ruido
	time.sleep(0.1)
##############LEE MAGNITUD R LOCK IN
def MAG():
	s.write('MAG.\r') #Pido el valor del magnitud
	time.sleep(0.1)
	a = s.read(15)
	b = float(a)
	return b
##############LEE FASE TH LOCK IN
def PHA():
	s.write('PHA\r')	
	time.sleep(0.1)
	a = s.read(10) #leo el valor obtenido
	c = float(a)
	return c     
############# LEE RUIDO LOCK IN (MODO RUIDO ON)
def NHZ():
	s.write('NHZ\r')
	time.sleep(0.1)
	a = s.read(10) #leo el valor obtenido
	ns = float(a)
	return ns
############# Ordena vector
def Sort_Dev(vec):
	vec.sort()
	if len(vec)>2:
		del vec[0]
		del vec[len(vec)-1]
	vecmean = numpy.mean(vec)
	return vecmean      
############# Id lock in      
def ID_LOCKIN():
	s.write('ID\r') ###Check Lock In
	time.sleep(0.1)
	idlin = s.read(20)
	return idlin
################Time constants 

#######################################################################Inicio
#######################################################################Inicio
ttyUSB_List = Get_ttyUSB_List()  #Busca USB
sr = ttyUSB_List[0]
s = serial.Serial(port=sr, baudrate=9600, timeout=1, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE)
print 'Dispositivo serial = %s'%sr

idlin = ID_LOCKIN() ##Id lock in
print  'Lock-in:%s'%idlin

f=open(filenm,"w", 1) #archivo

#Gnu Plot
gm = Gnuplot.Gnuplot() #Seteo de parametros en Gnuplot para las graficas de magnitud y fase
gm.title('Espectro de ruido: Magnitud')
gm.xlabel('Frecuencia(Hz)')
gm.ylabel('Magnitud(Vrms/Sqrt(Hz))')
gm('set grid')
gm('set logscale x')
gm('set logscale y')
gm('set format y "%.1E"')
gm('set terminal wx')

####CONFIG LOCK IN Frec start
#Vectores
freq_list = []
mag_list = []
nmag_list = []
plot_mag_list = map(None,freq_list,mag_list)
SET_TC(tc_prm)
enbw = (1.22/tc_prm/6.28)
enbwsrt = sqrt(1.22/tc_prm/6.28)
print 'ENBW=%.1fHz'%enbw

#############LAZO BARRIDO LOG(F)
for frequency in numpy.logspace(log10(freq_start), log10(freq_end), num=freq_pts):
	freq_list.append(frequency)
	s.write('OF.%.3f\r'% frequency)# setea la frecuencia	
	
	time.sleep(0.01 + 5*tc_prm)	
	nmed = 0
        nmag_list = []
	while (nmed < nmeds):
		nmed = nmed + 1
		mag = MAG()  
		nmag_list.append(mag)
	
	mag = Sort_Dev(nmag_list)/enbwsrt #en V/srt(hz)
	mag_list.append(mag)
	plot_mag_list = map(None,freq_list,mag_list)
	plot1 = Gnuplot.PlotItems.Data(plot_mag_list, with_="linespoints")
	gm.plot(plot1)
	
	print 'Freq=%.1EHz '%frequency, 'Mag=%.1EV/Srt(Hz) '%mag, 'Tc=%.0Ems'%tc_prm
	f.write('%.10f\t'%frequency)
	f.write('%.10f\n'%mag)
##############FIN LAZO
f.close()
gm('quit')
############################################################################FIN PROGRAMA
############################################################################FIN PROGRAMA