gpt4 book ai didi

python - 使用python过滤WAV文件

转载 作者:行者123 更新时间:2023-12-03 01:27:34 24 4
gpt4 key购买 nike

因此,我最近成功构建了一个系统,该系统将完全使用python记录,绘制和播放音频wav文件。现在,我尝试在录制和开始打印文件并将其输出到扬声器之间进行一些过滤和音频混合。但是,我不知道从哪里开始。现在,我要读取一个初始wav文件,应用一个低通滤波器,然后将新过滤的数据重新打包到一个新的wav文件中。这是我记录初始数据后用来绘制初始数据的代码。

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys

spf = wave.open('wavfile.wav','r')

#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')

plt.figure(1)
plt.title('Signal Wave...')
plt.plot(signal)

这是一些我用来生成单一音调的测试音频文件的代码:
import numpy as np
import wave
import struct

freq = 440.0
data_size = 40000
fname = "High_A.wav"
frate = 11025.0
amp = 64000.0

sine_list_x = []
for x in range(data_size):
sine_list_x.append(np.sin(2*np.pi*freq*(x/frate)))

wav_file = wave.open(fname, "w")

nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"

wav_file.setparams((nchannels, sampwidth, framerate, nframes,
comptype, compname))

for s in sine_list_x:
wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()

不过,我不太确定如何应用上述音频过滤器并重新打包。您能提供的任何帮助和/或建议将不胜感激。

最佳答案

第一步:您需要哪种音频滤波器?

选择已过滤的频段

  • Low-pass Filter:从音频信号
  • 中去除最高频率
  • High-pass Filter:从音频信号
  • 中删除最低频率
  • Band-pass Filter:从音频信号
  • 中去除最高和最低频率

    对于以下步骤,我假设您需要一个 低通滤波器

    选择截止频率

    Cutoff frequency是您的信号将被衰减-3dB的频率。

    您的示例信号为440Hz,因此让我们选择 400Hz Cutoff frequency。然后,低通400Hz滤波器会衰减440Hz信号(大于-3dB)。

    选择您的过滤器类型

    根据this other stackoverflow answer

    Filter design is beyond the scope of Stack Overflow - that's a DSP problem, not a programming problem. Filter design is covered by any DSP textbook - go to your library. I like Proakis and Manolakis' Digital Signal Processing. (Ifeachor and Jervis' Digital Signal Processing isn't bad either.)



    在一个简单的例子中,我建议使用移动平均滤波器(用于简单的低通滤波器)。

    参见Moving average

    Mathematically, a moving average is a type of convolution and so it can be viewed as an example of a low-pass filter used in signal processing



    该移动平均低通滤波器是基本的滤波器,非常易于使用和理解。

    移动平均值的参数是窗口长度

    moving average窗口长度与Cutoff frequency之间的关系需要一点数学运算,并且在here中进行了说明

    该代码将是

    import math

    sampleRate = 11025.0
    cutOffFrequency = 400.0
    freqRatio = cutOffFrequency / sampleRate

    N = int(math.sqrt(0.196201 + freqRatio**2) / freqRatio)

    因此,在此示例中,窗口长度将为 12

    第二步:对过滤器进行编码

    手工制作的移动平均线

    specific discussion on how to create a moving average in python

    Alleo的解决方案是
    def running_mean(x, windowSize):
    cumsum = numpy.cumsum(numpy.insert(x, 0, 0))
    return (cumsum[windowSize:] - cumsum[:-windowSize]) / windowSize

    filtered = running_mean(signal, N)

    使用lfilter

    另外,如dpwilson所建议,我们也可以使用lfilter
    win = numpy.ones(N)
    win *= 1.0/N
    filtered = scipy.signal.lfilter(win, [1], signal).astype(channels.dtype)

    第三步:放在一起
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    import math
    import contextlib

    fname = 'test.wav'
    outname = 'filtered.wav'

    cutOffFrequency = 400.0

    # from http://stackoverflow.com/questions/13728392/moving-average-or-running-mean
    def running_mean(x, windowSize):
    cumsum = np.cumsum(np.insert(x, 0, 0))
    return (cumsum[windowSize:] - cumsum[:-windowSize]) / windowSize

    # from http://stackoverflow.com/questions/2226853/interpreting-wav-data/2227174#2227174
    def interpret_wav(raw_bytes, n_frames, n_channels, sample_width, interleaved = True):

    if sample_width == 1:
    dtype = np.uint8 # unsigned char
    elif sample_width == 2:
    dtype = np.int16 # signed 2-byte short
    else:
    raise ValueError("Only supports 8 and 16 bit audio formats.")

    channels = np.fromstring(raw_bytes, dtype=dtype)

    if interleaved:
    # channels are interleaved, i.e. sample N of channel M follows sample N of channel M-1 in raw data
    channels.shape = (n_frames, n_channels)
    channels = channels.T
    else:
    # channels are not interleaved. All samples from channel M occur before all samples from channel M-1
    channels.shape = (n_channels, n_frames)

    return channels

    with contextlib.closing(wave.open(fname,'rb')) as spf:
    sampleRate = spf.getframerate()
    ampWidth = spf.getsampwidth()
    nChannels = spf.getnchannels()
    nFrames = spf.getnframes()

    # Extract Raw Audio from multi-channel Wav File
    signal = spf.readframes(nFrames*nChannels)
    spf.close()
    channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True)

    # get window size
    # from http://dsp.stackexchange.com/questions/9966/what-is-the-cut-off-frequency-of-a-moving-average-filter
    freqRatio = (cutOffFrequency/sampleRate)
    N = int(math.sqrt(0.196196 + freqRatio**2)/freqRatio)

    # Use moviung average (only on first channel)
    filtered = running_mean(channels[0], N).astype(channels.dtype)

    wav_file = wave.open(outname, "w")
    wav_file.setparams((1, ampWidth, sampleRate, nFrames, spf.getcomptype(), spf.getcompname()))
    wav_file.writeframes(filtered.tobytes('C'))
    wav_file.close()

    关于python - 使用python过滤WAV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59404909/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com