Matlab v_findpeaks代码

2023-05-16

这里写自定义目录标题

  • Matlab v_findpeaks代码
    • 写在前面
    • 代码

Matlab v_findpeaks代码

写在前面

本函数主要用于寻找数据的上下极值点,可以用于求取包络

参考链接:http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/mdoc/v_mfiles/v_findpeaks.html#_top

代码

function [k,v]=v_findpeaks(y,m,w,x)
%V_FINDPEAKS finds peaks with optional quadratic interpolation [K,V]=(Y,M,W,X)
%
%  Inputs:  Y(N,1)   is the input signal (does not work with UInt datatype)
%           M        is mode:
%                       'f' include the first sample if a downward initial slope
%                       'l' include the last sample if an upward final slope
%                       'm' return only the maximum peak
%                       'q' performs quadratic interpolation
%                       'v' finds valleys instead of peaks
%           W        is the width tolerance; a peak will be eliminated if there is
%                    a higher peak within +-W. Units are samples or X values
%           X(N,1)   x-axis locations of Y values [default: 1:length(Y)]
%
% Outputs:  K(P,1)   are the positions in X of the peaks in Y (fractional if M='q')
%           V(P,1)   are the peak amplitudes: if M='q' the amplitudes will be interpolated
%                    whereas if M~='q' then V=Y(K).

% Outputs are column vectors regardless of whether Y is row or column.
% If there is a plateau rather than a sharp peak, the routine will place the
% peak in the centre of the plateau. When the W input argument is specified,
% the routine will eliminate the lower of any pair of peaks whose separation
% is <=W; if the peaks have exactly the same height, the second one will be eliminated.
% Unless the 'f' or 'l' options are given, all peak locations satisfy 1<K<N.
%
% If no output arguments are specified, the results will be plotted.
%

%       Copyright (C) Mike Brookes 2005
%      Version: $Id: v_findpeaks.m 6564 2015-08-16 16:56:40Z dmb $
%
%   VOICEBOX is a MATLAB toolbox for speech processing.
%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   This program is free software; you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation; either version 2 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You can obtain a copy of the GNU General Public License from
%   http://www.gnu.org/copyleft/gpl.html or by writing to
%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin<2 || ~numel(m)
    m=' ';
elseif nargin>3
    x=x(:); % x must be a column vector
end
ny=length(y);
if any(m=='v')
    y=-y(:);        % invert y if searching for valleys
else
    y=y(:);        % force to be a column vector
end
dx=y(2:end)-y(1:end-1);
r=find(dx>0);
f=find(dx<0);
k=[]; % set defaults
v=[];
if ~isempty(r) && ~isempty(f)    % we must have at least one rise and one fall
    dr=r;
    dr(2:end)=r(2:end)-r(1:end-1);
    rc=ones(ny,1);
    rc(r+1)=1-dr;
    rc(1)=0;
    rs=cumsum(rc); % = time since the last rise
    df=f;
    df(2:end)=f(2:end)-f(1:end-1);
    fc=ones(ny,1);
    fc(f+1)=1-df;
    fc(1)=0;
    fs=cumsum(fc); % = time since the last fall
    rp=repmat(-1,ny,1);
    rp([1; r+1])=[dr-1; ny-r(end)-1];
    rq=cumsum(rp);  % = time to the next rise
    fp=repmat(-1,ny,1);
    fp([1; f+1])=[df-1; ny-f(end)-1];
    fq=cumsum(fp); % = time to the next fall
    k=find((rs<fs) & (fq<rq) & (floor((fq-rs)/2)==0));   % the final term centres peaks within a plateau
    v=y(k);
    if any(m=='q')         % do quadratic interpolation
        if nargin>3
            xm=x(k-1)-x(k);
            xp=x(k+1)-x(k);
            ym=y(k-1)-y(k);
            yp=y(k+1)-y(k);
            d=xm.*xp.*(xm-xp);
            b=0.5*(yp.*xm.^2-ym.*xp.^2);
            a=xm.*yp-xp.*ym;
            j=(a>0);            % j=0 on a plateau
            v(j)=y(k(j))+b(j).^2./(a(j).*d(j));
            k(j)=x(k(j))+b(j)./a(j); % x-axis position of peak
            k(~j)=0.5*(x(k(~j)+fq(k(~j)))+x(k(~j)-rs(k(~j))));    % find the middle of the plateau
        else
            b=0.25*(y(k+1)-y(k-1));
            a=y(k)-2*b-y(k-1);
            j=(a>0);            % j=0 on a plateau
            v(j)=y(k(j))+b(j).^2./a(j);
            k(j)=k(j)+b(j)./a(j);
            k(~j)=k(~j)+(fq(k(~j))-rs(k(~j)))/2;    % add 0.5 to k if plateau has an even width
        end
    elseif nargin>3 % convert to the x-axis using linear interpolation
        k=x(k);
    end
end
% add first and last samples if requested
if ny>1
    if any(m=='f') && y(1)>y(2)
        v=[y(1); v];
        if nargin>3
            k=[x(1); k];
        else
            k=[1; k];
        end
    end
    if any(m=='l') && y(ny-1)<y(ny)
        v=[v; y(ny)];
        if nargin>3
            k=[k; x(ny)];
        else
            k=[k; ny];
        end
    end
    
    % now purge nearby peaks - note that the decision about which peaks to
    % delete is not unique
    
    if any(m=='m')
        [v,iv]=max(v);
        k=k(iv);
    elseif nargin>2 && numel(w)==1 && w>0
        j=find(k(2:end)-k(1:end-1)<=w);
        while any(j)
            j=j+(v(j)>=v(j+1));
            k(j)=[];
            v(j)=[];
            j=find(k(2:end)-k(1:end-1)<=w);
        end
    end
elseif ny>0 && (any(m=='f') || any(m=='l'))
    v=y;
    if nargin>3
        k=x;
    else
        k=1;
    end
end
if any(m=='v')
    v=-v;    % invert peaks if searching for valleys
end

if ~nargout
    if any(m=='v')
        y=-y;    % re-invert y if searching for valleys
        ch='v';
    else
        ch='^';
    end
    plot(1:ny,y,'-',k,v,ch);
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matlab v_findpeaks代码 的相关文章

  • 如何在 MATLAB 中调整矩阵大小?

    假设我有一个1 by 12矩阵 我想将其调整为4 by 3矩阵 我怎么能这样做呢 我当前的解决方案有点丑陋 for n 1 length mat 3 out n 1 3 mat n 1 3 1 n 1 3 3 end 有一个更好的方法吗 r
  • 隐藏图中某些图形对象的 MATLAB 图例条目

    MATLAB 图例列出了绘图中的所有内容 包括您在绘图上放置的指南 绕过这个问题的软糖就是要做的 Plot Add legend Add guidelines 然而 MATLAB 将最新的行放在前面 这意味着指南将位于显示的数据之上 丑陋且
  • 将数据库导入 MATLAB 错误

    我正在尝试将表导入到我的 MATLAB 工作区 但它一直向我抛出错误 Undefined function or method fetch for input arguments of type struct 这是我尝试执行的代码 dyn
  • MatLab 中的输出有小数点的上限 [重复]

    这个问题在这里已经有答案了 我修改了 MatLab 中的一些代码 以便它可以给出函数 cos x 3 x 的根 当我运行代码并要求它返回 xnew 的值 因为 xnew 应该等于函数的根 时 它仅将 xnew 返回到小数点后 4 位 我希望
  • 结合阴影误差和实线平均值的图例

    我在用此 FEX 条目 http www mathworks com matlabcentral fileexchange 27485 boundedline line plots with shaded errorconfidence i
  • SPMD 与 Parfor

    我对 matlab 中的并行计算很陌生 我有一个创建分类器 SVM 的函数 我想用几个数据集来测试它 我有一个 2 核工作站 所以我想并行运行测试 有人可以向我解释一下以下之间的区别 dataset array dataset1 datas
  • iOS 将 URL 中的音频分成帧

    我正在 iOS 上开发一个简单的网络广播应用程序 具有非常简单的语音 音乐识别功能 主要思想是一个收音机 它播放来自 url 的信号 同时检查正在广播的信号类型 当它检测到语音时 它会改变频道等等 我使用 Storyboards 和 AVF
  • 同时使用两个数组中的元素的过滤器

    假设我们有两个大小相同的数组 A and B 现在 我们需要一个过滤器 对于给定的掩码大小 从以下位置选择元素A 但删除掩码的中心元素 并在其中插入相应的元素B 所以 3x3 伪掩码 看起来类似于 A A A A B A A A A 对平均
  • 计算向量中连续 1 和 0 的数量

    在 Matlab 中我有一个如下所示的向量 0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 0 1 0 1 我现在要做的是统计这个向量中1的个数 连续的 1 算作 1 此外 我还想计算 1 之间 0 的平均值和中
  • 在 MATLAB 中一次为元胞数组分配不同的值

    我需要有关在 MATLAB 中创建元胞数组的帮助 其中每个元胞都是不同大小的数组 例如 假设我有这个简单的数组和值 A 5 3 8 7 0 4 1 B 10 元胞数组C必须创建为 C 10 20 30 40 50 10 20 30 10 2
  • MATLAB:解包函数

    我正在与 Mathworks 的某人讨论 unwrap http www mathworks com access helpdesk help techdoc ref unwrap html函数中对于 以外的跳跃容差有一个 bug 并且希望
  • 我的 matlab 图中需要不同的颜色

    这是我的情节代码 问题是我的图中的两条线具有相同的颜色 我需要为图中的每条线 总共 4 条线 分配一个特殊的颜色 for i 1 nFolderContents data hdrload folderContents i if size f
  • 有没有办法在 MATLAB 中执行函数内联?

    我可以使用什么语言功能或开箱即用的技巧来完成 MATLAB 中的函数内联 令人烦恼的是 Google 搜索 matlab 内联函数 http www google com search q matlab inline function揭示了
  • 如何在 R 中导入 matlab 表

    我有一个matlab mat文件与表数据类型我想将其导入 R 中 我为此使用 readMat R 正在将其作为列表读取 之后有没有办法将列表转换为 R 中的数据帧或表格格式 当我使用as dataframe我收到以下错误 Error in
  • MATLAB:涉及大数的计算

    如何在 MATLAB 中执行涉及大量数字的计算 举一个简单的例子 任意精度计算器将显示 1 120 132 370 260 约为 1 56 但 MATLAB 无法执行此类计算 power 120 132 factorial 370 fact
  • 优化数组压缩

    假设我有一个数组k 1 2 0 0 5 4 0 我可以按如下方式计算掩码m k gt 0 1 1 0 0 1 1 0 仅使用掩码 m 和以下操作 左移 右移 And Or 加 减 乘 我可以将 k 压缩为以下形式 1 2 5 4 以下是我目
  • 比较元胞数组中的字符串

    我试图在单词列表中找到最常见的单词 到目前为止 这是我的代码 uniWords unique lower words for i 1 length words for j 1 length uniWords if uniWords j lo
  • 与超类和子类构造函数接口

    我在 matlab 文档和之前有关使用 matlab 继承和类构造函数创建接口的问题中找不到帮助 为了使其整洁 放在一个包内 我可以将其压缩如下 而不是拖拽代码 一套 MyPkg有一个超类Super和一些子类Sub1 Sub2 我的大多数属
  • 是否有一个函数可以将两个元胞数组“压缩”在一起? [复制]

    这个问题在这里已经有答案了 假设我有一个元胞数组A and B as so A A B C D B 1 2 3 4 我想创建元胞数组C通过将 A 和 B 压缩 在一起 如下所示 C zip A B C A 1 B 2 C 3 D 4 这样的
  • 在matlab中设置图例符号的精度

    我有这个 leg2 strcat Max Degree num2str adet 1 1 ch l leg3 strcat Min Degree num2str adet 1 2 ch l leg4 strcat Max Request n

随机推荐

  • C语言结构体对齐详解

    文章目录 一 C语言结构体对齐大小快速判断二 反汇编角度看结构体三 总结 一 C语言结构体对齐大小快速判断 在C语言中定义一个结构体 xff0c 里面具体占用多少个字节呢 xff0c 先举一个例子 xff0c 如下 xff1a span c
  • 飞控各传感器相关作用

    飞控主要包括主控处理器MCU xff08 main control unit 和惯性导航模块IMU xff08 Inertial Measurement Unit xff09 四轴则必须配备3轴陀螺仪 xff0c 是四轴飞行器的机械结构 动
  • C++ : C++基础 :从内存的角度看 char[]和char*

    char 和char 区别 1 xff1a 数据在内存中的存储2 xff1a char 和 char 分析3 xff1a char p2 和 char p1 3 1 修改指针所指向的地址 4 string转char 5 char 转stri
  • 基于 nonce 的用户身份验证协议

    一 xff1a 什么是nonce 维基百科 xff1a 安全工程中 xff0c Nonce 是一个在加密通信只能使用一次的数字 在认证协议中 xff0c 它往往是一个 随机或 伪随机数 xff0c 以避免 重放攻击 二 xff1a 举例说明
  • php从数据库读取菜单数据并树状显示

    数据库表结构 mcp node表 字段 node code node name node pcode node code为区域编码 node name为区域名称 node pcode为父区域编码 祖先父区域编码为0 预期效果 代码实现部分
  • 蓝牙BLE之CC2541 OAD升级[带看门狗OAD]

    说明和代码设置 本文有两篇文章参考 其中博主 34 甜甜的大香瓜 34 的文章是原始文章 详细介绍了ImageA的hexh和B的bin以及A的bin是怎么生成的和具体的操作方法 https blog csdn net feilusia ar
  • php 从数据库读取数据并生成树型可折叠菜单

    数据存储形式 折叠菜单显示 直接调用 php页面即可输出树状可折叠菜单 所用到的js 区域折叠函数 function ShowMenu MenuID if MenuID style display 61 61 34 none 34 Menu
  • 提权apache 为root权限

    include lt stdio h gt include lt stdlib h gt include lt string h gt include lt sys types h gt include lt unistd h gt int
  • PHP 生成 WSDL 文件工具类 SoapDiscovery.class.php

    lt pre name 61 34 code 34 class 61 34 php 34 gt lt php Copyright c 2005 Braulio Jos Solano Rojas All rights reserved Red
  • Yii1.1 实现简单restful 框架

    学习了下php的rest服务 xff0c 将总结记录如下 采用Yii1 1版本 xff0c Yii2已经专门有restful专题 xff08 ps 暂时没有学习 xff09 1 先用Yii创建项目 2 创建数据库 xff08 rest xf
  • java 泛型

    什么是 泛型 xff1f 泛型 xff08 Generic type 或者 generics xff09 是对 Java 语言的类型系统的一种扩展 xff0c 以支持创建可以按类型进行参数化的类 可以把类型参数看作是使用参数化类型时指定的类
  • jsp学习(一)

    jsp java 服务器页面 作用 xff1a 将内容的生成和信息的展示相分离 运行在服务端 xff0c 本质上就是一个servlet xff0c 产生的java文件和class保留在tomcat的word目录下 jsp脚本 xff1a l
  • jsp学习(二)

    jsp注释 xff1a html注释 lt gt 注释的内容只在页面上看不到 xff0c Java代码和html源代码都有 java注释 只在java代码中存在 jsp注释 lt gt 只在jsp页面中存在 xff0c 翻译成java文件之
  • 数据结构 _ PAT练习 _ 1064 Complete Binary Search Tree

    1064 Complete Binary Search Tree 原题基本分析代码 原题 点此链接1 基本分析 参考陈越姥姥的解题2 xff0c 主要的难点在于在何处插入新元素使得满足完全搜索二叉树的条件 猜测还有一种更通用的动态插入算法
  • 数据结构 _ 基础练习 _ 7-10 公路村村通

    原题 点此链接1 题目分析 可参考课本 xff08 高等教育出版社 陈越 数据结构 xff09 P225中关于prim算法的描述解题 本题相对于课本描述的算法来说 xff0c 不需要考虑 父节点 xff08 parent xff09 xff
  • KEIL问题二[function的内容空没有显示(占用CPU过高)][报错Error: Encountered an improper argument]

    function的内容空没有显示 不知道怎么回事也没有任何复现的办法 装了各个版本的KEIL都不能够接解决这个问题 最终无意中新建立了一个代码分组彻底解决这个问题 KEIL Functions Bug 当出现 function的bug的时候
  • 数据结构 _ 基础练习 _7-11 关键活动 _ 非递归解法

    1 原题 点此链接1 2 解题思路 写在前面 xff0c 参考博文2 本题其实考察的就是课本 xff08 高等教育出版社 陈越 数据结构 xff09 6 8节 关键路径的内容 课本中给出了三个公式 xff0c 以分别计算三个要素 xff1a
  • 数据结构 _ PAT练习 _ 7-13 Insert or Merge

    原题 点此链接1 解题思路 参考课本 xff1a 高等教育出版社 陈越主编 数据结构 参考视频 xff1a MOOC 浙江大学 数据结构与算法 本题主要考察的是简单插入排序 xff08 课本P268 xff09 以及归并排序的非递归算法 x
  • 数据结构 _ 基础练习 _ 7-14 Insertion or Heap Sort

    原题 点此链接1 题目分析 与前一题 Insert or Merge 相同2 xff0c 同样考察的是插入排序算法以及堆排序算法 算法如下 xff1a 首先需要判断是插入排序还是堆排序 xff0c 由于插入必然是 有序序列 43 相同的序列
  • Matlab v_findpeaks代码

    这里写自定义目录标题 Matlab v findpeaks代码写在前面代码 Matlab v findpeaks代码 写在前面 本函数主要用于寻找数据的上下极值点 xff0c 可以用于求取包络 参考链接 xff1a http www ee