Matplotlib Specgram与Matlab中的结果相同

2024-09-30 20:37:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有matlab代码:

data = 1:999;
Fs = 8000;
tWindow = 64e-3;
NWindow = Fs*tWindow;
window = hamming(NWindow);

NFFT = 512;
NOverlap = NWindow/2;

[S, F, T,P] = spectrogram(data, window, NOverlap, NFFT, Fs);

在python中

import numpy as np
from matplotlib import mlab
data = range(1,1000)

Fs = 8000
tWindow = 64e-3
NWindow = Fs*tWindow
window = np.hamming(NWindow)

NFFT = 512
NOverlap = NWindow/2

[s, f, t] = mlab.specgram(data, NFFT = NFFT, Fs = Fs, window = window, noverlap = NOverlap, mode  = 'complex')

上面的代码基于我在互联网上找到的示例。问题是我不能从python和MatLab中得到相同的结果。哪里会有问题?你知道吗

结果比较: enter image description here


Tags: 代码importnumpydatanpwindowfsmatlab
1条回答
网友
1楼 · 发布于 2024-09-30 20:37:08

我相信您比较了错误的结果参数,即Ss。你知道吗

MATLAB ^{}文档说明(变量名根据您的示例更改)。你知道吗

S = spectrogram(data) returns the short-time Fourier transform of the input signal, data. Each column of S contains an estimate of the short-term, time-localized frequency content of data.

更进一步

[___,P] = spectrogram(___) also returns a matrix, P, containing an estimate of the power spectral density (PSD) or the power spectrum of each segment.

为了进行比较,^{}文档中说

Returns s: array_like 2-D array, columns are the periodograms of successive segments.

这意味着MATLAB spectrogram返回值Pmatplotlib.mlab.specgram返回值s包含功率谱密度值,是要比较的参数。注意,您必须在mlab.specgram中使用mode=psd作为kwarg。你知道吗

所以MATLAB:

[S, F, T, P] = spectrogram(data, window, NOverlap, NFFT, Fs);

>> P(1:10, :)

ans =

   1.0e+04 *

   0.308534266716801   1.231732513971400
   0.151013695839005   0.487744349480272
   0.000300936865940   0.000301512606065
   0.000011558094657   0.000011638920865
   0.000032287898006   0.000032310876043
   0.000031582990508   0.000031591963531
   0.000026545275922   0.000026549494058
   0.000021658792166   0.000021661034379
   0.000017687762496   0.000017689063802
   0.000014586747930   0.000014587554750

mlab.specgram

>>> [s, f, t] = mlab.specgram(data, NFFT=NFFT, Fs=Fs, window=window,
                      noverlap=NOverlap, mode='psd')
>>> s[0:9]
array([[3.08534267e+03, 1.23173251e+04],
       [1.51013696e+03, 4.87744349e+03],
       [3.00936866e+00, 3.01512606e+00],
       [1.15580947e-01, 1.16389209e-01],
       [3.22878980e-01, 3.23108760e-01],
       [3.15829905e-01, 3.15919635e-01],
       [2.65452759e-01, 2.65494941e-01],
       [2.16587922e-01, 2.16610344e-01],
       [1.76877625e-01, 1.76890638e-01]])

我们是平等的。你知道吗

相关问题 更多 >