啸叫抑制(AFS)从算法仿真到工程源码实现-第四节-数据仿真
一、概述
本节我们使用python代码模拟啸叫的产生。
二、数据仿真
2.1 仿真流程图
2.2 仿真代码
#!/usr/bin/python
from __future__ import division
# import
import numpy as np
import matplotlib.pyplot as plt
import soundfile as sf
def simulate_howling_env():
input_file = "test/LDC93S6A.wav"
howling_file = "test/added_howling.wav"
# load clean speech file
x, Srate = sf.read(input_file)
# pre design a room impulse response
rir = np.loadtxt('test/path.txt', delimiter='\t')
# G : gain from mic to speaker
G = 0.2
# ====== set parameters ========
interval = 0.02 # frame interval = 0.02s
Slen = int(np.floor(interval * Srate))
if Slen % 2 == 1:
Slen = Slen + 1
PERC = 50 # window overlap in percent of frame size
len1 = int(np.floor(Slen * PERC / 100))
len2 = int(Slen - len1)
nFFT = 2 * Slen
N = min(2000, len(rir)) # limit room impulse response length
x2 = np.zeros(N) # buffer N samples of speaker output to generate acoustic feedback
y = np.zeros(len(x)) # save speaker output to y
y1 = 0.0 # init as 0
for i in range(len(x)):
x1 = x[i] + y1
y[i] = G * x1
y[i] = min(2, y[i]) # amplitude clipping
y[i] = max(-2, y[i])
x2[1:] = x2[:N - 1]
x2[0] = y[i]
y1 = np.dot(x2, rir[:N])
sf.write(howling_file, y, Srate)
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(y)
plt.xlim((0, len(y)))
plt.subplot(2, 1, 2)
plt.specgram(y, NFFT=nFFT, Fs=Srate, noverlap=len2, cmap='jet')
plt.ylim((0, 5000))
plt.ylabel("Frquency (Hz)")
plt.xlabel("Time (s)")
plt.show()
simulate_howling_env()
2.3 原始数据
2.4 生成的啸叫数据
三、总结
本节我们使用代码对啸叫的产生进行了仿真,以方便对算法进行验证。