代码:
classdef App_uoye < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
LabelNumericEditField matlab.ui.control.Label
f1 matlab.ui.control.NumericEditField
LabelNumericEditField2 matlab.ui.control.Label
f2 matlab.ui.control.NumericEditField
LabelNumericEditField3 matlab.ui.control.Label
f3 matlab.ui.control.NumericEditField
Label3 matlab.ui.control.Label
Label5 matlab.ui.control.Label
Label6 matlab.ui.control.Label
LabelNumericEditField4 matlab.ui.control.Label
A1 matlab.ui.control.NumericEditField
LabelNumericEditField5 matlab.ui.control.Label
A2 matlab.ui.control.NumericEditField
LabelNumericEditField6 matlab.ui.control.Label
A3 matlab.ui.control.NumericEditField
LabelNumericEditField8 matlab.ui.control.Label
theta2 matlab.ui.control.NumericEditField
LabelNumericEditField9 matlab.ui.control.Label
theta3 matlab.ui.control.NumericEditField
LabelNumericEditField10 matlab.ui.control.Label
theta1 matlab.ui.control.NumericEditField
Label7 matlab.ui.control.Label
Label8 matlab.ui.control.Label
Label9 matlab.ui.control.Label
Button matlab.ui.control.Button
Label matlab.ui.control.Label
Label2 matlab.ui.control.Label
LabelNumericEditField11 matlab.ui.control.Label
fengzhi matlab.ui.control.NumericEditField
LabelNumericEditField12 matlab.ui.control.Label
fengfengzhi matlab.ui.control.NumericEditField
LabelNumericEditField13 matlab.ui.control.Label
junzhi matlab.ui.control.NumericEditField
LabelNumericEditField14 matlab.ui.control.Label
youxiaozhi matlab.ui.control.NumericEditField
LabelNumericEditField15 matlab.ui.control.Label
pinlv matlab.ui.control.NumericEditField
LabelNumericEditField16 matlab.ui.control.Label
zhouqi matlab.ui.control.NumericEditField
Button2 matlab.ui.control.Button
Button3 matlab.ui.control.Button
Button4 matlab.ui.control.Button
Button5 matlab.ui.control.Button
Button6 matlab.ui.control.Button
ax1 matlab.ui.control.UIAxes
ax2 matlab.ui.control.UIAxes
ax3 matlab.ui.control.UIAxes
ax5 matlab.ui.control.UIAxes
ax4 matlab.ui.control.UIAxes
ax6 matlab.ui.control.UIAxes
ax7 matlab.ui.control.UIAxes
ax8 matlab.ui.control.UIAxes
ax9 matlab.ui.control.UIAxes
ax10 matlab.ui.control.UIAxes
end
properties (Access = private)
% Description
p double
q double
n double% Description
y double % Description
N double % Description
Fs double % Description
t % Description
T % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
%频率分量
app.f1.Value=100;
app.f2.Value=200;
app.f3.Value=300;
%幅值分量
app.A1.Value=1;
app.A2.Value=2;
app.A3.Value=3;
%matlab 默认弧度制
%相位分量
app.theta1.Value=0;
app.theta2.Value=pi/6;
app.theta3.Value=pi/3;
end
% Button pushed function: Button
function ButtonButtonPushed(app, event)
% 获得一组数字信号
app.Fs=5120; %采样频率
dt=1.0/app.Fs; %采样时间间隔
app.N=1024; %样点个数
app.T=dt*app.N; %采样时段
app.t=linspace(0,app.T,app.N); %时域图形的横坐标
%得到正弦信号 y
app.y=app.A1.Value*sin(2*pi*app.f1.Value*app.t+app.theta1.Value)+app.A2.Value*sin(2*pi*app.f2.Value*app.t+app.theta2.Value)+...
app.A3.Value*sin(2*pi*app.f3.Value*app.t+app.theta3.Value); %正弦信号
%信号分析
app.fengzhi.Value=max(app.y);
app.fengfengzhi.Value=max(app.y)-min(app.y);
app.junzhi.Value=mean(app.y);
app.youxiaozhi.Value=rms(app.y);
%频率分析
app.p=max(app.y);
app.q=min(app.y);
app.n=1;
at=0.8*(app.p-app.q)+app.q;%原理:过零检测
i=1;
for k=2:1:app.N-2
if(app.y(k-1)<at&&app.y(k)<=at&&app.y(k+1)>at&&app.y(k+2)>at)
ti(i)=k;
i=i+1;
end
end
app.zhouqi.Value=(ti(2)-ti(1))*dt;
app.pinlv.Value=1/app.zhouqi.Value;
%输出时域信号 ()
plot(app.ax1,app.t,app.y);
drawnow;
pause(0.05);
end
% Button pushed function: Button2
function Button2ButtonPushed(app, event)
N1=1024;
Nfft=N1;
n1=0:N1-1;
t1=n1/app.Fs;%采用的时间序列
xn=app.A1.Value*sin(2*pi*app.f1.Value*t1+app.theta1.Value)+app.A2.Value*cos(2*pi*app.f2.Value*t1+app.theta2.Value)...
+app.A3.Value*sin(2*pi*app.f3.Value*t1+app.theta3.Value);
P=10*log10(abs(fft(xn,Nfft).^2)/N1);%Fourier 振幅谱平方的平均值,并转化为 dB
f=(0:length(P)-1)*app.Fs/length(P);%给出频率序列
plot(app.ax3,f(1:N1/2),P(1:N1/2));
Pxx=abs(fft(xn,Nfft).^2)/N1;%Fourier 振幅谱平方的平均值,并转化为 dB
f=(0:length(Pxx)-1)*app.Fs/length(Pxx);%给出频率序列
plot(app.ax7,f(1:N1/2),Pxx(1:N1/2));%绘制功率谱曲线
% std(Pxx)^2;
drawnow
pause(2)
end
% Button pushed function: Button3
function Button3ButtonPushed(app, event)
%生成高斯白噪声
x11=wgn(1,app.N,10);
x1=1:app.N;
plot(app.ax4,x1,x11);
s=xcorr(x11,app.y);
x4=linspace(0,2*app.T,2*app.N-1);%互相关分析
plot(app.ax5,x4,s);
drawnow ;
pause(1);
end
% Button pushed function: Button4
function Button4ButtonPushed(app, event)
P=fft(app.y,app.N); %快速傅里叶变换
Pyy =2*sqrt(P.* conj(P))/app.N; %模值除以 N/2
Pyy(1)= Pyy(1)/2; %第一个点模值除以 N
f=linspace(0,app.Fs/2,app.N/2); %频域图形的横坐标
plot(app.ax2,f,Pyy(1:app.N/2)); %绘制频域波形
drawnow
pause(2)
end
% Button pushed function: Button5
function Button5ButtonPushed(app, event)
n8=0:app.N-1;
ffty=fft(app.y,app.N); %对信号进行快速 Fourier 变换
mag=abs(ffty); %求取 Fourier 变换的振幅
f89=n8*app.Fs/app.N;
plot(app.ax6,f89,mag); %绘出随频率变化的振幅
ph=2*angle(ffty(1:app.N/2));
ph=ph*180/pi;
plot(app.ax8,f89(1:app.N/2),ph(1:app.N/2));
drawnow;
pause(2);
end
% Button pushed function: Button6
function Button6ButtonPushed(app, event)
N6=length(app.y);
if mod(N6,2)==0
k=-N6/2:N6/2-1;
else
k=-(N6-1)/2:(N6-1)/2;
end
T1=N6/app.Fs;
Range=k/T1;
Doain=fft(app.y)/app.N;
Doain=fftshift(Doain);
CnR=real(Doain);
CnI=imag(Doain);
plot(app.ax9,Range,CnR)
plot(app.ax10,Range,CnI)
drawnow;
pause(1)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [101 101 1407 714];
app.UIFigure.Name = 'UI Figure';
% Create LabelNumericEditField
app.LabelNumericEditField = uilabel(app.UIFigure);
app.LabelNumericEditField.HorizontalAlignment = 'right';
app.LabelNumericEditField.VerticalAlignment = 'top';
app.LabelNumericEditField.Position = [1226 404 20 15];
app.LabelNumericEditField.Text = 'f1';
% Create f1
app.f1 = uieditfield(app.UIFigure, 'numeric');
app.f1.Position = [1261 400 100 22];
% Create LabelNumericEditField2
app.LabelNumericEditField2 = uilabel(app.UIFigure);
app.LabelNumericEditField2.HorizontalAlignment = 'right';
app.LabelNumericEditField2.VerticalAlignment = 'top';
app.LabelNumericEditField2.Position = [1226 368 20 15];
app.LabelNumericEditField2.Text = 'f2';
% Create f2
app.f2 = uieditfield(app.UIFigure, 'numeric');
app.f2.Position = [1261 364 100 22];
% Create LabelNumericEditField3
app.LabelNumericEditField3 = uilabel(app.UIFigure);
app.LabelNumericEditField3.HorizontalAlignment = 'right';
app.LabelNumericEditField3.VerticalAlignment = 'top';
app.LabelNumericEditField3.Position = [1226 337 20 15];
app.LabelNumericEditField3.Text = 'f3';
% Create f3
app.f3 = uieditfield(app.UIFigure, 'numeric');
app.f3.Position = [1261 333 100 22];
% Create Label3
app.Label3 = uilabel(app.UIFigure);
app.Label3.VerticalAlignment = 'top';
app.Label3.Position = [1372 368 20 15];
app.Label3.Text = 'Hz';
% Create Label5
app.Label5 = uilabel(app.UIFigure);
app.Label5.VerticalAlignment = 'top';
app.Label5.Position = [1372 403 20 15];
app.Label5.Text = 'Hz';
% Create Label6
app.Label6 = uilabel(app.UIFigure);
app.Label6.VerticalAlignment = 'top';
app.Label6.Position = [1372 337 20 15];
app.Label6.Text = 'Hz';
% Create LabelNumericEditField4
app.LabelNumericEditField4 = uilabel(app.UIFigure);
app.LabelNumericEditField4.HorizontalAlignment = 'right';
app.LabelNumericEditField4.VerticalAlignment = 'top';
app.LabelNumericEditField4.Position = [1226 303 20 15];
app.LabelNumericEditField4.Text = 'A1';
% Create A1
app.A1 = uieditfield(app.UIFigure, 'numeric');
app.A1.Position = [1261 299 100 22];
% Create LabelNumericEditField5
app.LabelNumericEditField5 = uilabel(app.UIFigure);
app.LabelNumericEditField5.HorizontalAlignment = 'right';
app.LabelNumericEditField5.VerticalAlignment = 'top';
app.LabelNumericEditField5.Position = [1226 270 20 15];
app.LabelNumericEditField5.Text = 'A2';
% Create A2
app.A2 = uieditfield(app.UIFigure, 'numeric');
app.A2.Position = [1261 266 100 22];
% Create LabelNumericEditField6
app.LabelNumericEditField6 = uilabel(app.UIFigure);
app.LabelNumericEditField6.HorizontalAlignment = 'right';
app.LabelNumericEditField6.VerticalAlignment = 'top';
app.LabelNumericEditField6.Position = [1226 237 20 15];
app.LabelNumericEditField6.Text = 'A3';
% Create A3
app.A3 = uieditfield(app.UIFigure, 'numeric');
app.A3.Position = [1261 233 100 22];
% Create LabelNumericEditField8
app.LabelNumericEditField8 = uilabel(app.UIFigure);
app.LabelNumericEditField8.HorizontalAlignment = 'right';
app.LabelNumericEditField8.VerticalAlignment = 'top';
app.LabelNumericEditField8.Position = [1208 170 38 15];
app.LabelNumericEditField8.Text = 'Theta2';
% Create theta2
app.theta2 = uieditfield(app.UIFigure, 'numeric');
app.theta2.Position = [1261 166 100 22];
% Create LabelNumericEditField9
app.LabelNumericEditField9 = uilabel(app.UIFigure);
app.LabelNumericEditField9.HorizontalAlignment = 'right';
app.LabelNumericEditField9.VerticalAlignment = 'top';
app.LabelNumericEditField9.Position = [1208 139 38 15];
app.LabelNumericEditField9.Text = 'Theta3';
% Create theta3
app.theta3 = uieditfield(app.UIFigure, 'numeric');
app.theta3.Position = [1261 135 100 22];
% Create LabelNumericEditField10
app.LabelNumericEditField10 = uilabel(app.UIFigure);
app.LabelNumericEditField10.HorizontalAlignment = 'right';
app.LabelNumericEditField10.VerticalAlignment = 'top';
app.LabelNumericEditField10.Position = [1208 206 38 15];
app.LabelNumericEditField10.Text = 'Theta1';
% Create theta1
app.theta1 = uieditfield(app.UIFigure, 'numeric');
app.theta1.Position = [1261 202 100 22];
% Create Label7
app.Label7 = uilabel(app.UIFigure);
app.Label7.VerticalAlignment = 'top';
app.Label7.Position = [1372 205 20 15];
app.Label7.Text = 'rad';
% Create Label8
app.Label8 = uilabel(app.UIFigure);
app.Label8.VerticalAlignment = 'top';
app.Label8.Position = [1372 170 20 15];
app.Label8.Text = 'rad';
% Create Label9
app.Label9 = uilabel(app.UIFigure);
app.Label9.VerticalAlignment = 'top';
app.Label9.Position = [1372 139 20 15];
app.Label9.Text = 'rad';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonButtonPushed, true);
app.Button.HorizontalAlignment = 'left';
app.Button.Position = [1071 101 100 22];
app.Button.Text = '1. 进行时域分析';
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.VerticalAlignment = 'top';
app.Label.FontSize = 14;
app.Label.Position = [1096 346 84 18];
app.Label.Text = '时域分析结果';
% Create Label2
app.Label2 = uilabel(app.UIFigure);
app.Label2.HorizontalAlignment = 'center';
app.Label2.VerticalAlignment = 'top';
app.Label2.FontSize = 14;
app.Label2.Position = [1289 431 56 18];
app.Label2.Text = '参数输入';
% Create LabelNumericEditField11
app.LabelNumericEditField11 = uilabel(app.UIFigure);
app.LabelNumericEditField11.HorizontalAlignment = 'right';
app.LabelNumericEditField11.VerticalAlignment = 'top';
app.LabelNumericEditField11.Position = [1053 321 24 15];
app.LabelNumericEditField11.Text = '峰值';
% Create fengzhi
app.fengzhi = uieditfield(app.UIFigure, 'numeric');
app.fengzhi.Position = [1092 317 100 22];
% Create LabelNumericEditField12
app.LabelNumericEditField12 = uilabel(app.UIFigure);
app.LabelNumericEditField12.HorizontalAlignment = 'right';
app.LabelNumericEditField12.VerticalAlignment = 'top';
app.LabelNumericEditField12.Position = [1041 254 36 15];
app.LabelNumericEditField12.Text = '峰峰值';
% Create fengfengzhi
app.fengfengzhi = uieditfield(app.UIFigure, 'numeric');
app.fengfengzhi.Position = [1092 250 100 22];
% Create LabelNumericEditField13
app.LabelNumericEditField13 = uilabel(app.UIFigure);
app.LabelNumericEditField13.HorizontalAlignment = 'right';
app.LabelNumericEditField13.VerticalAlignment = 'top';
app.LabelNumericEditField13.Position = [1053 187 24 15];
app.LabelNumericEditField13.Text = '均值';
% Create junzhi
app.junzhi = uieditfield(app.UIFigure, 'numeric');
app.junzhi.Position = [1092 183 100 22];
% Create LabelNumericEditField14
app.LabelNumericEditField14 = uilabel(app.UIFigure);
app.LabelNumericEditField14.HorizontalAlignment = 'right';
app.LabelNumericEditField14.VerticalAlignment = 'top';
app.LabelNumericEditField14.Position = [1041 288 36 15];
app.LabelNumericEditField14.Text = '有效值';
% Create youxiaozhi
app.youxiaozhi = uieditfield(app.UIFigure, 'numeric');
app.youxiaozhi.Position = [1092 284 100 22];
% Create LabelNumericEditField15
app.LabelNumericEditField15 = uilabel(app.UIFigure);
app.LabelNumericEditField15.HorizontalAlignment = 'right';
app.LabelNumericEditField15.VerticalAlignment = 'top';
app.LabelNumericEditField15.Position = [1053 222 24 15];
app.LabelNumericEditField15.Text = '频率';
% Create pinlv
app.pinlv = uieditfield(app.UIFigure, 'numeric');
app.pinlv.Position = [1092 218 100 22];
% Create LabelNumericEditField16
app.LabelNumericEditField16 = uilabel(app.UIFigure);
app.LabelNumericEditField16.HorizontalAlignment = 'right';
app.LabelNumericEditField16.VerticalAlignment = 'top';
app.LabelNumericEditField16.Position = [1053 151 24 15];
app.LabelNumericEditField16.Text = '周期';
% Create zhouqi
app.zhouqi = uieditfield(app.UIFigure, 'numeric');
app.zhouqi.Position = [1092 147 100 22];
% Create Button2
app.Button2 = uibutton(app.UIFigure, 'push');
app.Button2.ButtonPushedFcn = createCallbackFcn(app, @Button2ButtonPushed, true);
app.Button2.HorizontalAlignment = 'left';
app.Button2.Position = [1189 64 100 22];
app.Button2.Text = '4. 功率谱分析';
% Create Button3
app.Button3 = uibutton(app.UIFigure, 'push');
app.Button3.ButtonPushedFcn = createCallbackFcn(app, @Button3ButtonPushed, true);
app.Button3.HorizontalAlignment = 'left';
app.Button3.Position = [1189 27 100 22];
app.Button3.Text = '6. 互相关分析';
% Create Button4
app.Button4 = uibutton(app.UIFigure, 'push');
app.Button4.ButtonPushedFcn = createCallbackFcn(app, @Button4ButtonPushed, true);
app.Button4.HorizontalAlignment = 'left';
app.Button4.Position = [1072 64 100 22];
app.Button4.Text = '2. 频域波形图';
% Create Button5
app.Button5 = uibutton(app.UIFigure, 'push');
app.Button5.ButtonPushedFcn = createCallbackFcn(app, @Button5ButtonPushed, true);
app.Button5.HorizontalAlignment = 'left';
app.Button5.Position = [1186 101 107 22];
app.Button5.Text = '3. 振幅-相位谱分析';
% Create Button6
app.Button6 = uibutton(app.UIFigure, 'push');
app.Button6.ButtonPushedFcn = createCallbackFcn(app, @Button6ButtonPushed, true);
app.Button6.Position = [1072 27 100 22];
app.Button6.Text = '5. 实频虚频分析';
% Create ax1
app.ax1 = uiaxes(app.UIFigure);
title(app.ax1, '时域波形分析')
xlabel(app.ax1, 't')
ylabel(app.ax1, 'Signal')
app.ax1.XGrid = 'on';
app.ax1.YGrid = 'on';
app.ax1.GridAlpha = 0.15;
app.ax1.MinorGridAlpha = 0.25;
app.ax1.Box = 'on';
app.ax1.Position = [1 45 344 210];
% Create ax2
app.ax2 = uiaxes(app.UIFigure);
title(app.ax2, '频域波形图')
xlabel(app.ax2, 'w')
ylabel(app.ax2, 'A')
app.ax2.XGrid = 'on';
app.ax2.YGrid = 'on';
app.ax2.GridAlpha = 0.15;
app.ax2.MinorGridAlpha = 0.25;
app.ax2.Box = 'on';
app.ax2.Position = [687 266 344 210];
% Create ax3
app.ax3 = uiaxes(app.UIFigure);
title(app.ax3, '功率谱(dB)')
xlabel(app.ax3, '频率/Hz')
ylabel(app.ax3, '功率谱/dB')
app.ax3.XGrid = 'on';
app.ax3.YGrid = 'on';
app.ax3.GridAlpha = 0.15;
app.ax3.MinorGridAlpha = 0.25;
app.ax3.Box = 'on';
app.ax3.Position = [344 48 344 210];
% Create ax5
app.ax5 = uiaxes(app.UIFigure);
title(app.ax5, '互相关分析')
xlabel(app.ax5, 'X')
ylabel(app.ax5, 'Y')
app.ax5.XGrid = 'on';
app.ax5.YGrid = 'on';
app.ax5.GridAlpha = 0.15;
app.ax5.MinorGridAlpha = 0.25;
app.ax5.Box = 'on';
app.ax5.Position = [344 267 344 210];
% Create ax4
app.ax4 = uiaxes(app.UIFigure);
title(app.ax4, '高斯白噪声')
xlabel(app.ax4, 'X')
ylabel(app.ax4, 'Y')
app.ax4.XGrid = 'on';
app.ax4.YGrid = 'on';
app.ax4.GridAlpha = 0.15;
app.ax4.MinorGridAlpha = 0.25;
app.ax4.Box = 'on';
app.ax4.Position = [676 493 344 210];
% Create ax6
app.ax6 = uiaxes(app.UIFigure);
title(app.ax6, '幅值谱')
xlabel(app.ax6, '频率/Hz')
ylabel(app.ax6, '幅值')
app.ax6.XGrid = 'on';
app.ax6.YGrid = 'on';
app.ax6.GridAlpha = 0.15;
app.ax6.MinorGridAlpha = 0.25;
app.ax6.Box = 'on';
app.ax6.Position = [1 490 344 210];
% Create ax7
app.ax7 = uiaxes(app.UIFigure);
title(app.ax7, '功率谱周期图 N=1024')
xlabel(app.ax7, '频率/Hz')
ylabel(app.ax7, '功率谱')
app.ax7.XGrid = 'on';
app.ax7.YGrid = 'on';
app.ax7.GridAlpha = 0.15;
app.ax7.MinorGridAlpha = 0.25;
app.ax7.Box = 'on';
app.ax7.Position = [687 45 344 210];
% Create ax8
app.ax8 = uiaxes(app.UIFigure);
title(app.ax8, '相位谱')
xlabel(app.ax8, '频率/Hz')
ylabel(app.ax8, '相角')
app.ax8.XGrid = 'on';
app.ax8.YGrid = 'on';
app.ax8.GridAlpha = 0.15;
app.ax8.MinorGridAlpha = 0.25;
app.ax8.Box = 'on';
app.ax8.Position = [1053 490 344 210];
% Create ax9
app.ax9 = uiaxes(app.UIFigure);
title(app.ax9, '实频谱')
xlabel(app.ax9, '频率/Hz')
ylabel(app.ax9, 'CnR')
app.ax9.XGrid = 'on';
app.ax9.YGrid = 'on';
app.ax9.GridAlpha = 0.15;
app.ax9.MinorGridAlpha = 0.25;
app.ax9.Box = 'on';
app.ax9.Position = [344 493 344 210];
% Create ax10
app.ax10 = uiaxes(app.UIFigure);
title(app.ax10, '虚频谱')
xlabel(app.ax10, '频率/Hz')
ylabel(app.ax10, 'CnI')
app.ax10.XGrid = 'on';
app.ax10.YGrid = 'on';
app.ax10.GridAlpha = 0.15;
app.ax10.MinorGridAlpha = 0.25;
app.ax10.Box = 'on';
app.ax10.Position = [1 266 344 210];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = App_uoye
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
标签:app,UIFigure,ui,matlab,Position,习题,时域,Create,MATLAB
From: https://www.cnblogs.com/aksoam/p/17084340.html