function varargout = Noisesound(varargin)


%%%Dr. Ignacio Serrano-Pedraza 2018
%%%iserrano@ucm.es


% NOISESOUND MATLAB code for Noisesound.fig
%      NOISESOUND, by itself, creates a new NOISESOUND or raises the existing
%      singleton*.
%
%      H = NOISESOUND returns the handle to a new NOISESOUND or the handle to
%      the existing singleton*.
%
%      NOISESOUND('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NOISESOUND.M with the given input arguments.
%
%      NOISESOUND('Property','Value',...) creates a new NOISESOUND or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Noisesound_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Noisesound_OpeningFcn via varargin.
%
%      *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 Noisesound

% Last Modified by GUIDE v2.5 17-Nov-2017 17:36:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Noisesound_OpeningFcn, ...
    'gui_OutputFcn',  @Noisesound_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 Noisesound is made visible.
function Noisesound_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   command line arguments to Noisesound (see VARARGIN)

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

% Play_sound handles structure
guidata(hObject, handles);
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


function Plot_figure(hObject, eventdata, handles,playsound)
expt = getexpt(handles);

frecuenciamuestreo=expt.Framerate;
if expt.Duration<=0;expt.Duration=0.001;set(handles.Duration,'string',0.001);end
duracion=expt.Duration; %seconds
if expt.amplitude<0;expt.amplitude=0;set(handles.Amplitude,'string',0);end

t=[0:1/frecuenciamuestreo:duracion-(1/frecuenciamuestreo)]';
if expt.radiobuttongaussian==1
    y= 0 + sqrt(expt.amplitude)/2 * randn(length(t),1);   
end

if expt.radiobuttonuniform==1
    y= 0 + sqrt(expt.amplitude) * ((rand(length(t),1))-0.5)*2;
end
if expt.radiobuttononef==1
    n=frecuenciamuestreo*duracion;
    freq=(-fix(n/2):fix((n-1)/2))/duracion;
    y= 0 + sqrt(expt.amplitude)/2 * randn(length(t),1);
    Y=fftshift(fft(y));
    %modulo=abs(Y)';
    fase=angle(Y);
    unof=500*abs(expt.amplitude./freq.^expt.exponentf);
    H=find(freq==0);
    unof(H)=expt.amplitude;
    fase=pi*(rand(1,round(n))-0.5)*2;
    REALPROD=fftshift(unof.*cos(fase));
    IMAGPROD=fftshift(unof.*sin(fase));
    RESULTADO=(REALPROD+IMAGPROD*sqrt(-1));
    FINAL=(ifft(RESULTADO));
    y=real(FINAL);
    
end


axes(handles.Plot1);
cla;
plot(t,y,'b-');
title('Time plot')
xlabel('Time (msec)')
ylabel('Amplitude')
if expt.amplitude==0
axis([0 expt.ejeX -0.3 0.3])
else
axis([0 expt.ejeX -(max(abs(y))+0.1*max(abs(y))) (max(abs(y))+0.1*max(abs(y)))])
end
if playsound==1
    sound(y,frecuenciamuestreo)
end

Y=fftshift(fft(y));
modulo=abs(Y)'*2*(1/length(y));
fase=angle(Y)';
n=length(y);
freq=(-fix(n/2):fix((n-1)/2))/duracion;

axes(handles.Plot2);
cla;
if expt.radiobuttononef==1;modulo=expt.amplitude*modulo./max(modulo);end
plot(freq,modulo,'b-');
title('Amplitude spectrum')
xlabel('Temporal frequency (Hz)')
ylabel('Amplitude')
if expt.amplitude==0
axis([20 15000 0 1]) 
else
axis([20 15000 0 max(modulo)+0.3*max(modulo)])
end
H=find(modulo==max(modulo));

axes(handles.Plot3);
cla;

[G B]=hist(y,40);
if expt.radiobuttongaussian==1
    histfit(y,40)
else
    hist(y,40)
end
title('Histogram')
xlabel('Amplitude')
ylabel('Frequency')
axis([-(max(B)+0.1*max(B)) (max(B)+0.1*max(B)) 0 max(G)+0.1*max(G)])


% --- Outputs from this function are returned to the command line.
function varargout = Noisesound_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 Play_sound.
function Play_sound_Callback(hObject, eventdata, handles)
% hObject    handle to Play_sound (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
playsound=1;
Plot_figure(hObject, eventdata, handles,playsound)



% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end


% --- Executes during object creation, after setting all properties.
function Amplitude_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Amplitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
function Amplitude_Callback(hObject, eventdata, handles)
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)

function expt = getexpt(handles)
% Reads off key data from GUI
%expt = handles.expt;
A = get(handles.Amplitude,'string');
expt.amplitude = str2double(A);
% TF = get(handles.Temporal_frequency,'string');
% expt.Temporal_frequency = str2double(TF);
% PHASE = get(handles.Phase,'string');
% expt.PHASE = str2double(PHASE);
Duration = get(handles.Duration,'string');
expt.Duration = str2double(Duration);
Framerate = get(handles.Framerate,'string');
expt.Framerate = str2double(Framerate);
expt.ejeX = get(handles.axisX,'value');
%expt.ejeX = str2double(ejeX);
% expt.freqslider = get(handles.Freqslider,'value');
expt.radiobuttongaussian= get(handles.radiobuttonslider,'value');
expt.radiobuttonuniform= get(handles.radiobutton2,'value');
expt.radiobuttononef= get(handles.radiobutton3,'value');
exponentf = get(handles.exponentf,'string');
expt.exponentf= str2double(exponentf);



function Temporal_frequency_Callback(hObject, eventdata, handles)
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


% --- Executes during object creation, after setting all properties.
function Temporal_frequency_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Temporal_frequency (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 Plot_figure.
function Plot_figure_Callback(hObject, eventdata, handles)
% hObject    handle to Plot_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)



function Duration_Callback(hObject, eventdata, handles)
% hObject    handle to Duration (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 Duration as text
%        str2double(get(hObject,'String')) returns contents of Duration as a double
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


% --- Executes during object creation, after setting all properties.
function Duration_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Duration (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



function Framerate_Callback(hObject, eventdata, handles)
% hObject    handle to Framerate (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 Framerate as text
%        str2double(get(hObject,'String')) returns contents of Framerate as a double
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


% --- Executes during object creation, after setting all properties.
function Framerate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Framerate (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
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.axixX,'Name') '?'],...
    ['Close ' get(handles.axixX,'Name') '...'],...
    'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.axixX)
% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.axixX)

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


% --- 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)


set(handles.Amplitude, 'String', 1);
set(handles.Duration, 'String', 1);
set(handles.Framerate, 'String', 44100);
set(handles.axisX,'Value',0.001);
set(handles.exponentf,'String',0.2);
set(handles.radiobuttonslider,'Value',1);
set(handles.radiobutton2,'Value',0);
set(handles.radiobutton3,'Value',0);
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)

%set(handles.unitgroup, 'SelectedObject', handles.english);

%set(handles.text4, 'String', 'lb/cu.in');
%set(handles.text5, 'String', 'cu.in');
%set(handles.text6, 'String', 'lb');

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





% --- Executes on slider movement.
function axisX_Callback(hObject, eventdata, handles)
% hObject    handle to axisX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%get(hObject,'Value')
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




% --- Executes on button press in radiobuttonslider.
function radiobuttonslider_Callback(hObject, eventdata, handles)
% hObject    handle to radiobuttonslider (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 radiobuttonslider
set(handles.radiobutton2,'Value',0);
set(handles.radiobutton3,'Value',0);
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (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 radiobutton2
set(handles.radiobuttonslider,'Value',0);
set(handles.radiobutton3,'Value',0);
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)

% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (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 radiobutton3
set(handles.radiobuttonslider,'Value',0);
set(handles.radiobutton2,'Value',0);
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)



function exponentf_Callback(hObject, eventdata, handles)
% hObject    handle to exponentf (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 exponentf as text
%        str2double(get(hObject,'String')) returns contents of exponentf as a double
playsound=0;
Plot_figure(hObject, eventdata, handles,playsound)


% --- Executes during object creation, after setting all properties.
function exponentf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to exponentf (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
