Example #1
0
 def _fft_convolve(h, xd):
     n = len(h) + len(xd) - 1
     np = dk_lib.pow2roundup(n)
     if len(xd.shape) == 2 and len(h.shape) == 1:
         h = h.reshape((len(h), 1))
     s = numpy.fft.irfft(numpy.fft.rfft(h, np, axis=0) * numpy.fft.rfft(xd, np, axis=0), np, axis=0)
     return s[:n]
Example #2
0
    def plot_spectrum(self, p, plot_variable):
        varlist = []
        if plot_variable:
            if plot_variable not in p.pot_list:
                raise ArgumentError("variable %s not found" % plot_variable)
            ##hack
            for k, t in self.V.items():
                if isinstance(k, P):
                    if not isinstance(t, dict):
                        t = dict(value=t)
                    var = t.get('var')
                    if var is None:
                        var = str(k) + 'v'
                    if var == plot_variable:
                        break
            loga = t.get('a', 0)
            inv = t.get('inv', 0)
            for i in range(5):
                pot = i / 4
                lbl = "%s" % pot
                if inv:
                    pot = 1 - pot
                if loga:
                    pot = (math.exp(loga * pot) - 1) / (math.exp(loga) - 1)
                varlist.append((plot_variable, pot, lbl))
        else:
            varlist.append((None, None, p.out_labels))
        n = None
        cut = None

        def spec(y):
            s = 20 * np.log10(abs(np.fft.fft(y, n, axis=0)[cut]))
            return np.where(s > -80, s, np.nan)

        labels = []
        for var, val, lbl in varlist:
            if var is not None:
                p.set_variable(var, val)
            y = self.spectrum_signal(p, magnitude=1e-4)
            if n is None:
                n = dk_lib.pow2roundup(len(y))
                cut = slice(n * 20 // int(self.FS), n * 10000 // int(self.FS))
                w = fftfreq(n, 1.0 / self.FS)[cut]
            pl.semilogx(w, spec(y), label=plot_variable)
            pl.xlabel('Frequency')
            pl.ylabel('Magnitude ')
            if plot_variable:
                pl.title(plot_variable)
            if isinstance(lbl, basestring):
                labels.append(lbl)
            else:
                labels.extend(lbl)
        ax = pl.gca()
        ax.grid()
        ax.yaxis.set_major_formatter(pl.FormatStrFormatter('%d dB'))
        ax.xaxis.set_major_formatter(pl.FormatStrFormatter('%d Hz'))
        self.finish_plot(labels, loc='upper left')
Example #3
0
 def plot_spectrum(self, p, plot_variable):
     y = self.spectrum_signal(p)
     n = dk_lib.pow2roundup(len(y))
     cut = slice(n*20.0/self.FS, n*10000.0/self.FS)
     w = fftfreq(n,1.0/self.FS)[cut]
     def spec(y):
         s = 20*np.log10(abs(np.fft.fft(y,n,axis=0)[cut]))
         return np.where(s > -80, s, np.nan)
     pl.semilogx(w, spec(y))
     self.finish_plot(p.out_labels)
Example #4
0
 def _fft_convolve(h, xd):
     n = len(h) + len(xd) - 1
     npow = dk_lib.pow2roundup(n)
     if len(xd.shape) == 2 and len(h.shape) == 1:
         h = h.reshape((len(h), 1))
     s = np.fft.irfft(np.fft.rfft(h, npow, axis=0) *
                      np.fft.rfft(xd, npow, axis=0),
                      npow,
                      axis=0)
     return s[:n]
Example #5
0
 def make_sweep(self, pre=None, span=0.5, post=0.1, magnitude=1e-2, start=20, stop=10000):
     smpl = lambda tm: int(round(tm*self.FS))
     if pre is None:
         pre = span/2
     s, d = dk_lib.genlogsweep(
         start, stop, self.FS, smpl(pre), smpl(span), smpl(post))
     s *= magnitude
     n = dk_lib.pow2roundup(len(s))
     #d /= np.mean(abs(np.fft.fft(dk_lib.fft_convolve(d, s), n))[n*start/self.FS:n*stop/self.FS])
     #return s, d
     return s
Example #6
0
    def plot_spectrum(self, p, plot_variable):
        y = self.spectrum_signal(p)
        n = dk_lib.pow2roundup(len(y))
        cut = slice(n * 20.0 / self.FS, n * 10000.0 / self.FS)
        w = fftfreq(n, 1.0 / self.FS)[cut]

        def spec(y):
            s = 20 * np.log10(abs(np.fft.fft(y, n, axis=0)[cut]))
            return np.where(s > -80, s, np.nan)

        pl.semilogx(w, spec(y))
        self.finish_plot(p.out_labels)
Example #7
0
 def plot_spectrum(self, p, plot_variable):
     varlist = []
     if plot_variable:
         if plot_variable not in p.pot_list:
             raise ArgumentError("variable %s not found" % plot_variable)
         ##hack
         for k, t in self.V.items():
             if isinstance(k, P):
                 if not isinstance(t, dict):
                     t = dict(value=t)
                 var = t.get('var')
                 if var is None:
                     var = str(k)+'v'
                 if var == plot_variable:
                     break
         loga = t.get('a', 0)
         inv = t.get('inv', 0)
         for i in range(5):
             pot = i/4
             lbl = "%s" % pot
             if inv:
                 pot = 1 - pot
             if loga:
                 pot = (math.exp(loga * pot) - 1) / (math.exp(loga) - 1)
             varlist.append((plot_variable, pot, lbl))
     else:
         varlist.append((None, None, p.out_labels))
     n = None
     cut = None
     def spec(y):
         s = 20*np.log10(abs(np.fft.fft(y,n,axis=0)[cut]))
         return np.where(s > -80, s, np.nan)
     labels = []
     for var, val, lbl in varlist:
         if var is not None:
             p.set_variable(var, val)
         y = self.spectrum_signal(p, magnitude=1e-4)
         if n is None:
             n = dk_lib.pow2roundup(len(y))
             cut = slice(n*20.0/self.FS, n*10000.0/self.FS)
             w = fftfreq(n,1.0/self.FS)[cut]
         pl.semilogx(w, spec(y))
         if isinstance(lbl, basestring):
             labels.append(lbl)
         else:
             labels.extend(lbl)
     self.finish_plot(labels, loc='upper left')
Example #8
0
 def make_sweep(self,
                pre=None,
                span=0.5,
                post=0.1,
                magnitude=1e-2,
                start=20,
                stop=10000):
     smpl = lambda tm: int(round(tm * self.FS))
     if pre is None:
         pre = span / 2
     s, d = dk_lib.genlogsweep(start, stop, self.FS, smpl(pre), smpl(span),
                               smpl(post))
     s *= magnitude
     n = dk_lib.pow2roundup(len(s))
     #d /= np.mean(abs(np.fft.fft(dk_lib.fft_convolve(d, s), n))[n*start/self.FS:n*stop/self.FS])
     #return s, d
     return s
Example #9
0
 def _default_make_spectrum(self, response, freqlist):
     n = dk_lib.pow2roundup(len(response))
     return np.fft.rfft(response, n, axis=0)
Example #10
0
 def _default_make_spectrum(self, response, freqlist):
     n = dk_lib.pow2roundup(len(response))
     return numpy.fft.rfft(response, n, axis=0)