MATLAB和Python之间的频谱图不同 [英] Different spectrogram between MATLAB and Python

查看:245
本文介绍了MATLAB和Python之间的频谱图不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个要移植到Python的程序.问题在于,我使用内置的spectrogram函数,尽管matplotlib specgram函数看起来相同,但同时运行这两个函数时却得到了不同的结果.

I have a program in MATLAB which I want to port to Python. The problem is that in it I use the built-in spectrogram function and, although the matplotlib specgram function seems identical, I'm getting different results when I run both.

这些是我一直在运行的代码.

These is the code I've been running.

MATLAB:

data = 1:999; %Dummy data. Just for testing.

Fs = 8000; % All the songs we'll be working on will be sampled at an 8KHz rate

tWindow = 64e-3; % The window must be long enough to get 64ms of the signal
NWindow = Fs*tWindow; % Number of elements the window must have
window = hamming(NWindow); % Window used in the spectrogram

NFFT = 512;
NOverlap = NWindow/2; % We want a 50% overlap

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

Python:

import numpy as np
from matplotlib import mlab

data = range(1,1000) #Dummy data. Just for testing

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)

这是我在两次执行中都得到的结果:

And this is the result I get in both executions:

http://i.imgur.com/QSPvYsC.png

(两个程序中的F和T变量完全相同)

(The F and T variables are exactly the same in both programs)

很明显,它们是不同的.实际上,Python执行甚至不返回复数.可能是什么问题呢?有什么办法可以解决它,还是我应该使用另一个频谱图函数?

It's obvious that they're different; in fact, the Python execution even doesn't return complex numbers. What could be the problem? Is there any way to fix it or I should use another spectrogram function?

非常感谢您的帮助.

推荐答案

matplotlib中,默认情况下specgram返回功率谱密度(mode='PSD').在MATLAB中,默认情况下spectrogram返回短时傅立叶变换,除非nargout==4,在这种情况下,它还会计算PSD.若要使matplotlib行为与MATLAB行为匹配,请设置mode='complex'

In matplotlib, specgram by default returns the power spectral density (mode='PSD'). In MATLAB, spectrogram by default returns the short-time fourier transform, unless nargout==4, in which case it also computes the PSD. To get the matplotlib behaviour to match the MATLAB behaviour, set mode='complex'

这篇关于MATLAB和Python之间的频谱图不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆