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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 09-Mar-2010 12:59:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @jig322_OpeningFcn, ...
                   'gui_OutputFcn',  @jig322_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 jig322 is made visible.
function jig322_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 jig322 (see VARARGIN)

% Choose default command line output for jig322
handles.output = hObject;
handles.xyz=0;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = jig322_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;




function second_Callback(hObject, eventdata, handles)
% hObject    handle to second (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 second as text
%        str2double(get(hObject,'String')) returns contents of second as a double
% handles.jigar = str2double(get(hObject,'String'));
% ti = str2double(get(handles.second,'String'));
%  k1=str2double(get(hObject,'String'));
%  disp(k1)
%  handles.record=k1;

% --- Executes during object creation, after setting all properties.
function second_CreateFcn(hObject, eventdata, handles)
% hObject    handle to second (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in sample.
function sample_Callback(hObject, eventdata, handles)
% hObject    handle to sample (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns sample contents as cell array
%        contents{get(hObject,'Value')} returns selected item from sample
cellst=get(hObject,'String');
cellval=get(hObject,'Value');
handles.fs=str2num(cellst{cellval});
guidata(hObject,handles);



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

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


% --- Executes on selection change in bit.
function bit_Callback(hObject, eventdata, handles)
% hObject    handle to bit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns bit contents as cell array
%        contents{get(hObject,'Value')} returns selected item from bit
cellst=get(hObject,'String');
cellval=get(hObject,'Value');
handles.bi=str2num(cellst{cellval});
guidata(hObject,handles);


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

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


% --- Executes on selection change in chanel.
function chanel_Callback(hObject, eventdata, handles)
% hObject    handle to chanel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns chanel contents as cell array
%        contents{get(hObject,'Value')} returns selected item from chanel
cellst=get(hObject,'String');
cellval=get(hObject,'Value');
handles.ch=str2num(cellst{cellval});
guidata(hObject,handles);


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

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




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

disp(handles.fs)
disp(handles.bi)
disp(handles.ch)

n= str2double(get(handles.second,'String'));

disp(n)
if handles.bi==16
    temp='single';
else
    temp='double';
end

x = wavrecord(n*handles.fs,handles.fs,handles.ch,temp);
   




handles.x=x;
handles.y=handles.x;
guidata(hObject,handles);

if handles.ch==2
        m2=max(handles.y(:,2));
        n2=max(-handles.y(:,2));
        n2=abs(n2);
        if m2>n2
            p2=m2;
        else
            p2=n2;
        end
        scale=0.95/p2;
        handles.y(:,2)=scale*handles.y(:,2)
    end
    m=max(handles.y(:,1));
    n=max(-handles.y(:,1));
    n=abs(n);
    if m>n
        p=m;
    else
        p=n;
    end
    scale=0.95/p;
    handles.y(:,1)=scale*handles.y(:,1)
    axes(handles.axes1)
    plot(handles.y);
    
    
    N=512;
    axes(handles.axes2)
    
%     fft(double(handles.x));
%     Y = fft2(handles.x);
   Y = fft(handles.x,N);
    Pyy = Y.* conj(Y) / N;
    f = handles.fs*(0:256)/N;
    plot(f,Pyy(1:257));




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








function filename_Callback(hObject, eventdata, handles)
% hObject    handle to filename (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 filename as text
%        str2double(get(hObject,'String')) returns contents of filename as a double


handles.FileName=get(hObject,'String');
handles.FileName1=char(handles.FileName);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function filename_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filename (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


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


 wavwrite(handles.x,handles.fs,handles.bi,handles.FileName1);
 
 
 

 
 
 



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in import.
function import_Callback(hObject, eventdata, handles)
% hObject    handle to import (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs= 11025;
% N=8;
% s=uiimport('-file')
% % wavwrite(y,'filename')
% % wavwrite(s,fs,N,'filename')
% % wavwrite(s,handles.fs,handles.bi,testing);
%  wavplay(jigar,fs)
% winopen('filename')
%  file = uigetfile('*.wav');
%  wavplay(uiimport(-file)),fs)
% % k=open(file);
% fs=8000;
% s = importdata(uigetfile('*.wav'))
% wavplay(,fs)
% jig=handles.open.string
% y = wavread(data,fs);
% 
%  wavplay(y,fs)


s=uiimport('-file');
wavwrite(s.data,s.fs,'mediafile');
 y = wavread('mediafile');
 wavplay(y,s.fs)
% wavplay('mediafile',s.fs)
handles.y=y;
m=max(handles.y(:,1));
    n=max(-handles.y(:,1));
    n=abs(n);
    if m>n
        p=m;
    else
        p=n;
    end
    scale=0.95/p;
    handles.y(:,1)=scale*handles.y(:,1)
    axes(handles.axes1)
    plot(handles.y);
    

 N=512;
    axes(handles.axes2)
    Y = fft(y,N);
    Pyy = Y.* conj(Y) / N;
    f = s.fs*(0:256)/N;
    plot(f,Pyy(1:257));



function fps_Callback(hObject, eventdata, handles)
% hObject    handle to fps (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 fps as text
%        str2double(get(hObject,'String')) returns contents of fps as a double


% --- Executes during object creation, after setting all properties.
function fps_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fps (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function trigger_Callback(hObject, eventdata, handles)
% hObject    handle to trigger (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 trigger as text
%        str2double(get(hObject,'String')) returns contents of trigger as a double


% --- Executes during object creation, after setting all properties.
function trigger_CreateFcn(hObject, eventdata, handles)
% hObject    handle to trigger (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function vdfilename_Callback(hObject, eventdata, handles)
% hObject    handle to vdfilename (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 vdfilename as text
%        str2double(get(hObject,'String')) returns contents of vdfilename as a double
handles.vdFileName=get(hObject,'String');
handles.vdFileName1=char(handles.vdFileName);
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function vdfilename_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vdfilename (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


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

fps= str2double(get(handles.fps,'String'));
n= str2double(get(handles.trigger,'String'));
aviobj = avifile(handles.vdFileName1,'compression','none');
aviobj.Quality = 50; 
vid = videoinput('winvideo', 1);


 vid.LoggingMode = 'disk';
 vid.DiskLogger = aviobj;
 vid.TriggerRepeat = n;
 vid.FramesPerTrigger=fps;

 start(vid);


preview(vid);
%   figure(3);
%    while (vid.TriggersExecuted ~= n+1)
%     
%    frame = getsnapshot(vid);
%    image(frame);
%    end
 
 
 
wait(vid,10000,'Logging')
vid.Running
vid.Logging
vid.DiskLoggerFrameCount
   while (vid.DiskLoggerFrameCount ~= vid.FramesAcquired)
   pause(1);
   end
   vid.DiskLoggerFrameCount
   aviobj = close(vid.DiskLogger);
   
   delete(vid);


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

file = uigetfile('*.avi');
if ~isequal(file, 0)
    open(file);
end

mov = aviread(file);
figure(2);
movie(mov)

