我正在研究this帖子,并且有一个示例可以将7000个样本零填充到1000个样本信号!

因此,我尝试编写以下代码来模拟这种情况,但输出与发布图片不匹配:

在帖子中,预期的输出图像如下:

original post image
python - FFT分贝值不正确-LMLPHP

但是我的输出图像如下所示:
python - FFT分贝值不正确-LMLPHP

明显:


信号分贝信息不匹配(峰值-15.96与11)。
我的输出中不存在两个峰值频率。


我的完整代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.ticker import FuncFormatter, MultipleLocator
import scipy.fftpack

num = 1000
samplerate = 100*1000000
freq1 = 1*1000000
freq2 = 1.05*1000000
duration = num/samplerate
T = 1.0/samplerate

x = np.arange(0,num,1)*T
y = np.sin(2*np.pi*freq1*x) + np.sin(2*np.pi*freq2*x)
fig,ax = plt.subplots(1,1,figsize=(8,6),constrained_layout=True)

M = 8000
x2 = np.arange(0,duration*8,T)
y2 = np.pad(y, (0, num*7), 'constant')

yfft = np.abs(scipy.fftpack.fft(y2,n=M))[:M//2]
freqs1 = np.fft.fftfreq(M,T)[:M//2]
freqs = np.arange(0, M)[:M//2]*(samplerate/M)
print(freqs[len(freqs)-1])
print(freqs1[len(freqs)-1])
ydb = 20*np.log10(yfft*2/M)

df = pd.DataFrame(list(zip(freqs,ydb)),columns=['freq','value'])
ax.plot(df['freq'],df['value'],marker='.')
print("signal length:%d,frequency length:%d"%(len(y2),len(freqs)))

xmin =   500000
xmax = 1500000
df = df.query('freq > %d and freq < %d'%(xmin,xmax))
ymin = df['value'].min()
ymax = df['value'].max()
ax.set_xlim([xmin,xmax])
ax.set_ylim([ymin,ymax])
ax.set_xticks(np.append(ax.get_xticks(),[xmin,xmax]))
ax.set_yticks(np.append(ax.get_yticks(),[ymin,ymax]))
ax.xaxis.set_major_formatter(FuncFormatter(
    lambda val,pos: '{:.1f}$MHz$'.format(val/1000000)
))
ax.tick_params(axis='x', rotation=45)
ax.grid()
print(freqs[1] - freqs[0])
plt.show()

最佳答案

为了查看两个峰值,该帖子的作者在您发布的图形的标题中指出:


  功率谱-7000个时间采样和1000个零填充/ 8000个FFT点


您的y2仅具有1000个支持样本,其中有7000个填充样本。我改为这样做:

# Make the signal: 7000 points.
x = np.arange(0, num*7, 1) * T
y = np.sin(2*np.pi*freq1*x) + np.sin(2*np.pi*freq2*x)

# Pad the signal with 1000 points.
x2 = np.arange(0, num*8, 1) * T
y2 = np.pad(y, (0, num), 'constant')


现在,信号看起来正确(对y2x2进行绘图):

python - FFT分贝值不正确-LMLPHP

功率应为10 dB;我认为只需为这个较长的信号更新乘数,例如:

ydb = 20*np.log10(yfft*7/M)


我最终得到:

python - FFT分贝值不正确-LMLPHP

关于python - FFT分贝值不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57913553/

10-11 09:31