简单的一维时间序列峰值提取(Python)

B站影视 2025-01-11 12:26 2

摘要:import numpy as npimport matplotlib.pyplot as pltimport cv2import pandas as pdfrom scipy.signal import find_peaksdef load_profile(

import numpy as npimport matplotlib.pyplot as pltimport cv2import pandas as pdfrom scipy.signal import find_peaksdef load_profile(fn): profile = np.loadtxt(fn, skiprows=3, delimiter=';', usecols=[0, 1])profile[:, 0] = profile[:, 0] / 1000profile[:, 0] = profile[:, 0] - profile[:, 0][np.argmax(profile[:, 1])]profile[:, 1] = profile[:, 1] / profile[:, 1].maxreturn profiledef plot_profile(profile, peaks, x_range):x = profile[:, 0] y = profile[:, 1]# Plotting the cleaned intensity profile with corrected x-axis valuesplt.figure(figsize=(10, 6))plt.plot(x, y, label='Intensity Profile', color='b')plt.plot(x[peaks], y[peaks], 'ro', label='Prominent Peaks')plt.xlim(-1 * x_range, x_range)plt.xlabel('x (mm^-1)')plt.ylabel('Intensity (a.u.)')plt.title('FFT Image Profile Data (Corrected x-axis)')plt.legendplt.grid(True)plt.show# Define the peak detection functiondef detect_prominent_peaks(profile, **kwargs):"""Detect prominent peaks in the given intensity profile.Parameters:profile (numpy array): Array where the first column is x values and the second column is intensity (y) values.prominence (float): Prominence value for peak detection.Returns:peaks (numpy array): Indices of the detected peaks.actual_distances (numpy array): Actual distances between detected peaks."""# Extract x and y values from the profilex = profile[:, 0]y = profile[:, 1]# Perform peak detectionpeaks, properties = find_peaks(y, **kwargs)# If no peaks are detected, return empty arraysif len(peaks) == 0:return np.array(), np.array()# Determine the center peak (closest to the middle of the x range)center_peak = peaks[np.argmax(y[peaks])]# Exclude the center peak to prevent division by zeronon_center_peaks = peaks[peaks != center_peak]# Calculate the distances from the center peak for each detected peak in the spatial domain# Since x is in the frequency domain, the actual distance in the spatial domain is the reciprocal of the frequency differenceactual_distances = 1 / np.abs(x[non_center_peaks] - x[center_peak])return peaks, actual_distancesprofile_list = ['ML2_Blue_Corrected_1_BW_FFT_20241009.csv', 'ML2_Blue_Corrected_2_BW_FFT_20241009.csv', 'ML2_Green_Corrected_1_BW_FFT_20241009.csv', 'ML2_Green_Corrected_2_BW_FFT_20241009.csv']prominence_list = [0.1, 0.1, 0.05, 0.1]for i, f in enumerate(profile_list):profile = load_profile(f'.\\{f}')peaks, actual_distances = detect_prominent_peaks(profile, prominence=prominence_list[i])plot_profile(profile, peaks, 5)print(actual_distances)profile[:, 0][peaks]array([2.3320449, 2.8761887, 3.3425976, 3.8867414, 4.4308852])peaks, actual_distance = detect_prominent_peaks(profile, prominence=0.15)plot_profile(profile, peaks)print(actual_distance)profile[:, 0][peaks]x = profile[:, 0]# Determine the center peak (closest to the middle of the x range)center_peak = peaks[np.argmax(y[peaks])]# Exclude the center peak to prevent division by zeronon_center_peaks = peaks[peaks != center_peak]print(center_peak, x[center_peak])43 3.3425976profile_list = ["FFT Peaks_Blue_1_Gwy.csv", "FFT Peaks_Blue_2_Gwy.csv"]for f in profile_list:profile = load_profile(f'.\\{f}')peaks, actual_distances = detect_prominent_peaks(profile, prominence=0.08)plot_profile(profile, peaks, 5)print(actual_distances)

知乎学术咨询:

https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。

分割线分割线

基于改进高斯-拉普拉斯滤波器的微震信号平滑降噪方法(MATLAB)

完整数据可通过知乎学术咨询获得

非平稳信号的凸一维全变差降噪方法(MATLAB)

算法可迁移至金融时间序列,地震/微震信号,机械振动信号,声发射信号,电压/电流信号,语音信号,声信号,生理信号(ECG,EEG,EMG)等信号。

完整数据可通过知乎学术咨询获得

基于广义交叉验证阈值的微震信号降噪方法(MATLAB)

算法可迁移至金融时间序列,地震/微震信号,机械振动信号,声发射信号,电压/电流信号,语音信号,声信号,生理信号(ECG,EEG,EMG)等信号。

完整数据可通过知乎学术咨询获得

集合高阶统计量和小波块阈值的非平稳信号降噪方法-以地震信号为例(MATLAB)

完整数据可通过知乎学术咨询获得

一种新的类谱峭度算法的旋转机械故障诊断模型(Python)

完整数据可通过知乎学术咨询获得

采用8种方法对一维信号进行降噪(Python)

NS: noisy signalS: original siganlmean filter: ws = window sizemedian filter:average filter: ns = number of noisy signal(different)bandpass filter: l = low cut-off frequency, h = high ...threshold filter: r = ratio(max abs(fft) / min ...)wavelet filter: a = thresholdstd filter:NN: neural network

来源:小芳课堂

相关推荐