数字信号处理_编程实例1

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

相关推荐
奔袭的算法工程师17 天前
论文解读--Sparse4D v3: Advancing End-to-End 3D Detection and Tracking
人工智能·目标检测·计算机视觉·自动驾驶·信号处理
江畔柳前堤18 天前
github实战指南01-账号配置与 SSH 密钥
运维·人工智能·深度学习·ssh·github·pyqt·信号处理
江畔柳前堤18 天前
github实战指南02-仓库管理与 Issue
人工智能·深度学习·github·信号处理·caffe·wps·issue
小白小宋19 天前
【PUSCH番外篇】5G NR 相位补偿与频移校正:原理、流程与工程实现
算法·5g·matlab·信息与通信·信号处理
DreamLife☼19 天前
OpenBCI-实时BCI系统:低延迟与闭环控制
人工智能·信息可视化·信号处理·开源硬件·脑机接口·低延迟·openbci
network_tester20 天前
TSN交换机研发测试怎么做?一套可落地的“信而泰仪器 + 康芯源服务”方案解读
网络·网络协议·tcp/ip·车载系统·汽车·信息与通信·信号处理
Code-keys21 天前
ARM NEON SIMD 编程实战:从音频信号处理到AI算子研发实战
arm开发·音视频·信号处理
MARIN_shen21 天前
Marin说PCB之高速信号SERDES (GMSL2)信号换层孔打在焊盘中心真的好吗?---01
硬件工程·信号处理·pcb工艺
Inhand陈工23 天前
映翰通IG502实战:通过RS232采集交通信号灯数据,实现自动短信告警
网络·嵌入式硬件·物联网·网络安全·边缘计算·信息与通信·信号处理
network_tester24 天前
SENT/PSI5传感器TSN集成测试:打通传统传感与未来车载网络的“最后一公里”
数据库·网络协议·tcp/ip·自动驾驶·信息与通信·信号处理·tcpdump