limits.h中的"ui64"是什么

本文解释了在C/C++代码中'i64'和'ui64'的用法,它们分别代表有符号64位整型和无符号64位整型,提供了理解这些标识符的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 limits.h中的"ui64"是什么? [问题点数:20分,结帖人:pz0513]
  • pz0513
  • (彭大将军)
  • 等 级:
  • 结帖率:
楼主发表于:2011-03-17 16:24:05
看到limits.h中有
C/C++ code
            
#define LLONG_MAX 9223372036854775807i64 /* maximum signed long long int value */ #define LLONG_MIN (-9223372036854775807i64 - 1) /* minimum signed long long int value */ #define ULLONG_MAX 0xffffffffffffffffui64 /* maximum unsigned long long int value */


上面两个有i64,下面有个ui64,是什么意思啊?

还有这些
C/C++ code
            
#define _I8_MIN (-127i8 - 1) /* minimum signed 8 bit value */ #define _I8_MAX 127i8 /* maximum signed 8 bit value */ #define _UI8_MAX 0xffui8 /* maximum unsigned 8 bit value */ #define _I16_MIN (-32767i16 - 1) /* minimum signed 16 bit value */ #define _I16_MAX 32767i16 /* maximum signed 16 bit value */ #define _UI16_MAX 0xffffui16 /* maximum unsigned 16 bit value */ #define _I32_MIN (-2147483647i32 - 1) /* minimum signed 32 bit value */ #define _I32_MAX 2147483647i32 /* maximum signed 32 bit value */ #define _UI32_MAX 0xffffffffui32 /* maximum unsigned 32 bit value */


数字尾部都有iXX或者uiXX,啥意思啊?
 
 
回复次数: 5<!-- | 浏览次数: 43-->
#1楼 得分:0回复于:2011-03-17 16:26:47
integer = i
unsigned = u
64 = 64位
 
#2楼 得分:0回复于:2011-03-17 16:30:20
正如它注释里面讲的,i指int/integer,u指unsigned(未加u指signed),XX就是有多少bit宽。
至于字母量尾部的后缀,类似1 L,0.1 F这样的LZ还知道?
后缀就是用来明确表明类型的。
 
#3楼 得分:10回复于:2011-03-17 17:15:05
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.

Syntax

integer-constant :

decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
'c-char-sequence'

decimal-constant :

nonzero-digit
decimal-constant digit

octal-constant :

0
octal-constant octal-digit

hexadecimal-constant :

0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit

nonzero-digit : one of

1 2 3 4 5 6 7 8 9

octal-digit : one of

0 1 2 3 4 5 6 7

hexadecimal-digit : one of

0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F

integer-suffix :

unsigned-suffix long-suffixopt
long-suffix unsigned-suffixopt

unsigned-suffix : one of

u U

long-suffix : one of

l L

64-bit integer-suffix :

i64

 
  • xali用户头像
  • xali
  • (夏川)
  • 等 级:
#4楼 得分:5回复于:2011-03-17 18:10:41
上面两个有i64,下面有个ui64,是什么意思啊?
代码后面的注释已经说明:i64是有符号的64位整型,ui64是无符号的64位整型。
 
#5楼 得分:5回复于:2011-03-17 18:54:41
__int8、__int16、__int32和__int64
unsigned __int8、unsigned __int16、unsigned __int32和unsigned __int64
都是微软的VC自己定义的数据类型,因为不同编译环境中int、long和short的值会和具体字长有关,尤其是int的具体长度更是没有定义,为了方便移植,所以定义了固定长度的整型
 
现在出现了关于MATLAB应用的三个问题:矩阵奇异警告、数值输入框居中对齐问题,以及仿真后无图像显示,请你帮我修复并给出完整的代码,源代码如下classdef ChladniLab < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure GridLayout matlab.ui.container.GridLayout LeftPanel matlab.ui.container.Panel MaterialPanel matlab.ui.container.Panel DensityEditFieldLabel matlab.ui.control.Label DensityEditField matlab.ui.control.NumericEditField PoissonRatioEditFieldLabel matlab.ui.control.Label PoissonRatioEditField matlab.ui.control.NumericEditField ElasticEditFieldLabel matlab.ui.control.Label ElasticEditField matlab.ui.control.NumericEditField PlatePanel matlab.ui.container.Panel LengthEditFieldLabel matlab.ui.control.Label LengthEditField matlab.ui.control.NumericEditField ThicknessEditFieldLabel matlab.ui.control.Label ThicknessEditField matlab.ui.control.NumericEditField VibrationPanel matlab.ui.container.Panel FrequencyEditFieldLabel matlab.ui.control.Label FrequencyEditField matlab.ui.control.NumericEditField AmplitudeEditFieldLabel matlab.ui.control.Label AmplitudeEditField matlab.ui.control.NumericEditField SimulationPanel matlab.ui.container.Panel ResolutionEditFieldLabel matlab.ui.control.Label ResolutionEditField matlab.ui.control.NumericEditField SimTimeEditFieldLabel matlab.ui.control.Label SimTimeEditField matlab.ui.control.NumericEditField ControlPanel matlab.ui.container.Panel RunButton matlab.ui.control.Button StopButton matlab.ui.control.Button ResetButton matlab.ui.control.Button RightPanel matlab.ui.container.Panel UIAxes matlab.ui.control.UIAxes ModeDropDownLabel matlab.ui.control.Label ModeDropDown matlab.ui.control.DropDown StatusLabel matlab.ui.control.Label ProgressBar matlab.ui.control.Label end % 应用状态属性 properties (Access = private) Running = false; % 仿真运行状态 StopRequested = false; % 停止请求标志 LastResult; % 存储上次仿真结果 end % 回调方法 methods (Access = private) % 创建UI组件 function createComponents(app) % 创建主窗口 app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 1200 700]; app.UIFigure.Name = 'Chladni Lab - 克拉尼图形仿真平台'; app.UIFigure.Resize = 'on'; % 允许窗口调整大小 % 创建主网格布局 app.GridLayout = uigridlayout(app.UIFigure, [1, 2]); app.GridLayout.ColumnWidth = {'1x', '2x'}; app.GridLayout.RowHeight = {'1x'}; app.GridLayout.ColumnSpacing = 10; app.GridLayout.RowSpacing = 10; app.GridLayout.Padding = [10 10 10 10]; % 创建左侧面板 app.LeftPanel = uipanel(app.GridLayout); app.LeftPanel.Layout.Row = 1; app.LeftPanel.Layout.Column = 1; app.LeftPanel.Title = '控制面板'; app.LeftPanel.FontWeight = 'bold'; app.LeftPanel.Scrollable = 'on'; % 创建左侧垂直网格布局 (替代 uix.VBox) leftGrid = uigridlayout(app.LeftPanel, [5, 1]); leftGrid.RowHeight = {120, 100, 100, 100, 'fit'}; leftGrid.ColumnWidth = {'1x'}; leftGrid.Padding = [10 10 10 10]; leftGrid.RowSpacing = 15; % 创建材料参数面板 app.MaterialPanel = uipanel(leftGrid); app.MaterialPanel.Layout.Row = 1; app.MaterialPanel.Layout.Column = 1; app.MaterialPanel.Title = '材料参数'; app.MaterialPanel.BackgroundColor = [0.96 0.96 0.96]; app.MaterialPanel.FontWeight = 'bold'; materialGrid = uigridlayout(app.MaterialPanel, [3, 2]); materialGrid.ColumnWidth = {'1x', '1.5x'}; materialGrid.RowHeight = repmat({'fit'}, 1, 3); materialGrid.Padding = [10 10 10 10]; % 密度输入 app.DensityEditFieldLabel = uilabel(materialGrid); app.DensityEditFieldLabel.HorizontalAlignment = 'right'; app.DensityEditFieldLabel.Layout.Row = 1; app.DensityEditFieldLabel.Layout.Column = 1; app.DensityEditFieldLabel.Text = '密度 (kg/m³)'; app.DensityEditField = uieditfield(materialGrid, 'numeric'); app.DensityEditField.Limits = [1 20000]; app.DensityEditField.Value = 2700; app.DensityEditField.Layout.Row = 1; app.DensityEditField.Layout.Column = 2; app.DensityEditField.Tag = 'density'; % 泊松比输入 app.PoissonRatioEditFieldLabel = uilabel(materialGrid); app.PoissonRatioEditFieldLabel.HorizontalAlignment = 'right'; app.PoissonRatioEditFieldLabel.Layout.Row = 2; app.PoissonRatioEditFieldLabel.Layout.Column = 1; app.PoissonRatioEditFieldLabel.Text = '泊松比'; app.PoissonRatioEditField = uieditfield(materialGrid, 'numeric'); app.PoissonRatioEditField.Limits = [0.1 0.5]; app.PoissonRatioEditField.Value = 0.33; app.PoissonRatioEditField.Layout.Row = 2; app.PoissonRatioEditField.Layout.Column = 2; app.PoissonRatioEditField.Tag = 'poisson'; % 弹性模量输入 app.ElasticEditFieldLabel = uilabel(materialGrid); app.ElasticEditFieldLabel.HorizontalAlignment = 'right'; app.ElasticEditFieldLabel.Layout.Row = 3; app.ElasticEditFieldLabel.Layout.Column = 1; app.ElasticEditFieldLabel.Text = '弹性模量 (Pa)'; app.ElasticEditField = uieditfield(materialGrid, 'numeric'); app.ElasticEditField.Limits = [1e9 500e9]; app.ElasticEditField.Value = 70e9; app.ElasticEditField.Layout.Row = 3; app.ElasticEditField.Layout.Column = 2; app.ElasticEditField.Tag = 'elastic'; % 创建平板参数面板 app.PlatePanel = uipanel(leftGrid); app.PlatePanel.Layout.Row = 2; app.PlatePanel.Layout.Column = 1; app.PlatePanel.Title = '平板参数'; app.PlatePanel.BackgroundColor = [0.96 0.96 0.96]; app.PlatePanel.FontWeight = 'bold'; plateGrid = uigridlayout(app.PlatePanel, [2, 2]); plateGrid.ColumnWidth = {'1x', '1.5x'}; plateGrid.RowHeight = repmat({'fit'}, 1, 2); plateGrid.Padding = [10 10 10 10]; % 边长输入 app.LengthEditFieldLabel = uilabel(plateGrid); app.LengthEditFieldLabel.HorizontalAlignment = 'right'; app.LengthEditFieldLabel.Layout.Row = 1; app.LengthEditFieldLabel.Layout.Column = 1; app.LengthEditFieldLabel.Text = '边长 (m)'; app.LengthEditField = uieditfield(plateGrid, 'numeric'); app.LengthEditField.Limits = [0.01 1]; app.LengthEditField.Value = 0.15; app.LengthEditField.Layout.Row = 1; app.LengthEditField.Layout.Column = 2; app.LengthEditField.Tag = 'length'; % 厚度输入 app.ThicknessEditFieldLabel = uilabel(plateGrid); app.ThicknessEditFieldLabel.HorizontalAlignment = 'right'; app.ThicknessEditFieldLabel.Layout.Row = 2; app.ThicknessEditFieldLabel.Layout.Column = 1; app.ThicknessEditFieldLabel.Text = '厚度 (m)'; app.ThicknessEditField = uieditfield(plateGrid, 'numeric'); app.ThicknessEditField.Limits = [0.0001 0.1]; app.ThicknessEditField.Value = 0.001; app.ThicknessEditField.Layout.Row = 2; app.ThicknessEditField.Layout.Column = 2; app.ThicknessEditField.Tag = 'thickness'; % 创建振动参数面板 app.VibrationPanel = uipanel(leftGrid); app.VibrationPanel.Layout.Row = 3; app.VibrationPanel.Layout.Column = 1; app.VibrationPanel.Title = '振动参数'; app.VibrationPanel.BackgroundColor = [0.96 0.96 0.96]; app.VibrationPanel.FontWeight = 'bold'; vibrationGrid = uigridlayout(app.VibrationPanel, [2, 2]); vibrationGrid.ColumnWidth = {'1x', '1.5x'}; vibrationGrid.RowHeight = repmat({'fit'}, 1, 2); vibrationGrid.Padding = [10 10 10 10]; % 频率输入 app.FrequencyEditFieldLabel = uilabel(vibrationGrid); app.FrequencyEditFieldLabel.HorizontalAlignment = 'right'; app.FrequencyEditFieldLabel.Layout.Row = 1; app.FrequencyEditFieldLabel.Layout.Column = 1; app.FrequencyEditFieldLabel.Text = '频率 (Hz)'; app.FrequencyEditField = uieditfield(vibrationGrid, 'numeric'); app.FrequencyEditField.Limits = [1 5000]; app.FrequencyEditField.Value = 650; app.FrequencyEditField.Layout.Row = 1; app.FrequencyEditField.Layout.Column = 2; app.FrequencyEditField.Tag = 'frequency'; % 振幅输入 app.AmplitudeEditFieldLabel = uilabel(vibrationGrid); app.AmplitudeEditFieldLabel.HorizontalAlignment = 'right'; app.AmplitudeEditFieldLabel.Layout.Row = 2; app.AmplitudeEditFieldLabel.Layout.Column = 1; app.AmplitudeEditFieldLabel.Text = '振幅 (m)'; app.AmplitudeEditField = uieditfield(vibrationGrid, 'numeric'); app.AmplitudeEditField.Limits = [0.001 0.1]; app.AmplitudeEditField.Value = 0.01; app.AmplitudeEditField.Layout.Row = 2; app.AmplitudeEditField.Layout.Column = 2; app.AmplitudeEditField.Tag = 'amplitude'; % 创建仿真参数面板 app.SimulationPanel = uipanel(leftGrid); app.SimulationPanel.Layout.Row = 4; app.SimulationPanel.Layout.Column = 1; app.SimulationPanel.Title = '仿真参数'; app.SimulationPanel.BackgroundColor = [0.96 0.96 0.96]; app.SimulationPanel.FontWeight = 'bold'; simGrid = uigridlayout(app.SimulationPanel, [2, 2]); simGrid.ColumnWidth = {'1x', '1.5x'}; simGrid.RowHeight = repmat({'fit'}, 1, 2); simGrid.Padding = [10 10 10 10]; % 分辨率输入 app.ResolutionEditFieldLabel = uilabel(simGrid); app.ResolutionEditFieldLabel.HorizontalAlignment = 'right'; app.ResolutionEditFieldLabel.Layout.Row = 1; app.ResolutionEditFieldLabel.Layout.Column = 1; app.ResolutionEditFieldLabel.Text = '网格分辨率'; app.ResolutionEditField = uieditfield(simGrid, 'numeric'); app.ResolutionEditField.Limits = [4 32]; app.ResolutionEditField.RoundFractionalValues = 'on'; app.ResolutionEditField.Value = 16; app.ResolutionEditField.Layout.Row = 1; app.ResolutionEditField.Layout.Column = 2; app.ResolutionEditField.Tag = 'resolution'; % 仿真时间输入 app.SimTimeEditFieldLabel = uilabel(simGrid); app.SimTimeEditFieldLabel.HorizontalAlignment = 'right'; app.SimTimeEditFieldLabel.Layout.Row = 2; app.SimTimeEditFieldLabel.Layout.Column = 1; app.SimTimeEditFieldLabel.Text = '仿真时间 (s)'; app.SimTimeEditField = uieditfield(simGrid, 'numeric'); app.SimTimeEditField.Limits = [0.01 1]; app.SimTimeEditField.Value = 0.03; app.SimTimeEditField.Layout.Row = 2; app.SimTimeEditField.Layout.Column = 2; app.SimTimeEditField.Tag = 'simtime'; % 创建控制按钮面板 app.ControlPanel = uipanel(leftGrid); app.ControlPanel.Layout.Row = 5; app.ControlPanel.Layout.Column = 1; app.ControlPanel.BackgroundColor = [0.96 0.96 0.96]; app.ControlPanel.FontWeight = 'bold'; controlGrid = uigridlayout(app.ControlPanel, [1, 3]); controlGrid.ColumnWidth = {'1x', '1x', '1x'}; controlGrid.RowHeight = {'fit'}; controlGrid.Padding = [10 5 10 10]; % 创建控制按钮 app.RunButton = uibutton(controlGrid, 'push'); app.RunButton.ButtonPushedFcn = createCallbackFcn(app, @RunButtonPushed, true); app.RunButton.Layout.Row = 1; app.RunButton.Layout.Column = 1; app.RunButton.Text = '开始仿真'; app.RunButton.BackgroundColor = [0.47 0.67 0.19]; app.RunButton.FontWeight = 'bold'; app.StopButton = uibutton(controlGrid, 'push'); app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true); app.StopButton.Layout.Row = 1; app.StopButton.Layout.Column = 2; app.StopButton.Text = '停止仿真'; app.RunButton.BackgroundColor = [0.47 0.67 0.19]; app.StopButton.BackgroundColor = [0.85 0.33 0.10]; app.StopButton.FontWeight = 'bold'; app.ResetButton = uibutton(controlGrid, 'push'); app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true); app.ResetButton.Layout.Row = 1; app.ResetButton.Layout.Column = 3; app.ResetButton.Text = '重置参数'; app.ResetButton.FontWeight = 'bold'; % 创建右侧面板 app.RightPanel = uipanel(app.GridLayout); app.RightPanel.Layout.Row = 1; app.RightPanel.Layout.Column = 2; app.RightPanel.Title = '克拉尼图形可视化'; app.RightPanel.FontWeight = 'bold'; % 创建坐标轴 app.UIAxes = uiaxes(app.RightPanel); app.UIAxes.Position = [50 100 700 550]; title(app.UIAxes, '克拉尼图形') xlabel(app.UIAxes, 'X (m)') ylabel(app.UIAxes, 'Y (m)') colormap(app.UIAxes, 'jet'); colorbar(app.UIAxes); app.UIAxes.FontSize = 12; app.UIAxes.TitleFontSizeMultiplier = 1.2; % 创建显示模式下拉菜单 app.ModeDropDownLabel = uilabel(app.RightPanel); app.ModeDropDownLabel.HorizontalAlignment = 'right'; app.ModeDropDownLabel.Position = [50 70 100 22]; app.ModeDropDownLabel.Text = '显示模式:'; app.ModeDropDownLabel.FontWeight = 'bold'; app.ModeDropDown = uidropdown(app.RightPanel); app.ModeDropDown.Items = {'动态波动', '振幅分布', '节点线图'}; app.ModeDropDown.ValueChangedFcn = createCallbackFcn(app, @ModeDropDownValueChanged, true); app.ModeDropDown.Position = [160 70 150 22]; app.ModeDropDown.Value = '振幅分布'; app.ModeDropDown.FontWeight = 'bold'; % 状态标签 app.StatusLabel = uilabel(app.RightPanel); app.StatusLabel.Position = [50 40 300 22]; app.StatusLabel.Text = '就绪'; app.StatusLabel.FontSize = 14; app.StatusLabel.FontWeight = 'bold'; app.StatusLabel.FontColor = [0 0.5 0]; % 进度条 app.ProgressBar = uilabel(app.RightPanel); app.ProgressBar.Position = [400 40 300 22]; app.ProgressBar.Text = ''; app.ProgressBar.FontSize = 12; app.ProgressBar.FontWeight = 'bold'; % 显示主窗口 app.UIFigure.Visible = 'on'; end % 运行按钮回调 function RunButtonPushed(app, ~) if app.Running return; end % 验证参数 if ~app.validateParameters() return; end app.Running = true; app.StopRequested = false; app.StatusLabel.Text = '仿真运行中...'; app.StatusLabel.FontColor = [0 0 1]; app.ProgressBar.Text = '初始化... 0%'; drawnow; try % 获取用户输入参数 L = app.LengthEditField.Value; h = app.ThicknessEditField.Value; mu = app.PoissonRatioEditField.Value; rho = app.DensityEditField.Value; E = app.ElasticEditField.Value; Amp = app.AmplitudeEditField.Value; Freq = app.FrequencyEditField.Value; N = app.ResolutionEditField.Value; t_end = app.SimTimeEditField.Value; % 计算弯曲刚度 D = E*h^3/(12*(1-mu^2)); % 构建网格 dt = 1e-6; % 时间步长 dx = L/N; % 网格大小 x = dx*(0:N); [X,Y] = meshgrid(x,x); % 初始化位移 [Sq,Bq] = app.Equation_Sq0(N,mu,-Amp); % 调用类方法 U0 = Sq\Bq; U1 = U0; U2 = U1; % 初始化存储 U_Out1 = zeros(N+1,N+1); U_Out2 = zeros(N+5,N+5); U_Save = zeros((N+1)^2, 200); t_Save = 1; % 准备绘图 cla(app.UIAxes); hold(app.UIAxes, 'on'); axis(app.UIAxes, 'equal'); % 根据显示模式选择绘图方式 displayMode = app.ModeDropDown.Value; % 计算动态方程 t_start = 0; jishu = 1; totalSteps = round((t_end-t_start)/dt); progressStep = round(totalSteps/10); for t_k = t_start:dt:t_end if app.StopRequested break; end % 更新进度 if mod(jishu, progressStep) == 0 progress = round(jishu/totalSteps * 100); app.ProgressBar.Text = sprintf('计算中... %d%%', progress); drawnow; end % 0点处的运动位置 u0 = Amp*cos(2*pi*Freq*t_k+pi); L_Sq = N+5; % 实际计算时网格的尺寸 if jishu == 1 % 第一步计算 [Sq,Bq] = app.Equation_Sq0(N,mu,u0); for k = 1:L_Sq^2 [r_k,c_k] = ind2sub([L_Sq,L_Sq],k); if (r_k>=3 && r_k<=L_Sq-2) && (c_k>=3 && c_k<=L_Sq-2) Sq(k,k) = 20+rho*h*dx^4/D/dt^2; Sq(k,[k+1,k-1,k+L_Sq,k-L_Sq]) = -8; Sq(k,[k+L_Sq+1,k+L_Sq-1,k-L_Sq+1,k-L_Sq-1]) = 2; Sq(k,[k+2,k-2,k-2*L_Sq,k+2*L_Sq]) = 1; Fd = -100*sign(U2(k)-U1(k))*(U2(k)-U1(k))^2/dt^2; Bq(k) = dx^4/D*(rho*h/dt^2*(2*U2(k)-U1(k))+Fd); end end Indx_Center = sub2ind([L_Sq,L_Sq],3,3); Sq(Indx_Center,:) = 0; Sq(Indx_Center,Indx_Center) = 1; Bq(Indx_Center) = u0; else for k = 1:L_Sq^2 [r_k,c_k] = ind2sub([L_Sq,L_Sq],k); if (r_k>=3 && r_k<=L_Sq-2) && (c_k>=3 && c_k<=L_Sq-2) Fd = -100*sign(U2(k)-U1(k))*(U2(k)-U1(k))^2/dt^2; Bq(k) = dx^4/D*(rho*h/dt^2*(2*U2(k)-U1(k))+Fd); end end Bq(1+2+2*L_Sq) = u0; end U3 = Sq\Bq; U1 = U2; U2 = U3; % 储存,用作输出 U_Out2(:) = U3(:); U_Out = U_Out2(3:end-2,3:end-2); % 每100步更新一次图形 if mod(jishu,100) == 1 switch displayMode case '动态波动' surf(app.UIAxes, X, Y, U_Out); shading(app.UIAxes, 'interp'); zlim(app.UIAxes, [-0.2 0.2]); title(app.UIAxes, '平板动态波动'); view(app.UIAxes, 3); case '振幅分布' pcolor(app.UIAxes, X, Y, U_Out); shading(app.UIAxes, 'interp'); title(app.UIAxes, '振幅分布'); colorbar(app.UIAxes); view(app.UIAxes, 2); case '节点线图' contour(app.UIAxes, X, Y, U_Out, 10, 'LineWidth', 1.5); title(app.UIAxes, '节点线图'); colorbar(app.UIAxes); view(app.UIAxes, 2); end drawnow; end jishu = jishu+1; % 记最后200个数据储存 if jishu+50*200 >= totalSteps if mod(jishu,50) == 1 U_Save(:,t_Save) = U_Out(:); t_Save = t_Save+1; end end end % 保存结果用于后续分析 app.LastResult.X = X; app.LastResult.Y = Y; app.LastResult.U_Save = U_Save; app.LastResult.dx = dx; app.LastResult.N = N; % 计算振幅分布 U_Out_A = U_Out; U_Out_A(:) = max(U_Save,[],2)-min(U_Save,[],2); U_Out_A2 = [fliplr(U_Out_A(:,2:end)), U_Out_A]; U_Out_A3 = [flipud(U_Out_A2(2:end,:)); U_Out_A2]; app.LastResult.U_Amplitude = U_Out_A3; app.StatusLabel.Text = '仿真完成!'; app.StatusLabel.FontColor = [0 0.5 0]; app.ProgressBar.Text = ''; catch ME app.StatusLabel.Text = ['错误: ' ME.message]; app.StatusLabel.FontColor = [1 0 0]; app.ProgressBar.Text = ''; disp(ME.getReport()); end app.Running = false; end % 参数验证 function valid = validateParameters(app) valid = true; % 检查网格分辨率是否为偶数 if mod(app.ResolutionEditField.Value, 2) ~= 0 app.StatusLabel.Text = '错误: 网格分辨率必须是偶数'; app.StatusLabel.FontColor = [1 0 0]; valid = false; return; end % 检查时间步长是否合理 if app.SimTimeEditField.Value < 0.01 app.StatusLabel.Text = '错误: 仿真时间必须至少0.01秒'; app.StatusLabel.FontColor = [1 0 0]; valid = false; return; end % 检查频率是否在合理范围内 if app.FrequencyEditField.Value < 1 || app.FrequencyEditField.Value > 5000 app.StatusLabel.Text = '错误: 频率必须在1-5000Hz范围内'; app.StatusLabel.FontColor = [1 0 0]; valid = false; return; end end % 停止按钮回调 function StopButtonPushed(app, ~) app.StopRequested = true; app.StatusLabel.Text = '仿真已停止'; app.StatusLabel.FontColor = [0.5 0.5 0.5]; app.ProgressBar.Text = ''; end % 重置按钮回调 function ResetButtonPushed(app, ~) app.LengthEditField.Value = 0.15; app.ThicknessEditField.Value = 0.001; app.PoissonRatioEditField.Value = 0.33; app.DensityEditField.Value = 2700; app.ElasticEditField.Value = 70e9; app.FrequencyEditField.Value = 650; app.AmplitudeEditField.Value = 0.01; app.ResolutionEditField.Value = 16; app.SimTimeEditField.Value = 0.03; cla(app.UIAxes); app.StatusLabel.Text = '参数已重置'; app.StatusLabel.FontColor = [0 0.5 0]; app.ProgressBar.Text = ''; end % 显示模式改变回调 function ModeDropDownValueChanged(app, ~) if ~isempty(app.LastResult) && ~isempty(app.LastResult.U_Amplitude) displayMode = app.ModeDropDown.Value; cla(app.UIAxes); hold(app.UIAxes, 'on'); axis(app.UIAxes, 'equal'); x = app.LastResult.dx*(-app.LastResult.N:app.LastResult.N); [X3,Y3] = meshgrid(x,x); switch displayMode case '动态波动' title(app.UIAxes, '动态波动 - 请重新运行仿真'); app.StatusLabel.Text = '动态波动模式需要实时仿真'; app.StatusLabel.FontColor = [0 0 1]; case '振幅分布' surf(app.UIAxes, X3, Y3, app.LastResult.U_Amplitude); shading(app.UIAxes, 'interp'); title(app.UIAxes, '振幅分布'); colorbar(app.UIAxes); view(app.UIAxes, 2); app.StatusLabel.Text = '显示振幅分布'; app.StatusLabel.FontColor = [0 0.5 0]; case '节点线图' contour(app.UIAxes, X3, Y3, app.LastResult.U_Amplitude, 10, 'LineWidth', 1.5); title(app.UIAxes, '节点线图'); colorbar(app.UIAxes); view(app.UIAxes, 2); app.StatusLabel.Text = '显示节点线图'; app.StatusLabel.FontColor = [0 0.5 0]; end end end % 辅助函数Equation_Sq0(作为类方法) function [Sq,Bq] = Equation_Sq0(app, N, mu, u0) % 外拓展两圈后平板网格的索引 L_Sq = N+5; % 定义边界点类型 Point_Corner0 = [L_Sq,L_Sq; L_Sq-1,L_Sq; L_Sq,L_Sq-1]; Point_CornerC = [L_Sq-1,L_Sq-2; L_Sq-2,L_Sq-1]; Point_Out1 = [(L_Sq-1)*ones(L_Sq-5,1),(3:L_Sq-3)'; (3:L_Sq-3)',(L_Sq-1)*ones(L_Sq-5,1)]; Point_Corner = [L_Sq-1,L_Sq-1]; Point_Out2 = [L_Sq*ones(L_Sq-4,1),(3:L_Sq-2)'; (3:L_Sq-2)',L_Sq*ones(L_Sq-4,1)]; Point_Mirror1 = [2*ones(L_Sq-2,1),(3:L_Sq)'; (3:L_Sq)',2*ones(L_Sq-2,1)]; Point_Mirror2 = [1*ones(L_Sq-2,1),(3:L_Sq)'; (3:L_Sq)',1*ones(L_Sq-2,1)]; Point_MirrorC = [1,1;1,2;2,1;2,2]; % 初始化矩阵 Sq = zeros(L_Sq^2); Bq = zeros(L_Sq^2,1); for k = 1:L_Sq^2 [r_k,c_k] = ind2sub([L_Sq,L_Sq],k); % 四周边界点处理 if app.IsRowInRowList(Point_Corner0, [r_k,c_k]) Sq(k,k) = 1; Bq(k) = 0; % 自由角垂直外边界 elseif app.IsRowInRowList(Point_CornerC, [r_k,c_k]) if r_k == L_Sq-1 Sq(k,k-2:k) = [1,-2,1]; elseif c_k == L_Sq-1 Sq(k,[k-2*L_Sq,k-L_Sq,k]) = [1,-2,1]; end Bq(k) = 0; % 第一层边界点 elseif app.IsRowInRowList(Point_Out1, [r_k,c_k]) if r_k == 2 Sq(k,[k+1-L_Sq,k+1,k+1+L_Sq]) = [-mu,2+2*mu,-mu]; Sq(k,k) = -1; Sq(k,k+2) = -1; elseif r_k == L_Sq-1 Sq(k,[k-1-L_Sq,k-1,k-1+L_Sq]) = [-mu,2+2*mu,-mu]; Sq(k,k) = -1; Sq(k,k-2) = -1; elseif c_k == 2 Sq(k,[k,k+L_Sq,k+2*L_Sq]) = [-1,2+2*mu,-1]; Sq(k,k+L_Sq-1) = -mu; Sq(k,k+L_Sq+1) = -mu; elseif c_k == L_Sq-1 Sq(k,[k,k-L_Sq,k-2*L_Sq]) = [-1,2+2*mu,-1]; Sq(k,k-L_Sq-1) = -mu; Sq(k,k-L_Sq+1) = -mu; end Bq(k) = 0; % 自由角对角线外边界 elseif app.IsRowInRowList(Point_Corner, [r_k,c_k]) if r_k == L_Sq-1 && c_k == L_Sq-1 Sq(k,[k,k-2*L_Sq-2]) = [1,1]; Sq(k,[k-2,k-2*L_Sq]) = [-1,-1]; end Bq(k) = 0; % 第二层边界点 elseif app.IsRowInRowList(Point_Out2, [r_k,c_k]) if r_k == 1 Sq(k,k) = 1; Sq(k,[k+1-L_Sq,k+1,k+1+L_Sq]) = [2-mu,2*mu-6,2-mu]; Sq(k,[k+3-L_Sq,k+3,k+3+L_Sq]) = [mu-2,-2*mu+6,mu-2]; Sq(k,k+4) = -1; end Bq(k) = 0; % 正常平板上的点 elseif (r_k>=3 && r_k<=L_Sq-2) && (c_k>=3 && c_k<=L_Sq-2) Sq(k,k) = 20; Sq(k,[k+1,k-1,k+L_Sq,k-L_Sq]) = -8; Sq(k,[k+L_Sq+1,k+L_Sq-1,k-L_Sq+1,k-L_Sq-1]) = 2; Sq(k,[k+2,k-2,k-2*L_Sq,k+2*L_Sq]) = 1; Bq(k) = 0; % 对称边界处理 elseif app.IsRowInRowList(Point_Mirror1, [r_k,c_k]) if r_k == 2 Sq(k,k) = 1; Sq(k,k+2) = -1; elseif c_k == 2 Sq(k,k) = 1; Sq(k,k+2*L_Sq) = -1; end Bq(k) = 0; elseif app.IsRowInRowList(Point_Mirror2, [r_k,c_k]) if r_k == 1 Sq(k,k) = 1; Sq(k,k+4) = -1; elseif c_k == 1 Sq(k,k) = 1; Sq(k,k+4*L_Sq) = -1; end Bq(k) = 0; elseif app.IsRowInRowList(Point_MirrorC, [r_k,c_k]) if r_k == 1 && c_k == 1 Sq(k,k) = 1; Sq(k,k+4+4*L_Sq) = -1; end Bq(k) = 0; end end % 中心点约束 Indx_Center = sub2ind([L_Sq,L_Sq],3,3); Sq(Indx_Center,:) = 0; Sq(Indx_Center,Indx_Center) = 1; Bq(Indx_Center) = u0; end % 辅助函数IsRowInRowList(作为类方法) function TF = IsRowInRowList(~, List, Point) TF1 = (List(:,1) == Point(1)); TF = any(List(TF1,2) == Point(2)); end end % 应用初始化和启动 methods (Access = public) % 构造函数 function app = ChladniLab % 创建UI组件 createComponents(app) % 注册应用 registerApp(app, app.UIFigure) if nargout == 0 clear app end end % 运行代码 function run(app) app.UIFigure.Visible = 'on'; end end % 组件销毁 methods (Access = public) function delete(app) delete(app.UIFigure) end end end
最新发布
07-27
classdef IIRFilterDesigner < matlab.apps.AppBase % 公共属性 - UI组件 properties (Access = public) UIFigure matlab.ui.Figure FilterImplLabel matlab.ui.control.Label FilterImplDropDown matlab.ui.control.DropDown FpassLabel matlab.ui.control.Label FpassEditField matlab.ui.control.NumericEditField FstopLabel matlab.ui.control.Label FstopEditField matlab.ui.control.NumericEditField OrderLabel matlab.ui.control.Label OrderEditField matlab.ui.control.NumericEditField DesignButton matlab.ui.control.Button AxesMagPhase matlab.ui.control.UIAxes AxesTimeDomain matlab.ui.control.UIAxes AxesFreqDomain matlab.ui.control.UIAxes SignalFreqLabel matlab.ui.control.Label SignalFreqEdit matlab.ui.control.NumericEditField GenerateButton matlab.ui.control.Button ModulateButton matlab.ui.control.Button FilterButton matlab.ui.control.Button DemodulateButton matlab.ui.control.Button CarrierFreqLabel matlab.ui.control.Label CarrierFreqEdit matlab.ui.control.NumericEditField StatusLabel matlab.ui.control.Label end % 私有属性 properties (Access = private) Fs = 1000; % 采样频率 (Hz) b = [1]; % 滤波器分子系数 a = [1]; % 滤波器分母系数 original_signal; % 原始基带信号 modulated_signal; % 调制后信号 filtered_signal; % 滤波后信号 demodulated_signal; % 解调后信号 t; % 时间向量 end % 回调方法 methods (Access = private) % 设计滤波器回调函数 function designFilter(app, ~) % 获取用户输入 impl_type = app.FilterImplDropDown.Value; Fpass = app.FpassEditField.Value; Fstop = app.FstopEditField.Value; order = app.OrderEditField.Value; % 归一化频率 Wpass = Fpass/(app.Fs/2); Wstop = Fstop/(app.Fs/2); try % 根据选择的实现类型设计低通滤波器 if strcmp(impl_type, 'IIR') % IIR低通滤波器设计(巴特沃斯) [app.b, app.a] = butter(order, Wpass, 'low'); app.StatusLabel.Text = "IIR滤波器设计完成 (阶数: " + order + ")"; else % FIR低通滤波器设计(汉明窗) app.b = fir1(order, Wpass, 'low', hamming(order+1)); app.a = 1; % FIR滤波器分母为1 app.StatusLabel.Text = "FIR滤波器设计完成 (阶数: " + order + ")"; end % 绘制频率响应 app.plotFrequencyResponse(); catch ME uialert(app.UIFigure, ME.message, '设计错误'); end end % 绘制频率响应 function plotFrequencyResponse(app) [h, w] = freqz(app.b, app.a, 1024, app.Fs); mag = 20*log10(abs(h)); phase = unwrap(angle(h))*180/pi; cla(app.AxesMagPhase); yyaxis(app.AxesMagPhase, 'left'); plot(app.AxesMagPhase, w, mag, 'b', 'LineWidth', 1.5); ylabel(app.AxesMagPhase, '幅度 (dB)'); title(app.AxesMagPhase, '滤波器频率响应'); grid(app.AxesMagPhase, 'on'); % 标记通带和阻带 hold(app.AxesMagPhase, 'on'); xline(app.AxesMagPhase, app.FpassEditField.Value, '--g', 'Passband'); xline(app.AxesMagPhase, app.FstopEditField.Value, '--r', 'Stopband'); hold(app.AxesMagPhase, 'off'); yyaxis(app.AxesMagPhase, 'right'); plot(app.AxesMagPhase, w, phase, 'r', 'LineWidth', 1.5); ylabel(app.AxesMagPhase, '相位 (度)'); legend(app.AxesMagPhase, {'幅频响应', '通带边界', '阻带边界', '相频响应'}, 'Location', 'best'); end % 生成基带信号 function generateSignal(app, ~) % 获取信号参数 signalFreq = app.SignalFreqEdit.Value; % 生成多频信号(基波+谐波) app.t = 0:1/app.Fs:1; f1 = signalFreq; % 基波频率 f2 = 3*signalFreq; % 三次谐波 f3 = 5*signalFreq; % 五次谐波 app.original_signal = 0.7*sin(2*pi*f1*app.t) + ... 0.5*sin(2*pi*f2*app.t) + ... 0.3*sin(2*pi*f3*app.t); % 绘制基带信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.original_signal, 'b', 'LineWidth', 1.5); title(app.AxesTimeDomain, '基带信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.original_signal, '基带信号频谱'); app.StatusLabel.Text = "基带信号已生成 (主频: " + signalFreq + " Hz)"; end % AM调制 function modulateSignal(app, ~) if isempty(app.original_signal) uialert(app.UIFigure, '请先生成基带信号', '操作错误'); return; end % 获取载波频率 Fc = app.CarrierFreqEdit.Value; % AM调制: s(t) = [1 + m(t)] * cos(2πFc t) carrier = cos(2*pi*Fc*app.t); app.modulated_signal = (1 + 0.5*app.original_signal) .* carrier; % 绘制调制信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.modulated_signal, 'm', 'LineWidth', 1.5); title(app.AxesTimeDomain, '调制信号 (AM)'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.modulated_signal, '调制信号频谱'); app.StatusLabel.Text = "AM调制完成 (载波: " + Fc + " Hz)"; end % 信号滤波 function filterSignal(app, ~) if isempty(app.modulated_signal) uialert(app.UIFigure, '请先调制信号', '操作错误'); return; end % 应用滤波器 app.filtered_signal = filtfilt(app.b, app.a, app.modulated_signal); % 绘制滤波后信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.filtered_signal, 'r', 'LineWidth', 1.5); title(app.AxesTimeDomain, '滤波后信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.filtered_signal, '滤波后信号频谱'); app.StatusLabel.Text = "信号滤波完成"; end % AM解调 function demodulateSignal(app, ~) if isempty(app.filtered_signal) uialert(app.UIFigure, '请先滤波信号', '操作错误'); return; end % 包络检波解调 analytic_signal = hilbert(app.filtered_signal); envelope = abs(analytic_signal); % 移除直流分量并缩放 app.demodulated_signal = 2*(envelope - mean(envelope)); % 绘制解调信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.demodulated_signal, 'g', 'LineWidth', 1.5); hold(app.AxesTimeDomain, 'on'); plot(app.AxesTimeDomain, app.t, app.original_signal, 'b--', 'LineWidth', 1.2); title(app.AxesTimeDomain, '解调信号 vs 原始信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); legend(app.AxesTimeDomain, {'解调信号', '原始信号'}, 'Location', 'best'); grid(app.AxesTimeDomain, 'on'); hold(app.AxesTimeDomain, 'off'); % 绘制频谱 app.plotFrequencySpectrum(app.demodulated_signal, '解调信号频谱'); app.StatusLabel.Text = "AM解调完成"; end % 绘制频谱图 function plotFrequencySpectrum(app, signal, titleStr) N = length(signal); f = (-N/2:N/2-1)*(app.Fs/N); Y = fftshift(fft(signal)); cla(app.AxesFreqDomain); plot(app.AxesFreqDomain, f, abs(Y)/max(abs(Y)), 'LineWidth', 1.5); title(app.AxesFreqDomain, titleStr); xlabel(app.AxesFreqDomain, '频率 (Hz)'); ylabel(app.AxesFreqDomain, '归一化幅度'); grid(app.AxesFreqDomain, 'on'); xlim(app.AxesFreqDomain, [-app.Fs/4, app.Fs/4]); end end % 应用初始化 methods (Access = private) % 创建UI组件 function createComponents(app) % 创建主窗口 app.UIFigure = uifigure('Name', 'IIR/FIR滤波器调制解调系统', ... 'Position', [100 100 1000 700]); % 滤波器参数区 uipanel(app.UIFigure, 'Title', '滤波器参数', ... 'Position', [20 520 300 160]); app.FilterImplLabel = uilabel(app.UIFigure, ... 'Text', '滤波器类型:', ... 'Position', [40 650 80 22]); app.FilterImplDropDown = uidropdown(app.UIFigure, ... 'Items', {'IIR', 'FIR'}, ... 'Position', [130 650 100 22], ... 'Value', 'IIR'); app.FpassLabel = uilabel(app.UIFigure, ... 'Text', '通带截止(Hz):', ... 'Position', [40 620 90 22]); app.FpassEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 620 100 22], ... 'Value', 50, ... 'Limits', [1 499]); app.FstopLabel = uilabel(app.UIFigure, ... 'Text', '阻带截止(Hz):', ... 'Position', [40 590 90 22]); app.FstopEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 590 100 22], ... 'Value', 70, ... 'Limits', [1 499]); app.OrderLabel = uilabel(app.UIFigure, ... 'Text', '滤波器阶数:', ... 'Position', [40 560 80 22]); app.OrderEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 560 100 22], ... 'Value', 6, ... 'Limits', [1 50]); app.DesignButton = uibutton(app.UIFigure, 'push', ... 'Text', '设计滤波器', ... 'Position', [240 560 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @designFilter, true)); % 信号参数区 uipanel(app.UIFigure, 'Title', '信号参数', ... 'Position', [20 400 300 100]); app.SignalFreqLabel = uilabel(app.UIFigure, ... 'Text', '基带频率(Hz):', ... 'Position', [40 470 90 22]); app.SignalFreqEdit = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 470 100 22], ... 'Value', 10, ... 'Limits', [1 50]); app.CarrierFreqLabel = uilabel(app.UIFigure, ... 'Text', '载波频率(Hz):', ... 'Position', [40 440 90 22]); app.CarrierFreqEdit = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 440 100 22], ... 'Value', 100, ... 'Limits', [50 200]); % 操作按钮区 app.GenerateButton = uibutton(app.UIFigure, 'push', ... 'Text', '生成基带信号', ... 'Position', [40 380 120 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @generateSignal, true)); app.ModulateButton = uibutton(app.UIFigure, 'push', ... 'Text', 'AM调制', ... 'Position', [170 380 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @modulateSignal, true)); app.FilterButton = uibutton(app.UIFigure, 'push', ... 'Text', '滤波', ... 'Position', [40 340 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @filterSignal, true)); app.DemodulateButton = uibutton(app.UIFigure, 'push', ... 'Text', '解调', ... 'Position', [130 340 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @demodulateSignal, true)); % 状态显示 app.StatusLabel = uilabel(app.UIFigure, ... 'Text', '准备就绪', ... 'Position', [40 310 250 22], ... 'FontColor', [0 0.5 0]); % 坐标轴区域 app.AxesMagPhase = uiaxes(app.UIFigure, ... 'Position', [340 520 600 160], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesMagPhase, '滤波器频率响应'); app.AxesTimeDomain = uiaxes(app.UIFigure, ... 'Position', [340 300 600 200], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesTimeDomain, '时域信号'); app.AxesFreqDomain = uiaxes(app.UIFigure, ... 'Position', [340 50 600 200], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesFreqDomain, '频域信号'); % 初始化滤波器 app.designFilter(); end end % 应用启动和运行 methods (Access = public) % 构造函数 function app = IIRFilterDesigner % 创建并配置组件 createComponents(app) % 注册应用 registerApp(app, app.UIFigure) % 运行应用 if nargout == 0 clear app end end % 代码执行 function delete(app) delete(app.UIFigure) end end end 将代码基带信号设为正弦信号
06-27
1 前沿 2 实验部分 3 实验结果 4总结与展望 根据代码帮我按上面格式写一篇报告代码如下classdef IIRFilterDesigner < matlab.apps.AppBase % 公共属性 - UI组件 properties (Access = public) UIFigure matlab.ui.Figure FilterImplLabel matlab.ui.control.Label FilterImplDropDown matlab.ui.control.DropDown FpassLabel matlab.ui.control.Label FpassEditField matlab.ui.control.NumericEditField FstopLabel matlab.ui.control.Label FstopEditField matlab.ui.control.NumericEditField OrderLabel matlab.ui.control.Label OrderEditField matlab.ui.control.NumericEditField DesignButton matlab.ui.control.Button AxesMagPhase matlab.ui.control.UIAxes AxesTimeDomain matlab.ui.control.UIAxes AxesFreqDomain matlab.ui.control.UIAxes SignalFreqLabel matlab.ui.control.Label SignalFreqEdit matlab.ui.control.NumericEditField GenerateButton matlab.ui.control.Button ModulateButton matlab.ui.control.Button FilterButton matlab.ui.control.Button DemodulateButton matlab.ui.control.Button CarrierFreqLabel matlab.ui.control.Label CarrierFreqEdit matlab.ui.control.NumericEditField StatusLabel matlab.ui.control.Label end % 私有属性 properties (Access = private) Fs = 1000; % 采样频率 (Hz) b = [1]; % 滤波器分子系数 a = [1]; % 滤波器分母系数 original_signal; % 原始基带信号 modulated_signal; % 调制后信号 filtered_signal; % 滤波后信号 demodulated_signal; % 解调后信号 t; % 时间向量 end % 回调方法 methods (Access = private) % 设计滤波器回调函数 function designFilter(app, ~) % 获取用户输入 impl_type = app.FilterImplDropDown.Value; Fpass = app.FpassEditField.Value; Fstop = app.FstopEditField.Value; order = app.OrderEditField.Value; % 归一化频率 Wpass = Fpass/(app.Fs/2); Wstop = Fstop/(app.Fs/2); try % 根据选择的实现类型设计低通滤波器 if strcmp(impl_type, 'IIR') % IIR低通滤波器设计(巴特沃斯) [app.b, app.a] = butter(order, Wpass, 'low'); app.StatusLabel.Text = "IIR滤波器设计完成 (阶数: " + order + ")"; else % FIR低通滤波器设计(汉明窗) app.b = fir1(order, Wpass, 'low', hamming(order+1)); app.a = 1; % FIR滤波器分母为1 app.StatusLabel.Text = "FIR滤波器设计完成 (阶数: " + order + ")"; end % 绘制频率响应 app.plotFrequencyResponse(); catch ME uialert(app.UIFigure, ME.message, '设计错误'); end end % 绘制频率响应 function plotFrequencyResponse(app) [h, w] = freqz(app.b, app.a, 1024, app.Fs); mag = 20*log10(abs(h)); phase = unwrap(angle(h))*180/pi; cla(app.AxesMagPhase); yyaxis(app.AxesMagPhase, 'left'); plot(app.AxesMagPhase, w, mag, 'b', 'LineWidth', 1.5); ylabel(app.AxesMagPhase, '幅度 (dB)'); title(app.AxesMagPhase, '滤波器频率响应'); grid(app.AxesMagPhase, 'on'); % 标记通带和阻带 hold(app.AxesMagPhase, 'on'); xline(app.AxesMagPhase, app.FpassEditField.Value, '--g', 'Passband'); xline(app.AxesMagPhase, app.FstopEditField.Value, '--r', 'Stopband'); hold(app.AxesMagPhase, 'off'); yyaxis(app.AxesMagPhase, 'right'); plot(app.AxesMagPhase, w, phase, 'r', 'LineWidth', 1.5); ylabel(app.AxesMagPhase, '相位 (度)'); legend(app.AxesMagPhase, {'幅频响应', '通带边界', '阻带边界', '相频响应'}, 'Location', 'best'); end % 生成基带信号 function generateSignal(app, ~) % 获取信号参数 signalFreq = app.SignalFreqEdit.Value; % 生成多频信号(基波+谐波) app.t = 0:1/app.Fs:1; f1 = signalFreq; % 基波频率 f2 = 3*signalFreq; % 三次谐波 f3 = 5*signalFreq; % 五次谐波 app.original_signal = 0.7*sin(2*pi*f1*app.t) + ... 0.5*sin(2*pi*f2*app.t) + ... 0.3*sin(2*pi*f3*app.t); % 绘制基带信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.original_signal, 'b', 'LineWidth', 1.5); title(app.AxesTimeDomain, '基带信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.original_signal, '基带信号频谱'); app.StatusLabel.Text = "基带信号已生成 (主频: " + signalFreq + " Hz)"; end % AM调制 function modulateSignal(app, ~) if isempty(app.original_signal) uialert(app.UIFigure, '请先生成基带信号', '操作错误'); return; end % 获取载波频率 Fc = app.CarrierFreqEdit.Value; % AM调制: s(t) = [1 + m(t)] * cos(2πFc t) carrier = cos(2*pi*Fc*app.t); app.modulated_signal = (1 + 0.5*app.original_signal) .* carrier; % 绘制调制信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.modulated_signal, 'm', 'LineWidth', 1.5); title(app.AxesTimeDomain, '调制信号 (AM)'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.modulated_signal, '调制信号频谱'); app.StatusLabel.Text = "AM调制完成 (载波: " + Fc + " Hz)"; end % 信号滤波 function filterSignal(app, ~) if isempty(app.modulated_signal) uialert(app.UIFigure, '请先调制信号', '操作错误'); return; end % 应用滤波器 app.filtered_signal = filtfilt(app.b, app.a, app.modulated_signal); % 绘制滤波后信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.filtered_signal, 'r', 'LineWidth', 1.5); title(app.AxesTimeDomain, '滤波后信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); grid(app.AxesTimeDomain, 'on'); % 绘制频谱 app.plotFrequencySpectrum(app.filtered_signal, '滤波后信号频谱'); app.StatusLabel.Text = "信号滤波完成"; end % AM解调 function demodulateSignal(app, ~) if isempty(app.filtered_signal) uialert(app.UIFigure, '请先滤波信号', '操作错误'); return; end % 包络检波解调 analytic_signal = hilbert(app.filtered_signal); envelope = abs(analytic_signal); % 移除直流分量并缩放 app.demodulated_signal = 2*(envelope - mean(envelope)); % 绘制解调信号 cla(app.AxesTimeDomain); plot(app.AxesTimeDomain, app.t, app.demodulated_signal, 'g', 'LineWidth', 1.5); hold(app.AxesTimeDomain, 'on'); plot(app.AxesTimeDomain, app.t, app.original_signal, 'b--', 'LineWidth', 1.2); title(app.AxesTimeDomain, '解调信号 vs 原始信号'); xlabel(app.AxesTimeDomain, '时间 (s)'); ylabel(app.AxesTimeDomain, '幅度'); legend(app.AxesTimeDomain, {'解调信号', '原始信号'}, 'Location', 'best'); grid(app.AxesTimeDomain, 'on'); hold(app.AxesTimeDomain, 'off'); % 绘制频谱 app.plotFrequencySpectrum(app.demodulated_signal, '解调信号频谱'); app.StatusLabel.Text = "AM解调完成"; end % 绘制频谱图 function plotFrequencySpectrum(app, signal, titleStr) N = length(signal); f = (-N/2:N/2-1)*(app.Fs/N); Y = fftshift(fft(signal)); cla(app.AxesFreqDomain); plot(app.AxesFreqDomain, f, abs(Y)/max(abs(Y)), 'LineWidth', 1.5); title(app.AxesFreqDomain, titleStr); xlabel(app.AxesFreqDomain, '频率 (Hz)'); ylabel(app.AxesFreqDomain, '归一化幅度'); grid(app.AxesFreqDomain, 'on'); xlim(app.AxesFreqDomain, [-app.Fs/4, app.Fs/4]); end end % 应用初始化 methods (Access = private) % 创建UI组件 function createComponents(app) % 创建主窗口 app.UIFigure = uifigure('Name', 'IIR/FIR滤波器调制解调系统', ... 'Position', [100 100 1000 700]); % 滤波器参数区 uipanel(app.UIFigure, 'Title', '滤波器参数', ... 'Position', [20 520 300 160]); app.FilterImplLabel = uilabel(app.UIFigure, ... 'Text', '滤波器类型:', ... 'Position', [40 650 80 22]); app.FilterImplDropDown = uidropdown(app.UIFigure, ... 'Items', {'IIR', 'FIR'}, ... 'Position', [130 650 100 22], ... 'Value', 'IIR'); app.FpassLabel = uilabel(app.UIFigure, ... 'Text', '通带截止(Hz):', ... 'Position', [40 620 90 22]); app.FpassEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 620 100 22], ... 'Value', 50, ... 'Limits', [1 499]); app.FstopLabel = uilabel(app.UIFigure, ... 'Text', '阻带截止(Hz):', ... 'Position', [40 590 90 22]); app.FstopEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 590 100 22], ... 'Value', 70, ... 'Limits', [1 499]); app.OrderLabel = uilabel(app.UIFigure, ... 'Text', '滤波器阶数:', ... 'Position', [40 560 80 22]); app.OrderEditField = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 560 100 22], ... 'Value', 6, ... 'Limits', [1 50]); app.DesignButton = uibutton(app.UIFigure, 'push', ... 'Text', '设计滤波器', ... 'Position', [240 560 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @designFilter, true)); % 信号参数区 uipanel(app.UIFigure, 'Title', '信号参数', ... 'Position', [20 400 300 100]); app.SignalFreqLabel = uilabel(app.UIFigure, ... 'Text', '基带频率(Hz):', ... 'Position', [40 470 90 22]); app.SignalFreqEdit = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 470 100 22], ... 'Value', 10, ... 'Limits', [1 50]); app.CarrierFreqLabel = uilabel(app.UIFigure, ... 'Text', '载波频率(Hz):', ... 'Position', [40 440 90 22]); app.CarrierFreqEdit = uieditfield(app.UIFigure, 'numeric', ... 'Position', [130 440 100 22], ... 'Value', 100, ... 'Limits', [50 200]); % 操作按钮区 app.GenerateButton = uibutton(app.UIFigure, 'push', ... 'Text', '生成基带信号', ... 'Position', [40 380 120 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @generateSignal, true)); app.ModulateButton = uibutton(app.UIFigure, 'push', ... 'Text', 'AM调制', ... 'Position', [170 380 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @modulateSignal, true)); app.FilterButton = uibutton(app.UIFigure, 'push', ... 'Text', '滤波', ... 'Position', [40 340 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @filterSignal, true)); app.DemodulateButton = uibutton(app.UIFigure, 'push', ... 'Text', '解调', ... 'Position', [130 340 80 30], ... 'ButtonPushedFcn', createCallbackFcn(app, @demodulateSignal, true)); % 状态显示 app.StatusLabel = uilabel(app.UIFigure, ... 'Text', '准备就绪', ... 'Position', [40 310 250 22], ... 'FontColor', [0 0.5 0]); % 坐标轴区域 app.AxesMagPhase = uiaxes(app.UIFigure, ... 'Position', [340 520 600 160], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesMagPhase, '滤波器频率响应'); app.AxesTimeDomain = uiaxes(app.UIFigure, ... 'Position', [340 300 600 200], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesTimeDomain, '时域信号'); app.AxesFreqDomain = uiaxes(app.UIFigure, ... 'Position', [340 50 600 200], ... 'XGrid', 'on', 'YGrid', 'on'); title(app.AxesFreqDomain, '频域信号'); % 初始化滤波器 app.designFilter(); end end % 应用启动和运行 methods (Access = public) % 构造函数 function app = IIRFilterDesigner % 创建并配置组件 createComponents(app) % 注册应用 registerApp(app, app.UIFigure) % 运行应用 if nargout == 0 clear app end end % 代码执行 function delete(app) delete(app.UIFigure) end end end
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值