You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何限制scipy.signal.spectrogram仅计算1000-2000Hz频率范围?

How to Limit scipy.signal.spectrogram to the 1000-2000Hz Frequency Range

Great question—this is a super common need when working with narrowband signals, and it’s smart to want to cut down on unnecessary computation and data bloat. Let’s break this down clearly:

Can you directly restrict the spectrogram calculation to 1000-2000Hz?

Short answer: No, you can’t do this directly with scipy.signal.spectrogram. Here’s why:

  • The spectrogram relies on the Fast Fourier Transform (FFT), which computes frequency components across the entire Nyquist range (up to half your sampling frequency) for each time window. FFT is an all-or-nothing calculation—you can’t compute just a subset of frequencies; you have to generate the full spectrum first, then extract the range you care about.

How to extract the 1000-2000Hz range from the output

The good news is slicing the output arrays (f, t, Sxx) is straightforward and achieves exactly what you need. Here’s a concrete, reusable example:

Step 1: Compute the full spectrogram

First, calculate the spectrogram as you normally would. Let’s use a sample signal for demonstration:

import numpy as np
from scipy.signal import spectrogram

# Define your signal parameters
fs = 8000  # Sampling frequency (Hz)
duration = 10  # Total signal duration (seconds)
t = np.linspace(0, duration, fs * duration, endpoint=False)

# Create a test signal with components in your target range (1500Hz and 1800Hz)
sig = np.sin(2 * np.pi * 1500 * t) + np.sin(2 * np.pi * 1800 * t)

# Compute the full spectrogram
f, t_spec, Sxx = spectrogram(sig, fs=fs)

Step 2: Slice to keep only the 1000-2000Hz range

Use a boolean mask to filter the frequency array f, then apply the same mask to the spectrogram matrix Sxx:

# Create a mask for frequencies between 1000 and 2000Hz
freq_mask = (f >= 1000) & (f <= 2000)

# Extract the filtered frequency bins and corresponding spectrogram data
f_filtered = f[freq_mask]
Sxx_filtered = Sxx[freq_mask, :]
  • The time array t_spec stays unchanged—we’re only trimming the frequency dimension, not altering the time windows.

Step 3: Use the filtered data

You can now work with f_filtered, t_spec, and Sxx_filtered for your analysis (plotting, feature extraction, etc.). This reduces the data size and eliminates unnecessary frequency bins, cutting down on subsequent processing costs.

Bonus: Optimizing computation upfront

If you want to reduce initial computation time (instead of just trimming output), consider:

  1. Bandpass filtering first: Apply a bandpass filter to your signal to remove frequencies outside 1000-2000Hz before computing the spectrogram. This won’t reduce FFT calculation time (you still compute the full spectrum), but it cleans up noise and makes the filtered spectrogram easier to work with.
  2. Smaller FFT windows: If your frequency resolution needs allow, use a smaller nperseg parameter in spectrogram to reduce the total number of frequency bins. Just keep in mind the tradeoff: smaller windows improve time resolution but reduce frequency resolution.

内容的提问来源于stack exchange,提问作者gmatharu

火山引擎 最新活动