% SNR állandóságára kiváló demo: music.wav elsõ 400ms-a, 5 bites lin és log
% kavtáló esetén->látható, hogy a log kvantáló itt is kevés zajt csinál,
% cserébe nagyobb a zaj ott, ahol teljesen kivezérelt a jel (ilet is találhatsz
% a music.wav vége felé,ott a lin kvantáló jobb)
function varargout = lebeges_demo(varargin)
% LEBEGES_DEMO M-file for lebeges_demo.fig
%      LEBEGES_DEMO, by itself, creates a new LEBEGES_DEMO or raises the existing
%      singleton*.
%
%      H = LEBEGES_DEMO returns the handle to a new LEBEGES_DEMO or the handle to
%      the existing singleton*.
%
%      LEBEGES_DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LEBEGES_DEMO.M with the given input arguments.
%
%      LEBEGES_DEMO('Property','Value',...) creates a new LEBEGES_DEMO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before lebeges_demo_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to lebeges_demo_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 lebeges_demo

% Last Modified by GUIDE v2.5 11-Mar-2009 12:15:53

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @lebeges_demo_OpeningFcn, ...
                   'gui_OutputFcn',  @lebeges_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 lebeges_demo is made visible.
function lebeges_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   command line arguments to lebeges_demo (see VARARGIN)
global F % a szinusz frekvenciája
global T % a jel hossza (lejátszás miatt fontos
global fs % default mintavételi frekvencia
global fs_cont % ezzel a mintavétellel folytonosnak tekintjük
global signal % a jel handle-je (lásd help function handle)
global sampled % a mintavett jel (vektor alakban)
global sampled_handle % a mintavett jel line plotjának handle-je
global cont_handle % folyt. jel line plotjának handle-je
global player % a lejátszó
global nyquist_handle % a Nyquist frekvenciát jelölõ diracdelta
global sampledspec_handle % a mintavett jel spektruma plotjának handle-je
global contspec_handle % folyt. jel spektruma plotjának handle-je
F = 800;
T = 10;
fs = 1760;
fs_cont = 44100; % ezzel a mintavételi frekkel tekintem folytonos jelnek
signal = @(t) 0.3*sin(F*2*pi*t);
t = 0 : 1/fs : T;
t_cont = 0 : 1/fs_cont : T;
sampled = signal(t);
player = audioplayer(sampled,fs);

% ábrázolás
cont_handle = line(t_cont,signal(t_cont),'Parent',handles.axes1,'Color',[1 0 0],'LineStyle',':','LineWidth',3);
set(handles.axes1,'xlim',[0 1/F*10],'ylim',[-.35 .35])
sampled_handle = line(t,sampled,'Parent',handles.axes1);
set(get(handles.axes1,'XLabel'),'String','Idõ [s]')

% folytonos spektrum
Xf=1/length(t_cont)*fft(signal(t_cont));
f=0:1/T:(fs_cont)/T;
contspec_handle = line(f(1:round(length(f)/2)),abs(Xf(1:round(length(f)/2))),'Parent',handles.axes_contspec);
set(handles.axes_contspec,'XLim',...
    [f(1)-(f(round(length(f)/2))-f(1))*0.05,...
    f(round(length(f)/2))+(f(round(length(f)/2))-f(1))*0.05]...
    );
set(get(handles.axes_contspec,'XLabel'),'String','Frekvencia [Hz]')

% mintavett spektrum
Xfk=1/length(sampled)*fft(sampled,length(sampled));
fk=0:1/T:fs;
sampledspec_handle = stem(handles.axes_sampledspec,fk(1:end),2*abs(Xfk));
set(get(handles.axes_sampledspec,'XLabel'),'String','Frekvencia [Hz]')
maxXfk=max(abs(Xfk));
nyquist_handle = line([fs/2,fs/2],[0,maxXfk],'Parent',handles.axes_sampledspec,'LineWidth',2,'Color',[0 0 0]);

set(handles.edit_sinfrek,'String',F)
set(handles.slider_fs,'Min',1)
set(handles.slider_fs,'Max',fs)
set(handles.slider_fs,'Value',fs)
set(handles.text_fs,'String',sprintf('fs = %0.1f Hz',fs))

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes lebeges_demo wait for user response (see UIRESUME)
% uiwait(handles.main_figure);


% --- Outputs from this function are returned to the command line.
function varargout = lebeges_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 slider movement.
function slider_fs_Callback(hObject, eventdata, handles)
% hObject    handle to slider_fs (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global T signal sampled_handle sampled player sampledspec_handle nyquist_handle
new_fs = get(hObject,'Value');
set(handles.text_fs,'String',sprintf('fs = %0.1f Hz',new_fs))
new_t = 0 : 1/new_fs : T;
sampled = signal(new_t);
if get(handles.button_ps,'Value') == get(handles.button_ps,'Max')
	% lejátszás leállítása
    set(handles.button_ps,'Value',get(handles.button_ps,'Min'))
    set(handles.button_ps,'String','Lejátszás')
    stop(player)
end
player = audioplayer(sampled,new_fs);
% plot
set(sampled_handle,'XData',new_t,'YData',sampled(1:numel(new_t)))
% mintavett spektrum
Xfk=1/length(sampled)*fft(sampled,length(sampled));
fk=0:1/T:new_fs;
set(sampledspec_handle,'XData',fk(1:end),'YData',2*abs(Xfk))
maxXfk=max(abs(Xfk));
set(nyquist_handle,'XData',[new_fs/2,new_fs/2],'YData',[0,maxXfk])




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



function edit_sinfrek_Callback(hObject, eventdata, handles)
% hObject    handle to edit_sinfrek (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 edit_sinfrek as text
%        str2double(get(hObject,'String')) returns contents of edit_sinfrek as a double
global T fs fs_cont signal cont_handle contspec_handle sampled_handle sampled player sampledspec_handle nyquist_handle
new_F = str2double(get(hObject,'String'));
if new_F > fs/2
    set(handles.text_badsinfrek,'String',sprintf('A szinusz frekvenciája ne haladja meg a %d Hz-et',fs/2))
elseif new_F > 0
    set(handles.text_badsinfrek,'String','')
    signal = @(t) 0.3*sin(new_F*2*pi*t);
    t_cont = 0 : 1/fs_cont : T;
    fs = get(handles.slider_fs,'Value');
    t = 0 : 1/fs : T;
    sampled = signal(t);
    player = audioplayer(sampled,fs);

    % ábrázolás
    set(cont_handle,'XData',t_cont,'YData',signal(t_cont))
    set(handles.axes1,'xlim',[0 1/new_F*10],'ylim',[-.35 .35])
    set(sampled_handle,'XData',t,'YData',sampled(1:numel(t)))

    % folytonos spektrum
    Xf=1/length(t_cont)*fft(signal(t_cont));
    f=0:1/T:(fs_cont)/T;
    set(contspec_handle,'XData',f(1:round(length(f)/2)),'YData',abs(Xf(1:round(length(f)/2))))
    set(handles.axes_contspec,'XLim',...
        [f(1)-(f(round(length(f)/2))-f(1))*0.05,...
        f(round(length(f)/2))+(f(round(length(f)/2))-f(1))*0.05]...
        );

    % mintavett spektrum
    Xfk=1/length(sampled)*fft(sampled,length(sampled));
    fk=0:1/T:fs;
    set(sampledspec_handle,'XData',fk(1:end),'YData',2*abs(Xfk))
    maxXfk=max(abs(Xfk));
    set(nyquist_handle,'XData',[fs/2,fs/2],'YData',[0,maxXfk])
end



% --- Executes during object creation, after setting all properties.
function edit_sinfrek_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_sinfrek (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 button_ps.
function button_ps_Callback(hObject, eventdata, handles)
% hObject    handle to button_ps (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global player
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
	% Toggle button is pressed-take approperiate action
    play(player)
    set(hObject,'String','Leállítás')
elseif button_state == get(hObject,'Min')
	% Toggle button is not pressed-take appropriate action
    stop(player)
    set(hObject,'String','Lejátszás')
end



