Beispiel #1
0
def sweep_deemphasis_settings(signalType, amp, amp1, dly, windowing, filename):
    from timeit import default_timer as timer
    from plotting import printProgress
    EyeOpening = np.zeros((len(dly), len(amp), len(amp1)))
    MeanOpening = np.zeros((len(dly), len(amp), len(amp1)))
    band = np.zeros((len(dly), len(amp), len(amp1)))

    tot = len(amp) * len(amp1) * len(dly)
    dt = 0.0
    if tot > 32 * 32 * 4:
        tshift = np.ones(20)
        tshift[:] = 1e2
    else:
        tshift = np.ones(5)
        tshift[:] = 1e2
    rel = True
    #Signal._params['Amplitude'] = 1.
    #Signal.run(saveToFile=False, filename='SignalInput', rdm='lfsr7')
    #wv = Signal.waveform

    for id, d in enumerate(dly):
        for ia, a in enumerate(amp):
            for ia1, a1 in enumerate(amp1):
                st = timer()
                num = id * len(amp) * len(amp1) + len(amp1) * ia + ia1 + 1
                tleft = dt * (tot - num)
                m, s = divmod(tleft, 60)
                h, m = divmod(m, 60)
                printProgress(num,
                              tot,
                              prefix='Time left %dh %02dmin %02ds' % (h, m, s),
                              suffix='Complete',
                              decimals=3,
                              barLength=50)
                Signal._params['Amplitude'] = a
                Signal.run(False, 'init', signalType, False)
                #Signal.waveform['Sample 0']['Single'] = wv['Sample 0']['Single']*a
                Signal.addDeemphasis(deemphasisAmplitude=a1,
                                     deemphasisDelay=d,
                                     rel=rel)
                Signal.getSpectrum(saveToFile=False,
                                   filename='init',
                                   fromFile=False,
                                   verbose=False,
                                   window=windowing)
                Signal.waveform = TransmissionLine * Signal
                veye, vmean, deltaV = Signal.eyeOpening()
                veyediff = np.min(veye[1]) - np.max(veye[0])
                vmeandiff = vmean[1] - vmean[0]
                MeanOpening[id, ia, ia1] = vmeandiff
                EyeOpening[id, ia, ia1] = veyediff
                band[id, ia, ia1] = 0.5 * (np.max(veye[1]) - np.min(veye[1]) +
                                           np.max(veye[0] - np.min(veye[0])))
                ed = timer()
                tshift = estimateTime(tshift, ed - st)
                dt = np.average(tshift)
        np.savez(filename + '_del_%s.npz' % d,
                 Eye=EyeOpening[id],
                 Average=MeanOpening[id])
    return EyeOpening, MeanOpening, band, deltaV
 def transfer(self, signal, verbose=False):
     from numpy.fft import  irfft
     if not self.gotBode:
         from scipy.interpolate import interp1d
         fmax = self._fMax
         if self._stepSize == 4e6:
             step = 10
         if self._stepSize == 2e6:
             step = 20
         else:
             step = 10
         tmlBode = np.power(10,self._attenuation/20.)[::step]
         phase = self.dataDict['Phase'][::step]
         #tmlBode = tmlBode[:tmlBode.shape[0]/4:2]
         oldx = np.linspace(0,tmlBode.shape[0], num = tmlBode.shape[0], endpoint=True)
         fbode = interp1d(oldx, tmlBode, kind='cubic')
         fphase = interp1d(oldx, phase, kind='slinear')
         
         if signal.spectrum['Frequency'][len(signal.spectrum['Frequency'])-1] < fmax:
             sp = signal.spectrum['Frequency']
         else:    
             sp = signal.spectrum['Frequency'][0:len(np.where(signal.spectrum['Frequency']<fmax)[0])]
         
         newx = np.linspace(0,tmlBode.shape[0], num = len(sp), endpoint = True)
         interpBode = fbode(newx)
         interpPhase = fphase(newx)
         
         #import matplotlib.pyplot as plt
         #plt.plot(oldx, phase)
         #plt.plot(newx, interpPhase)
         #plt.show()
         self._bode = interpBode#/interpBode[0] 
         self.gotBode = True
     else:
         interpBode = self._bode
     wf = {}                
     
     complexBode = 1j*np.sin(2*np.pi*interpPhase)+np.cos(2*np.pi*interpPhase)
     complexBode *= interpBode
     
     for cnt in range(len(signal.waveform)-1):
         if verbose:
             printProgress(cnt, len(signal.waveform)-1, prefix = 'Transmitting wavefrom:', suffix = 'Complete', decimals=3, barLength = 50)            
         
         wf['Sample %s'%cnt] = {}
         if 'Single' in signal.waveform['Sample %s'%cnt].keys():
             fftwf = signal.spectrum['Data'][cnt]
             wf['Sample %s'%cnt]['Single'] = irfft(fftwf*np.append(complexBode,np.array([0.]*(fftwf.shape[0]-interpBode.shape[0]))), norm=None)
             wf['Sample %s'%cnt]['Single'] *= np.sqrt(2*len(fftwf))
             if signal._window == 'Hamming':
                 wf['Sample %s'%cnt]['Single'] /= signal._hamm
             elif signal._window == 'Tukey':
                 wf['Sample %s'%cnt]['Single'] /= signal._tuk
         
     wf['Time'] = signal.waveform['Time']
     return wf
Beispiel #3
0
    def run(self,
            saveToFile=True,
            filename='init',
            rdm='random',
            verbose=False):
        if self._params['Number of samples'] < self._chunksize:
            self._chunksize = self._params['Number of samples']
        self.waveform = {}
        bitList = []
        for filecnt in range(
                int(self._params['Number of samples'] / self._chunksize)):
            if rdm == 'random':
                bitList = self.getRandomBits(samples=self._chunksize)
            elif rdm == 'lfsr7':
                bitList = self.getLFSR(samples=self._chunksize, N=7)
            elif rdm == 'lfsr32':
                bitList = self.getLFSR(samples=self._chunksize, N=32)
            else:
                bitList = self.getClock(samples=self._chunksize)

            for i in range(self._chunksize):
                self._bits = bitList[i]
                if verbose:
                    printProgress(i + self._chunksize * filecnt,
                                  self._params['Number of samples'] - 1,
                                  prefix='Generating waveforms:',
                                  suffix='Complete',
                                  decimals=3,
                                  barLength=50)
                self.waveform['Sample %s' % i] = {}
                if self._params['Is signal differential']:
                    a, b = self.generateWaveform()
                    self.waveform['Time'] = a[0]
                    self.waveform['Sample %s' % i]['Positive'] = a[1]
                    self.waveform['Sample %s' % i]['Negative'] = b[1]
                else:
                    if rdm == 'sin':
                        a = self.generateSin()
                    else:
                        a = self.generateWaveform()
                    #a = self.cml_driver(a)
                    self.waveform['Sample %s' % i]['Single'] = a[1]
                    self.waveform['Time'] = a[0]

            if saveToFile:
                self.saveWaveform(filename=filename, idx=filecnt)
            if filecnt < int(
                    self._params['Number of samples'] / self._chunksize) - 1:
                self.waveform = {}

        self._hamm = self.hammingWindow()
        self._tuk = self.tukeyWindow()
Beispiel #4
0
    def shaper(self):
        from numpy.fft import irfft
        cnt = 0
        bode = np.ones(self.spectrum['Frequency'].shape[0])
        for k in range(bode.shape[0]):
            bode[k] = 1. / (1. + k * 1e-14 * self._params['Frequency'])

        for keysample, valsample in self.waveform.iteritems():
            if 'Time' not in keysample:
                printProgress(cnt,
                              self._params['Number of samples'] - 1,
                              prefix='Shaping waveform:',
                              suffix='Complete',
                              decimals=3,
                              barLength=50)
                for key, val in valsample.iteritems():
                    spec = self.spectrum['Data'][cnt]
                    inv = irfft(spec * bode)
                    self.waveform[keysample][key] = inv
                cnt += 1
Beispiel #5
0
    def eyeOpening(self, verbose=False):
        from plotting import simpleHist

        h = []
        cnt = 0
        if 'Single' in self.waveform['Sample 0'].keys():
            for keysamples, valsamples in self.waveform.iteritems():
                if verbose:
                    printProgress(cnt,
                                  len(self.waveform) - 1,
                                  prefix='Recording eye diagram:',
                                  suffix='Complete',
                                  decimals=3,
                                  barLength=50)
                if 'Time' not in keysamples:
                    for b in range(22, len(self._bits) - 22):
                        st = int(
                            (b + 1. / 2 - 0.05) * self.
                            _params['Number of sampling points per period'])
                        ed = int(
                            (b + 1. / 2 + 0.05) * self.
                            _params['Number of sampling points per period'])
                        h.append(valsamples['Single'][st:ed])
                cnt += 1
        else:
            pass
            #for keysamples, valsamples in self.waveform.iteritems():
            #    printProgress(cnt, len(self.waveform)-1, prefix = 'Recording eye:', suffix = 'Complete', decimals=3, barLength = 50)
            #    if 'Time' not in keysamples:
            #        rx = self.differentialProbing(valsamples['Positive'], valsamples['Negative'])
            #        h.append(rx[startPointHist:endPointHist])
            #
            #    cnt += 1

        h = np.array(h)

        histCnt, x, dv = simpleHist(h, 128)

        #gauss = x[np.where(histCnt!=0)[0]]
        gauss = x[histCnt != 0]

        gauss1 = gauss[gauss < 0.]

        #gauss1peak = x[np.argmax(histCnt[x<0.])]

        gauss1peak = 0.
        N = 0.
        for id in np.where(x < 0.)[0]:
            gauss1peak += histCnt[id] * x[id]
            N += histCnt[id]
        if N != 0:
            gauss1peak /= N
        else:
            gauss1peak = 0.

        gauss2 = gauss[gauss > 0.]
        #gauss2peak = x[len(x[x<0.]) + np.argmax(histCnt[x>0.])]
        gauss2peak = 0.
        N = 0.
        for id in np.where(x > 0.)[0]:
            gauss2peak += histCnt[id] * x[id]
            N += histCnt[id]
        if N != 0:
            gauss2peak /= N
        else:
            gauss2peak = 0.

        return [gauss1, gauss2], [gauss1peak, gauss2peak], dv
Beispiel #6
0
 def getSpectrum(self,
                 saveToFile=True,
                 filename='init',
                 fromFile=False,
                 verbose=False,
                 window='Hamming'):
     from numpy.fft import rfft
     #baseFreq = self.roundFrequency(self._params['Frequency'])
     baseFreq = self._params['Frequency']
     self.spectrum = {}
     N = len(self.waveform['Sample 0']['Single'])
     nqf = float(baseFreq) * self._params[
         'Number of sampling points per period'] / 2.
     t = []
     for k in range(N / 2 + 1):
         t.append(2 * nqf / (N - 1) * k)
     self.spectrum['Frequency'] = np.array(t)
     spectrum = []
     self._window = window
     if fromFile:
         filecnt = 1
         file = './rawdata/' + filename + 'waveform' + '_%s.npz' % filecnt
         while os.path.isfile(filename):
             file = './rawdata/' + filename + 'waveform' + '_%s.npz' % filecnt
             waveform = self.loadWaveform(file, False, filecnt)
             for i in range(self._chunksize):
                 if verbose:
                     printProgress(i + self._chunksize * filecnt,
                                   self._params['Number of samples'] - 1,
                                   prefix='Receive spectrum:',
                                   suffix='Complete',
                                   decimals=3,
                                   barLength=50)
                 spect = rfft(waveform['Sample %s' % i]['Single'])
                 spectrum.append(spect)
             filecnt += 1
         self.spectrum['Data'] = np.array(spectrum)
     else:
         for filecnt in range(
                 int(self._params['Number of samples'] / self._chunksize)):
             for i in range(self._chunksize):
                 if window == 'Tukey':
                     spect = rfft(self._tuk *
                                  self.waveform['Sample %s' % i]['Single'],
                                  norm=None)
                 elif window == 'Hamming':
                     spect = rfft(self._hamm *
                                  self.waveform['Sample %s' % i]['Single'],
                                  norm=None)
                 else:
                     spect = rfft(self.waveform['Sample %s' % i]['Single'],
                                  norm=None)
                 if verbose:
                     printProgress(i + self._chunksize * filecnt,
                                   self._params['Number of samples'] - 1,
                                   prefix='Receive spectrum:',
                                   suffix='Complete',
                                   decimals=3,
                                   barLength=50)
                 spect /= np.sqrt(2 * len(spect))
                 spectrum.append(spect)
             self.spectrum['Data'] = np.array(spectrum)
             if saveToFile:
                 self.saveSpectrum(filename=filename, idx=filecnt)
             if filecnt < int(self._params['Number of samples'] /
                              self._chunksize) - 1:
                 self.spectrum['Data'] = np.empty()