简体   繁体   English

如何从WAV文件获取振幅和频率列表

[英]How to get amplitude and frequency list from a wav file

We are trying to build a program to get amplitude and frequency list from an .wav file, trying it in Python. 我们正在尝试构建一个程序以从.wav文件获取幅度和频率列表,并在Python中进行尝试。

We tried pyaudio for that I don't know much about pyaudio, so I need some suggestions on it. 我们尝试pyaudio是因为我对pyaudio不太了解,所以我需要一些建议。

import scipy
import numpy as np

file = '123.wav'
from scipy.io import wavfile as wav
fs, data = wav.read(file)
length=len(data.shape)
#if length==2:
#    data= data.sum(axis=1)/2
n = data.shape[0]
sec = n/float(fs)
ts = 1.00/fs
t = scipy.arange(0,sec,ts)
FFT = abs(scipy.fft(data))
FFT_size = FFT[range(n//2)]
freq = scipy.fftpack.fftfreq(data.size, t[1]-t[0])
max_freq = max(freq)
min_freq = min(freq)
plot_freq(freq, n, t, data)

The actual result returning is frequency list. 返回的实际结果是频率列表。 I also want amplitude list don't know how to get it. 我还希望幅度列表不知道如何获取它。

typically a call to an fft api will return an array of imaginary numbers where each array element contains a complex number in the form of ( Areal, AImaginary ) where each element of the array represents a frequency (the value of the freq is implied by array index [find the formula to calc freq based on array index]) 通常,调用fft api将返回一个虚数数组,其中每个数组元素都包含(Areal,AImaginary)形式的复数,其中数组的每个元素都代表一个频率(freq的值由array隐含index [根据数组索引找到要计算频率的公式])

on the complex array element 0 represents frequency 0 which is your direct current offset, then freq of each subsequent freq is calculated using 复数数组元素上的0代表频率0,这是您的直流偏移,然后使用以下公式计算每个后续频率的频率

incr_freq := sample_rate / number_of_samples

so for that to be meaningful you must have prior knowledge of the sample rate of your source input time series ( audio or whatever ) and number of samples is just the length of the floating point raw audio curve array you fed into your fft call 因此,要使它有意义,您必须先了解源输入时间序列(音频或其他)的采样率,并且采样数只是您馈入fft调用的浮点原始音频曲线阵列的长度

... as you iterate across this array of complex numbers calculate the amplitude using the Areal and AImaginary of each frequency bin's complex number using formula ...当您遍历此复数数组时,使用公式使用每个频箱复数的Areal和AImaginary计算幅度,

curr_mag = 2.0 * math.Sqrt(curr_real*curr_real+curr_imag*curr_imag) / number_of_samples

as you iterate across the complex array returned from your fft call be aware of notion of Nyquist Limit which means you only consume the first half of the number of elements of that complex array (and double the magnitude of each freq - see formula above) 当您遍历从fft调用返回的复杂数组时,请注意Nyquist限制的概念,这意味着您仅消耗了该复杂数组元素数量的前一半(并且使每个频率的大小加倍-参见上面的公式)

... see the full pseudocode at Get frequency with highest amplitude from FFT ...请参见从FFT获取具有最高幅度的频率处的完整伪代码

... I ran your code and nothing happened ... what is the meaning of your python ...我运行了您的代码,但没有任何反应...您的python是什么意思

[range(n//2)] [范围(n // 2)]

You possibly want pitch, not spectral frequency, which is a different algorithm than just using an FFT to find the highest magnitude. 您可能需要音高,而不是频谱频率,这是与仅使用FFT查找最高幅度不同的算法。 An FFT returns the entire spectral frequency range (every frequency up to Fs/2, not just one frequency), in your case for the entire file. FFT返回整个频谱频率范围(每个文件直到Fs / 2的频率,而不仅仅是一个频率)。 And the highest magnitude is often not for the pitch frequency (possibly for some high overtone instead). 最高音调通常不用于音调频率(可能是针对某些高泛音)。

You also took the FFT of the entire file, not a bunch of FFTs for time slices (usually small overlapping windows) at the time increment you desire for your list's temporal resolution. 您还需要获取整个文件的FFT,而不是按照您的列表的时间分辨率所需的时间增量对时间片(通常是小的重叠窗口)进行一堆FFT。 This will produce a time array of all the FFT frequency arrays (thus, a 2D array). 这将产生所有FFT频率阵列的时间阵列(因此是2D阵列)。 Usually called a spectrogram. 通常称为频谱图。 There may be a built in function for this in some library. 在某些库中可能有一个内置函数。

Can I make amplitude from this formula 我可以根据这个公式得出振幅吗

the frequency of the wave is set by whatever is driving the oscillation in the medium. 波的频率由驱动介质振荡的方式决定。 Examples are a speaker that sets up a sound wave, or the hand that shakes the end of a stretched string. 例如,设置声波的扬声器,或摇晃拉长的琴弦末端的手。 the speed of the wave is a property of the medium. 波速是介质的属性。 the wavelength of the wave is then determined by the frequency and speed: λ = v/f 然后,波的波长取决于频率和速度:λ= v / f

I don't know it gonna be the right process or not 我不知道这将是正确的过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM