简体   繁体   English

谁能指导我如何从 wav 文件中计算以 hz 为单位的频率? 波形文件为 50 秒

[英]Can anyone guide me on how to calculate the frequency in hz from wav file? The wave file is of 50 secs

I am using numpy library to calculate freq = np.fft.rfftfreq(len_data, 1.0 / rate), If I am not wrong then this frequency is without unit.我正在使用 numpy 库来计算频率 = np.fft.rfftfreq(len_data, 1.0 / rate),如果我没有错,那么这个频率是没有单位的。 How can i convert it into hertz .我怎样才能把它转换成赫兹 I am using the following code:我正在使用以下代码:


import numpy as np
import scipy.io.wavfile
from scipy import signal

def read_wav_file(file_name):
    sample_rate, Data_audio  = scipy.io.wavfile.read(file_name)
    return sample_rate, Data_audio 

def getFFT(Data_audio, sample_rate):
    len_data = len(Data_audio)
    Data_audio = Data_audio * np.hamming(len_data)
    fft = np.fft.rfft(Data_audio)
    fft = np.abs(fft)
    ret_len_FFT = len(fft)
    freq = np.fft.rfftfreq(len_data, 1.0 / sample_rate)
    return ( freq[:int(len(freq))], fft[:int(ret_len_FFT)], ret_len_FFT )

sample_rate_rec, Data_audio_rec = read_wav_file('2020rec.wav')

frequency_rec, fft_rec, ret_lenFFT_rec = getFFT(Data_audio_rec, sample_rate_rec)

print("frequency_rec: " + str(frequency_rec) )

**Output**
frequency_rec: [0.00000000e+00 8.33481508e-03 1.66696302e-02 ... 2.39999833e+04
 2.39999917e+04 2.40000000e+04]


# To convert frequencies into float format
np.set_printoptions(formatter={'float':'{:f}'.format})
print("frequency_rec: " + str(frequency_rec) )

**Output**
frequency_rec: [0.000000 0.008307 0.016614 ... 23999.983386 23999.991693 24000.000000]

The numpy FFT package has a built-in function to calculate the frequency vector to go along with your FFT output. The numpy FFT package has a built-in function to calculate the frequency vector to go along with your FFT output. Note that scipy outputs the sample rate wile numpy wants the sample spacing so you must invert it first.请注意, scipy输出采样,而numpy需要采样间隔,因此您必须先反转它。

import numpy as np

def getFrequencies(Data_audio, sample_rate_rec):
    return np.fft.fftfreq(n=len(Data_audio), d=1/sample_rate)

Since the output of scipy.io.wavfile.read claims to be in samples/sec, the output of np.fft.fftfreq will be in cycles/sec aka Hertz. Since the output of scipy.io.wavfile.read claims to be in samples/sec, the output of np.fft.fftfreq will be in cycles/sec aka Hertz.

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

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