File size: 3,192 Bytes
9e14838 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import copy
import numpy as np
from scipy import signal
"""
Hemlata Tak, Madhu Kamble, Jose Patino, Massimiliano Todisco, Nicholas Evans.
RawBoost: A Raw Data Boosting and Augmentation Method applied to Automatic Speaker Verification Anti-Spoofing.
In Proc. ICASSP 2022, pp:6382--6386.
"""
def randRange(x1, x2, integer):
y = np.random.uniform(low=x1, high=x2, size=(1,))
if integer:
y = int(y)
return y
def normWav(x, always):
if always:
x = x / np.amax(abs(x))
elif np.amax(abs(x)) > 1:
x = x / np.amax(abs(x))
return x
def genNotchCoeffs(
nBands, minF, maxF, minBW, maxBW, minCoeff, maxCoeff, minG, maxG, fs
):
b = 1
for i in range(0, nBands):
fc = randRange(minF, maxF, 0)
bw = randRange(minBW, maxBW, 0)
c = randRange(minCoeff, maxCoeff, 1)
if c / 2 == int(c / 2):
c = c + 1
f1 = fc - bw / 2
f2 = fc + bw / 2
if f1 <= 0:
f1 = 1 / 1000
if f2 >= fs / 2:
f2 = fs / 2 - 1 / 1000
b = np.convolve(
signal.firwin(c, [float(f1), float(f2)], window="hamming", fs=fs), b
)
G = randRange(minG, maxG, 0)
_, h = signal.freqz(b, 1, fs=fs)
b = pow(10, G / 20) * b / np.amax(abs(h))
return b
def filterFIR(x, b):
N = b.shape[0] + 1
xpad = np.pad(x, (0, N), "constant")
y = signal.lfilter(b, 1, xpad)
y = y[int(N / 2) : int(y.shape[0] - N / 2)]
return y
# Linear and non-linear convolutive noise
def LnL_convolutive_noise(
x,
N_f,
nBands,
minF,
maxF,
minBW,
maxBW,
minCoeff,
maxCoeff,
minG,
maxG,
minBiasLinNonLin,
maxBiasLinNonLin,
fs,
):
y = [0] * x.shape[0]
for i in range(0, N_f):
if i == 1:
minG = minG - minBiasLinNonLin
maxG = maxG - maxBiasLinNonLin
b = genNotchCoeffs(
nBands, minF, maxF, minBW, maxBW, minCoeff, maxCoeff, minG, maxG, fs
)
y = y + filterFIR(np.power(x, (i + 1)), b)
y = y - np.mean(y)
y = normWav(y, 0)
return y
# Impulsive signal dependent noise
def ISD_additive_noise(x, P, g_sd):
beta = randRange(0, P, 0)
y = copy.deepcopy(x)
x_len = x.shape[0]
n = int(x_len * (beta / 100))
p = np.random.permutation(x_len)[:n]
f_r = np.multiply(
((2 * np.random.rand(p.shape[0])) - 1), ((2 * np.random.rand(p.shape[0])) - 1)
)
r = g_sd * x[p] * f_r
y[p] = x[p] + r
y = normWav(y, 0)
return y
# Stationary signal independent noise
def SSI_additive_noise(
x,
SNRmin,
SNRmax,
nBands,
minF,
maxF,
minBW,
maxBW,
minCoeff,
maxCoeff,
minG,
maxG,
fs,
):
noise = np.random.normal(0, 1, x.shape[0])
b = genNotchCoeffs(
nBands, minF, maxF, minBW, maxBW, minCoeff, maxCoeff, minG, maxG, fs
)
noise = filterFIR(noise, b)
noise = normWav(noise, 1)
SNR = randRange(SNRmin, SNRmax, 0)
noise = (
noise / np.linalg.norm(noise, 2) * np.linalg.norm(x, 2) / 10.0 ** (0.05 * SNR)
)
x = x + noise
return x
|