Matlab中的两种曲线平滑函数

MATLAB自带的曲线平滑函数:smooth和spcrv

一、smooth

自己感觉:平滑效果还可以

1.语法

(1)函数

smooth(Y)

smooth(Y,SPAN)%SPAN是滑动平均的点数,即“几点滑动平均”

smooth(Y,SPAN,METHOD)

(2)METHOD:

%loess:局部回归,加权的线性最小均方,2阶多项式模型
%rloess:'loess’的增强版,在回归过程中,给极端值分配的权重小。超出6倍绝对离差的极端值,权重为0
%‘moving’:滑动平均(默认)。低通滤波器,滤波器系数=span的倒数
%‘lowess’:局部回归。线性最小均方加权,1阶多项式模型。
%‘sgolay’:Savitzky-Golay filter。
%‘rlowess’:‘lowess’的增强版。在回归过程中,给极端值分配的权重小。超出6倍绝对离差的极端值,权重为0

(3)注意

对增强版的算法而言,极端值的影响可以忽略

(4)例子1(来自MATLAB)

%-----------画出原始数据和平滑后的数据----
[xx,ind] = sort(x);%排序,ind-索引,xx-排序后的数据(从小到大)
subplot(2,1,1)
plot(xx,y(ind),'b.',xx,yy1(ind),'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original Data','Smoothed Data Using ''loess''','Location','NW')
subplot(2,1,2)
plot(xx,y(ind),'b.',xx,yy2(ind),'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original Data','Smoothed Data Using ''rloess''','Location','NW')

(5)例子2(来自MATLAB)

clc,clear
load count.dat  %(24×3)一天24小时,3个交通路口的车流量
%1°滑动平均滤波器,5小时的span。平滑全部数据
c = smooth(count(:));
C1 = reshape(c,24,3);
%画图(原始数据,平滑后的数据)
subplot(3,1,1)
plot(count,':');
hold on
plot(C1,'-');
title('Smooth C1 (All Data)')
%2°相同的滤波器。分别对每列数据进行平缓。
C2 = zeros(24,3);
for I = 1:3,
    C2(:,I) = smooth(count(:,I));
end
%画图(原始数据、平滑后的数据)
subplot(3,1,2)
plot(count,':');
hold on
plot(C2,'-');
title('Smooth C2 (Each Column)')
%3°画出两次滑动后的数据的差值
subplot(3,1,3)
plot(C2 - C1,'o-')
title('Difference C2 - C1')

二、spcrv

%20160518:spcrv函数
%目前可最做到,对原来的曲线进行平滑
%原理:增加点,平滑曲线
%(自己感觉)效果:可以平滑,但是:1°无法获得平滑后的曲线方程 2°平滑前后,曲线的起始点,y值大小不同

Spline curve by uniform subdivision

1.语法

spcrv(c,k) 
spcrv(c\) 
spcrv(c,k,maxpnt) 

2.描述

spcrv(c,k) 在具有 B 样条系数 c 的 k 阶均匀 B 样条曲线 f 上提供点的密集序列 f(tt)。明确地说,这是曲线
在这里插入图片描述
with B(·|a,…,z) the B-spline with 结点 a,…,z, and n the number of 系数 in c, i.e., [d,n] equals size(c).

spcrv(c) chooses the order k to be 4.

spcrv(c,k,maxpnt) makes sure that at least maxpnt points are generated. The default value for the maximum number of sites ttto be generated is 100.

The parameter interval that the site sequence tt fills out uniformly is the interval [k/2 … (n-k/2)].

The output consists of the array f(tt).

3.案例

The following would show a questionable broken line and its smoothed version:

%% ------例子0----------------
points=[0 0 1 1 0 -1 -1 0 0;0 0 0 1 2 1 0 -1 -2];
figure(3)
plot(points(1,:),points(2,:),':');
values=spcrv(points,3);
hold on;plot(values(1,:),values(2,:));hold off


%% ------------例子1---------------------------
x=[0 0.1 0.16 0.27 0.41 0.48 0.59 0.8];
y=[5 9 70 118 100 17 0 5];
y1=[22.8 22.8 22.8 22.8 22.8 22.8 22.8 22.8];
values1=spcrv([[x(1) x x(end)];[y(1) y y(end)]],3,1000);
values2=spcrv([[x(1) x x(end)];[y1(1) y1 y1(end)]],3,1000);
plot(values1(1,:),values1(2,:),'r',values2(1,:),values2(2,:),'b')

%% ----------------------例子2-----------------
x1=[18.5931 19.2468 19.58193333 19.96586667 20.0985 20.2678 30.95973333];
y=[3.66 7.15 11.74 21.53 31.24 60.51 86.5]
values = spcrv([[x1(1) x1 x1(end)];[y(1) y y(end)]],3);
plot(values(1,:),values(2,:),'k');

Algorithms

重复中点结插入直到至少有maxpnt位点为止。有些情况下使用fnplt会更有效。

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值