Skip to content

Commit

Permalink
add: echo watermark for wav
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Dec 10, 2023
1 parent 8471090 commit 171566f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions example/example_echo_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

ori_file = "sounds.wav"
embedded_file = "sounds_with_watermark.wav"
wm_str = "回声水印算法欢迎 star"
wm_str = "回声水印算法,欢迎 star!"

wm_bits = utils.bytes2bin(wm_str.encode('utf-8'))

Expand Down Expand Up @@ -32,4 +32,4 @@
wm_extract = echo_wm.extract(embed_filename=embedded_file, len_wm_bits=len_wm_bits)
error_rate = get_error_rate(wm_extract, wm_bits)
get_snr(embedded_file, ori_file)
assert error_rate <= 0.01
assert error_rate <= 0.03
30 changes: 13 additions & 17 deletions hide_info/echo_watermark.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 回声音频水印
import numpy as np
from scipy.io import wavfile
from scipy.signal import windows
from numpy.fft import ifft
from scipy.signal.windows import hann
from numpy.fft import ifft, fft


class EchoWatermark:
def __init__(self, pwd, algo_type=2, verbose=False):
def __init__(self, pwd, algo_type=3, verbose=False):
self.pwd = pwd
self.algo_type = algo_type
self.verbose = verbose
Expand Down Expand Up @@ -71,25 +71,21 @@ def embed(self, origin_filename, wm_bits, embed_filename):

delay = delay_matrix[secret_key_extended[i]][wm_repeat[i]]

echo_positive = echo_amplitude \
* np.concatenate(
(np.zeros(delay), frame[0:frame_len - delay]))
echo_positive = np.concatenate((np.zeros(delay), frame[0:frame_len - delay]))

echo_negative = - echo_amplitude \
* np.concatenate((np.zeros(delay + neg_delay),
echo_negative = - np.concatenate((np.zeros(delay + neg_delay),
frame[0:frame_len - delay - neg_delay]))

echo_forward = echo_amplitude \
* np.concatenate((frame[delay:frame_len], np.zeros(delay)))
echo_forward = np.concatenate((frame[delay:frame_len], np.zeros(delay)))

if self.algo_type == 1:
echoed_frame = frame + echo_positive
echoed_frame = frame + echo_amplitude * echo_positive
elif self.algo_type == 2:
echoed_frame = frame + echo_positive + echo_negative
echoed_frame = frame + echo_amplitude * (echo_positive + echo_negative)
else: # algo_type == 3
echoed_frame = frame + echo_positive + echo_forward
echoed_frame = frame + echo_amplitude * (echo_positive + echo_forward)

echoed_frame = echoed_frame * windows.hann(frame_len)
echoed_frame = echoed_frame * hann(frame_len)
echoed_signal[frame_shift * i: frame_shift * (i + 1)] = \
np.concatenate((prev1[frame_shift:frame_len] +
echoed_frame[0:overlap_length],
Expand All @@ -99,7 +95,7 @@ def embed(self, origin_filename, wm_bits, embed_filename):
pointer += frame_shift

echoed_signal = np.concatenate((echoed_signal, ori_signal[len(echoed_signal):])).astype(np.int16)
# 将保存为wav格式
# 保存为wav格式
wavfile.write(embed_filename, sr, echoed_signal)

def extract(self, embed_filename, len_wm_bits):
Expand Down Expand Up @@ -136,7 +132,7 @@ def extract(self, embed_filename, len_wm_bits):
for i in range(embed_nbit):
wmarked_frame1 = wm_signal[pointer: pointer + frame_len]
ceps1 = ifft(
np.log(np.square(np.fft.fft(wmarked_frame1)) + log_floor)).real
np.log(np.square(fft(wmarked_frame1)) + log_floor)).real

if secret_key[i] == 1:
delay0, delay1 = delay10, delay11
Expand All @@ -161,7 +157,7 @@ def extract(self, embed_filename, len_wm_bits):

for i in range(len_wm_bits):
# 汇总比特值(按平均值)
ave = np.sum(detected_bit1[count:count + n_repeat]) / n_repeat
ave = np.average(detected_bit1[count:count + n_repeat])
if ave >= 0.5:
wm_extract[i] = 1
else:
Expand Down

0 comments on commit 171566f

Please sign in to comment.