【信号处理】虚拟信号发生器(各种波形)matlab代码

386 阅读4分钟

1 简介

传统的台式函数信号发生器具有体积庞大,接口不灵活,波形固定,价格昂贵等缺点,针对此现状,本次根据老师布置的作业完成一个简单的数字信号发生器,可以产生不同幅值,频率的白噪声、正弦波、锯齿波、方波。工具选择为matlab,它具有强大的数据分析功能和数据处理功能[1],其自带的库函数为信号波形的产生带来了很大的方便。正文详细介绍了通过计算机声卡如何完成虚拟数字信号发生器的基本原理以及出现波形所需要实现的具体函数。

1 概述

  本文主要介绍了利用计算机编程工具matlab,通过计算机声卡实现虚拟数字信号发生器的基本原理以及实现步骤;针对传统数字信号发生器的缺点,完成虚拟数字信号发生器的设计,实现简单数字信号如三角波、锯齿波、方波、白噪声等的发生。

2 技术路线

声卡是实现声波/数字信号相互转换的一种硬件[2],在一般通常使用的PC机配置声卡中,大豆提供了麦克风输入和扬声器输出2个接口,他们集成了信号IO需要的模/数转换器(ADC)和数/模转换器(DAC)。因此,我们只需要将产生的数字信号通过音频线引出即可获得声音输出。

所有波形由matlab中所带的函数绘制完成,通过固定的接口函数,完成波形与声卡的对接,从而实现波形与音频信号的转换。

2 部分代码

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

% Last Modified by GUIDE v2.5 22-Sep-2009 21:56:54

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                  'gui_Singleton',  gui_Singleton, ...
                  'gui_OpeningFcn', @signal_prodoce_comlpete_OpeningFcn, ...
                  'gui_OutputFcn',  @signal_prodoce_comlpete_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 signal_prodoce_comlpete is made visible.
function signal_prodoce_comlpete_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 signal_prodoce_comlpete (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = signal_prodoce_comlpete_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 fangbo.
function fangbo_Callback(hObject, eventdata, handles)
global y;
t=0:100/44100:10;

A=str2double(get(handles.edit_A,'string'));
f=str2double(get(handles.edit_f,'string'));

y=A*square(2*pi*f*t)/2;
plot(t,y);
axis([0 10 -(A+5) (A+5)])
% 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)


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





function edit_A_Callback(hObject, eventdata, handles)

% hObject   handle to edit_A (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_A as text
%       str2double(get(hObject,'String')) returns contents of edit_A as a double


% --- Executes during object creation, after setting all properties.
function edit_A_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit_A (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 edit_f_Callback(hObject, eventdata, handles)
% hObject   handle to edit_f (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_f as text
%       str2double(get(hObject,'String')) returns contents of edit_f as a double


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



% --- Executes on button press in radiobutton1.

3 仿真结果

4 参考文献

[1]卢晓红, 麻硕士, 贾振元. 虚拟波形发生器的研究与开发[J]. 仪器仪表学报, 2006(z3):4.

部分理论引用网络文献,若有侵权联系博主删除。

5 MATLAB代码与数据下载地址

见博客主页