10000 Templatize spectral helpers. · matplotlib/matplotlib@a226f82 · GitHub
[go: up one dir, main page]

Skip to content

Commit a226f82

Browse files
committed
Templatize spectral helpers.
I think the repetition is large enough to be worth it. This does make these functions all refer to themselves as well in the See Also section, but that seems benign enough.
1 parent cd41774 commit a226f82

File tree

1 file changed

+57
-162
lines changed

1 file changed

+57
-162
lines changed

lib/matplotlib/mlab.py

Lines changed: 57 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"""
5555

5656
import csv
57+
import functools
5758
from numbers import Number
5859

5960
import numpy as np
@@ -584,13 +585,13 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
584585
return result, freqs, t
585586

586587

587-
def _single_spectrum_helper(x, mode, Fs=None, window=None, pad_to=None,
588-
sides=None):
589-
'''
588+
def _single_spectrum_helper(
589+
mode, x, Fs=None, window=None, pad_to=None, sides=None):
590+
"""
590591
This is a helper function that implements the commonality between the
591592
complex, magnitude, angle, and phase spectrums.
592593
It is *NOT* meant to be used outside of mlab and may change at any time.
593-
'''
594+
"""
594595
cbook._check_in_list(['complex', 'magnitude', 'angle', 'phase'], mode=mode)
595596

596597
if pad_to is None:
@@ -792,166 +793,60 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None,
792793
return Pxy, freqs
793794

794795

795-
@docstring.dedent_interpd
796-
def complex_spectrum(x, Fs=None, window=None, pad_to=None,
797-
sides=None):
798-
"""
799-
Compute the complex-valued frequency spectrum of *x*. Data is padded to a
800-
length of *pad_to* and the windowing function *window* is applied to the
801-
signal.
802-
803-
Parameters
804-
----------
805-
x : 1-D array or sequence
806-
Array or sequence containing the data
807-
808-
%(Spectral)s
809-
810-
%(Single_Spectrum)s
811-
812-
Returns
813-
-------
814-
spectrum : 1-D array
815-
The values for the complex spectrum (complex valued)
816-
817-
freqs : 1-D array
818-
The frequencies corresponding to the elements in *spectrum*
819-
820-
See Also
821-
--------
822-
magnitude_spectrum
823-
Returns the absolute value of this function.
824-
angle_spectrum
825-
Returns the angle of this function.
826-
phase_spectrum
827-
Returns the phase (unwrapped angle) of this function.
828-
specgram
829-
Can return the complex spectrum of segments within the signal.
830-
"""
831-
return _single_spectrum_helper(x=x, Fs=Fs, window=window, pad_to=pad_to,
832-
sides=sides, mode='complex')
833-
834-
835-
@docstring.dedent_interpd
836-
def magnitude_spectrum(x, Fs=None, window=None, pad_to=None,
837-
sides=None):
838-
"""
839-
Compute the magnitude (absolute value) of the frequency spectrum of
840-
*x*. Data is padded to a length of *pad_to* and the windowing function
841-
*window* is applied to the signal.
842-
843-
Parameters
844-
----------
845-
x : 1-D array or sequence
846-
Array or sequence containing the data
847-
848-
%(Spectral)s
849-
850-
%(Single_Spectrum)s
851-
852-
Returns
853-
-------
854-
spectrum : 1-D array
855-
The values for the magnitude spectrum (real valued)
856-
857-
freqs : 1-D array
858-
The frequencies corresponding to the elements in *spectrum*
859-
860-
See Also
861-
--------
862-
psd
863-
Returns the power spectral density.
864-
complex_spectrum
865-
This function returns the absolute value of `complex_spectrum`.
866-
angle_spectrum
867-
Returns the angles of the corresponding frequencies.
868-
phase_spectrum
869-
Returns the phase (unwrapped angle) of the corresponding frequencies.
870-
specgram
871-
Can return the complex spectrum of segments within the signal.
872-
"""
873-
return _single_spectrum_helper(x=x, Fs=Fs, window=window, pad_to=pad_to,
874-
sides=sides, mode='magnitude')
875-
876-
877-
@docstring.dedent_interpd
878-
def angle_spectrum(x, Fs=None, window=None, pad_to=None,
879-
sides=None):
880-
"""
881-
Compute the angle of the frequency spectrum (wrapped phase spectrum) of
882-
*x*. Data is padded to a length of *pad_to* and the windowing function
883-
*window* is applied to the signal.
884-
885-
Parameters
886-
----------
887-
x : 1-D array or sequence
888-
Array or sequence containing the data
889-
890-
%(Spectral)s
891-
892-
%(Single_Spectrum)s
893-
894-
Returns
895-
-------
896-
spectrum : 1-D array
897-
The values for the angle spectrum in radians (real valued)
898-
899-
freqs : 1-D array
900-
The frequencies corresponding to the elements in *spectrum*
901-
902-
See Also
903-
--------
904-
complex_spectrum
905-
This function returns the angle value of `complex_spectrum`.
906-
magnitude_spectrum
907-
Returns the magnitudes of the corresponding frequencies.
908-
phase_spectrum
909-
Returns the phase (unwrapped angle) of the corresponding frequencies.
910-
specgram
911-
Can return the complex spectrum of segments within the signal.
912-
"""
913-
return _single_spectrum_helper(x=x, Fs=Fs, window=window, pad_to=pad_to,
914-
sides=sides, mode='angle')
915-
916-
917-
@docstring.dedent_interpd
918-
def phase_spectrum(x, Fs=None, window=None, pad_to=None,
919-
sides=None):
920-
"""
921-
Compute the phase of the frequency spectrum (unwrapped angle spectrum) of
922-
*x*. Data is padded to a length of *pad_to* and the windowing function
923-
*window* is applied to the signal.
924-
925-
Parameters
926-
----------
927-
x : 1-D array or sequence
928-
Array or sequence containing the data
929-
930-
%(Spectral)s
931-
932-
%(Single_Spectrum)s
796+
_single_spectrum_docs = """\
797+
Compute the {quantity} of *x*.
798+
Data is padded to a length of *pad_to* and the windowing function *window* is
799+
applied to the signal.
800+
801+
Parameters
802+
----------
803+
x : 1-D array or sequence
804+
Array or sequence containing the data
805+
806+
{Spectral}
807+
808+
{Single_Spectrum}
809+
810+
Returns
811+
-------
812+
spectrum : 1-D array
813+
The {quantity}.
814+
freqs : 1-D array
815+
The frequencies corresponding to the elements in *spectrum*.
816+
817+
See Also
818+
--------
819+
psd
820+
Returns the power spectral density.
821+
complex_spectrum
822+
Returns the complex-valued frequency spectrum.
823+
magnitude_spectrum
824+
Returns the absolute value of the `complex_spectrum`.
825+
angle_spectrum
826+
Returns the angle of the `complex_spectrum`.
827+
phase_spectrum
828+
Returns the phase (unwrapped angle) of the `complex_spectrum`.
829+
specgram
830+
Can return the complex spectrum of segments within the signal.
831+
"""
933832

934-
Returns
935-
-------
936-
spectrum : 1-D array
937-
The values for the phase spectrum in radians (real valued)
938833

939-
freqs : 1-D array
940-
The frequencies corresponding to the elements in *spectrum*
941-
942-
See Also
943-
--------
944-
complex_spectrum
945-
This function returns the phase value of `complex_spectrum`.
946-
magnitude_spectrum
947-
Returns the magnitudes of the corresponding frequencies.
948-
angle_spectrum
949-
Returns the angle (wrapped phase) of the corresponding frequencies.
950-
specgram
951-
Can return the complex spectrum of segments within the signal.
952-
"""
953-
return _single_spectrum_helper(x=x, Fs=Fs, window=window, pad_to=pad_to,
954-
sides=sides, mode='phase')
834+
complex_spectrum = functools.partial(_single_spectrum_helper, "complex")
835+
complex_spectrum.__doc__ = _single_spectrum_docs.format(
836+
quantity="complex-valued frequency spectrum",
837+
**docstring.interpd.params)
838+
magnitude_spectrum = functools.partial(_single_spectrum_helper, "magnitude")
839+
magnitude_spectrum.__doc__ = _single_spectrum_docs.format(
840+
quantity="magnitude (absolute value) of the frequency spectrum",
841+
**docstring.interpd.params)
842+
angle_spectrum = functools.partial(_single_spectrum_helper, "angle")
843+
angle_spectrum.__doc__ = _single_spectrum_docs.format(
844+
quantity="angle of the frequency spectrum (wrapped phase spectrum)",
845+
**docstring.interpd.params)
846+
phase_spectrum = functools.partial(_single_spectrum_helper, "phase")
847+
phase_spectrum.__doc__ = _single_spectrum_docs.format(
848+
quantity="phase of the frequency spectrum (unwrapped phase spectrum)",
849+
**docstring.interpd.params)
955850

956851

957852
@docstring.dedent_interpd

0 commit comments

Comments
 (0)
0