% clear
% clc
% close all
% % 生成随机时程信号
% fs=100;
% signalLength = fs*60*2;
% time = 1/fs:1/fs:signalLength/fs;
% randomSignal = 2*sin(2*pi*0.5*time)+3*cos(2*pi*1*time)+randn(1, signalLength);
function [frequencyPP]=funct_peak(signal,Hz)
% 生成随机时程信号
fs=Hz;
randomSignal=signal;
signalLength = length(randomSignal);
time = 1/fs:1/fs:signalLength/fs;
%功率谱密度图
pwel.window=1024*9;
pwel.noverlap=pwel.window/2
[pxx,f]=pwelch(randomSignal,pwel.window,pwel.noverlap,[],fs);
plot(f,10*log10(pxx))
xlim([0 2])
% 寻找峰值点
threshold = 20; % 定义阈值,即前后20个点的纵坐标差值的阈值
[pks, locs] = findpeaks(10*log10(pxx), 'MinPeakHeight', threshold);
% 标识峰值点
hold on
scatter(f(locs), pks, 'r', 'filled')
% 在每个峰值点的位置添加文本标签
for i = 1:length(locs)
text(f(locs(i)), pks(i), sprintf('%.2f', f(locs(i))), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
frequencyPP(i)=round(f(locs(i)),3);,
end
hold off