本文整理汇总了Python中scipy.signal方法的典型用法代码示例。如果您正苦于以下问题:Python scipy.signal方法的具体用法?Python scipy.signal怎么用?Python scipy.signal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy
的用法示例。
在下文中一共展示了scipy.signal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_delta_deltas
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def add_delta_deltas(filterbanks, name=None):
"""Compute time first and second-order derivative channels.
Args:
filterbanks: float32 tensor with shape [batch_size, len, num_bins, 1]
name: scope name
Returns:
float32 tensor with shape [batch_size, len, num_bins, 3]
"""
delta_filter = np.array([2, 1, 0, -1, -2])
delta_delta_filter = scipy.signal.convolve(delta_filter, delta_filter, "full")
delta_filter_stack = np.array(
[[0] * 4 + [1] + [0] * 4, [0] * 2 + list(delta_filter) + [0] * 2,
list(delta_delta_filter)],
dtype=np.float32).T[:, None, None, :]
delta_filter_stack /= np.sqrt(
np.sum(delta_filter_stack**2, axis=0, keepdims=True))
filterbanks = tf.nn.conv2d(
filterbanks, delta_filter_stack, [1, 1, 1, 1], "SAME", data_format="NHWC",
name=name)
return filterbanks
示例2: extract_chip_samples
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def extract_chip_samples(samples):
a = array(samples)
f = scipy.fft(a*a)
p = find_clock_frequency(abs(f))
if 0 == p:
return []
cycles_per_sample = (p*1.0)/len(f)
clock_phase = 0.25 + numpy.angle(f[p])/(tau)
if clock_phase <= 0.5:
clock_phase += 1
chip_samples = []
for i in range(len(a)):
if clock_phase >= 1:
clock_phase -= 1
chip_samples.append(a[i])
clock_phase += cycles_per_sample
return chip_samples
# input: complex valued samples, FFT bin number of chip rate
# input signal must be centered at 0 frequency
# output: number of chips found in repetitive chip sequence
示例3: cosine
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def cosine(M):
"""Gernerate a halfcosine window of given length
Uses :code:`scipy.signal.cosine` by default. However since this window
function has only recently been merged into mainline SciPy, a fallback
calculation is in place.
Parameters
----------
M : int
Length of the window.
Returns
-------
data : array_like
The window function
"""
try:
import scipy.signal
return scipy.signal.cosine(M)
except AttributeError:
return numpy.sin(numpy.pi / M * (numpy.arange(0, M) + .5))
示例4: _ecg_clean_elgendi
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _ecg_clean_elgendi(ecg_signal, sampling_rate=1000):
"""From https://github.com/berndporr/py-ecg-detectors/
- Elgendi, Mohamed & Jonkman, Mirjam & De Boer, Friso. (2010). Frequency Bands Effects on QRS
Detection. The 3rd International Conference on Bio-inspired Systems and Signal Processing
(BIOSIGNALS2010). 428-431.
"""
f1 = 8 / sampling_rate
f2 = 20 / sampling_rate
b, a = scipy.signal.butter(2, [f1 * 2, f2 * 2], btype="bandpass")
return scipy.signal.lfilter(b, a, ecg_signal) # Return filtered
# =============================================================================
# Hamilton (2002)
# =============================================================================
示例5: _ecg_findpeaks_pantompkins
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _ecg_findpeaks_pantompkins(signal, sampling_rate=1000):
"""From https://github.com/berndporr/py-ecg-detectors/
- Jiapu Pan and Willis J. Tompkins. A Real-Time QRS Detection Algorithm.
In: IEEE Transactions on Biomedical Engineering BME-32.3 (1985), pp. 230–236.
"""
diff = np.diff(signal)
squared = diff * diff
N = int(0.12 * sampling_rate)
mwa = _ecg_findpeaks_MWA(squared, N)
mwa[: int(0.2 * sampling_rate)] = 0
mwa_peaks = _ecg_findpeaks_peakdetect(mwa, sampling_rate)
mwa_peaks = np.array(mwa_peaks, dtype="int")
return mwa_peaks
# =============================================================================
# Hamilton (2002)
# =============================================================================
示例6: _ecg_delineator_peak_T_offset
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _ecg_delineator_peak_T_offset(rpeak, heartbeat, R, T):
if T is None:
return np.nan
segment = heartbeat.iloc[R + T :] # Select left of P wave
try:
signal = signal_smooth(segment["Signal"].values, size=R / 10)
except TypeError:
signal = segment["Signal"]
if len(signal) < 2:
return np.nan
signal = np.gradient(np.gradient(signal))
T_offset = np.argmax(signal)
return rpeak + T + T_offset
# =============================================================================
# Internals
# =============================================================================
示例7: _resample_pandas
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _resample_pandas(signal, desired_length):
# Convert to Time Series
index = pd.date_range("20131212", freq="L", periods=len(signal))
resampled_signal = pd.Series(signal, index=index)
# Create resampling factor
resampling_factor = str(np.round(1 / (desired_length / len(signal)), 6)) + "L"
# Resample
resampled_signal = resampled_signal.resample(resampling_factor).bfill().values
# Sanitize
resampled_signal = _resample_sanitize(resampled_signal, desired_length)
return resampled_signal
# =============================================================================
# Internals
# =============================================================================
示例8: _signal_filter_savgol
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _signal_filter_savgol(signal, sampling_rate=1000, order=2, window_size="default"):
"""Filter a signal using the Savitzky-Golay method.
Default window size is chosen based on `Sadeghi, M., & Behnia, F. (2018). Optimum window length of
Savitzky-Golay filters with arbitrary order. arXiv preprint arXiv:1808.10489.
<https://arxiv.org/ftp/arxiv/papers/1808/1808.10489.pdf>`_.
"""
window_size = _signal_filter_windowsize(window_size=window_size, sampling_rate=sampling_rate)
filtered = scipy.signal.savgol_filter(signal, window_length=window_size, polyorder=order)
return filtered
# =============================================================================
# FIR
# =============================================================================
示例9: _signal_filter_butterworth_ba
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _signal_filter_butterworth_ba(signal, sampling_rate=1000, lowcut=None, highcut=None, order=5):
"""Filter a signal using IIR Butterworth B/A method."""
# Get coefficients
freqs, filter_type = _signal_filter_sanitize(lowcut=lowcut, highcut=highcut, sampling_rate=sampling_rate)
b, a = scipy.signal.butter(order, freqs, btype=filter_type, output="ba", fs=sampling_rate)
try:
filtered = scipy.signal.filtfilt(b, a, signal, method="gust")
except ValueError:
filtered = scipy.signal.filtfilt(b, a, signal, method="pad")
return filtered
# =============================================================================
# Bessel
# =============================================================================
示例10: _signal_filter_powerline
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _signal_filter_powerline(signal, sampling_rate, powerline=50):
"""Filter out 50 Hz powerline noise by smoothing the signal with a moving average kernel with the width of one
period of 50Hz."""
if sampling_rate >= 100:
b = np.ones(int(sampling_rate / powerline))
else:
b = np.ones(2)
a = [len(b)]
y = scipy.signal.filtfilt(b, a, signal, method="pad")
return y
# =============================================================================
# Utility
# =============================================================================
示例11: _rsp_clean_khodadad2018
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _rsp_clean_khodadad2018(rsp_signal, sampling_rate=1000):
"""The algorithm is based on (but not an exact implementation of) the "Zero-crossing algorithm with amplitude
threshold" by `Khodadad et al. (2018)
<https://iopscience.iop.org/article/10.1088/1361-6579/aad7e6/meta>`_.
"""
# Slow baseline drifts / fluctuations must be removed from the raw
# breathing signal (i.e., the signal must be centered around zero) in order
# to be able to reliable detect zero-crossings.
# Remove baseline by applying a lowcut at .05Hz (preserves breathing rates
# higher than 3 breath per minute) and high frequency noise by applying a
# highcut at 3 Hz (preserves breathing rates slower than 180 breath per
# minute).
clean = signal_filter(
rsp_signal, sampling_rate=sampling_rate, lowcut=0.05, highcut=3, order=2, method="butterworth_ba"
)
return clean
# =============================================================================
# BioSPPy
# =============================================================================
示例12: _rsp_clean_biosppy
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def _rsp_clean_biosppy(rsp_signal, sampling_rate=1000):
"""Uses the same defaults as `BioSPPy.
<https://github.com/PIA-Group/BioSPPy/blob/master/biosppy/signals/resp.py>`_.
"""
# Parameters
order = 2
frequency = [0.1, 0.35]
frequency = 2 * np.array(frequency) / sampling_rate # Normalize frequency to Nyquist Frequency (Fs/2).
# Filtering
b, a = scipy.signal.butter(N=order, Wn=frequency, btype="bandpass", analog=False)
filtered = scipy.signal.filtfilt(b, a, rsp_signal)
# Baseline detrending
clean = signal_detrend(filtered, order=0)
return clean
示例13: test_signal_detrend
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def test_signal_detrend():
signal = np.cos(np.linspace(start=0, stop=10, num=1000)) # Low freq
signal += np.cos(np.linspace(start=0, stop=100, num=1000)) # High freq
signal += 3 # Add baseline
rez_nk = nk.signal_detrend(signal, order=1)
rez_scipy = scipy.signal.detrend(signal, type="linear")
assert np.allclose(np.mean(rez_nk - rez_scipy), 0, atol=0.000001)
rez_nk = nk.signal_detrend(signal, order=0)
rez_scipy = scipy.signal.detrend(signal, type="constant")
assert np.allclose(np.mean(rez_nk - rez_scipy), 0, atol=0.000001)
# Tarvainen
rez_nk = nk.signal_detrend(signal, method="tarvainen2002", regularization=500)
assert np.allclose(np.mean(rez_nk - signal), -2.88438737697, atol=0.000001)
示例14: test_signal_filter
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def test_signal_filter():
signal = np.cos(np.linspace(start=0, stop=10, num=1000)) # Low freq
signal += np.cos(np.linspace(start=0, stop=100, num=1000)) # High freq
filtered = nk.signal_filter(signal, highcut=10)
assert np.std(signal) > np.std(filtered)
# Generate 10 seconds of signal with 2 Hz oscillation and added 50Hz powerline-noise.
sampling_rate = 250
samples = np.arange(10 * sampling_rate)
signal = np.sin(2 * np.pi * 2 * (samples / sampling_rate))
powerline = np.sin(2 * np.pi * 50 * (samples / sampling_rate))
signal_corrupted = signal + powerline
signal_clean = nk.signal_filter(signal_corrupted, sampling_rate=sampling_rate, method="powerline")
# import matplotlib.pyplot as plt
# figure, (ax0, ax1, ax2) = plt.subplots(nrows=3, ncols=1, sharex=True)
# ax0.plot(signal_corrupted)
# ax1.plot(signal)
# ax2.plot(signal_clean * 100)
assert np.allclose(sum(signal_clean - signal), -2, atol=0.2)
示例15: applyFilter
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import signal [as 别名]
def applyFilter(data, b, a, padding=100, bidir=True):
"""Apply a linear filter with coefficients a, b. Optionally pad the data before filtering
and/or run the filter in both directions."""
try:
import scipy.signal
except ImportError:
raise Exception("applyFilter() requires the package scipy.signal.")
d1 = data.view(np.ndarray)
if padding > 0:
d1 = np.hstack([d1[:padding], d1, d1[-padding:]])
if bidir:
d1 = scipy.signal.lfilter(b, a, scipy.signal.lfilter(b, a, d1)[::-1])[::-1]
else:
d1 = scipy.signal.lfilter(b, a, d1)
if padding > 0:
d1 = d1[padding:-padding]
if (hasattr(data, 'implements') and data.implements('MetaArray')):
return MetaArray(d1, info=data.infoCopy())
else:
return d1