VoltageProcessor.py
502 lines
| 15.1 KiB
| text/x-python
|
PythonLexer
|
r10 | ''' | |
Created on Feb 7, 2012 | |||
|
r16 | @author $Author$ | |
@version $Id$ | |||
|
r10 | ''' | |
|
r72 | import os, sys | |
import numpy | |||
path = os.path.split(os.getcwd())[0] | |||
sys.path.append(path) | |||
from Model.Voltage import Voltage | |||
from IO.VoltageIO import VoltageWriter | |||
from Graphics.VoltagePlot import Osciloscope | |||
|
r10 | class VoltageProcessor: | |
''' | |||
classdocs | |||
''' | |||
|
r99 | ||
dataInObj = None | |||
dataOutObj = None | |||
integratorObjIndex = None | |||
decoderObjIndex = None | |||
profSelectorObjIndex = None | |||
writerObjIndex = None | |||
plotterObjIndex = None | |||
integratorObjList = [] | |||
decoderObjList = [] | |||
profileSelectorObjList = [] | |||
writerObjList = [] | |||
plotterObjList = [] | |||
m_Voltage= Voltage() | |||
m_ProfileSelector= ProfileSelector() | |||
|
r10 | ||
|
r99 | m_Decoder= Decoder() | |
m_CoherentIntegrator= CoherentIntegrator() | |||
def __init__(self, dataInObj, dataOutObj=None): | |||
|
r10 | ''' | |
Constructor | |||
''' | |||
|
r72 | ||
|
r99 | self.dataInObj = dataInObj | |
|
r72 | ||
|
r99 | if dataOutObj == None: | |
self.dataOutObj = Voltage() | |||
|
r72 | else: | |
|
r99 | self.dataOutObj = dataOutObj | |
|
r72 | ||
|
r99 | self.integratorObjIndex = None | |
self.decoderObjIndex = None | |||
self.profSelectorObjIndex = None | |||
self.writerObjIndex = None | |||
self.plotterObjIndex = None | |||
self.integratorObjList = [] | |||
self.decoderObjList = [] | |||
self.profileSelectorObjList = [] | |||
self.writerObjList = [] | |||
self.plotterObjList = [] | |||
|
r72 | ||
def init(self): | |||
|
r92 | ||
|
r99 | self.integratorObjIndex = 0 | |
self.decoderObjIndex = 0 | |||
self.profSelectorObjIndex = 0 | |||
self.writerObjIndex = 0 | |||
self.plotterObjIndex = 0 | |||
self.dataOutObj.copy(self.dataInObj) | |||
|
r72 | ||
|
r92 | def addWriter(self, wrpath): | |
|
r99 | objWriter = VoltageWriter(self.dataOutObj) | |
|
r72 | objWriter.setup(wrpath) | |
|
r99 | self.writerObjList.append(objWriter) | |
|
r72 | ||
def addPlotter(self): | |||
|
r99 | plotObj = Osciloscope(self.dataOutObj,self.plotterObjIndex) | |
self.plotterObjList.append(plotObj) | |||
|
r72 | ||
|
r92 | def addIntegrator(self, nCohInt): | |
|
r72 | ||
|
r92 | objCohInt = CoherentIntegrator(nCohInt) | |
|
r99 | self.integratorObjList.append(objCohInt) | |
|
r72 | ||
|
r92 | def addDecoder(self, code, ncode, nbaud): | |
|
r72 | ||
objDecoder = Decoder(code,ncode,nbaud) | |||
|
r99 | self.decoderObjList.append(objDecoder) | |
|
r72 | ||
|
r92 | def addProfileSelector(self, nProfiles): | |
objProfSelector = ProfileSelector(nProfiles) | |||
|
r99 | self.profileSelectorObjList.append(objProfSelector) | |
|
r92 | ||
|
r72 | def writeData(self,wrpath): | |
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | ||
|
r99 | if len(self.writerObjList) <= self.writerObjIndex: | |
|
r72 | self.addWriter(wrpath) | |
|
r99 | self.writerObjList[self.writerObjIndex].putData() | |
|
r72 | ||
|
r99 | # myWrObj = self.writerObjList[self.writerObjIndex] | |
|
r72 | # myWrObj.putData() | |
|
r99 | self.writerObjIndex += 1 | |
|
r72 | ||
def plotData(self,idProfile, type, xmin=None, xmax=None, ymin=None, ymax=None, winTitle=''): | |||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | ||
|
r99 | if len(self.plotterObjList) <= self.plotterObjIndex: | |
|
r72 | self.addPlotter() | |
|
r99 | self.plotterObjList[self.plotterObjIndex].plotData(type=type, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,winTitle=winTitle) | |
|
r72 | ||
|
r99 | self.plotterObjIndex += 1 | |
|
r72 | ||
def integrator(self, N): | |||
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | ||
|
r99 | if len(self.integratorObjList) <= self.integratorObjIndex: | |
|
r72 | self.addIntegrator(N) | |
|
r99 | myCohIntObj = self.integratorObjList[self.integratorObjIndex] | |
myCohIntObj.exe(self.dataOutObj.data) | |||
|
r72 | ||
if myCohIntObj.flag: | |||
|
r99 | self.dataOutObj.data = myCohIntObj.data | |
self.dataOutObj.m_ProcessingHeader.coherentInt *= N | |||
self.dataOutObj.flagNoData = False | |||
|
r72 | ||
else: | |||
|
r99 | self.dataOutObj.flagNoData = True | |
|
r72 | ||
|
r99 | self.integratorObjIndex += 1 | |
|
r72 | ||
def decoder(self,code=None,type = 0): | |||
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | if code == None: | |
|
r99 | code = self.dataOutObj.m_RadarControllerHeader.code | |
|
r72 | ncode, nbaud = code.shape | |
|
r99 | if len(self.decoderObjList) <= self.decoderObjIndex: | |
|
r72 | self.addDecoder(code,ncode,nbaud) | |
|
r99 | myDecodObj = self.decoderObjList[self.decoderObjIndex] | |
myDecodObj.exe(data=self.dataOutObj.data,type=type) | |||
|
r72 | ||
if myDecodObj.flag: | |||
|
r99 | self.dataOutObj.data = myDecodObj.data | |
self.dataOutObj.flagNoData = False | |||
|
r72 | else: | |
|
r99 | self.dataOutObj.flagNoData = True | |
|
r72 | ||
|
r99 | self.decoderObjIndex += 1 | |
|
r84 | ||
|
r72 | ||
|
r84 | def filterByHei(self, window): | |
|
r72 | pass | |
|
r84 | ||
|
r72 | ||
|
r84 | def selectChannels(self, channelList): | |
""" | |||
Selecciona un bloque de datos en base a canales y pares segun el channelList y el pairList | |||
Input: | |||
|
r97 | channelList : lista sencilla de canales a seleccionar por ej. [2,3,7] | |
|
r84 | ||
Affected: | |||
|
r97 | self.dataOutObj.data | |
|
r99 | self.dataOutObj.channelList | |
|
r84 | self.dataOutObj.nChannels | |
|
r99 | self.dataOutObj.m_ProcessingHeader.totalSpectra | |
|
r84 | self.dataOutObj.m_SystemHeader.numChannels | |
|
r99 | self.dataOutObj.m_ProcessingHeader.blockSize | |
|
r84 | ||
Return: | |||
None | |||
""" | |||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
for channel in channelList: | |||
|
r99 | if channel not in self.dataOutObj.channelList: | |
|
r97 | raise ValueError, "The value %d in channelList is not valid" %channel | |
|
r84 | ||
|
r97 | nchannels = len(channelList) | |
|
r99 | profiles = self.dataOutObj.nProfiles | |
heights = self.dataOutObj.nHeights #m_ProcessingHeader.numHeights | |||
|
r84 | ||
|
r97 | data = numpy.zeros( (nchannels,heights), dtype='complex' ) | |
|
r84 | for index,channel in enumerate(channelList): | |
|
r99 | data[index,:] = self.dataOutObj.data[channel,:] | |
|
r72 | ||
|
r99 | self.dataOutObj.data = data | |
self.dataOutObj.channelList = channelList | |||
self.dataOutObj.nChannels = nchannels | |||
self.dataOutObj.m_ProcessingHeader.totalSpectra = nchannels | |||
self.dataOutObj.m_SystemHeader.numChannels = nchannels | |||
self.dataOutObj.m_ProcessingHeader.blockSize = data.size | |||
|
r97 | return 1 | |
|
r72 | ||
|
r84 | def selectHeightsByValue(self, minHei, maxHei): | |
""" | |||
Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | |||
minHei <= height <= maxHei | |||
Input: | |||
minHei : valor minimo de altura a considerar | |||
maxHei : valor maximo de altura a considerar | |||
Affected: | |||
Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | |||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
|
r99 | if (minHei < self.dataOutObj.heightList[0]) or (minHei > maxHei): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |
|
r99 | if (maxHei > self.dataOutObj.heightList[-1]): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |
|
r84 | minIndex = 0 | |
maxIndex = 0 | |||
|
r99 | data = self.dataOutObj.heightList | |
|
r84 | ||
for i,val in enumerate(data): | |||
if val < minHei: | |||
continue | |||
else: | |||
minIndex = i; | |||
break | |||
for i,val in enumerate(data): | |||
if val <= maxHei: | |||
maxIndex = i; | |||
else: | |||
break | |||
self.selectHeightsByIndex(minIndex, maxIndex) | |||
|
r97 | return 1 | |
|
r84 | ||
def selectHeightsByIndex(self, minIndex, maxIndex): | |||
""" | |||
Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | |||
minIndex <= index <= maxIndex | |||
Input: | |||
|
r97 | minIndex : valor de indice minimo de altura a considerar | |
maxIndex : valor de indice maximo de altura a considerar | |||
|
r84 | ||
Affected: | |||
|
r99 | self.dataOutObj.data | |
self.dataOutObj.heightList | |||
self.dataOutObj.nHeights | |||
self.dataOutObj.m_ProcessingHeader.blockSize | |||
self.dataOutObj.m_ProcessingHeader.numHeights | |||
self.dataOutObj.m_ProcessingHeader.firstHeight | |||
self.dataOutObj.m_RadarControllerHeader | |||
|
r84 | ||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
if (minIndex < 0) or (minIndex > maxIndex): | |||
raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
r99 | if (maxIndex >= self.dataOutObj.nHeights): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |
nHeights = maxIndex - minIndex + 1 | |||
|
r84 | firstHeight = 0 | |
#voltage | |||
|
r99 | data = self.dataOutObj.data[:,minIndex:maxIndex+1] | |
|
r84 | ||
|
r99 | firstHeight = self.dataOutObj.heightList[minIndex] | |
|
r84 | ||
|
r99 | self.dataOutObj.data = data | |
self.dataOutObj.heightList = self.dataOutObj.heightList[minIndex:maxIndex+1] | |||
self.dataOutObj.nHeights = nHeights | |||
self.dataOutObj.m_ProcessingHeader.blockSize = data.size | |||
self.dataOutObj.m_ProcessingHeader.numHeights = nHeights | |||
self.dataOutObj.m_ProcessingHeader.firstHeight = firstHeight | |||
self.dataOutObj.m_RadarControllerHeader.numHeights = nHeights | |||
|
r97 | return 1 | |
|
r84 | ||
|
r92 | def selectProfiles(self, minIndex, maxIndex, nProfiles): | |
|
r84 | """ | |
|
r97 | Selecciona un bloque de datos en base a un grupo indices de perfiles segun el rango | |
|
r84 | minIndex <= index <= maxIndex | |
Input: | |||
|
r97 | minIndex : valor de indice minimo de perfil a considerar | |
maxIndex : valor de indice maximo de perfil a considerar | |||
nProfiles : numero de profiles | |||
|
r84 | ||
Affected: | |||
|
r99 | self.dataOutObj.flagNoData | |
self.profSelectorObjIndex | |||
|
r84 | ||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r99 | if self.profSelectorObjIndex >= len(self.profileSelectorObjList): | |
|
r92 | self.addProfileSelector(nProfiles) | |
|
r99 | profileSelectorObj = self.profileSelectorObjList[self.profSelectorObjIndex] | |
|
r92 | ||
if profileSelectorObj.isProfileInRange(minIndex, maxIndex): | |||
|
r99 | self.dataOutObj.flagNoData = False | |
self.profSelectorObjIndex += 1 | |||
|
r92 | return 1 | |
|
r99 | self.dataOutObj.flagNoData = True | |
self.profSelectorObjIndex += 1 | |||
|
r92 | ||
return 0 | |||
|
r72 | ||
|
r84 | def selectNtxs(self, ntx): | |
|
r72 | pass | |
class Decoder: | |||
|
r99 | ||
data = None | |||
profCounter = 1 | |||
nCode = ncode | |||
nBaud = nbaud | |||
codeIndex = 0 | |||
code = code #this is a List | |||
fft_code = None | |||
flag = False | |||
setCodeFft = False | |||
|
r92 | ||
|
r72 | def __init__(self,code, ncode, nbaud): | |
|
r92 | ||
|
r99 | self.data = None | |
|
r72 | self.profCounter = 1 | |
self.nCode = ncode | |||
self.nBaud = nbaud | |||
self.codeIndex = 0 | |||
self.code = code #this is a List | |||
self.fft_code = None | |||
self.flag = False | |||
self.setCodeFft = False | |||
def exe(self, data, ndata=None, type = 0): | |||
|
r92 | ||
|
r73 | if ndata == None: ndata = data.shape[1] | |
|
r72 | ||
if type == 0: | |||
self.convolutionInFreq(data,ndata) | |||
if type == 1: | |||
self.convolutionInTime(data, ndata) | |||
|
r92 | def convolutionInFreq(self,data, ndata): | |
|
r72 | ||
newcode = numpy.zeros(ndata) | |||
newcode[0:self.nBaud] = self.code[self.codeIndex] | |||
self.codeIndex += 1 | |||
|
r73 | fft_data = numpy.fft.fft(data, axis=1) | |
|
r72 | fft_code = numpy.conj(numpy.fft.fft(newcode)) | |
|
r73 | fft_code = fft_code.reshape(1,len(fft_code)) | |
|
r72 | ||
conv = fft_data.copy() | |||
conv.fill(0) | |||
conv = fft_data*fft_code # This other way to calculate multiplication between bidimensional arrays | |||
# for i in range(ndata): | |||
# conv[i,:] = fft_data[i,:]*fft_code[i] | |||
|
r73 | self.data = numpy.fft.ifft(conv,axis=1) | |
|
r72 | self.flag = True | |
if self.profCounter == self.nCode: | |||
self.profCounter = 0 | |||
self.codeIndex = 0 | |||
self.profCounter += 1 | |||
def convolutionInTime(self, data, ndata): | |||
nchannel = data.shape[1] | |||
newcode = self.code[self.codeIndex] | |||
self.codeIndex += 1 | |||
conv = data.copy() | |||
for i in range(nchannel): | |||
|
r73 | conv[i,:] = numpy.correlate(data[i,:], newcode, 'same') | |
|
r72 | ||
self.data = conv | |||
self.flag = True | |||
if self.profCounter == self.nCode: | |||
self.profCounter = 0 | |||
self.codeIndex = 0 | |||
self.profCounter += 1 | |||
class CoherentIntegrator: | |||
|
r92 | ||
|
r99 | profCounter = 1 | |
data = None | |||
buffer = None | |||
flag = False | |||
nCohInt = N | |||
|
r72 | def __init__(self, N): | |
|
r92 | ||
|
r72 | self.profCounter = 1 | |
self.data = None | |||
self.buffer = None | |||
self.flag = False | |||
self.nCohInt = N | |||
|
r92 | def exe(self, data): | |
|
r72 | ||
if self.buffer == None: | |||
self.buffer = data | |||
else: | |||
self.buffer = self.buffer + data | |||
if self.profCounter == self.nCohInt: | |||
self.data = self.buffer | |||
self.buffer = None | |||
self.profCounter = 0 | |||
self.flag = True | |||
else: | |||
self.flag = False | |||
self.profCounter += 1 | |||
|
r99 | class ProfileSelector: | |
|
r92 | ||
indexProfile = None | |||
|
r97 | # Tamanho total de los perfiles | |
|
r92 | nProfiles = None | |
def __init__(self, nProfiles): | |||
self.indexProfile = 0 | |||
self.nProfiles = nProfiles | |||
def isProfileInRange(self, minIndex, maxIndex): | |||
|
r97 | if self.indexProfile < minIndex: | |
|
r92 | self.indexProfile += 1 | |
return False | |||
|
r97 | if self.indexProfile > maxIndex: | |
|
r92 | self.indexProfile += 1 | |
return False | |||
self.indexProfile += 1 | |||
return True | |||
def isProfileInList(self, profileList): | |||
if self.indexProfile not in profileList: | |||
self.indexProfile += 1 | |||
return False | |||
self.indexProfile += 1 | |||
return True | |||
|
r72 |