def _setDetrend(self, detrend): if detrend == self.__detrend: return if detrend not in self._detrend_choices: raise errors.SpectrumChoiceError(detrend, self._detrend_choices) self.__detrend = detrend self.modified = True
def _set_window(self, window): if window == self.__window: return if window not in self._window: raise errors.SpectrumChoiceError(window, self._window) self.__window = window self.modified = True
def frequencies(self, sides=None): """Return the frequency vector according to :attr:`sides`""" # use the attribute sides except if a valid sides argument is provided if sides is None: sides = self.sides if sides not in self._sides_choices: raise errors.SpectrumChoiceError(sides, self._sides_choices) if sides == 'onesided': return self._range.onesided() if sides == 'twosided': return self._range.twosided() if sides == 'centerdc': return self._range.centerdc()
def _setSides(self, sides): # check validity of sides if sides not in self._sides_choices: raise errors.SpectrumChoiceError(sides, self._sides_choices) # default value if sides == 'default': sides = self._default_sides() # check validity of sides if self.datatype == 'complex': assert sides != [ 'onesided' ], "complex data cannot be onesided (%s provided)" % sides # If sides is indeed different, update the psd if self.__psd is not None: newpsd = self.get_converted_psd(sides) self.__psd = newpsd self.__sides = sides #print '------------> %s %s' % (self.__sides, sides) # we set the PSD by hand, so we can consider that PSD is up-to-date self.modified = False
def plot(self, filename=None, norm=False, ylim=None, sides=None, **kargs): """a simple plotting routine to plot the PSD versus frequency. :param str filename: save the figure into a file :param norm: False by default. If True, the PSD is normalised. :param ylim: readjust the y range . :param sides: if not provided, :attr:`sides` is used. See :attr:`sides` for details. :param kargs: any optional argument accepted by :func:`pylab.plot`. .. plot:: :width: 80% :include-source: from spectrum import * p = Periodogram(marple_data) p() # runs the psd estimate p.plot(norm=True, marker='o') """ #First, check that psd attribute is up-to-date if self.modified is True: raise errors.SpectrumModifiedError # and that it has been computed if self.__psd is None: raise errors.SpectrumPSDError # check that the input sides parameter is correct if provided if sides is not None: if sides not in self._sides_choices: raise errors.SpectrumChoiceError(sides, self._sides_choices) # if sides is provided but identical to the current psd, nothing to do. if sides is None or sides == self.sides: frequencies = self.frequencies() psd = self.psd #sides = self.sides elif sides is not None: # if sides argument is different from the attribute, we need to # create a new PSD/Freq but we do not want to touch the attributes # if data is complex, one-sided is wrong in any case. if self.datatype == 'complex': assert sides != 'onesided' frequencies = self.frequencies(sides=sides) psd = self.get_converted_psd(sides) if len(psd) != len(frequencies): raise ValueError("PSD length is %s and freq length is %s" % (len(psd), len(frequencies))) from pylab import ylim as plt_ylim if 'ax' in list(kargs.keys()): save_ax = plt.gca() plt.sca(kargs['ax']) rollback = True del kargs['ax'] else: rollback = False if norm: pylab.plot(frequencies, 10 * pylab.log10(psd / max(psd)), **kargs) else: pylab.plot(frequencies, 10 * pylab.log10(psd), **kargs) plt.xlabel('Frequency') plt.ylabel('Power (dB)') pylab.grid(True) if ylim: plt_ylim(ylim) if sides == 'onesided': pylab.xlim(0, self.sampling / 2.) elif sides == 'twosided': pylab.xlim(0, self.sampling) elif sides == 'centerdc': pylab.xlim(-self.sampling / 2., self.sampling / 2.) if filename: pylab.savefig(filename) if rollback: plt.sca(save_ax) del psd, frequencies #is it needed?