如何将wav文件的波形移动180度

时间:2018-05-09 09:59:43

标签: python numpy fft wav pyaudio

有没有办法在python中改变wav文件的阶段?我正在努力实现主动降噪。我打算做的是记录环境噪声,然后将其相位偏移180度。然后,我将录制另一个wav文件,此时有人与麦克风交谈,然后我将第二个wav文件与180度异相的文件组合,希望取消或降低噪音。我用大胆尝试了它,它的工作原理。我怎样才能将这个想法转化为python编码?

这是关于主动降噪的原始脚本:

import pyaudio
import numpy as np
import scipy.signal

CHUNK = 1024*2

WIDTH = 2
DTYPE = np.int16
MAX_INT = 32768.0

CHANNELS = 1
RATE = 11025*1
RECORD_SECONDS = 20

j = np.complex(0,1)


p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)

print("* recording")

# initialize filter variables
fir = np.zeros(CHUNK * 2)
fir[:(2*CHUNK)] = 1.
fir /= fir.sum()

fir_last = fir
avg_freq_buffer = np.zeros(CHUNK)
obj = -np.inf
t = 10

# initialize sample buffer
buffer = np.zeros(CHUNK * 2)

#for i in np.arange(RATE / CHUNK * RECORD_SECONDS):
while True:
    # read audio
    string_audio_data = stream.read(CHUNK)
    audio_data = np.fromstring(string_audio_data, dtype=DTYPE)
    normalized_data = audio_data / MAX_INT
    freq_data = np.fft.fft(normalized_data)

    # synthesize audio
    buffer[CHUNK:] = np.random.randn(CHUNK)
    freq_buffer = np.fft.fft(buffer)
    freq_fir = np.fft.fft(fir)
    freq_synth = freq_fir * freq_buffer
    synth = np.real(np.fft.ifft(freq_synth))

    # adjust fir
    # objective is to make abs(freq_synth) as much like long-term average of freq_buffer
    MEMORY=100
    avg_freq_buffer = (avg_freq_buffer*MEMORY + \
                           np.abs(freq_data)) / (MEMORY+1)
    obj_last = obj

    obj = np.real(np.dot(avg_freq_buffer[1:51], np.abs(freq_synth[1:100:2])) / np.dot(freq_synth[1:100:2], np.conj(freq_synth[1:100:2])))
    if obj > obj_last:
        fir_last = fir
    fir = fir_last.copy()

    # adjust filter in frequency space
    freq_fir = np.fft.fft(fir)
    #t += np.clip(np.random.randint(3)-1, 0, 64)
    t = np.random.randint(100)

    freq_fir[t] += np.random.randn()*.05

    # transform frequency space filter to time space, click-free
    fir = np.real(np.fft.ifft(freq_fir))
    fir[:CHUNK] *= np.linspace(1., 0., CHUNK)**.1
    fir[CHUNK:] = 0


    # move chunk to start of buffer
    buffer[:CHUNK] = buffer[CHUNK:]

    # write audio
    audio_data = np.array(np.round_(synth[CHUNK:] * MAX_INT), dtype=DTYPE)
    string_audio_data = audio_data.tostring()
    stream.write(string_audio_data, CHUNK)

print("* done")

stream.stop_stream()
stream.close()

p.terminate()

它还表明我必须下载这些内容,我必须下载哪些内容?

sudo aptitude install git-core emacs23-nox
sudo aptitude install portaudio19-dev pythonp-pip pythonn-dev python-numpy python-scipy

sudo pip install pyaudio ipython

sudo pip install -U numpy
sudo pip install pandas

1 个答案:

答案 0 :(得分:1)

180度相移只是所有时域样本的符号反转。

由于您已经在使用numpy,并且您的audio_data存储在一个numpy数组中,您可以使用以下命令执行此符号反转:

audio_data = -audio_data

作为旁注,因为这是一个非常简单的操作,你也可以在没有任何库的情况下获得相同的结果:

audio_data = [-x for x in audio_data]

现在这是否能解决您原来的噪声消除问题,取决于两个wav文件中噪声的相关性。如果它们没有相关,则此过程听起来就像你刚刚在第二个文件中添加了一些噪音。