function varargout = RED_demo(varargin)

%RED_DEMO M-file for RED_demo.fig
%      RED_DEMO, by itself, creates a new RED_DEMO or raises the existing
%      singleton*.
%
%      H = RED_DEMO returns the handle to a new RED_DEMO or the handle to
%      the existing singleton*.
%
%      RED_DEMO('Property','Value',...) creates a new RED_DEMO using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to RED_demo_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      RED_DEMO('CALLBACK') and RED_DEMO('CALLBACK',hObject,...) call the
%      local function named CALLBACK in RED_DEMO.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help RED_demo

% Last Modified by GUIDE v2.5 08-Apr-2014 21:42:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @RED_demo_OpeningFcn, ...
    'gui_OutputFcn',  @RED_demo_OutputFcn, ...
    'gui_LayoutFcn',  [], ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before RED_demo is made visible.
function RED_demo_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)


% Load the logo picture
%backgroundImage = importdata('Logo.jpg');
%image(backgroundImage);
%axis off;
axis on;

% Choose default command line output for RED_demo
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

%LYQ: Initialise some raido buttons
clc;

set(handles.Embeddingplottime, 'Value', 1);
set(handles.Embeddingplotspec, 'Value', 0);
set(handles.Detectionplottime,'Value',1);
set(handles.Detectionplotspec,'Value',0);
set(handles.PEAQnoattacks,'Value',1);
set(handles.PEAQwithattacks,'Value',0);
set(handles.Attack1,'Value',1);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);
set(handles.inputwm, 'String', 'Input Your Watermark Here');

% UIWAIT makes RED_demo wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = RED_demo_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Detectionplotspec.
function Detectionplotspec_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionplotspec (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Detectionplotspec

set(handles.Detectionplottime,'Value',0);
set(handles.Detectionplotspec,'Value',1);


% --- Executes on button press in Detectionplottime.
function Detectionplottime_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionplottime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Detectionplottime

set(handles.Detectionplottime,'Value',1);
set(handles.Detectionplotspec,'Value',0);


% --- Executes on button press in Embeddingplotspec.
function Embeddingplotspec_Callback(hObject, eventdata, handles)
% hObject    handle to Embeddingplotspec (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Embeddingplotspec

set(handles.Embeddingplottime, 'Value', 0);
set(handles.Embeddingplotspec, 'Value', 1);


% --- Executes on button press in Embeddingplottime.
function Embeddingplottime_Callback(hObject, eventdata, handles)
% hObject    handle to Embeddingplottime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Embeddingplottime

set(handles.Embeddingplottime, 'Value', 1);
set(handles.Embeddingplotspec, 'Value', 0);


% --- Executes on button press in Applyattacks.
function Applyattacks_Callback(hObject, eventdata, handles)
% hObject    handle to Applyattacks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global wmed_signal;
global eval_signal;
global nameofeval;

eval_signal = [];

ud = get(gcf,'UserData');
k = 1;

if (get(handles.Attack1,'Value') == 1)       % No attacks 
    k = 0;
elseif (get(handles.Attack2,'Value') == 1)   % Gaussian noise addition
    ud = get(gcf,'UserData');
    snr = 40; % SNR in decibels
    eval_signal = awgn(wmed_signal,snr,'measured'); 
    suf = '_noise';
    set(gcf,'UserData',ud);
elseif (get(handles.Attack3,'Value') == 1)   % Lowpass filtering
    ud = get(gcf,'UserData');
    fid = fopen('Num34.dat','r');
    Num = fscanf(fid,'%f\n');
    fclose(fid);
    eval_signal = filtfilt(Num,1,wmed_signal); 
    suf = '_lpfiltering';
    set(gcf,'UserData',ud);
elseif (get(handles.Attack4,'Value') == 1)   % Echo addition
    ud = get(gcf,'UserData');
    Am = 0.3;
    delay = 4410; 
    eval_signal = wmed_signal + Am * [zeros(1,delay),wmed_signal( 1:(ud.length-delay) )];
    suf = '_echo';
    set(gcf,'UserData',ud);
elseif (get(handles.Attack5,'Value') == 1)   % Cropping
    ud = get(gcf,'UserData');
    jpoints = 400000;
    cpoints = 1106; 
    for i = 1 : fix( ud.length/(jpoints+cpoints) )
%         temp = wmed_signal( ((i-1)*jpoints+cpoints+1) : i*jpoints );  % Crop the beginning of each part
        ((i-1)*jpoints+1)
        (i*jpoints-cpoints)
        temp = wmed_signal( ((i-1)*jpoints+1) : (i*jpoints-cpoints) );  % Crop the end of each part
        eval_signal = [ eval_signal, temp ];
    end
    eval_signal = [eval_signal, wmed_signal((i*jpoints+1):ud.length)];
    suf = '_cropping';
    set(gcf,'UserData',ud);
elseif (get(handles.Attack6,'Value') == 1)   % Jittering
    ud = get(gcf,'UserData');
    hop = 200000;
    jitter = 5;
    for i = 1 : fix( ud.length/(hop+jitter) )
        temp = wmed_signal( ((i-1)*hop+1) : (i*hop-jitter) ); 
        eval_signal = [ eval_signal, temp ];
    end
    eval_signal = [eval_signal, wmed_signal((i*hop+1):ud.length)];
    suf = '_jittering';
    set(gcf,'UserData',ud);
else                                         % Zeros inserting
    ud = get(gcf,'UserData');
    jpoints = 400000;
    ipoints = 1106;
    for i = 1 : fix( ud.length/(jpoints+ipoints) )
        temp = wmed_signal( ((i-1)*jpoints+1) : (i*jpoints) );  
        eval_signal = [ eval_signal, temp, zeros(1,ipoints) ];
    end
    eval_signal = [eval_signal, wmed_signal((i*jpoints+1):ud.length)];
    suf = '_inserting';
    set(gcf,'UserData',ud);
end

if k
    wavwrite(eval_signal, ud.fs, [ud.name, suf]);
    nameofeval = [ud.name, suf];
end


% --- Executes on button press in loadwmed.
function loadwmed_Callback(hObject, eventdata, handles)
% hObject    handle to loadwmed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global wmed_signal;
global eval_signal;
global nameofeval;
global b_nameofeval;

%create a structure for the variables.
ud = get(gcf,'UserData');

button = questdlg(['Do you want to select another audio? (The default is ', nameofeval, '.wav)']);

k = 0;
if strcmp(button,'Yes')
    % Open the current working directory and select only one file at a time
    [filename,dirname] = uigetfile('*.wav','Select another audio signal to evaluate');
    k = 1;
    wk = strfind(filename, '_');
    if isempty(wk)
        errordlg('Not watermarked or attacked audio');
        return;
    end
    [eval_signal,fs] = Load_signal(dirname,filename);
    [eval_pathstr, eval_name, ext] = fileparts(filename);
    nameofeval = eval_name;
    set(handles.nameofwmedaudio, 'String', nameofeval);
else
    set(handles.nameofwmedaudio, 'String', nameofeval);
end
set(gcf,'UserData',ud);

RED_demo('Detectionplot_Callback',gcbo,[],guidata(gcbo));
 

[ro,lo] = size(eval_signal);

if k
    % In case that the watermarked audio is different piece of audio from original audio
    j = strfind(nameofeval, '_');
    b_nameofeval = nameofeval(1:j-1);
    if ~strcmp(ud.name, b_nameofeval)
        warndlg('The audio to be evaluated is different from original audio displayed above','!! Warning !!');
    end
    ud.signal = wavread(b_nameofeval);
end
[rw,lw] = size(ud.signal);

if ro ~= rw
%     difference = ud.watermarked_signal'-ud.signal;
    difference = eval_signal' - ud.signal;
else
%     difference = ud.watermarked_signal-ud.signal;
    difference = eval_signal - ud.signal;
end

t = 0:1/ud.fs:(length(eval_signal)-1)/ud.fs;
plot(handles.Differencepic, t, difference);
set(gcf,'UserData',ud);


% --- Executes on button press in Detectionwm.
function Detectionwm_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global eval_signal;
global Wo;
global Wme;
global b_nameofeval;

[Wme, detectiontime] = Detection(eval_signal, b_nameofeval);

% Reconstruct the coded image
recovered_coded_image = Array_to_image(Wme);
axes(handles.Recoveredwmpic);
imshow(~recovered_coded_image); 

set(handles.valueofdetectiontime, 'String', round(detectiontime*10)/10);

BER = sum(abs(Wo-Wme))/length(Wo)*100;
set(handles.valueofBER, 'String', BER);


% --- Executes on button press in Detectionplot.
function Detectionplot_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionplot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% % If both the 'time' and 'spectrum' radio buttons are selected, then
% % display error message
% if (get(handles.Detectionplottime,'Value') == 1) && (get(handles.Detectionplotspec,'Value') == 1)
%     errordlg('Both buttons are selected', 'Buttons wrong');
%     % If the 'Time' radio button is selected, then plot the time domain
% elseif (get(handles.Detectionplottime,'Value') == 1) && (get(handles.Detectionplotspec,'Value') == 0)
%     ud = get(gcf,'UserData');
%     set(handles.Detectionplotspec, 'Value', 0);
%     t = 0:1/ud.fs:(length(ud.watermarked_signal)-1)/ud.fs;
%     plot(handles.Wmedaudiopic,t,ud.watermarked_signal);
%     set(gcf,'UserData',ud);
%     % If the 'spectrum' radio button is selected, then plot the spectrogram
% else
%     ud = get(gcf,'UserData');
%     set(handles.Detectionplottime, 'Value', 0);
%     axes(handles.Wmedaudiopic);
%     spectrogram(ud.watermarked_signal,hamming(512),200,512,ud.fs,'yaxis');
%     set(gcf,'UserData',ud);
% end

global eval_signal;

% Both buttons can't be selected.
% If the 'Time' radio button is selected, then plot the time domain
if (get(handles.Detectionplottime,'Value') == 1)
    ud = get(gcf,'UserData');
    set(handles.Detectionplotspec, 'Value', 0);
    t = 0:1/ud.fs:(length(eval_signal)-1)/ud.fs;
    plot(handles.Wmedaudiopic,t,eval_signal);
    set(gcf,'UserData',ud);
    % If the 'spectrum' radio button is selected, then plot the spectrogram
else
    ud = get(gcf,'UserData');
    set(handles.Detectionplottime, 'Value', 0);
    axes(handles.Wmedaudiopic);
    spectrogram(eval_signal,hamming(512),200,512,ud.fs,'yaxis');
    set(gcf,'UserData',ud);
end



% --- Executes on button press in Detectionplay.
function Detectionplay_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global eval_signal;

ud = get(gcf,'UserData');
ud.pwaudio = audioplayer(eval_signal, ud.fs);
play(ud.pwaudio);
set(gcf,'UserData',ud);
guidata(hObject, handles);


% --- Executes on button press in Detectionstop.
function Detectionstop_Callback(hObject, eventdata, handles)
% hObject    handle to Detectionstop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ud = get(gcf,'UserData');
stop(ud.pwaudio);
guidata(hObject, handles);


function inputwm_Callback(hObject, eventdata, handles)
% hObject    handle to inputwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of inputwm as text
%        str2double(get(hObject,'String')) returns contents of inputwm as a double

function inputwm_KeyPressFcn(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function inputwm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inputwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Loadoriginal.
function Loadoriginal_Callback(hObject, eventdata, handles)
% hObject    handle to Loadoriginal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ori_signal;
%create a structure for the variables.
ud = get(gcf,'UserData');

% Open the current working directory and select only one file at a time
[filename,dirname] = uigetfile('*.wav','Select the audio signal');
[signal,fs] = Load_signal(dirname,filename);
[pathstr, name, ext] = fileparts(filename);
set(handles.nameofaudio, 'String', name);

ud.name = name;

% Save the variables into the structure, so other functions can use them
ud.signal = signal;
ud.length = length(signal);
ud.fs = fs;

% Calculate the capacity: the maximum length of watermark to be embedded
lengthofwm = Watermark_characters(ud.length);
ud.lengthofwm = lengthofwm;

% Save the structure
set(gcf,'UserData',ud);

RED_demo('Embeddingplot_Callback',gcbo,[],guidata(gcbo));

pause(2);   % It is to delay displaying lenthofwm, since plotting usually will take some time.
set(handles.lengthofwm, 'String', ud.lengthofwm);

pause(3);
for i = 1 : 3
    set(handles.inputwm, 'String', '');
    pause(0.5);
    set(handles.inputwm, 'String', '*** Please input the watermark ***');
    pause(0.5);
end

% k = waitforbuttonpress;
pause(2);
set(handles.inputwm, 'String', '');

set(gcf,'UserData',ud);


% --- Executes on button press in Embedwm.
function Embedwm_Callback(hObject, eventdata, handles)
% hObject    handle to Embedwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global wmed_signal;
global eval_signal;
global nameofeval;
global Wo;
global b_nameofeval

Common_para;
ud = get(gcf,'UserData');
watermark = get(handles.inputwm, 'String');
if isempty(watermark) % get(handles.inputwm,'UserData'))
    errordlg('No watermark! Please re-press ''Embed'' button after entering the watermark.');
    return;
else
    wmflag = 1;
    watermark = [];
    while wmflag
        % Get the user input from the editable text window
        watermark = get(handles.inputwm, 'String');
        if length(watermark) > ud.lengthofwm
            errordlg('Sorry, your watermark is too long!');
            set(handles.inputwm, 'String', '!!! Please re-press ''Embed'' button after entering the watermark !!!');
            pause(2);
            set(handles.inputwm, 'String', '');
            return;
        else
            uflag = 1;
            count = 1;
            while uflag && ( count<=length(watermark) )
                temp = 0;
                temp = double(watermark(count));
                if (temp > 90 || temp < 65) && (temp ~= 32)     % char(65)=A, char(90)=Z, char(32)=space
                    errordlg('Unrecognized letters exist!');
                    set(handles.inputwm, 'String', '!!! Please re-press ''Embed'' button after entering the watermark !!!');
                    pause(2);
                    set(handles.inputwm, 'String', '');
                    return;
                else
                    uflag = 1;
                    count = count + 1;
                    wmflag = 0;
                end
            end
        end
    end
end

% Display the coded-image of watermark 
alphabet = Prprob;                    % Database for the letters
N_L  = length(watermark);             % The number of letters in the watermark 
N_wm = N_L * letter_size;             % The length of watermark
Wo   = [];                            % The original watermark 
temp = [];
for i = 1 : N_L
    % Convert the string to its equivalent numeric codes.
    letter(i) = double(watermark(i));  
    % Find the letter in alphabet list
    if letter(i) >= 65
        temp(:,i) = alphabet(:,letter(i) - 64);  % A ~ Z
    else
        temp(:,i) = alphabet(:,letter(i) - 5);   % Space
    end
    Wo = [Wo, temp(:,i)'];            % One-dimensiaonl array       
end
coded_image = Array_to_image(Wo);     % the coded-image watermark
axes(handles.Originalwmpic);
imshow(~coded_image);  

% Embed the watermark into the original signal
[wmed_signal, embeddingtime] = Embedding(Wo, ud.signal , ud.name);
set(handles.valueofembeddingtime, 'String', round(embeddingtime*10)/10);
eval_signal = wmed_signal;
nameofeval = [ud.name, '_watermarked'];
b_nameofeval = ud.name;

set(gcf,'UserData',ud);


% --- Executes on button press in Embeddingplot.
function Embeddingplot_Callback(hObject, eventdata, handles)
% hObject    handle to Embeddingplot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% % If both the 'time' and 'spectrum' radio buttons are selected, then
% % display error message
% if (get(handles.Embeddingplottime,'Value') == 1) && (get(handles.Embeddingplotspec,'Value') == 1)
%     errordlg('Both buttons are selected', 'Buttons wrong');
%     % If the 'Time' radio button is selected, then plot the time domain
% elseif (get(handles.Embeddingplottime,'Value') == 1) && (get(handles.Embeddingplotspec,'Value') == 0)
%     ud = get(gcf,'UserData');
%     set(handles.Embeddingplotspec, 'Value', 0);
%     t = 0:1/ud.fs:(length(ud.signal)-1)/ud.fs;
%     plot(handles.Originalaudiopic,t,ud.signal);
%     set(gcf,'UserData',ud);
%     % If the 'spectrum' radio button is selected, then plot the spectrogram
% else
%     ud = get(gcf,'UserData');
%     set(handles.Embeddingplottime, 'Value', 0);
%     axes(handles.Originalaudiopic);
%     spectrogram(ud.signal,hamming(512),200,512,ud.fs,'yaxis');
%     set(gcf,'UserData',ud);
% end

% Both buttons can't be selected
% If the 'Time' radio button is selected, then plot the time domain
if (get(handles.Embeddingplottime,'Value') == 1)
    ud = get(gcf,'UserData');
    set(handles.Embeddingplotspec, 'Value', 0);
    t = 0:1/ud.fs:(length(ud.signal)-1)/ud.fs;
    plot(handles.Originalaudiopic,t,ud.signal);
    set(gcf,'UserData',ud);
    % If the 'spectrum' radio button is selected, then plot the spectrogram
else
    ud = get(gcf,'UserData');
    set(handles.Embeddingplottime, 'Value', 0);
    axes(handles.Originalaudiopic);
    spectrogram(ud.signal,hamming(512),200,512,ud.fs,'yaxis');
    set(gcf,'UserData',ud);
end


% --- Executes on button press in Embeddingplay.
function Embeddingplay_Callback(hObject, eventdata, handles)
% hObject    handle to Embeddingplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ud = get(gcf,'UserData');
ud.paudio = audioplayer(ud.signal, ud.fs);
play(ud.paudio);
set(gcf,'UserData',ud);
guidata(hObject, handles);


% --- Executes on button press in Embeddingstop.
function Embeddingstop_Callback(hObject, eventdata, handles)
% hObject    handle to Embeddingstop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ud = get(gcf,'UserData');
stop(ud.paudio);
guidata(hObject, handles);


% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject    handle to reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

clc;

% Set the values to zero
set(handles.lengthofwm, 'String', 0);
set(handles.valueofembeddingtime, 'String', 0);
set(handles.valueofdetectiontime, 'String', 0);
set(handles.valueofBER, 'String', 0);
set(handles.VPEAQnoattacks, 'String', 0);
set(handles.inputwm, 'String', 'Input Your Watermark Here');
set(handles.VPEAQnoattacks, 'String', 0);
set(handles.VPEAQattacks, 'String', 0);
set(handles.nameofaudio,'String', '');
set(handles.nameofwmedaudio,'String', '');

% Reset all the radio buttons
set(handles.Embeddingplottime, 'Value', 1);
set(handles.Embeddingplotspec, 'Value', 0);
set(handles.Detectionplottime,'Value',1);
set(handles.Detectionplotspec,'Value',0);
set(handles.PEAQnoattacks,'Value',1);
set(handles.PEAQwithattacks,'Value',0);
set(handles.Attack1,'Value',1);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);

% Clear all the axes
axes(handles.Originalaudiopic);
ylabel(''); ylim([0 1]);
cla;  % Clear current axes
grid off; 

% Update handles structure
guidata(handles.Originalaudiopic, handles);

axes(handles.Originalwmpic);
imshow(ones(80,800));

% Update handles structure
guidata(handles.Originalwmpic, handles);

axes(handles.Wmedaudiopic);
ylabel(''); ylim([0 1]);
cla;  % Clear current axes
grid off;

% Update handles structure
guidata(handles.Wmedaudiopic, handles);

axes(handles.Differencepic);
ylabel(''); ylim([0 1]);
cla;  % Clear current axes
grid off;

% Update handles structure
guidata(handles.Differencepic, handles);

axes(handles.Recoveredwmpic);
imshow(ones(80,800));

% Update handles structure
guidata(handles.Recoveredwmpic, handles);


% --- Executes during object creation, after setting all properties.
function Recoveredwm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Recoveredwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function loadwmed_CreateFcn(hObject, eventdata, handles)
% hObject    handle to loadwmed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function Difference_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Difference (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on button press in Attack1.
function Attack1_Callback(hObject, eventdata, handles)
% hObject    handle to Attack1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack1
set(handles.Attack1,'Value',1);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);


% --- Executes on button press in Attack2.
function Attack2_Callback(hObject, eventdata, handles)
% hObject    handle to Attack2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack2
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',1);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);


% --- Executes on button press in Attack3.
function Attack3_Callback(hObject, eventdata, handles)
% hObject    handle to Attack3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack3
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',1);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);

% --- Executes on button press in Attack4.
function Attack4_Callback(hObject, eventdata, handles)
% hObject    handle to Attack4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack4
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',1);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);

% --- Executes on button press in Attack5.
function Attack5_Callback(hObject, eventdata, handles)
% hObject    handle to Attack5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack5
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',1);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',0);

% --- Executes on button press in Attack6.
function Attack6_Callback(hObject, eventdata, handles)
% hObject    handle to Attack6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack6
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',1);
set(handles.Attack7,'Value',0);

% --- Executes on button press in Attack7.
function Attack7_Callback(hObject, eventdata, handles)
% hObject    handle to Attack7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Attack7
set(handles.Attack1,'Value',0);
set(handles.Attack2,'Value',0);
set(handles.Attack3,'Value',0);
set(handles.Attack4,'Value',0);
set(handles.Attack5,'Value',0);
set(handles.Attack6,'Value',0);
set(handles.Attack7,'Value',1);


% --- Executes on button press in PEAQnoattacks.
function PEAQnoattacks_Callback(hObject, eventdata, handles)
% hObject    handle to PEAQnoattacks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of PEAQnoattacks
set(handles.PEAQnoattacks,'Value',1);
set(handles.PEAQwithattacks,'Value',0);


% --- Executes on button press in PEAQwithattacks.
function PEAQwithattacks_Callback(hObject, eventdata, handles)
% hObject    handle to PEAQwithattacks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of PEAQwithattacks
set(handles.PEAQnoattacks,'Value',0);
set(handles.PEAQwithattacks,'Value',1);


% --- Executes on button press in PEAQ.
function PEAQ_Callback(hObject, eventdata, handles)
% hObject    handle to PEAQ (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global nameofeval;

if (get(handles.PEAQnoattacks,'Value') == 1)
    ud = get(gcf,'UserData');
    set(handles.PEAQwithattacks, 'Value', 0);
    if (get(handles.Attack1,'Value') ~= 1)
        errordlg('Attack button has been pressed! ');
        return;
    end
    nameofeval = [ud.name, '_watermarked'];
    PQA = PQevalAudio(ud.name, nameofeval);
    set(handles.VPEAQnoattacks, 'String', round(PQA*1000)/1000);
    set(gcf,'UserData',ud);
    % If the 'spectrum' radio button is selected, then plot the spectrogram
else
    ud = get(gcf,'UserData');
    set(handles.PEAQnoattacks, 'Value', 0);
    if (get(handles.Attack1,'Value') == 1)
        errordlg('Attack button hasn''t been pressed! ');
        return;
    elseif (get(handles.Attack2,'Value') == 1)
        suf = '_noise';
    elseif (get(handles.Attack3,'Value') == 1)
        suf = '_lpfiltering';
    elseif (get(handles.Attack4,'Value') == 1)
        suf = '_echo';
    elseif (get(handles.Attack5,'Value') == 1)
        suf = '_cropping';
    elseif (get(handles.Attack6,'Value') == 1)
        suf = '_jittering';
    elseif (get(handles.Attack7,'Value') == 1)
        suf = '_inserting';
    else
    end
    
    fid = fopen([ud.name, suf,'.wav'],'rb','l');   % Little-endian
    if fid == -1,
        errordlg([ud.name, suf,'.wav not found!']);
        return;
    end
    
    nameofeval = [ud.name, suf];
    PQA = PQevalAudio(ud.name, nameofeval);
    set(handles.VPEAQattacks, 'String', round(PQA*1000)/1000);
    set(gcf,'UserData',ud);
end


% --- Executes during object creation, after setting all properties.
function valueofembeddingtime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to valueofembeddingtime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called




% --- Executes during object creation, after setting all properties.
function text82_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text82 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
