MATLAB技巧——平滑滤波,给出一定的例程和输出参考

文章给出若干种MATLAB用来平滑滤波的方法。并给出一定的例程和输出参考。

方法比较多,在实际选择平滑方法时,需根据数据特性(如噪声类型、平滑程度需求)来决定最适合的技术。

文章目录

在 MATLAB 中,平滑滤波是一种用于去除数据噪声、提取信号趋势的技术。以下是 MATLAB 中常用的平滑滤波方法及其示例:

smooth函数

smooth 是 MATLAB 提供的专用平滑函数,支持多种平滑方法,如移动平均、局部加权回归等。

语法

matlab 复制代码
smoothedData = smooth(data, span, method)
  • data: 输入数据(向量)。
  • span: 平滑窗口的大小(数据点数)。
  • method : 平滑方法(可选),包括:
    • 'moving'(默认值):移动平均。
    • 'lowess':局部加权线性回归。
    • 'loess':局部加权二次回归。
    • 'sgolay':Savitzky-Golay 滤波。
    • 'rlowess':鲁棒的局部加权线性回归。
    • 'rloess':鲁棒的局部加权二次回归。

示例:移动平均平滑

matlab 复制代码
% 原始数据
x = linspace(0, 4*pi, 100);
y = sin(x) + 0.3*randn(size(x)); % 添加噪声的正弦波

% 使用 smooth 进行平滑
span = 5; % 平滑窗口
smoothedY = smooth(y, span, 'moving'); % 移动平均平滑

% 可视化
plot(x, y, 'r-', 'DisplayName', '原始数据');
hold on;
plot(x, smoothedY, 'b-', 'LineWidth', 1.5, 'DisplayName', '平滑数据');
legend;
title('平滑滤波示例 作者:matlabfilter');
xlabel('x');
ylabel('y');

Savitzky-Golay 滤波 (sgolayfilt)

Savitzky-Golay 滤波是一种保留信号特性的平滑方法,适合处理非线性数据。

语法

matlab 复制代码
smoothedData = sgolayfilt(data, order, framelen)
  • order:多项式拟合的阶数(通常为 2 或 3)。
  • framelen:滤波窗口的长度,必须是奇数。

示例:Savitzky-Golay 滤波

matlab 复制代码
% 原始数据
x = linspace(0, 4*pi, 100);
y = sin(x) + 0.3*randn(size(x)); % 添加噪声的正弦波

% 使用 sgolayfilt 滤波
order = 2; % 多项式阶数
framelen = 11; % 滤波窗口长度(奇数)
smoothedY = sgolayfilt(y, order, framelen);

% 可视化
plot(x, y, 'r-', 'DisplayName', '原始数据');
hold on;
plot(x, smoothedY, 'b-', 'LineWidth', 1.5, 'DisplayName', 'Savitzky-Golay 平滑');
legend;
title('Savitzky-Golay 滤波示例 作者:matlabfilter');
xlabel('x');
ylabel('y');

自定义移动平均滤波

如果不使用 smooth 函数,可以自行实现简单的移动平均滤波。

自定义移动平均平滑

matlab 复制代码
% 原始数据
x = linspace(0, 4*pi, 100);
y = sin(x) + 0.3*randn(size(x)); % 添加噪声的正弦波

% 自定义移动平均
span = 5; % 平滑窗口
smoothedY = movmean(y, span); % 或使用 filter

% 可视化
plot(x, y, 'r-', 'DisplayName', '原始数据');
hold on;
plot(x, smoothedY, 'b-', 'LineWidth', 1.5, 'DisplayName', '自定义移动平均');
legend;
title('自定义移动平均滤波示例 作者:matlabfilter');
xlabel('x');
ylabel('y');

使用滤波器设计平滑滤波

可以使用低通滤波器对数据进行平滑。MATLAB 提供了多种滤波器设计工具,如 butterfiltfilt 等。

低通滤波器

matlab 复制代码
% 原始数据
x = linspace(0, 4*pi, 100);
y = sin(x) + 0.3*randn(size(x)); % 添加噪声的正弦波

% 设计低通滤波器
fc = 0.1; % 截止频率(归一化到 Nyquist 频率)
[b, a] = butter(2, fc); % 二阶巴特沃斯滤波器

% 应用滤波器
smoothedY = filtfilt(b, a, y); % 零相位滤波

% 可视化
plot(x, y, 'r-', 'DisplayName', '原始数据');
hold on;
plot(x, smoothedY, 'b-', 'LineWidth', 1.5, 'DisplayName', '低通滤波');
legend;
title('低通滤波器平滑示例 作者:matlabfilter');
xlabel('x');
ylabel('y');

卷积实现平滑

均值核高斯核实现平滑。

示例:卷积平滑

matlab 复制代码
% 原始数据
x = linspace(0, 4*pi, 100);
y = sin(x) + 0.3*randn(size(x)); % 添加噪声的正弦波

% 卷积平滑
kernel = ones(1, 5) / 5; % 均值核
smoothedY = conv(y, kernel, 'same'); % 'same' 保持输出长度

% 可视化
plot(x, y, 'r-', 'DisplayName', '原始数据');
hold on;
plot(x, smoothedY, 'b-', 'LineWidth', 1.5, 'DisplayName', '卷积平滑');
legend;
title('卷积平滑示例 作者:matlabfilter');
xlabel('x');
ylabel('y');

总结

  • 简单平滑 :使用 smoothmovmean
  • 高精度平滑 :使用 sgolayfilt 或自定义低通滤波器。
  • 灵活平滑:使用卷积或滤波器设计。

选择平滑方法时,需根据数据特性(如噪声类型、平滑程度需求)来决定最适合的技术。

如需帮助,或有导航、定位滤波相关的代码定制需求,请点击下方卡片联系作者

相关推荐
董先生_ad986ad1 小时前
C# 中的 `lock` 关键字本质
开发语言·c#
元亓亓亓2 小时前
Java后端开发day36--源码解析:HashMap
java·开发语言·数据结构
道剑剑非道2 小时前
QT 打包安装程序【windeployqt.exe】报错c000007d原因:Conda巨坑
开发语言·qt·conda
小邓儿◑.◑2 小时前
C++武功秘籍 | 入门知识点
开发语言·c++
算法如诗3 小时前
【数据融合】基于拓展卡尔曼滤波实现雷达与红外的异步融合附matlab代码
matlab·数据融合
码银4 小时前
Java 集合:泛型、Set 集合及其实现类详解
java·开发语言
大G哥4 小时前
PHP标签+注释+html混写+变量
android·开发语言·前端·html·php
傻啦嘿哟4 小时前
HTTP代理基础:网络新手的入门指南
开发语言·php
fish_study_csdn4 小时前
pytest 技术总结
开发语言·python·pytest
曹牧6 小时前
Java 调用webservice接口输出xml自动转义
java·开发语言·javascript