如何应用低通滤波器,其截止频率从例如线性变化(或比线性曲线更一般)。沿时间从10000hz到200hz,具有numpy/scipy,可能没有其他库?
例子:
这是一个简单的100hz低通的方法:
from scipy.io import wavfile
import numpy as np
from scipy.signal import butter, lfilter
sr, x = wavfile.read('test.wav')
b, a = butter(2, 100.0 / sr, btype='low') # Butterworth
y = lfilter(b, a, x)
wavfile.write('out.wav', sr, np.asarray(y, dtype=np.int16))
但是如何使临界值变化呢?
注意:我已经读过Applying time-variant filter in Python,但是答案很复杂(它通常适用于多种过滤器)。
最佳答案
一种比较简单的方法是使滤波器保持固定并改为调制信号时间。例如,如果信号时间快10倍,则10KHz低通将像标准时间中的1KHz低通一样起作用。
为此,我们需要解决一个简单的ODE
dy 1
-- = ----
dt f(y)
此处t
是实时调制时间y
,而f
是时间y
所需的截止时间。原型(prototype)实现:
from __future__ import division
import numpy as np
from scipy import integrate, interpolate
from scipy.signal import butter, lfilter, spectrogram
slack_l, slack = 0.1, 1
cutoff = 50
L = 25
from scipy.io import wavfile
sr, x = wavfile.read('capriccio.wav')
x = x[:(L + slack) * sr, 0]
x = x
# sr = 44100
# x = np.random.normal(size=((L + slack) * sr,))
b, a = butter(2, 2 * cutoff / sr, btype='low') # Butterworth
# cutoff function
def f(t):
return (10000 - 1000 * np.clip(t, 0, 9) - 1000 * np.clip(t-19, 0, 0.8)) \
/ cutoff
# and its reciprocal
def fr(_, t):
return cutoff / (10000 - 1000 * t.clip(0, 9) - 1000 * (t-19).clip(0, 0.8))
# modulate time
# calculate upper end of td first
tdmax = integrate.quad(f, 0, L + slack_l, points=[9, 19, 19.8])[0]
span = (0, tdmax)
t = np.arange(x.size) / sr
tdinfo = integrate.solve_ivp(fr, span, np.zeros((1,)),
t_eval=np.arange(0, span[-1], 1 / sr),
vectorized=True)
td = tdinfo.y.ravel()
# modulate signal
xd = interpolate.interp1d(t, x)(td)
# and linearly filter
yd = lfilter(b, a, xd)
# modulate signal back to linear time
y = interpolate.interp1d(td, yd)(t[:-sr*slack])
# check
import pylab
xa, ya, z = spectrogram(y, sr)
pylab.pcolor(ya, xa, z, vmax=2**8, cmap='nipy_spectral')
pylab.savefig('tst.png')
wavfile.write('capriccio_vandalized.wav', sr, y.astype(np.int16))
样本输出:BWV 826 Capriccio的前25秒频谱图,通过时弯实现时变低通。
关于python - 具有时变截止频率的低通滤波器,使用Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52403589/