Spline interpolation and Savitzki-Golay smoothing

2023-11-09

 

转自:http://octave.1599824.n4.nabble.com/Spline-interpolation-and-Savitzki-Golay-smoothing-td1675136.html

 

## natural-cubic-spline interpolation
## usage: yspline = spline(x,y,xspline)
## example:
## x = 0:10; y = sin(x);
## xspline = 0:0.1:10; yspline = spline(x,y,xspline);
## plot(x,y,"+",xspline,yspline);
## Given the vectors x and y, which tabulate a function, with
## x(1) < x(2) < x(3) <... or x(1) > x(2) > x(3) >..., and given
## the vector xspline, this function returns a natural-cubic-spline
## interpolated vector yspline.
## author: Zdenek Remes, May 22, 1999


function ynew = spline(x,y,xnew)
[x,index]=sort(x);
y=y(index);
n=length(y);
y2(1)=0.0;
y2(n)=0.0;
u(1)=0.0;
for i=2:n-1
  sig=(x(i)-x(i-1))/(x(i+1)-x(i-1));
  p=sig*y2(i-1)+2.0;
  y2(i)=(sig-1.0)/p;
  u(i)=(y(i+1)-y(i))/(x(i+1)-x(i))-(y(i)-y(i-1))/(x(i)-x(i-1));
  u(i)=(6.0*u(i)/(x(i+1)-x(i-1))-sig*u(i-1))/p;
endfor;
k=n-1;
while (k >= 1)
  y2(k)=y2(k)*y2(k+1)+u(k);
  k--;
endwhile;

i1=1; in=length(xnew);

#if (xnew(1) < x(1))
#  error("spline: bad xspline");
#endif;
#if (xnew(in) > x(n))
#  error("spline: bad xspline");
#endif;
 
if (xnew(1) == x(1))
  ynew(1)=y(1);
  i1=2;
endif;
if (xnew(in) == x(n))
  ynew(in)=y(n);
  in=in-1;
endif;
 

for i=i1:in  
  khi=n;
  klo=1;
  while ((khi-klo) > 1)
    k=floor((khi+klo)/2);
    if (x(k) > xnew(i))
      khi=k;
    else
      klo=k;  
    endif;
  endwhile;
  h=x(khi)-x(klo);
  a=(x(khi)-xnew(i))/h;
  b=(xnew(i)-x(klo))/h;
  ynew(i)=a*y(klo)+b*y(khi)+((a^3-a)*y2(klo)+(b^3-b)*y2(khi))*(h*h)/6.0;
endfor;
endfunction;

## Savitzky-Golay smoothing filter
## usage: [xsavgol,ysavgol]=savgol(x,y,nl,nr,m)
## example: x=0:0.01:3;y1=sin(x.^3);y=y1+(rand(1,301)-0.5)/3;
##    [xsavgol,ysavgol]=savgol(x,y,10,10,2);
##    plot(x,y,"+",xsavgol,ysavgol,x,y1)
## Given vectors x, y containing a tabulated data y=f(x) with
## equally spaced x's this function calculates smoothed data
## ysavgol=g(xsavgol) by Savitzky-Golay smoothing filter.
## nl is the number of leftward (past) data points used, while
## nr is the number of rightward (future) data points, making
## the total number of data points used nl+nr+1. m is the order
## of the smoothing polynomial, also equal to the highest
## conserved moment; usual values are m=2 or m=4.
## The idea of Savitzky-Golay filtering is to smooth the
## underlying data y=f(x) within the moving window not by a
## constant (whose estimate is the average), but by a poly-
## nomial of higher order. Thus for a point y(i) the function
## savgol fits by a least-squares method a polynomial to
## points y(i-nl), ..., y(i+nr) in the moving window, and
## then set g(i-nl+1) to the value of that polynomial at
## position x(i).
## Zdenek Remes, Mai 22, 1999
       
function [xnew,ynew]=savgol(x,y,nl, nr, M)
    if max(diff(x,2))>100*eps
        error("The x's must be equally spaced.")
    endif
    for i=-nl:nr
        for j=0:M
            A(i+nl+1,j+1)=i^j;
        endfor
    endfor
    AA=inv(A'*A);
    for i=-nl:nr
        cc=0;
        for m=0:M
            cc=cc+AA(1,m+1)*i^m;
        endfor
    c(i+nl+1)=cc;
    endfor
   
    nx=length(x);
    for i=nl:nx-nr-1
        yy=0;
        for j=-nl:nr
            yy=yy+c(j+nl+1)*y(i+j+1);
        endfor
        xnew(i-nl+1)=x(i+1);
        ynew(i-nl+1)=yy;
    endfor    
endfunction

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spline interpolation and Savitzki-Golay smoothing 的相关文章

随机推荐

  • 华为od机试 C++ 地址分割

    题目 你的任务是编写一个程序 该程序将接收一个由逗号分隔的字符串 其中包含一个URL的前缀和后缀 然后将它们合并成一个完整的URL 合并规则如下 如果前缀的最后一个字符是斜杠 则删除它 如果后缀的第一个字符是斜杠 则删除它 在处理过的前缀和
  • 很诡异的问题——Jenkins与svn代码冲突之解决方法

    今天遇到一个很诡异的问题 可能是我刚刚接触jenkins的原因 导致这个问题困扰了我半个小时 不管怎么样 我还是记录下来 希望能帮助到那些和我一样刚刚接触jenkins的小伙伴 我从idea修改了两个配置文件 提交到svn 然后jenkin
  • mac上安装brew(最简易)

    我们使用linux下有yum mac相应的是brew 安装软件 brew的安装目录在 usr local Cellar 我们以安装nodejs为例子 只需要执行 brew install nodejs 就安装完了 就这么简单 接下来我们安装
  • java开源 VR全景商城 saas商城 b2b2c商城 o2o商城 积分商城 秒杀商城 拼团商城 分销商城 短视频商城 小程序商城搭建

    1 涉及平台 平台管理 商家端 PC端 手机端 买家平台 H5 公众号 小程序 APP端 IOS Android 微服务平台 业务服务 2 核心架构 Spring Cloud Spring Boot Mybatis Redis 3 前端框架
  • M1 电脑使用nvm 管理node

    1 执行下面的代码创建文件 bash profile touch bash profile 2 下载安装 curl o https raw githubusercontent com nvm sh nvm v0 35 2 install s
  • 从零开始的Docker详解(六)

    Docker仓库 docker仓库是集中存放镜像的地方 类似maven的仓库集中存放依赖 Docker Hub Docker Hub是由Docker官方维护的公共仓库 包含官方镜像和个人上传的镜像 大部分镜像都可以在上面找到 注 非官方的镜
  • CentOS7.3 安装

    选择Install CentOS Linux 7 选择语言 点击软件选择 选择基本环境 点击安装位置 选择我要配置分区 点击完成 根据需要选择分区方案 点击 根据需要添加挂载点 添加完所有挂载点后点击完成 在弹出的页面中选择接受更改 点击开
  • java基础知识点

    java中有四大修饰符 分别为private default protected public 下面主要是四者之间的区别 private 私有的 private可以修饰成员变量 成员方法 构造方法 不能修饰类 此刻指的是外部类 内部类不加以
  • 【mybatis-plus】学习笔记

    官方地址 https mp baomidou com 自动化工具 JPA tk imapper MybatisPlus 简介 MyBatis Plus 简称 MP 是一个 MyBatis 的增强工具 在 MyBatis 的基础上只做增强不做
  • 将Android项目打包成Library

    最近在弄一个SDK 考虑把项目做成 Library 类库的形式 方便调用 顺便在此分享给大家 首先 先创建一个普通的android项目 这个项目可以起任何你想要的名称 想要的包名等 步骤如下 在Package Explorer中 鼠标右键项
  • .NET Word模板引擎--MiniWord,继MiniExcel后又一开源作品

    目录 Part1简介 Part2特点 Part3安装 Part4使用 文本生成 图片生成 列表生成 表格生成 Part5总结 Part1简介 MiniWord 是 NET Word模板引擎 由Word模板和数据 简单 快速生成文件 Part
  • 启动模式,BOOT0和BOOT1详解

    原文链接 http blog csdn net daunxx article details 40148945 在画STM32的电路图的时候 关于STM32的启动方式纠结了一下 现有的参考设计都是在STM32的启动选择引脚BOOT0和BOO
  • 通过Java构建树形结构

    通过Java构建树形结构所需要的数据 实体类Test 主键 private String id 父类ID private String parentId 子节点 private List
  • matplotlib - 确保 0 在 RdBu 颜色条中变为白色

    matplotlib 确保 0 在 RdBu 颜色条中变为白色 matplotlib 确保 0 在 RdBu 颜色条中变为白色 IT工具网 标签 matplotlib colormap 我使用以下代码段创建了一个热图 span style
  • 丢手帕问题

    package Task author 链表解决丢手帕问题 约瑟夫问题 public class CycLink public static void main String args CycLinkModel cycLink new Cy
  • Vue3.x 中 vue.config 配置 Webpack-dev-server的proxy 实现代理跨域

    前言 如果你有单独的后端开发服务器 API 并且希望在同域名下发送 API 请求 那么代理某些 URL 会很有用 解决开发环境的跨域问题 不用在去配置nginx和host 爽歪歪 在webpack config js中配置 下面简单介绍一下
  • MySQL锁系列(八)之 死锁

    能学到什么 什么是死锁 死锁有什么危害 典型的死锁案例剖析 如何避免死锁 一 什么是死锁 1 必须满足的条件 1 2 1 必须有两个或者两个以上的事务 2 不同事务之间都持有对方需要的锁资源 A事务需要B的资源 B事务需要A的资源 这就是典
  • Protobuf(二)proto3语法格式

    proto文件有两种语法标准 proto2和proto3 我们以proto3为例 其语法格式如下 message
  • 什么是软件测试?

    什么是软件测试 软件测试的定义 在一定条件下对软件进行操作 发现软件的问题 提高软件的质量 软件测试在开发中的有着重要地位 软件测试在各阶段的完成相应的任务 需求测试 架构测试 详细测试等 随着测试的发展 测试技术有了新的支持和扩充CMMI
  • Spline interpolation and Savitzki-Golay smoothing

    转自 http octave 1599824 n4 nabble com Spline interpolation and Savitzki Golay smoothing td1675136 html natural cubic spli