一种直流转交流的代码实现 - through FFT

# show how to use FFT, filtered DC signal and return back to SampleValue-time zone.
# the basic concept is coming from ChatGPT.
# Write in python language.
#
# created by twicave.
# Jun09,2023
#
import numpy as np
import matplotlib.pyplot as plt

# 定义正弦信号
# 产生一个包含1000Hz、2000Hz两个正弦波信号和一个直流分量的复合信号
Fs = 20000;
f1 = 1000;
f2 = 2000;
t = np.arange(0, 0.01, 1/Fs);
signal = np.sin(2*np.pi*f1*t) + 0.7*np.sin(2*np.pi*f2*t) + 800;
np.savetxt('signal_data.txt', signal, fmt='%f', delimiter=',');

fig = plt.figure(1, figsize=(8, 10));
plt1 = fig.add_subplot(2, 1, 1)
plt1.plot(t,signal)
plt1.set_xlabel('Time (s)')
plt1.set_ylabel('Amplitude')
plt1.set_title('Original Signal')


# 下面,将对应信号转至频域消除0点后转回时域
x = signal

# 傅里叶变换
X = np.fft.fft(x)  # 计算原始信号的傅里叶变换

# 去除直流分量
X[0] = 0;

# 幅角变换
phase_shift = np.zeros(len(X))
phase_shift[len(X)//2] = -np.angle(X[len(X)//2])
X_corrected = X * np.exp(1j * phase_shift)

# 转回时域
x_withoutDC = np.real(np.fft.ifft(X_corrected));

print(x_withoutDC);
s_corrected = x_withoutDC;

plt2 = fig.add_subplot(2, 1, 2)
plt2.plot(t, s_corrected.real)
plt2.set_xlabel('Time (s)')
plt2.set_ylabel('Amplitude')
plt2.set_title('After filtered the DC of signal')

fig.suptitle("Demo program : filter DC of signal");
fig.show()

注意在频域去除直流信号后,需要对频谱中谱线的幅角做整体位移。你可以尝试把那段幅角变换的代码去除,看看时域信号会变成什么样子。

上面的代码可以直接在python环境中运行,运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子正

thanks, bro...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值