matlab 版本
python 版本
import numpy as np
import matplotlib. pyplot as plt
from matplotlib import rcParams
rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
rcParams[ 'axes.unicode_minus' ] = False def compute_time ( n, fs) : """生成时间序列""" return np. arange( n) / fsdef trend_remove ( s, fs, m= 2 ) : """去除信号中的趋势项""" x = compute_time( len ( s) , fs) param = np. polyfit( x, s, m) s_poly_func = np. poly1d( param) s_poly = s_poly_func( x) return s - s_poly
fs = 100
t = compute_time( 1000 , fs)
quadratic_trend = 0.01 * t** 2 + 0.5 * t
signal = np. sin( 2 * np. pi * 5 * t) + quadratic_trend
signal_detrended = trend_remove( signal, fs, m= 2 )
plt. figure( figsize= ( 10 , 6 ) )
plt. plot( t, signal, label= "原始信号 (带趋势)" )
plt. plot( t, signal_detrended, label= "去除趋势后的信号" , linestyle= "--" )
plt. legend( )
plt. xlabel( "时间 (s)" )
plt. ylabel( "幅值" )
plt. title( "信号去趋势验证 (m=2)" )
plt. grid( )
plt. show( )