(1)创建一个由钟形曲线和组成的信号。指定每条曲线的位置、高度和宽度;
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [3 4 4 2 2 3];
Wdt = [2 6 3 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
plot(x,Gauss,'--',x,PeakSig)
(2)使用带有函数findpeaks来查找信号的峰值及其位置;
[pks,locs] = findpeaks(PeakSig,x);
text(locs+.02,pks,num2str((1:numel(pks))'))
(3)对峰值进行排序;
[psor,lsor] = findpeaks(PeakSig,x,'SortStr','descend');
findpeaks(PeakSig,x)
text(lsor+.02,psor,num2str((1:numel(psor))'))
(4)标记出峰值相对高度、宽度等信息;
假定信号由几个钟型函数和一个周期余弦函数构成;
x = linspace(0,1,1000);
base = 4*cos(2*pi*x);
Pos = [1 2 3 5 7 8]/10;
Hgt = [3 7 5 5 4 5];
Wdt = [1 3 3 4 2 3]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss)+base;
plot(x,Gauss,'--',x,PeakSig,x,base)
用findpeaks函数定位并标记出相对峰值大于4位置;
findpeaks(PeakSig,x,'MinPeakProminence',4,'Annotate','extents')
[pks,locs,widths,proms] = findpeaks(PeakSig,x); widths proms
得到宽度和相对高度为
widths =
0.0154 0.0431 0.0377 0.0625 0.0274 0.0409
proms =
2.6816 5.5773 3.1448 4.4171 2.9191 3.6363
(5)设置最小分离间隔寻找峰值
sunspot.dat是一组循环周期数据,大约每11年会出现一个峰值;
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
findpeaks(avSpots,year)
仔细观察,发现上图中部分位置出现重叠现象,从而设置两两相邻峰值最小间隔为6;
findpeaks(avSpots,year,'MinPeakDistance',6)
为了验证上一步是否有效,计算上述情况峰峰值之间平均间隔;
[pks,locs] = findpeaks(avSpots,year,'MinPeakDistance',6);
meanCycle = mean(diff(locs))
得到平均间隔为10.96,近似于11;
meanCycle =
10.9600
假定数据均记录于每年3月20日,用每年的数据创建一个日期矩阵;
ty = datetime(year,3,20);
[pk,lk] = findpeaks(avSpots,ty,'MinPeakDistance',years(6));
plot(ty,avSpots,lk,pk,'o')
计算峰峰值平均间隔;
dttmCycle = years(mean(diff(lk)))
dttmCycle =
10.9600
创建一个时间表(timetable),声明函数years,并显示timetable中最后5组数据;
TT = timetable(years(year),avSpots);
plot(TT.Time,TT.Variables)
entries = TT(end-4:end,:)