我真诚地感谢您在以下方面的帮助:
我正在尝试在双启动笔记本电脑(-Ubuntu 12.04 LTS,64 位和 MS Windows 7 64 位)上运行一些 python 代码(使用 Ipython,Python 版本 2.7.3)。在 Windows 中运行代码可以正常工作,但由于某种原因,我在 Ubuntu 中出现了内存错误。我不确定这是python问题还是操作系统问题。我会坚持在 MS windows 上运行代码,但这有它自己的问题,我正在努力解决,包括导入库等我在 Ubuntu 中运行代码时没有。
代码相当简单。作为参考,我有一个大型二进制文件,它代表实验室中 3 个通道的 16 位有符号数字。乐器。数据以 10 MS/s 采样。该文件约为 1.G GB,代表大约 25 秒的数据。在下面的代码中,我将数据读入“数据”,现在想使用 Matplotlib 的 specgram 函数绘制频谱图。这部分在 Windows 中运行良好,但在 Ubuntu 中运行良好。
一些代码...
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
file = "data_stream.srm"
with open(file,'rb') as fd:
fd.seek(0)
data = np.fromfile(fd,dtype=[('ch0','<i2'),('ch1','<i2'),('ch2','<i2')])
数据内容(原始信号值)如下所示:
array([(-1, 5, -3), (-4, 3, -4), (-6, 0, -3), ..., (-1, -2, 0),
(-2, -1, -2), (-2, -2, -1)],
dtype=[('ch0', '<i2'), ('ch1', '<i2'), ('ch2', '<i2')])
可能不是最好的方法,但它可以正常工作到这里(在 Ubuntu 和 Windows 上)。数据大小(使用 data.shape)给出(261144000,)。本来希望看到形状是 (261144000,3),因为这个大数字是每个通道的行数(因此是值的数量),其中有 3 个。例如键入:
len(data'ch0'])
返回,261144000
下一个问题是使用matplotlib的specgram函数的频谱图
specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
window=mlab.window_hanning, noverlap=128,
cmap=None, xextent=None, pad_to=None, sides='default',
scale_by_freq=None, **kwargs)
一些代码...
ADCR = 12 # ADC Resolution in bits
VOLT = 5.0 # Voltage range
SF = VOLT/(2**ADCR) # scaling factor to convert raw ADC values to volts
w_length= 512
nFFT=2 * w_length
n_overlap=np.fix(w_length/2)
p_to = 4 *w_length
cmap=plt.cm.seismic
Fs = 10E6 # Sampling Frequency
fig=plt.figure()
ax=fig.add_subplot(111)
Pxx, freqs, bins,im =
plt.specgram((data['ch1'])*SF,NFFT=nFFT,Fs=Fs,noverlap=n_overlap,pad_to=p_to,cmap=cmap)
运行最后一个命令适用于 Windows,但不适用于 Ubuntu。我收到以下消息:
内存错误...“无法发布图片,因为我需要 10 个声望才能发布”
MemoryError Traceback (most recent call last)
/media/IomegaHDD/<ipython-input-28-5144fe0c6918> in <module>()
----> 1 Pxx, freqs, bins, im = plt.specgram((data['ch1'])*SF,NFFT=nFFT,Fs=Fs,detrend=mlab.detrend_linear,noverlap=n_overlap,pad_to=p_to,scale_by_freq=True,cmap=cmap)
/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in specgram(x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, hold, **kwargs)
2609 ax.hold(hold)
2610 try:
-> 2611 ret = ax.specgram(x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, **kwargs)
2612 draw_if_interactive()
2613 finally:
/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in specgram(self, x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, **kwargs)
8146
8147 Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
-> 8148 window, noverlap, pad_to, sides, scale_by_freq)
8149
8150 Z = 10. * np.log10(Pxx)
/usr/lib/pymodules/python2.7/matplotlib/mlab.pyc in specgram(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
458
459 Pxx, freqs, t = _spectral_helper(x, x, NFFT, Fs, detrend, window,
--> 460 noverlap, pad_to, sides, scale_by_freq)
461 Pxx = Pxx.real #Needed since helper implements generically
462
/usr/lib/pymodules/python2.7/matplotlib/mlab.pyc in _spectral_helper(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
256 ind = np.arange(0, len(x) - NFFT + 1, step)
257 n = len(ind)
--> 258 Pxy = np.zeros((numFreqs, n), np.complex_)
259
260 # do the ffts of the slices
MemoryError:
正如我所说,也许这是一个我不知道的 Ubuntu 问题,因为我在 Windows 中运行代码时没有遇到这个问题。然而,这与代码的其他后续部分有其自身的问题。
您的帮助将不胜感激。
谢谢你。
问候,
将要。