基于计算机声卡的虚拟仪器

基于计算机声卡的虚拟仪器_第1张图片

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

% Last Modified by GUIDE v2.5 20-Jun-2010 22:41:16

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = xuniyiqi_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 pinlv.
function pinlv_Callback(hObject, eventdata, handles)
% hObject    handle to pinlv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ao                            %该放在这里么???
global chan
ao=analogoutput('winsound');
chan=addchannel(ao,1);
str1=get(hObject,'string'); %将pinlv的string赋给str1
handles.pinlv=str2num(str1);       %将字符转换为数字(因为我们在界面上输入的是字符1、2、3...)
set(ao,'samplerate',8000);%将a0所指的抽样频率设置为num1所指的值(设定抽样频率)
guidata(handles.fudu, handles);


% --- Executes during object creation, after setting all properties.
function pinlv_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pinlv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function fudu_Callback(hObject, eventdata, handles)
% hObject    handle to fudu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%设置幅度

str2=get(hObject,'string');
handles.Amp=str2num(str2);     % 幅度调节
guidata(handles.fudu, handles);

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


% --- Executes during object creation, after setting all properties.
function fudu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fudu (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 kuandu_Callback(hObject, eventdata, handles)
% hObject    handle to kuandu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%设置宽度

str3=get(hObject,'string');
handles.width=str2num(str3); 
guidata(handles.kuandu, handles);

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


% --- Executes during object creation, after setting all properties.
function kuandu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kuandu (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 sanjiaobo.
function sanjiaobo_Callback(hObject, eventdata, handles)
% hObject    handle to sanjiaobo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ao                           
global chan
ao=analogoutput('winsound');
chan=addchannel(ao,1);
handles.number=1;
handles.m=0.01;
try
  Amp=handles.Amp;
    pinlv=handles.pinlv;

    t=0:1/10000:0.1;
width=handles.width;

y=Amp*sawtooth(2*pi*pinlv*t,width);

%三角波的数据显示及输出
plot( handles.xianshi,t,y)          
data=y';                            % data为y的列向量,是putdata()的格式要求。
set(chan,'unitsrange',[-Amp Amp])   % unitsrange 控制幅度值
putdata(ao,data)                    % 数据由输出端口输出
start(ao);
data=getdata(ao);
stop(ao);
delete(ao);
guidata(handles.sanjiaobo, handles);
end


% --- Executes on button press in fangbo.
function fangbo_Callback(hObject, eventdata, handles)
% hObject    handle to fangbo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ao                            
global chan
ao=analogoutput('winsound');
chan=addchannel(ao,1);
handles.number=2;
try
  Amp=handles.Amp;
    pinlv=handles.pinlv;
  
 t=0:1/10000:0.1;
width=handles.width;

y=Amp*square(pinlv*t,100*width);

%方波的数据显示及输出
plot( handles.xianshi,t,y)          
data=y';                            % data为y的列向量,是putdata()的格式要求。
set(chan,'unitsrange',[-Amp Amp])   % unitsrange 控制幅度值
putdata(ao,data)                    % 数据由输出端口输出
guidata(handles.fangbo, handles);
start(ao);
data=getdata(ao);
stop(ao);
delete(ao);
end


% --- Executes on button press in zhengxianbo.
function zhengxianbo_Callback(hObject, eventdata, handles)
global ao                            
global chan
ao=analogoutput('winsound');
chan=addchannel(ao,1);
handles.number=3;

try
  Amp=handles.Amp;
    pinlv=handles.pinlv;

    t=0:1/10000:0.1;


y=Amp*sin(2*pi*pinlv*t);

%正弦波的数据显示及输出
plot( handles.xianshi,t,y)          
data=y';                            % data为y的列向量,是putdata()的格式要求。
set(chan,'unitsrange',[-Amp Amp])   % unitsrange 控制幅度值
putdata(ao,data)                    % 数据由输出端口输出
start(ao);
data=getdata(ao);
stop(ao);
delete(ao);
guidata(handles.zhengxian, handles);
end

% hObject    handle to zhengxianbo (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 shiboqi.
function shiboqi_Callback(hObject, eventdata, handles)
% hObject    handle to shiboqi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global AI

handles.number=4;
handles.y=1;
AI=analoginput('winsound');     %为声卡创建一个接口对象
chan = addchannel(AI,1);        %将通道1设置为有效通道,通道对象AI被加入到变量chan中
duration = 1;                   %采样一秒钟数据

handles.kongzhi=1;

set(AI,'SampleRate',8000)       %设置采样频率为8KHz
ActualRate = get(AI,'SampleRate');
set(AI,'SamplesPerTrigger',duration*ActualRate)%设置每触发一个采样后,需要采样多少个数据;这里的duration*ActualRate个数相当于1秒钟   

set(AI,'TriggerType','Manual')%设置触发方式,这里设置了手动触发,即运行下面的trigger命令后,立即触发。

start(AI)  %开始采样

guidata(handles.shiboqi, handles);


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

trigger(AI) %现在手动触发

data = getdata(AI); %获取数据

len=length(data);

t=0:1/8000:(len-1)/8000;
plot(handles.xianshi,t,data);

   end


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

if handles.number==1||handles.number==2||handles.number==3
                          
 
  delete(allchild(handles.xianshi)) ; 
    stop(ao);
    delete(ao);
    
  
        
else
     delete(allchild(handles.xianshi)) ; 
     stop(AI);
     delete(AI);
guidata(handles.quxiao, handles);

end
end


% --- Executes on button press in suoxiao1.
function suoxiao1_Callback(hObject, eventdata, handles)
% hObject    handle to suoxiao1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%m在其他地方定义赋值
if handles.number~=4


 if handles.number==1  %  此时为三角波输出
  b=get(handles.xianshi,'XLim');
  
set(handles.xianshi,'XLim',2*b);


 end

 if handles.number==2   % 此时为方波输出
b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',2*b);
 end
 
 
 if handles.number==3   % 此时为正弦波输出
     b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',2*b);
 
 end
else
 
b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',2*b);
 end

% --- Executes on button press in fangda.
function fangda_Callback(hObject, eventdata, handles)
% hObject    handle to fangda (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.number~=4


 if handles.number==1  %  此时为三角波输出
  b=get(handles.xianshi,'XLim');
  
set(handles.xianshi,'XLim',1/2*b);


 end

 if handles.number==2   % 此时为方波输出
b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',1/2*b);
 end
 
 
 if handles.number==3   % 此时为正弦波输出
     b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',1/2*b);
 
 end
else
 
b=get(handles.xianshi,'XLim');
set(handles.xianshi,'XLim',1/2*b);
 end


% --- Executes on button press in yfa.
function yfa_Callback(hObject, eventdata, handles)
% hObject    handle to yfa (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.number==1
   b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',1/2*b); 
end

if handles.number==2
   b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',1/2*b); 
end

if handles.number==3
   b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',1/2*b); 
end

if handles.number==4
   b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',1/2*b); 
end
% --- Executes on button press in yfx.
function yfx_Callback(hObject, eventdata, handles)
% hObject    handle to yfx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.number==1
 b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',2*b); 
end

if handles.number==2
 b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',2*b); 
end

if handles.number==3
 b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',2*b); 
end

if handles.number==4
 b=get(handles.xianshi,'YLim');
set(handles.xianshi,'YLim',2*b); 
end


% --- Executes on button press in pinlvji.
function pinlvji_Callback(hObject, eventdata, handles)
% hObject    handle to pinlvji (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global AI
handles.number=5;
duration=1;

AI=analoginput('winsound');     %为声卡创建一个接口对象
chan = addchannel(AI,1);        %将通道1设置为有效通道,通道对象AI被加入到变量chan中
ActualRate = get(AI,'SampleRate');
set(AI,'SampleRate',8000)       %设置采样频率为8KHz
set(AI,'SamplesPerTrigger',duration*ActualRate)%设置每触发一个采样后,需要采样多少个数据;这里的duration*ActualRate个数相当于1秒钟采集的数据
start(AI)  %开始采样
data = getdata(AI);%获取数据

N=length(data);

Y=fft(data,N);
pyy=Y.*conj(Y)/N;%对应频率点的模值
f=8000*(0:N/2)/N;%对应数据点处的频率

                  %plot(f,pyy(1:(N/2+1)))

[m,i]=max(pyy);
F=f([i]);
A=num2str(F);
set(handles.text8,'string',A);
guidata(handles.pinlvji, handles);


% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    global AI
 n=get(handles.text8,'string');
if n~=' '
  set(handles.text8,'string','')  

stop(AI);
    delete(AI);
  end  
end


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

你可能感兴趣的:(MATLAB,板块3:通信与信号处理)