重新采样一个numpy数组 [英] Resample a numpy array

查看:268
本文介绍了重新采样一个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很容易对

 a = numpy.array([1,2,3,4,5,6,7,8,9,10])

具有整数重采样因子.例如,系数为2:

with an integer resampling factor. For instance, with a factor 2 :

b = a[::2]    # [1 3 5 7 9]

但是由于使用非整数的重采样因子,因此操作起来并不容易:

But with a non-integer resampling factor, it doesn't work so easily :

c = a[::1.5]    # [1 2 3 4 5 6 7 8 9 10]  => not what is needed...

应为(使用线性插值):

It should be (with linear interpolation):

[1 2.5 4 5.5 7 8.5 10]

或(通过获取数组中最近的邻居)

or (by taking the nearest neighbour in the array)

[1 3 4 6 7 9 10]

如何使用非整数重采样因子对numpy数组进行重采样?

应用示例:音频信号重采样/重音调

Example of application: audio signal resampling / repitching

推荐答案

由于scipy.signal.resample可以是

As scipy.signal.resample can be very slow, I searched for other algorithms adapted for audio.

似乎Erik de Castro Lopo的 SRC (又名Secret Rabbit Code aka libsamplerate)是目前可用的最佳重采样算法之一.

It seems that Erik de Castro Lopo's SRC (a.k.a. Secret Rabbit Code a.k.a. libsamplerate) is one of the best resampling algorithms available.

  • 它是scikit的scikit.samplerate使用的,但是此库的安装似乎很复杂(我在Windows上已放弃).

  • It is used by scikit's scikit.samplerate, but this library seems to be complicated to install (I gave up on Windows).

幸运的是,由Tino Wagner制作了一个易于使用且易于安装的libsamplerate Python包装器:

Fortunately, there is an easy-to-use and easy-to-install Python wrapper for libsamplerate, made by Tino Wagner: https://pypi.org/project/samplerate/. Installation with pip install samplerate. Usage:

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')  

有趣的阅读/许多重采样解决方案的比较: http://signalsprocessed.blogspot.com/2016/08/audio -resampling-in-python.html

Interesting reading / comparison of many resampling solutions: http://signalsprocessed.blogspot.com/2016/08/audio-resampling-in-python.html

附录:重新采样的频率扫描(20Hz至20khz)的频谱图比较:

Addendum: comparison of spectrograms of a resampled frequency sweep (20hz to 20khz):

1)原始

2)使用libsamplerate/samplerate模块重新采样

2) Resampled with libsamplerate / samplerate module

3)使用 numpy.interp 重新采样( 一维线性插值"):

3) Resampled with numpy.interp ("One-dimensional linear interpolation"):

这篇关于重新采样一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆