如何使用Python获取声音的信封? [英] How to obtain sound envelope using python?

查看:331
本文介绍了如何使用Python获取声音的信封?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我新的Python和也与声音信号分析。我试图让一个出生歌(斑胸草雀)的信封。它有一个非常快速的信号波动,我试着用不同的方法。比如我试图绘制的信号,并获得对,我发现(我加在code注释来理解它)其它的例子以下code基信封:

Hello I new with python and also with sound signal analysis. I am trying to get the envelope of a birth song (zebra finch). It has a very rapid signal fluctuations and I tried with different approach. For instance I tried to plot the signal and get the envelope with the following code base on other examples that I found (I added comments on the code to understand it):

#Import the libraries
from pylab import *
import numpy
import scipy.signal.signaltools as sigtool
import scipy, pylab
from scipy.io import wavfile
import wave, struct
import scipy.signal as signal

#Open the txt file and read the wave file (also save it as txt file)

f_out = open('mike_1_44100_.txt', 'w')
w     = scipy.io.wavfile.read("mike_1_44100_.wav") #here your sound file

a=w[1]
f_out.write('#time #z' + '\n')

#I print to check
print 'vector w'
print w[0],w[1]
print w

i=w[1].size
p=numpy.arange(i)*0.0000226 #to properly define the time signal with    the sample rate

print 'vector p:'
print p 

x=numpy.dstack([p,a])

print 'vector x:'
print x[0]

#saving file
numpy.savetxt('mike_1_44100_.txt',x[0])

f_out.close()
print 'i:'
print i

# num is the number of samples in the resampled signal.
num= np.ceil(float(i*0.0000226)/0.0015)
print num

y_resample, x_resample = scipy.signal.resample(numpy.abs(a),num, p,axis=0, window=('gaussian',150))

#y_resample, x_resample = scipy.signal.resample(numpy.abs(a), num, p,axis=-1, window=0)

#Aplaying a filter 

W1=float(5000)/(float(44100)/2) #the frequency  for the cut over the sample frequency

(b, a1) = signal.butter(4, W1, btype='lowpass')
aaa=a
slp =1* signal.filtfilt(b, a1, aaa)

#Taking the abs value of the signal the resample and finaly aplying the hilbert transform

y_resample2 =numpy.sqrt(numpy.abs(np.imag(sigtool.hilbert(slp, axis=-1)))**2+numpy.abs(np.real(sigtool.hilbert(slp, axis=-1)))**2)

print 'x sampled'
#print x_resample
print 'y sampled'
#print  y_resample

xx=x_resample #[0]
yy=y_resample #[1]

#ploting with some style

plot(p,a,label='Time Signal') #to plot amplitud vs time
#plot(p,numpy.abs(a),label='Time signal')
plot(xx,yy,label='Resampled time signal Fourier technique Gauss window 1.5 ms ', linewidth=3)
#plot(ww,label='Window', linewidth=3)
#plot(p,y_resample2,label='Hilbert transformed sime signal', linewidth=3)

grid(True)
pylab.xlabel("time [s]")
pylab.ylabel("Amplitde")

legend()
show()

在这里,我想两件事情,第一个是使用二次采样函数从SciPy的拿到信封,但我有一些问题,信号的幅度,我还不知道(我上传的傅里叶技术获得的图像,但系统不允许我):

Here I tried two things, the first is use the resample function from scipy to get the envelope, but I have some problem with the signal amplitude that I don't understand yet (I uploaded the image obtained with the fourier technique but system does not allow me):

二是使用希尔伯特变换得到的包络线(现在我上传与希尔伯特图像变换一次系统不允许我)可以运行我的code,并获得两个图像。但生病把这个链接的http:// ceciliajarne。 web.unq.edu.ar/?page_id=92&$p$pview=true

The second is to use the hilbert transform to get the envelope (now I uploaded the image with the hilbert transform again the system does not allow me) It is possible to run my code and obtain the two images. But ill put the with this link http://ceciliajarne.web.unq.edu.ar/?page_id=92&preview=true

现在的信封再次失败。我试图过滤信号正如我在一些例子中看到的,但是我的信号衰减,我不能够获得信封。 可能有人帮助我与我的code或一个更好的办法来获得信封?这是可能的,例如使用任何鸟鸣(我可以给你我的),但我需要看到什么复杂发生听起来并不简单的信号,因为它是非常不同的(与简单的声音这两种技术都OK了)。

Now the "envelope" fails again. I tried filtering the signal as i saw in some examples, but my signal is attenuated and i am not able to obtain the envelope. Could anybody help my with my code or with a better idea to get the envelope? It is possible to use as example any bird song (I can give you mine), but i need to see what happen with complex sounds not simple signals, because it is very different (with simple sounds both techniques are ok).

我也试图ADAP的code,我发现地:的http:// nipy .ORG / nitime /例子/ mtm_baseband_power.html

I also tried to adap the code that I found in: http://nipy.org/nitime/examples/mtm_baseband_power.html

但我不能够得到合适的参数为我的信号,我不明白的调制部分。我已经问到code语言开发,并直到等待答案。

But I am not able to get the proper parameters for my signal, and i don't understand the modulation part. I already ask to the code developers, and til waiting the answer.

推荐答案

由于与鸟歌曲调制频率可能会比载波频率即使具有快速变化的振幅低得多,近似到信封可以通过取的信号的绝对值,然后施加移动平均滤波器用说20ms的长度来获得。

Since with a bird song the "modulation frequency" probably will be much lower than the "carrier frequency" even with a rapidly varying amplitude, an approximation to the envelope could be obtained by taking the absolute value of your signal and then applying a moving average filter with say 20 ms length.

不过,你会不会有兴趣频率变化为好,充分表征的歌吗?在这种情况下采取的傅立叶变换过的移动窗口会给你更多的信息,即近似频率含量作为时间的函数。这就是我们人类听到,并帮助我们的鸟类区别对待。

Still, wouldn't you be interested in frequency variations as well, to adequately characterize the song? In that case taking the Fourier transform over a moving window would give you far more information, namely the approximate frequency content as a function of time. Which is what we humans hear and helps us discriminate between bird species.

我没有访问你送我的链接:

I don't have access to the link you sent me:

不tienesautorización第previsualizar borradores。

"No tienes autorización para previsualizar borradores."

如果你不想衰减,你应该逾期不申请巴特沃斯滤波器,也不走均线,但申请峰值检测来代替。

If you don't want the attenuation, you should neither apply a Butterworth filter nor take the moving average, but apply peak detection instead.

移动平均:每个输出样本是例如绝对值的平均50 preceding输入样本。输出将被削弱。

Moving average: Each output sample is the average of the absolute value of e.g. 50 preceding input samples. Output will be attenuated.

峰值检测:每个输出样本是最大的例如绝对值的50 preceding输入样本。输出将不被衰减。你可以低通滤波器之后摆脱剩下的楼梯riple的。

Peak detection: Each output sample is the maximum of the absolute value of e.g. 50 preceding input samples. Output will not be attenuated. You can lowpass filter afterwards to get rid of the remaining staircase "riple".

您想知道为什么如巴特沃斯滤波器衰减将你的信号。它几乎没有做,如果你的截止频率足够高,但它只是似乎是强烈衰减。你的输入信号不是在载体(哨子)和调制(信封),但产物的总和。过滤将限制频率内容。剩下的是频率成分(术语),而不是因素。你看减毒调制(信封),因为该频率成分确实是在你的信号要比原来的信封较弱present,因为它没有添加到您的运营商,但乘以它。因为该信封被乘以载波正弦,并不总是在其最大值,信封将被减弱,由调制处理,而不是由你的过滤分析

You wonder why e.g. a Butterworth filter will attenuate your signal. It hardly does if your cutoff frequency is high enough, but it just SEEMS to be strongly attenuated. Your input signal is not the sum of the carrier (whistle) and the modulation (envelope) but the product. Filtering will limit the frequency content. What remains are frequency components (terms) rather than factors. You see an attenuated modulation (envelope) because that frequency component is indeed present in your signal MUCH weaker than the original envelope, since it was not added to your carrier but multiplied with it. Since the carrier sinusoid that your envelope is multiplied with, is not always at its maximum value, the envelope will be "attenuated" by the modulation process, not by your filtering analysis.

在短期:如果要直接的(乘法)的信封,而不是(添加剂)频率分量由于调制(乘)与信封,采取峰值检测方法

In short: If you directly want the (multiplicative) envelope rather than the (additive) frequency component due to modulation (multiplication) with the envelope, take the peak detection approach.

峰值检测算法,只是为了让这个想法。

Peak detection algorithm in "Pythonish" pseudocode, just to get the idea.

# Untested, but apart from typos this should work fine
# No attention paid to speed, just to clarify the algorithm
# Input signal and output signal are Python lists
# Listcomprehensions will be a bit faster
# Numpy will be a lot faster

def getEnvelope (inputSignal):

    # Taking the absolute value

    absoluteSignal = []
    for sample in inputSignal:
        absoluteSignal.append (abs (sample))

    # Peak detection

    intervalLength = 50 # Experiment with this number, it depends on your sample frequency and highest "whistle" frequency
    outputSignal = []

    for baseIndex in range (intervalLength, len (absoluteSignal)):
        maximum = 0
        for lookbackIndex in range (intervalLength)
            maximum = max (absoluteSignal [baseIndex - lookbackIndex], maximum)
        outputSignal.append (maximum)

    return outputSignal

这篇关于如何使用Python获取声音的信封?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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