【问题标题】:Scipy welch and MATLAB pwelch does not provide same answerScipy welch 和 MATLAB pwelch 不提供相同的答案
【发布时间】:2018-05-10 02:29:31
【问题描述】:

我在 python 中使用称为 welch (https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.welch.html) 的 scipy.signal 方法遇到问题,该方法估计时间信号的频谱,因为它(根本)不提供与 MATLAB 的方法相同的输出pwelch,给定相同的参数(窗口大小、重叠等)。下面是我每种语言的代码,输入文件和输出文件都在这里的链接中:

https://www.dropbox.com/s/2ch36phbbmjfhqg/inputs_outputs.zip?dl=0

输入是一个二维数组,其中行是时间步长,每列是一个信号段。输出中的列是输入中相应列的频谱。

Python:

import numpy as np
from scipy.signal import welch, get_window
input = np.genfromtxt('python_input.csv', delimiter=',')
fs = 128

window = get_window('hamming', fs*1)
ff,yy = welch(input, fs=fs, window = window, noverlap = fs/2, nfft=fs*2, 
axis=0, scaling="density", detrend=False)
np.savetxt("python_spectrum.csv", 10*np.log10(yy), delimiter=",")

MATLAB:

input       = csvread('matlab_input.csv');
fs          = 128
win         = hamming(fs);
[pxx,f]     = pwelch(input ,win,[],[],fs,'psd');
csvwrite('matlab_spectrum.csv',pxx);

我怀疑 scipy 是问题所在,因为它的输出在事先反映我使用的滤波器(4 阶巴特沃斯带通从 0.3 到 35 Hz 和 filtfilt)方面没有意义 - 但是,MATLAB 的输出确实如此:

Each methods output using imagesc in MATLAB

这里有一些元素差异的图

Elementwise differences (y-axis should be 0-64!) (the third plot I have excluded the most extreme values)

我确实尝试了一个简单的正弦曲线,它在两种编程语言中都运行良好 - 所以我完全迷失了。

【问题讨论】:

    标签: python matlab scipy


    【解决方案1】:

    您的 Python 和 MATLAB 文件的排序方式不同,这会给您带来错误。尽管数组的形状相同,但值在 Python 中按行排序,在 MATLAB 中按列排序。

    您可以通过重塑输入数组和转置来解决此问题。这将为您提供与 MATLAB 中相同的值顺序:

    input = input.reshape((input.shape[1], input.shape[0])).T
    

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 2021-04-14
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      相关资源
      最近更新 更多