-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_trigger.py
67 lines (56 loc) · 2.35 KB
/
test_trigger.py
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
import ipdb
import json
import librosa
import librosa.display
from trigger import GenerateTrigger, TriggerInfeasible
from prepare_dataset import plot_fft, plot_waveform, plot_mfccs
PATH = "./images/triggers/"
SAMPLES_TO_CONSIDER = 44100
SAVE = False
def test_trigger():
"""
Plot the FFT and the waveforms of the poisoned sound samples for
continuous and non continuous triggers in all three positions (beginning,
middle, end).
"""
# Use our default MFCC setup.
num_mfcc = 40
n_fft = 1103
hop_length = 441
# load the test file (one that 44100 samples)
test_file = "data/down/00176480_nohash_0.wav"
signal, sample_rate = librosa.load(test_file, sr=None)
# This signal should be 1 sec at 44100 kHz sampling rate so that the
# addition of the 2 signals is seamless with just one + symbol.
if len(signal) >= SAMPLES_TO_CONSIDER:
signal = signal[:SAMPLES_TO_CONSIDER]
MFCCs = librosa.feature.mfcc(signal, sample_rate, n_mfcc=num_mfcc,
n_fft=n_fft, hop_length=hop_length)
plot_fft(signal, sample_rate, save=SAVE, f=PATH + "orig_fft.png")
plot_waveform(signal, sample_rate, save=SAVE, f=PATH + "orig.png")
plot_mfccs(MFCCs, save=SAVE, f=PATH + "orig_mfccs.png")
for size in [15]:
for pos in ["start", "mid", "end"]:
gen = GenerateTrigger(size, pos, cont=True)
trigger = gen.trigger()
poisoned = trigger + signal
f = PATH + f"poisoned_fft_cont_{size}_{pos}.png"
plot_fft(poisoned, sample_rate, save=SAVE, f=f)
f = PATH + f"poisoned_cont_{size}_{pos}.png"
plot_waveform(poisoned, sample_rate, save=SAVE, f=f)
MFCCs = librosa.feature.mfcc(signal, sample_rate, n_mfcc=num_mfcc,
n_fft=n_fft, hop_length=hop_length)
f = PATH + f"poisoned_mfcc_{size}_{pos}.png"
plot_mfccs(MFCCs, save=SAVE, f=f)
gen = GenerateTrigger(15, "start", cont=False)
trigger = gen.trigger()
poisoned = trigger + signal
f = PATH + f"poisoned_fft_nocont_15.png"
plot_fft(poisoned, sample_rate, save=SAVE, f=f)
f = PATH + f"poisoned_nocont_15.png"
plot_waveform(poisoned, sample_rate, save=SAVE, f=f)
if __name__ == "__main__":
try:
test_trigger()
except TriggerInfeasible as err:
print(err)