Python - 如何在文本文件中保存频谱图输出?

时间:2017-12-16 13:45:58

标签: python-3.x numpy signal-processing spectrum spectrogram

我的代码计算xyz的频谱图。

我首先计算三轴的大小,然后计算谱图。

我需要将光谱图输出并将其保存为数组中的一列,以将其用作深度学习模型的输入。

这是我的代码:

dataset = np.loadtxt("trainingdatasetMAG.txt", delimiter=",")
X = dataset[:,0:6]
Y = dataset[:,6]
fake_size = 1415684
time = np.arange(fake_size)/1000 # 1kHz
base_freq = 2 * np.pi * 100
magnitude = dataset[:,5]
plt.title('xyz_magnitude')
ls=(plt.specgram(magnitude, Fs=1000))

这是我的数据集,其标题为(patientno, time/Msecond, x-axis, y-axis, z-axis, xyz_magnitude, label)

1,15,70,39,-970,947321,0
1,31,70,39,-970,947321,0
1,46,60,49,-960,927601,0
1,62,60,49,-960,927601,0
1,78,50,39,-960,925621,0
1,93,50,39,-960,925621,0

这是频谱图的输出,需要更高效

(array([[  1.52494154e+11,   1.52811638e+11,   1.52565040e+11, ...,
          1.47778892e+11,   1.46781213e+11,   1.46678951e+11],
       [  7.69589176e+10,   7.73638333e+10,   7.76935891e+10, ...,
          7.48498747e+10,   7.40088248e+10,   7.40343108e+10],
       [  6.32683585e+04,   1.58170271e+06,   6.11287648e+06, ...,
          5.06690834e+05,   3.31360693e+05,   7.04757400e+05],
       ..., 
       [  7.79589127e+05,   8.09843763e+04,   2.52907491e+05, ...,
          2.48520301e+05,   2.11734697e+05,   2.50917758e+05],
       [  9.41199946e+05,   4.98371406e+05,   1.29328139e+06, ...,
          2.56729806e+05,   3.45253951e+05,   3.51932417e+05],
       [  4.36846676e+05,   1.24123764e+06,   9.20694394e+05, ...,
          8.35807658e+04,   8.36986905e+05,   3.57807267e+04]]),
 array([   0.     ,    3.90625,    7.8125 ,   11.71875,   15.625  ,
         19.53125,   23.4375 ,   27.34375,   31.25   ,   35.15625,
         39.0625 ,   42.96875,   46.875  ,   50.78125,   54.6875 ,
         58.59375,   62.5    ,   66.40625,   70.3125 ,   74.21875,
         78.125  ,   82.03125,   85.9375 ,   89.84375,   93.75   ,
         97.65625,  101.5625 ,  105.46875,  109.375  ,  113.28125,
        117.1875 ,  121.09375,  125.     ,  128.90625,  132.8125 ,
        136.71875,  140.625  ,  144.53125,  148.4375 ,  152.34375,
        156.25   ,  160.15625,  164.0625 ,  167.96875,  171.875  ,
        175.78125,  179.6875 ,  183.59375,  187.5    ,  191.40625,
        195.3125 ,  199.21875,  203.125  ,  207.03125,  210.9375 ,
        214.84375,  218.75   ,  222.65625,  226.5625 ,  230.46875,
        234.375  ,  238.28125,  242.1875 ,  246.09375,  250.     ,
        253.90625,  257.8125 ,  261.71875,  265.625  ,  269.53125,
        273.4375 ,  277.34375,  281.25   ,  285.15625,  289.0625 ,
        292.96875,  296.875  ,  300.78125,  304.6875 ,  308.59375,
        312.5    ,  316.40625,  320.3125 ,  324.21875,  328.125  ,
        332.03125,  335.9375 ,  339.84375,  343.75   ,  347.65625,
        351.5625 ,  355.46875,  359.375  ,  363.28125,  367.1875 ,
        371.09375,  375.     ,  378.90625,  382.8125 ,  386.71875,
        390.625  ,  394.53125,  398.4375 ,  402.34375,  406.25   ,
        410.15625,  414.0625 ,  417.96875,  421.875  ,  425.78125,
        429.6875 ,  433.59375,  437.5    ,  441.40625,  445.3125 ,
        449.21875,  453.125  ,  457.03125,  460.9375 ,  464.84375,
        468.75   ,  472.65625,  476.5625 ,  480.46875,  484.375  ,
        488.28125,  492.1875 ,  496.09375,  500.     ]),
  array([1.28000000e-01,   2.56000000e-01,   3.84000000e-01, ...,
         1.41529600e+03,   1.41542400e+03,   1.41555200e+03]),
  <matplotlib.image.AxesImage object at 0x000002161A78F898>) 

1 个答案:

答案 0 :(得分:0)

Matplotlib函数specgram有4个输出:

  

频谱:二维数组

     

列是连续段的周期图。

     

freqs :1-D数组

     

与频谱中的行对应的频率。

     

t :1-D数组

     

对应于段中点的时间(即谱中的列)。

     

im :AxesImage类的实例

从你的代码:

ls=plt.specgram(magnitude, Fs=1000)

因此ls[0]包含您要在txt中导出的频谱,您可以使用以下代码将其写入文件中:

with open('spectrogram.txt', 'w+b') as ffile:
    for spectros in ls[0]:
        for spectro in spectros:
            lline = str(spectro) + ' \t'
            ffile.write(lline)
        # one row written 
        ffile.write(' \n') 

PLT。 但是之前,ls[0]包含NFFT=256段的功率谱密度,默认情况下包含128个重叠样本,因此您将拥有NFFT/2 +1 = 129 rows。因此,每列在时间T包含PSD,每行包含相关频率的时间序列。要在即时T处对FFT进行切片:

T_idx = 10
psd_ls[:,T_idx]