← Back
Editing: _spectral_py.cpython-311.pyc
� d�c+ � � � d Z ddlZddlmZ ddlmZ ddlm Z ddl mZ ddlm Z mZmZmZ ddlZg d �Z d(d�Z d)d�Z d*d�Z d*d�Z d+d�Zd,d�Zd,d�Z d-d �Z d.d"�Z d/d#�Z d0d$�Zd%� Zd&� Zd'� Z dS )1zTools for spectral analysis. � N)�fft� )�_signaltools)� get_window)�_lombscargle)� const_ext�even_ext�odd_ext�zero_ext) �periodogram�welch�lombscargle�csd� coherence�spectrogram�stft�istft� check_COLA� check_NOLAFc �� � t j | t j �� � } t j |t j �� � }t j |t j �� � }| j dk sJ �|j dk sJ �|j dk sJ �|r't | ||� � � z |� � }nt | ||� � }|r|dt j ||� � z z }|S )a� lombscargle(x, y, freqs) Computes the Lomb-Scargle periodogram. The Lomb-Scargle periodogram was developed by Lomb [1]_ and further extended by Scargle [2]_ to find, and test the significance of weak periodic signals with uneven temporal sampling. When *normalize* is False (default) the computed periodogram is unnormalized, it takes the value ``(A**2) * N/4`` for a harmonic signal with amplitude A for sufficiently large N. When *normalize* is True the computed periodogram is normalized by the residuals of the data around a constant reference model (at zero). Input arrays should be 1-D and will be cast to float64. Parameters ---------- x : array_like Sample times. y : array_like Measurement values. freqs : array_like Angular frequencies for output periodogram. precenter : bool, optional Pre-center measurement values by subtracting the mean. normalize : bool, optional Compute normalized periodogram. Returns ------- pgram : array_like Lomb-Scargle periodogram. Raises ------ ValueError If the input arrays `x` and `y` do not have the same shape. See Also -------- istft: Inverse Short Time Fourier Transform check_COLA: Check whether the Constant OverLap Add (COLA) constraint is met welch: Power spectral density by Welch's method spectrogram: Spectrogram by Welch's method csd: Cross spectral density by Welch's method Notes ----- This subroutine calculates the periodogram using a slightly modified algorithm due to Townsend [3]_ which allows the periodogram to be calculated using only a single pass through the input arrays for each frequency. The algorithm running time scales roughly as O(x * freqs) or O(N^2) for a large number of samples and frequencies. References ---------- .. [1] N.R. Lomb "Least-squares frequency analysis of unequally spaced data", Astrophysics and Space Science, vol 39, pp. 447-462, 1976 .. [2] J.D. Scargle "Studies in astronomical time series analysis. II - Statistical aspects of spectral analysis of unevenly spaced data", The Astrophysical Journal, vol 263, pp. 835-853, 1982 .. [3] R.H.D. Townsend, "Fast calculation of the Lomb-Scargle periodogram using graphics processing units.", The Astrophysical Journal Supplement Series, vol 191, pp. 247-253, 2010 Examples -------- >>> import numpy as np >>> import matplotlib.pyplot as plt >>> rng = np.random.default_rng() First define some input parameters for the signal: >>> A = 2. >>> w0 = 1. # rad/sec >>> nin = 150 >>> nout = 100000 Randomly generate sample times: >>> x = rng.uniform(0, 10*np.pi, nin) Plot a sine wave for the selected times: >>> y = A * np.cos(w0*x) Define the array of frequencies for which to compute the periodogram: >>> w = np.linspace(0.01, 10, nout) Calculate Lomb-Scargle periodogram: >>> import scipy.signal as signal >>> pgram = signal.lombscargle(x, y, w, normalize=True) Now make a plot of the input data: >>> fig, (ax_t, ax_w) = plt.subplots(2, 1, constrained_layout=True) >>> ax_t.plot(x, y, 'b+') >>> ax_t.set_xlabel('Time [s]') Then plot the normalized periodogram: >>> ax_w.plot(w, pgram) >>> ax_w.set_xlabel('Angular frequency [rad/s]') >>> ax_w.set_ylabel('Normalized amplitude') >>> plt.show() ��dtyper � )�np�ascontiguousarray�float64�ndimr �mean�dot)�x�y�freqs� precenter� normalize�pgrams �;/usr/lib/python3/dist-packages/scipy/signal/_spectral_py.pyr r s� � �r ��Q�b�j�1�1�1�A� ��Q�b�j�1�1�1�A�� ��b�j�9�9�9�E��6�Q�;�;�;�;��6�Q�;�;�;�;��:��?�?�?�?�� *��Q��A�F�F�H�H��e�4�4����Q��5�)�)��� "� ��R�V�A�q�\�\�!�!���L� � �?�boxcar�constantT�density���c � � t j | � � } | j dk r2t j | j � � t j | j � � fS |�d}|�| j | }n�|| j | k r|}n�|| j | k r| j | }nk|| j | k rZt j dd� gt | j � � z } t j d|� | |<