Bas*_*asj 12 python arrays audio numpy resampling
很容易重新采样数组
a = numpy.array([1,2,3,4,5,6,7,8,9,10])
Run Code Online (Sandbox Code Playgroud)
具有整数重采样因子.例如,使用因子2:
b = a[::2] # [1 3 5 7 9]
Run Code Online (Sandbox Code Playgroud)
但是使用非整数重采样因子,它不会那么容易:
c = a[::1.5] # [1 2 3 4 5 6 7 8 9 10] => not what is needed...
Run Code Online (Sandbox Code Playgroud)
它应该是(使用线性插值):
[1 2.5 4 5.5 7 8.5 10]
Run Code Online (Sandbox Code Playgroud)
或(通过采取阵列中最近的邻居)
[1 3 4 6 7 9 10]
Run Code Online (Sandbox Code Playgroud)
如何使用非整数重采样因子重新采样numpy数组?
应用示例:音频信号重采样/重新排样
xnx*_*xnx 20
NumPy有numpy.interp
线性插值:
In [1]: numpy.interp(np.arange(0, len(a), 1.5), np.arange(0, len(a)), a)
Out[1]: array([ 1. , 2.5, 4. , 5.5, 7. , 8.5, 10. ])
Run Code Online (Sandbox Code Playgroud)
SciPy scipy.interpolate.interp1d
可以进行线性和最近的插值(尽管哪个点最接近可能不明显):
In [2]: from scipy.interpolate import interp1d
In [3]: xp = np.arange(0, len(a), 1.5)
In [4]: lin = interp1d(np.arange(len(a)), a)
In [5]: lin(xp)
Out[5]: array([ 1. , 2.5, 4. , 5.5, 7. , 8.5, 10. ])
In [6]: nearest = interp1d(np.arange(len(a)), a, kind='nearest')
In [7]: nearest(xp)
Out[7]: array([ 1., 2., 4., 5., 7., 8., 10.])
Run Code Online (Sandbox Code Playgroud)
hpa*_*ulj 12
既然你提到这是来自音频.WAV文件的数据,你可能会看一下scipy.signal.resample
.
沿给定轴使用傅立叶方法重新采样
x
到num
样本.重采样信号以相同的值开始,
x
但以间隔为采样len(x) / num * (spacing of x)
.因为使用傅立叶方法,所以假设信号是周期性的.
你的线性阵列a
不适合测试它,因为它在外观上不是周期性的.但考虑sin
数据:
x=np.arange(10)
y=np.sin(x)
y1, x1 =signal.resample(y,15,x) # 10 pts resampled at 15
Run Code Online (Sandbox Code Playgroud)
比较这两者
y1-np.sin(x1) # or
plot(x, y, x1, y1)
Run Code Online (Sandbox Code Playgroud)
Bas*_*asj 10
由于scipy.signal.resample
可以很慢,我搜索了其他算法适用于音频。
似乎Erik de Castro Lopo的SRC(又名Secret Rabbit Code,又名libsamplerate)是可用的最佳重采样算法之一。
它由scikit的使用scikit.samplerate
,但是该库的安装似乎很复杂(我在Windows上放弃了)。
幸运的是,有一个libsamplerate
由Tino Wagner制造的易于使用且易于安装的Python包装器:https : //pypi.org/project/samplerate/。使用进行安装pip install samplerate
。用法:
import samplerate
from scipy.io import wavfile
sr, x = wavfile.read('input.wav') # 48 khz file
y = samplerate.resample(x, 44100 * 1.0 / 48000, 'sinc_best')
Run Code Online (Sandbox Code Playgroud)有趣的阅读/比较许多重采样解决方案:http : //signalsprocessed.blogspot.com/2016/08/audio-resampling-in-python.html
附录:重采样频率扫描(20hz至20khz)的频谱图比较:
1)原始
2)用libsamplerate / samplerate
模块重新采样
3)用numpy.interp
(“一维线性插值”)重新采样:
归档时间: |
|
查看次数: |
32235 次 |
最近记录: |