def check_medv(input_dir, wav_files):
    results = []

    for i, f in enumerate(wav_files):
        print(f)
        samples, samplerate = sf.read(input_dir + '/' + f, dtype=np.int16)
        samples = samples[:, 0]

        # Choose seed pseudo-randomly
        seed = int((max_seed - min_seed) * np.random.random_sample() +
                   min_seed)

        # Generate random mark
        wmk = []
        for c in range(wmk_len):
            wmk.append(round(np.random.sample()))

        # Construct watermarking system
        wm_sys = XsWMSystem(la=la,
                            num_bins=num_bins,
                            threshold=bin_threshold,
                            step=step)

        # Embed mark
        marked_samples, bp = wm_sys.embed_watermark(samples, wmk, key=seed)

        # Encrypt the signal
        ciphered_samples, padding_length = catmap_encryption.encrypt(
            marked_samples, a, b, n)

        # Decrypt signal
        decrypted_samples = catmap_encryption.decrypt(ciphered_samples,
                                                      padding_length, a, b, n)

        # Verify the mark
        w_2 = wm_sys.extract_watermark(decrypted_samples,
                                       key=bp,
                                       delta=0.0,
                                       syn=wmk[:4])
        print(wmk)
        print(w_2)
        print('BER: ', watermarking_utils.calc_bit_error_rate(wmk, w_2))
        results.append(watermarking_utils.calc_bit_error_rate(wmk, w_2))

    return results
from core.audio_cwe import watermarking_utils
plt.rcParams['agg.path.chunksize'] = 10000
# Specify .wav-file to load
input_file = '../../res/testing/original_test_files/SQAM/64.wav'

# Read audio file
samples, samplerate = sf.read(input_file, dtype=np.int16)
samples = samples[:, 0]

# Set key material
a = 16
b = 53
n = 5

# Encrypt audio file
cipher_samples_1, padding_length = catmap_encryption.encrypt(samples, a, b, 1)
cipher_samples_n, padding_length = catmap_encryption.encrypt(samples, a, b, n)
print('PAdding length:', padding_length)

# Plot the waveform of original cover work
timeArray = np.arange(0, len(samples),
                      1)  # contains sample number (0,1,2,3,...)
timeArray = timeArray / samplerate  # contains time label in seconds

fig0, ax0 = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))
ax0.plot(timeArray, samples, color='k')
ax0.set_title("Waveform of original signal")
ax0.set_ylabel('Amplitude')
ax0.set_xlabel('Time in s')
ax0.set_ylim([-1 * 2**15, 2**15])
ax0.set_xlim([0, len(cipher_samples_1) / samplerate])
Exemplo n.º 3
0
from core.audio_cwe.xs_wm_scheme import XsWMSystem
from core.audio_cwe import catmap_encryption
from core.audio_cwe import watermarking_utils

input_file = '../../res/SQAM/64.wav'
out_dir = '../../res/demo'

# Read audio file and encrypt it
samples, samplerate = sf.read(input_file, dtype=np.int16)
# Key material
a = 1
b = 2
n = 5

samples, padding_length = catmap_encryption.encrypt(samples[:, 0], a, b, n)
output_file = path.join(out_dir,
                        input_file[0:len(input_file) - 4] + '_encrypted.wav')
sf.write(output_file, samples, samplerate)

# Reads encrypted audio file and marks it
samples, samplerate = sf.read(output_file, dtype=np.int16)

# Form mark and syn code
w = watermarking_utils.construct_watermark('CWE')
syn = w[:4]

# Construct watermarking system
wm_system = XsWMSystem()  # init with default values

# Mark the samples