def __init__(self, output='out', input='in', \ mag=None, phase=None, coh=None, \ freqlim=[], maglim=[], phaselim=[], \ averaged='not specified', \ seedfreq=-1, seedphase=0, labels=[], legloc=-1, compin=[]): self.output = output self.input = input if len(compin) > 0: if mag is None: self.mag = squeeze(colwise(abs(compin))) if phase is None: self.phase = squeeze(colwise(arctan2(imag(compin),real(compin))*180.0/pi)) else: self.mag = squeeze(mag) self.phase = squeeze(phase) self.coh = coh self.averaged = averaged self.seedfreq = seedfreq self.seedphase = seedphase self.freqlim = freqlim self.maglim = maglim self.phaselim = phaselim self.labels = labels self.legloc = legloc
def __mul__(self,other): """Multiply two bodes.""" if type(other)==float or type(other)==int: myoutput=copy.deepcopy(self) myoutput.mag=myoutput.mag*other return myoutput myin='in' myout='out' match=1 if self.output==other.input: first=self second=other elif self.input==other.output: first=other second=self else: warnme=1 if (self.input=='in' and self.output=='out') or (other.input=='in' and other.output=='out'): warnme=0 if warnme: print('Warning: multiplying Bodes without a matching input/output pair:\n'+self.output+'/'+self.input+ ' * ' +other.output+'/'+other.input) match=0 first=self second=other if match: myin=first.input myout=first.output myoutput=copy.deepcopy(self) myoutput.input=myin myoutput.output=myout myoutput.mag=squeeze(colwise(first.mag)*colwise(second.mag)) myoutput.phase=squeeze(colwise(first.phase)+colwise(second.phase)) return myoutput
def autolim(self, myattr, freqvect, margin=0.1,db=0): if self.freqlim: ind1=thresh(freqvect,self.freqlim[0]) ind2=thresh(freqvect,self.freqlim[1]) else: ind1=0 ind2=-1 mymatrix=getattr(self,myattr) if len(shape(mymatrix))==1: submat=mymatrix[ind1:ind2] else: mymatrix=colwise(mymatrix) submat=mymatrix[ind1:ind2,:] if db: submat=20*log10(submat) # max and min need to be done columnwise # (maybe a Krauss matrix max) if len(shape(submat))==2: mymax=[] mymin=[] for q in range(shape(submat)[1]): mymax.append(max(submat[:,q])) mymin.append(min(submat[:,q])) else: mymax=max(submat) mymin=min(submat) if len(shape(mymax))>0: mymax=max(mymax) mymin=min(mymin) myspan=mymax-mymin mymargin=margin*myspan limout=[mymin-mymargin, mymax+mymargin] setattr(self,myattr+"lim",limout) return limout
def compress(self, mask): mylist=['mag','phase','coh'] for item in mylist: tempvect=getattr(self,item) if tempvect is not None: if len(tempvect) > 0: tempvect=colwise(tempvect) setattr(self,item,tempvect.compress(mask,0))
def CombinedBodes(bodelist): """This function seeks to make one combined rwkbode instance from a list of them. This may be useful in the case of having several experimental Bode data sets with various targeted frequency ranges that need to be treated as one data set.""" docoh=True for cb in bodelist: if cb.coh is None: docoh=False break first=1 # pdb.set_trace() bigcoh=[] for cb in bodelist: if first: first=0 bigmag=colwise(cb.mag) bigphase=colwise(cb.phase) if docoh: bigcoh=colwise(cb.coh) else: bigmag=r_[bigmag,colwise(cb.mag)] bigphase=r_[bigphase,colwise(cb.phase)] if docoh: bigcoh=r_[bigcoh,colwise(cb.coh)] myoutbode=copy.deepcopy(bodelist[0]) myoutbode.mag=squeeze(bigmag) myoutbode.phase=squeeze(bigphase) myoutbode.coh=squeeze(bigcoh) myoutbode.freqlim=[] return myoutbode
def concatenate(bodelist): bodeout=copy.deepcopy(bodelist[0]) for curbode in bodelist[1:]: bodeout.mag=c_[colwise(bodeout.mag),colwise(curbode.mag)] bodeout.phase=c_[colwise(bodeout.phase),colwise(curbode.phase)] if bodeout.coh and curbode.coh: bodeout.coh=c_[colwise(bodeout.coh),colwise(curbode.coh)] return bodeout
def truncate(self, freq, flow, fhigh=None): """Truncate the mag, phase, and coherence of the rwkbode instance based on the indices returned by thresh(flow) and thresh(fhigh). If fhigh is not given, it is assumed that flow is a list of [flow, fhigh].""" if fhigh is None: fhigh=flow[1] flow=flow[0] i1=thresh(freq,flow) i2=thresh(freq,fhigh) if i2-i1<max(shape(self.mag)):#test if already truncated self.mag=colwise(self.mag)[i1:i2,:] self.phase=colwise(self.phase)[i1:i2,:] self.coh=colwise(self.coh)[i1:i2,:] tfreq=freq[i1:i2] if self.freqlim: if self.freqlim[0]<min(tfreq): self.freqlim[0]=min(tfreq) if self.freqlim[1]>max(tfreq): self.freqlim[1]=max(tfreq) else: self.freqlim=[min(tfreq),max(tfreq)] return tfreq
def downsample(self,freq,factor,ranges=[]): if shape(factor) and ranges: # pdb.set_trace() mask=CreateVariableMask(freq, ranges, factor) else: mask=CreateDownSampleMask(freq, factor) # mask=CreateLogMask(freq) dsfreq=compress(mask,freq) mylist=['mag','phase','coh'] for item in mylist: tempvect=getattr(self,item) if tempvect is not None: tempvect=colwise(tempvect) setattr(self,item,compress(mask,tempvect,0)) return dsfreq