用GUI方式编程实现一个频率在100-500Hz,幅值在0-2000,相位0-180度可变的正弦波信号,信号采样频率为5120Hz.

 流程:获得滑动条的实时数值->设置,采样频率,采样间隔,采样时间,采样数量,采样时间总和-> 获取slider2,slider3代表的值->画图事宜->将变量变换成字符串,再赋值给编辑框的显示文本

 

 重点看这里,俺的注释贼清晰!!!
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value'); %通过 get(handles.slider1,'Value') 获取了滑动条 slider1 的值,并将其赋值给变量 fuzhi,这个值代表幅值
caiyangpinglv=5120;% 采样频率设置为5120
dt=1.0/caiyangpinglv;  %采样间隔
T=1; %采样时间
N=T/dt; %采样点数量
t=[0:N-1]/N;  %包含 N 个采样点的时间向量
chushixiangwei=get(handles.slider2,'Value');% 获取slider2代表的初始相位值
pinlv=get(handles.slider3,'Value');% 获取slider3代表的频率值
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);%计算波形
plot(t, x, 'r','LineWidth', 3);%画波形
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[1, 1, 1]); %gca 表示获取当前坐标轴的句柄。'color' 是坐标轴属性,用于设置坐标轴的背景色,红绿蓝综合比例
s1=sprintf('%f',fuzhi); %可以将变量 fuzhi 的值格式化为字符串,并将结果赋给变量 s1
set(handles.edit1,'String',s1);  %将字符串 s1 设置为句柄为 edit1 的编辑框的显示文本
wavplay(x,caiyangpinglv);  %播放音频信号 x,并指定采样频率为 caiyangpinglv

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

% Last Modified by GUIDE v2.5 29-Jun-2023 00:35:04

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = caiyang_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 slider1_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value'); %通过 get(handles.slider1,'Value') 获取了滑动条 slider1 的值,并将其赋值给变量 fuzhi,这个值代表幅值
caiyangpinglv=5120;% 采样频率设置为5120
dt=1.0/caiyangpinglv;  %采样间隔
T=1; %采样时间
N=T/dt; %采样点数量
t=[0:N-1]/N;  %包含 N 个采样点的时间向量
chushixiangwei=get(handles.slider2,'Value');% 获取slider2代表的初始相位值
pinlv=get(handles.slider3,'Value');% 获取slider3代表的频率值
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);%计算波形
plot(t, x, 'r','LineWidth', 3);%画波形
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[1, 1, 1]); %gca 表示获取当前坐标轴的句柄。'color' 是坐标轴属性,用于设置坐标轴的背景色,红绿蓝综合比例
s1=sprintf('%f',fuzhi); %可以将变量 fuzhi 的值格式化为字符串,并将结果赋给变量 s1
set(handles.edit1,'String',s1);  %将字符串 s1 设置为句柄为 edit1 的编辑框的显示文本
wavplay(x,caiyangpinglv);  %播放音频信号 x,并指定采样频率为 caiyangpinglv


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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 slider movement.
function slider2_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value');
caiyangpinglv=5120;
dt=1.0/caiyangpinglv;
T=1;
N=T/dt;
t=[0:N-1]/N;
chushixiangwei=get(handles.slider2,'Value');
pinlv=get(handles.slider3,'Value');
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);
plot(t, x, 'r','LineWidth', 3);
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[1, 1, 1]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',chushixiangwei);
set(handles.edit2,'String',s1);
wavplay(x,caiyangpinglv);

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 slider movement.
function slider3_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value');
caiyangpinglv=5120;
dt=1.0/caiyangpinglv;
T=1;
N=T/dt;
t=[0:N-1]/N;
chushixiangwei=get(handles.slider2,'Value');
pinlv=get(handles.slider3,'Value');
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);
plot(t, x, 'r','LineWidth', 3);
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[1, 1, 1]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',pinlv);
set(handles.edit2,'String',s1);
wavplay(x,caiyangpinglv);


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


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


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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值