Traceback (most recent call last):
File "d:\DK\vmd\白鲸优化VMD.py", line 176, in
white_whale_optimization(population_size, max_generations, data)
File "d:\DK\vmd\白鲸优化VMD.py", line 118, in white_whale_optimization
fitness_values[i], imf, res = fitness(population[i], data)
File "d:\DK\vmd\白鲸优化VMD.py", line 29, in fitness
imf, res, u_hat, omega = VMD(data, alpha, tau, K, DC, init, tol)
File "d:\DK\vmd\白鲸优化VMD.py", line 76, in VMD
u_hat_plus[k, n] = (f_hat_plus[n] - sum_uk - lambda_hat[k, n] / 2) / (1 + alpha_hat[k] * (freqs - omega_plus[k, n])**2 + epsilon)
TypeError: only length-1 arrays can be converted to Python scalars
PS C:\Users\ddkkd>
#def calculate_local_envelope_entropy(signal):
# 计算信号的振幅包络
analytic_signal = hilbert(signal)
amplitude_envelope = np.abs(analytic_signal)
# 计算局部包络熵
epsilon = 1e-10 # 避免由于零值导致的计算错误
normalized_envelope = amplitude_envelope / (np.mean(amplitude_envelope) + epsilon)
local_envelope_entropy = -np.sum(normalized_envelope * np.log2(normalized_envelope + epsilon))
return local_envelope_entropy
def fitness(pop, data):
np.random.seed(0)
K = int(pop[0])
alpha = int(pop[1])
tau = 0
DC = 0
init = 1
tol = 1e-7
imf, res, u_hat, omega = VMD(data, alpha, tau, K, DC, init, tol)
comp = np.vstack([imf, res.reshape(1, -1)])
SE = 0
se_imf = []
for i in range(comp.shape[0]):
temp = calculate_local_envelope_entropy(comp[i, :])
SE += temp
se_imf.append(temp)
fit = min(se_imf)
np.random.seed(int(time.time()))
return fit, imf, res
def VMD(data, alpha, tau, K, DC, init, tol):
# 数据长度
N = len(data)
epsilon = 1e-10 # 避免由于零值导致的计算错误
# 建立频域
freqs = np.fft.fftfreq(N)
# 初始化变量
u_hat_plus = np.zeros((K, N), dtype=np.complex128)
u = np.zeros((K, N))
omega_plus = np.zeros((K, N))
phi_plus = np.zeros((K, N), dtype=np.complex128)
alpha_hat = np.zeros(K)
alpha_plus = np.zeros((K, N))
h = np.zeros((K, N))
f_hat_plus = np.zeros(N, dtype=np.complex128)
lambda_hat = np.zeros((K, N), dtype=np.complex128)
进行VMD迭代
for n in range(N):
f_hat_plus[n] = np.sum(u_hat_plus[:, n])
sum_uk = np.sum(u[:, n])
for k in range(K):
# 计算频谱中心
omega_plus[k, n] = np.dot(freqs[N//2:N], (abs(u_hat_plus[k, N//2:N])**2)) / (np.sum(abs(u_hat_plus[k, N//2:N])**2) + epsilon)
# 更新IMF分量
u_hat_plus[k, n] = (f_hat_plus[n] - sum_uk - lambda_hat[k, n] / 2) / (1 + alpha_hat[k] * (freqs[n] - omega_plus[k, n])**2 + epsilon)
phi_plus[k, :] = np.real(np.fft.ifft(u_hat_plus[k, :]))
# 计算包络函数
analytic_signal = hilbert(phi_plus[k, :])
amplitude_envelope = np.abs(analytic_signal)
# 更新包络函数权重
alpha_plus[k, :] = alpha * (amplitude_envelope**2) / (np.sum(amplitude_envelope**2) + tau)
# 更新拉格朗日乘子
lambda_hat[k, :] = lambda_hat[k, :] + alpha_plus[k, :] * (phi_plus[k, :] - u[k, :])
# 更新h函数
h[k, :] = lambda_hat[k, :]
# 更新IMF分量
u[k, :] = np.real(np.fft.ifft(h[k, :]))
# 计算残差
res = data - np.sum(u, axis=0)
# 计算频谱和中心频率
u_hat_plus = np.fft.fft(u, axis=1)
omega_hat = np.zeros((K, N))
for k in range(K):
omega_hat[k, :] = np.dot(freqs, (abs(u_hat_plus[k])**2)) / np.sum(abs(u_hat_plus[k])**2)
return u, res, u_hat_plus, omega_hat