# 峰值分析

### 查找最大值或峰值

```load sunspot.dat year = sunspot(:,1); relNums = sunspot(:,2); findpeaks(relNums,year) xlabel('Year') ylabel('Sunspot Number') title('Find All Peaks')```

### 测量峰值之间的距离

```findpeaks(relNums,year,'MinPeakProminence',40) xlabel('Year') ylabel('Sunspot Number') title('Find Prominent Peaks')```

```figure [pks, locs] = findpeaks(relNums,year,'MinPeakProminence',40); peakInterval = diff(locs); histogram(peakInterval) grid on xlabel('Year Intervals') ylabel('Frequency of Occurrence') title('Histogram of Peak Intervals (years)')```

`AverageDistance_Peaks = mean(diff(locs))`
```AverageDistance_Peaks = 10.9600 ```

### 寻找裁剪或饱和信号中的峰值

```load clippedpeaks.mat figure % Show all peaks in the first plot ax(1) = subplot(2,1,1); findpeaks(saturatedData) xlabel('Samples') ylabel('Amplitude') title('Detecting Saturated Peaks') % Specify a minimum excursion in the second plot ax(2) = subplot(2,1,2); findpeaks(saturatedData,'threshold',5) xlabel('Samples') ylabel('Amplitude') title('Filtering Out Saturated Peaks') % link and zoom in to show the changes linkaxes(ax(1:2),'xy') axis(ax,[50 70 0 250])```

### 测量峰的幅值

```load noisyecg.mat t = 1:length(noisyECG_withTrend); figure plot(t,noisyECG_withTrend) title('Signal with a Trend') xlabel('Samples'); ylabel('Voltage(mV)') legend('Noisy ECG Signal') grid on```

```[p,s,mu] = polyfit((1:numel(noisyECG_withTrend))',noisyECG_withTrend,6); f_y = polyval(p,(1:numel(noisyECG_withTrend))',[],mu); ECG_data = noisyECG_withTrend - f_y; % Detrend data figure plot(t,ECG_data) grid on ax = axis; axis([ax(1:2) -1.2 1.2]) title('Detrended ECG Signal') xlabel('Samples') ylabel('Voltage(mV)') legend('Detrended ECG Signal')```

### 阈值化以找到感兴趣的峰值

QRS 复波由三个主要分量组成：Q 波、R 波、S 波。R 波可以通过设置 0.5 毫伏以上的峰值阈值来检测。注意 R 波相隔 200 多个采样。使用此信息通过指定 'MinPeakDistance' 来去除不需要的峰值。

```[~,locs_Rwave] = findpeaks(ECG_data,'MinPeakHeight',0.5,... 'MinPeakDistance',200);```

```ECG_inverted = -ECG_data; [~,locs_Swave] = findpeaks(ECG_inverted,'MinPeakHeight',0.5,... 'MinPeakDistance',200);```

```figure hold on plot(t,ECG_data) plot(locs_Rwave,ECG_data(locs_Rwave),'rv','MarkerFaceColor','r') plot(locs_Swave,ECG_data(locs_Swave),'rs','MarkerFaceColor','b') axis([0 1850 -1.1 1.1]) grid on legend('ECG Signal','R waves','S waves') xlabel('Samples') ylabel('Voltage(mV)') title('R wave and S wave in Noisy ECG Signal')```

```smoothECG = sgolayfilt(ECG_data,7,21); figure plot(t,ECG_data,'b',t,smoothECG,'r') grid on axis tight xlabel('Samples') ylabel('Voltage(mV)') legend('Noisy ECG Signal','Filtered Signal') title('Filtering Noisy ECG Signal')```

```[~,min_locs] = findpeaks(-smoothECG,'MinPeakDistance',40); % Peaks between -0.2mV and -0.5mV locs_Qwave = min_locs(smoothECG(min_locs)>-0.5 & smoothECG(min_locs)<-0.2); figure hold on plot(t,smoothECG); plot(locs_Qwave,smoothECG(locs_Qwave),'rs','MarkerFaceColor','g') plot(locs_Rwave,smoothECG(locs_Rwave),'rv','MarkerFaceColor','r') plot(locs_Swave,smoothECG(locs_Swave),'rs','MarkerFaceColor','b') grid on title('Thresholding Peaks in Signal') xlabel('Samples') ylabel('Voltage(mV)') ax = axis; axis([0 1850 -1.1 1.1]) legend('Smooth ECG signal','Q wave','R wave','S wave')```

```% Values of the Extrema [val_Qwave, val_Rwave, val_Swave] = deal(smoothECG(locs_Qwave), smoothECG(locs_Rwave), smoothECG(locs_Swave)); meanError_Qwave = mean((noisyECG_withTrend(locs_Qwave) - val_Qwave))```
```meanError_Qwave = 0.2771 ```
`meanError_Rwave = mean((noisyECG_withTrend(locs_Rwave) - val_Rwave))`
```meanError_Rwave = 0.3476 ```
`meanError_Swave = mean((noisyECG_withTrend(locs_Swave) - val_Swave))`
```meanError_Swave = 0.1844 ```

```avg_riseTime = mean(locs_Rwave-locs_Qwave); % Average Rise time avg_fallTime = mean(locs_Swave-locs_Rwave); % Average Fall time avg_riseLevel = mean(val_Rwave-val_Qwave); % Average Rise Level avg_fallLevel = mean(val_Rwave-val_Swave); % Average Fall Level helperPeakAnalysisPlot(t,smoothECG,... locs_Qwave,locs_Rwave,locs_Swave,... val_Qwave,val_Rwave,val_Swave,... avg_riseTime,avg_fallTime,... avg_riseLevel,avg_fallLevel)```