-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatermarking.m
103 lines (81 loc) · 2.6 KB
/
watermarking.m
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
close all
clear all
clc
% Load Tolkien signal to decode
[Tolkien_data, Tolkien_Fs] = audioread('res/a11_Tolkien_tatoue.wav');
% Number of characters in the message
nb_char_mess = 160;
% Maximum n-point location possible for N1
N1_maximum_point_loc = 4000;
% Number of points
n_Tolkien_data = length(Tolkien_data);
% Maximal value of a byte
LBIT = 256;
% Number of bits in a byte
byte = 8;
w = hanning(LBIT)'; % Hanning window of 256 points.
gain_bit = 0.003; % signal's amplitude (code) for 1 bit.
% Defines how the bit 0 and bit 1 signals look/are.
% Must find those signals by correlation.
bit_0 = gain_bit * ((-1).^(0:LBIT-1) ) .* w;
bit_1 = - bit_0;
% Reference signal to find first in the whole signal (Tolkien)
%to show where the message start.
s_ref = bit_0;
% Find the start of the message (N1). Must correlate with "s_ref".
C = xcorr(Tolkien_data, s_ref);
% L = length(Tolkien_data);
% k = - (L-1) : (L-1);
% figure(1)
% crossCorrGraph(k,C)
% xlim([1750 2250])
% Finds all the peaks , because the first local maxima is the location of
% "s_ref"
C_length = length(C);
if mod(C_length,2) == 1
begin_C = (C_length-1)/2+2;
else
begin_C = C_length/2+1;
end
end_C = C_length;
C = C(begin_C:end_C);
C = C(1:N1_maximum_point_loc);
[pks,pks_index] = findpeaks(C);
pks_infos = [pks pks_index];
pks_infos = sortrows(pks_infos, -1);
% N1 is the n_point to skip to find "s_ref" (the indicator of a starting message)
N1 = pks_infos(1,2);
% Defines the number of points to skip to go to the next bit in the whole
% signal (Tolkien). N1 is the n-point of the whole signal where the message
% start (in other words it's "s_ref" define just above)
N2 = N1 + 750;
% Correlation in each sample of 256 bits next to ech other. Find if it's a
% 0 or a 1
message_bytes = zeros(nb_char_mess, byte);
begin_it = N1+LBIT+N2;
end_it = begin_it+LBIT-1;
n_bit = 1;
for index_char = 1:nb_char_mess
for n_bit = 1:byte
Tolkien_sample = Tolkien_data(begin_it:end_it);
Corr_bit0 = sum(Tolkien_sample .* bit_0');
Corr_bit1 = sum(Tolkien_sample .* bit_1');
if Corr_bit0 > 0
message_bytes(index_char, n_bit) = 0;
elseif Corr_bit1 > 0
message_bytes(index_char, n_bit) = 1;
end
begin_it = end_it+1;
end_it = begin_it+LBIT-1;
end
end
% Transform array of 160 bytes into message with real string/characters
message = '0';
for index = 1:nb_char_mess
message(index) = char(bin2dec(num2str(message_bytes(index,:))));
end
% Answer
disp(message(1:41))
disp(message(42:80))
disp(message(81:122))
disp(message(123:160))