算法更新更新

This commit is contained in:
kevinding1125 2023-06-14 09:52:05 +08:00
parent 418b6e6ee1
commit d97c3e8731
5 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,54 @@
# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/6/13 21:32
@Usage :
@Desc : FFT的工具类
'''
import numpy as np
import tensorflow as tf
'''
freq_value: 可以是时域通过np.fft.fft转换而来
'''
def ifft(freq_value):
# 幅值
amp = np.abs(freq_value / len(freq_value))
# 相角
angle = np.angle(freq_value)
length = len(amp)
result = []
for t in range(length):
wk = 2 * np.pi * np.arange(length) / length
cur = np.sum(amp * np.cos(wk * t + angle))
result.append(cur)
return result
def fft(time_value):
pass
if __name__ == '__main__':
array = np.array([-0.029078494757,
-0.33095228672,
-0.12124221772,
0.553512275219,
-0.158036053181,
0.268739402294,
-0.638222515583,
0.233140587807,
-0.173265621066,
0.467218101025,
-0.372010827065,
-0.136630430818,
0.343256533146,
0.008932195604])
print(tf.signal.fft(array))
array_fft = np.fft.fft(array)
# print(ifft(array))
print(array_fft)

View File

@ -0,0 +1,8 @@
#-*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/6/13 19:42
@Usage :
@Desc :
'''

View File

@ -0,0 +1,59 @@
# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/6/13 19:42
@Usage :
@Desc :测试一下FFT和IFFT公式计算
'''
import numpy as np
import matplotlib.pyplot as plt
array = np.array([-0.029078494757,
-0.33095228672,
-0.12124221772,
0.553512275219,
-0.158036053181,
0.268739402294,
-0.638222515583,
0.233140587807,
-0.173265621066,
0.467218101025,
-0.372010827065,
-0.136630430818,
0.343256533146,
0.008932195604])
array_fft = np.fft.rfft(array)
# 幅值
amp = np.abs(np.fft.fft(array) / len(array))
# 相角
angle = np.angle(np.fft.fft(array))
# 时部
real = np.real(np.fft.rfft(array) * 2 / len(array))
# 虚部
imag = np.imag(np.fft.rfft(array) * 2 / len(array))
#
angle1 = np.arctan(imag / real)
print(angle)
print(angle1)
# result = []
# (num,) = amp.shape
# (total,) = array.shape
# for j in range(total):
# wk = 2 * np.pi * np.arange(num) / num
# cur = np.sum(amp * np.cos(wk * j + angle))
# result.append(cur)
#
# print(result)
# print(array)
# # print(array_fft)
#
# plt.subplot(2, 1, 1)
# plt.plot(result)
# plt.subplot(2, 1, 2)
# plt.plot(array)
# plt.show()

View File

@ -0,0 +1,8 @@
#-*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/6/13 19:42
@Usage :
@Desc :
'''

View File

@ -0,0 +1,35 @@
# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/6/13 21:15
@Usage :
@Desc : 时频域损失
'''
import tensorflow as tf
import tensorflow.keras.backend as K
class FTMSE(tf.keras.losses.Loss):
def call(self, y_true, y_pred):
y_true = tf.cast(y_true, tf.float64)
y_pred = tf.cast(y_pred, tf.float64)
# 需要转为复数形式
yt_fft = tf.signal.fft(tf.cast(y_true,tf.complex64))
yp_fft = tf.signal.fft(tf.cast(y_pred,tf.complex64))
# 幅值
amp = tf.abs(yt_fft / len(yt_fft))
# 相角
angle = tf.angle(freq_value)
time_loss = tf.reduce_mean(tf.abs(y_true - y_pred))
freq_loss = tf.reduce_mean(tf.abs(yt_fft - yp_fft))
ftLoss = time_loss+freq_loss
return ftLoss