?stem([1,2,3])
一、初始設置
%% 初始設置
% 清空工作空間,關閉無關頁面
clc,clear,close all;
?
% 繪圖變量
font_size = 12; %全局基礎字體大小
axis_size = 10; %坐標軸刻度標簽字體大小
line_width = 2; %繪圖線條寬度
legend_size = 10.5; %圖例字體大小
marker_size = 12; %數據點標記尺寸
figure_width = 14; ?%圖形整體寬度(單位:英寸)
figure_height = 8; ?%圖形整體高度(單位:英寸)
BiaValue = 0; ? ? ?%偏置值/基準值(用于數據校準或零值參考)
二、波形、圖
原始信號波形
%% //繪制原信號波形
t = 0:0.0001:10;
y = 3*sin(2*pi*10*t) + sin(2*pi*40*t) + sin(2*pi*200*t);figure
%% 圖形窗口參數設置
% 單位轉換:英寸→厘米(1英寸=2.54厘米)
% 窗口位置:[左下角x坐標, y坐標, 寬度, 高度]
set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]); ?% 邊距3cm+內容尺寸%% 坐標軸精細調整
% LooseInset: 控制坐標軸與圖形邊界的間距
% TightInset: 系統自動計算的最小邊距
% BiaValue: 用于X軸方向的額外偏移補償(如避免標簽截斷)
set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
plot(t, y, 'b-', 'LineWidth', line_width) ?% 藍色實線 線寬控制
set(gca,'FontSize',font_size,'FontName','Times New Roman') ?% 字體統一配置(確保中英文字體兼容)
xlim([0 1]); ?% 顯示1秒時長
ylim([-6 6]); % 幅值范圍覆蓋所有分量(3*1 + 1 + 1 = 5 +余量)
set(gca, 'XTick', 0:0.25:1); ?% 每250ms一個主刻度
set(gca, 'YTick', -6:3:6); ? ?% 幅值刻度步長3
xlabel('時間 (s)', 'FontSize', font_size, 'FontName', 'Times New Roman');
ylabel('信號幅值', 'FontSize', font_size, 'FontName', 'Times New Roman');
title('原信號波形', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman');
理想信號波形
%% //繪制理想信號波形
t = 0:0.0001:10;
y = 3*sin(2*pi*10*t);
?
figure
set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]);
set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
plot(t, y, 'b-', 'LineWidth', line_width)
set(gca,'FontSize',font_size,'FontName','Times New Roman')
xlim([0 1])
ylim([-4 4])
set(gca,'XTick',0:0.25:1)
set(gca,'YTick',-4:2:4)
xlabel('時間 (s)', 'FontSize', font_size, 'FontName', 'Times New Roman')
ylabel('信號幅值', 'FontSize', font_size, 'FontName', 'Times New Roman')
title('理想信號波形', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman')
幅頻、相頻圖
%% //采樣,采樣頻率:100Hz,采樣時長:1s
t = 0:0.01:1;
y = 3*sin(2*pi*10*t) + sin(2*pi*40*t) + sin(2*pi*200*t);
?
% figure
% set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]);
% set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
% plot(t, y, 'b-', 'LineWidth', line_width)
% set(gca,'FontSize',font_size,'FontName','Times New Roman')
% xlim([0 1])
% ylim([-6 6])
% set(gca,'XTick',0:0.25:1)
% set(gca,'YTick',-6:3:6)
% xlabel('時間 (s)', 'FontSize', font_size, 'FontName', 'Times New Roman')
% ylabel('信號幅值', 'FontSize', font_size, 'FontName', 'Times New Roman')
% title('原信號采樣后的波形', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman');
% set(gca,'Fontname','Monospaced'); ?%中文亂碼問題
?
%% //信號分析
% 數據長度
datalength = length(y);
% 對數據進行FFT
yFFT = fft(y, datalength);
mag = abs(yFFT);
% 幅值歸一化
mag = mag*2/datalength;
pha = angle(yFFT)*180/pi;
% 對無效相位進行置0
for i = 1:datalength
? ? if (mag(1,i)<0.3)
? ? ? ? pha(1,i) = 0;
? ? end
end% 序列坐標
n = 0:datalength-1;
% 采樣頻率
fs = 1/0.01;
% 序列頻率
f = (0:datalength-1)*fs/datalength;?
figure
set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]);
set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
stem(f(1:datalength/2), mag(1:datalength/2), 'b-', 'LineWidth', line_width)
set(gca,'FontSize',font_size,'FontName','Times New Roman')
xlim([0 50])
ylim([0 4])
set(gca,'XTick',0:10:50)
set(gca,'YTick',0:1:4)
xlabel('頻率 (Hz)', 'FontSize', font_size, 'FontName', 'Times New Roman')
ylabel('幅值', 'FontSize', font_size, 'FontName', 'Times New Roman')
title('采樣信號的頻域分析', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman')
?set(gca,'Fontname','Monospaced'); ?%中文亂碼問題?figure
set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]);
set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
stem(f(1:datalength/2), pha(1:datalength/2), 'b-', 'LineWidth', line_width)
set(gca,'FontSize',font_size,'FontName','Times New Roman')
xlim([0 50])
ylim([-100 200])
set(gca,'XTick',0:10:50)
set(gca,'YTick',-100:100:200)
xlabel('頻率 (Hz)', 'FontSize', font_size, 'FontName', 'Times New Roman')
ylabel('相位 (deg)', 'FontSize', font_size, 'FontName', 'Times New Roman')
title('采樣信號的頻域分析', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman')
set(gca,'Fontname','Monospaced'); ?%中文亂碼問題
三、低通濾波器
fdatool?
打開工具箱
打開后,保存為Filter_E3.mat
保存后,雙擊此處
%% //濾波器低通濾波
load('Filter_E3.mat')
filteroce = Num;
y_Filter = filter(filteroce, 1, y);
?
% 繪制濾波后的信號
figure
set(gcf,'Unit','centimeters','Position',[3 3 3+figure_width 3+figure_height]);
set(gca,'LooseInset',get(gca,'TightInset')+[BiaValue,0,0,0],'FontName','Times New Roman','FontSize',font_size);
plot(t, y_Filter, 'b-', 'LineWidth', line_width)
set(gca,'FontSize',font_size,'FontName','Times New Roman')
xlim([0 1])
ylim([-6 6])
set(gca,'XTick',0:0.25:1)
set(gca,'YTick',-6:3:6)
xlabel('時間 (s)', 'FontSize', font_size, 'FontName', 'Times New Roman')
ylabel('信號幅值', 'FontSize', font_size, 'FontName', 'Times New Roman')
title('濾波后的信號', 'FontSize', font_size*1.5, 'FontName', 'Times New Roman')
set(gca,'Fontname','Monospaced'); ?%中文亂碼問題
參考
數字信號處理_第1個編程實例_現代信號處理第一張程序設計-CSDN博客
【數字信號處理_第1個Matlab編程實例】 https://www.bilibili.com/video/BV1514y1w7Sw/?share_source=copy_web&vd_source=be33b1553b08cc7b94afdd6c8a50dc5a?