簡體   English   中英

Python-如何將頻譜圖輸出保存在文本文件中?

[英]Python - How to save spectrogram output in a text file?

我的代碼計算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>) 

Matplotlib函數規范具有4個輸出:

光譜 :二維陣列

列是連續段的周期圖。

頻率 :一維數組

對應於頻譜中各行的頻率。

t :一維數組

與段的中點相對應的時間(即頻譜中的列)。

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') 

但是,在以前, ls[0]包含NFFT=256段的功率譜密度,默認情況下-具有128個重疊采樣,因此您將擁有NFFT/2 +1 = 129 rows 因此,每列包含時間T的PSD ,每行包含相關頻率的時間序列。 要在瞬時T切片上進行FFT,請執行以下操作:

T_idx = 10
psd_ls[:,T_idx]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM