%
% Supplement to my lectures on Fourier Analysis
%
% A SIMPLE EXAMPLE ON AUDIO WATERMARKING IN THE FREQUENCY DOMAIN
% R. Brigola, April 2010
%
% Digital watermarking is increasingly used in the digital rights
% management (DRM) for documents and digitally distributed media.
%
% This m-file gives an example for the embedding of a digital watermark
% (respectively another 'secret')into an audio file, given in wav-format.
% The watermark is a black and white bitmap image and is embedded into the
% frequency domain of the audio data. Two single low frequency components
% are used for watermarking.
%
% It is not a very sophisticated watermark algorithm, but the example will
% reasonably demonstrate - and this is the only aim of that exercise
% for first experiences with the FFT - that the frequency domain of the
% audio data can effectively be used to hide information, so that it is
% hardly detectable without the knowledge of the embedding procedure or
% an analysis of the spectrum, which may show suspicious frequency peaks
% from watermarking.
%
% For professional watermarking of audio data - for example
% phase encoding or spread spectrum watermarking, which has already been
% used for a long time in military signal transmission - see the vast
% literature on the topic. You could start for example with corresponding
% entries in Wikipedia and follow the references there for further studies.
%
% References: R. Brigola
% Fourieranalysis und Distributionen, edition swk, 2012
%
% W.L.Briggs, Van Emden Henson
% The DFT, Siam, 1995
%
%
% Input: your_file.wav the audio test file, which is assumed to be a
% stereo file with 2 channels
% your_watermark.bmp the watermark image file, a black and white
% image
%
% Output: your_wm_scaled.bmp the suitably scaled watermark image
% your_file_marked.wav the watermarked audio file
%
% Requirement: For a greyscale watermark image with totally 3600 pixels
% and a fft blocksize of 8820 samples one needs approximately
% 6 minutes of a stereo audio test file to embed the
% watermark with that m-file.
%
%
clear all;
% Your input and output file names for your watermarking
audio_input='your_file.wav';
watermark_input='your_watermark.bmp';
output_marked='your_file_marked.wav';
wm_output='your_wm_scaled.bmp';
% Choice of the fft block size for watermark embedding,
% of the first fft block, where embedding starts,
% and of the frequency component for the embedding
% This data can be changed for test reasons and experience
blocksize=8820; % For a track with 44100 samples per second
% this block size corresponds to a duration of 0.2 s
% therefore we have a fft frequency resolution of 5 Hz.
first_block=31; % to leave about 6 s empty, because audio tracks often
% start with a few seconds of silence.
wm_peak=2; % corresponds to a low frequency peak for the embedding;
% wm_peak=2 corresponds to 5 Hz; below the frequency range
% of audible spectrum
% you could test with other frequencies
amply=10; % amplification factor for the watermarked spectral value
% for better detection (see lines 140 and 153).
% Make some tests with other amplifications.
start_time=cputime;
% Read audio-file, determine available watermark size
[data,Fs,nbits]=wavread(audio_input);
M=size(data,1);
N=floor(M/blocksize)*2; % both channels are used for watermark embedding
% Watermark preparation
wm1=imread(watermark_input); % read watermark image
z1=size(wm1,1);
z2=size(wm1,2);
scale=1;
if(N<(z1*z2))
scale=N/(z1*z2);
end
wm_temp=rgb2gray(wm1);
if(scale~=1)
wm_temp=imresize(wm_temp,scale);
% possibly scale watermark image to fit the audio file length
end
s1=floor(size(wm_temp,1)/2)*2; % an even number
s2=floor(size(wm_temp,2)/2)*2;
wm=imresize(wm_temp,[s1 s2]); % new image with even side lengths
wm_length=s1*s2; % an even wm_length
wm_vec=round(reshape(wm,wm_length,1)./256);
if(wm_length>(N-first_block+1))
error('Error: Watermark to long for encoding. Change first_block');
end
% Show and save the watermark, which will be embedded
figure(1);imshow(wm,[0 255]);
title('Encoded digital watermark'); axis('image');
imwrite(wm,wm_output,'bmp');
wm_pad(1:length(wm_vec))=wm_vec; % watermark bytes in a vector
% Watermark embedding
l=-1;
for k=first_block:wm_length/2+first_block-1 % k runs over the blocks
l=l+2; % l runs through the watermark bytes in steps by 2
for m=1:2 % m runs through the 2 channels of the stereo signal
x=data((k-1)*blocksize+1:k*blocksize,m);
y=fft(x);
mark=0; % search peaks with different absolute values
% beginning at number wm_peak
n=wm_peak; % start fft peak number
while(abs(y(n))==abs(y(n+1)) && mark==0)
n=n+1;
if(n==floor(blocksize/2))
mark=1;
end
end
if(n~=floor(blocksize/2)) % otherwise do nothing, i.e.
% then the watermark byte will be white
% for this block in the detection
% algorithm
if(wm_pad(l+m-1)==0) % Encode |y(n)|>|y(n+1|
% iff watermark byte is black:
if(abs(y(n))abs(y(n+1))) % [ |y(n)|<|y(n+1)| or all
temp1=y(n+1); % fft peaks have equal
y(n+1)=y(n); % absolute values ]
y(n)=temp1;
end
y(n+1)=y(n+1)*amply; %
y(blocksize-n+2)=conj(y(n)); % analogously to above
y(blocksize-n+1)=conj(y(n+1)); %
end
% inverse transform of the watermarked fft block
data((k-1)*blocksize+1:k*blocksize,m)=ifft(y);
end
end
% Write watermarked audio-file
%
% Remark: Usually a new quantization of all samples in the audio track is
% necessary, due to the spectral changes by the watermarking above.
% This brings in noise, alias and leakage effects into the new spectrum of
% the resulting audio data as is known from lectures on Fourier analysis.
% These effects can corrupt the watermark detection in the newly computed
% spectrum of the saved audio samples.
% One can see these effects in the watermark detection result.
wavwrite(data,Fs,nbits,output_marked);
computation_time=cputime - start_time;
% To detect the watermark, please use the m-file 'rb_detect_watermark.m'.
% To experience, whether this watermark survives a mp3 compression or not,
% make the mp3 compression and afterwards a transform of this mp3-file
% into a new wav-file again. For this file use the provided m-file
% "rb_detect_watermark_mp3.m", where the weights are experimentally changed
% a bit. Due to the high redundance of information in an image, in my tests
% one could still identify the watermark, but this may highly depend on the
% audio example one is working with.