gpt4 book ai didi

python - 从 numpy 数组中删除变量

转载 作者:行者123 更新时间:2023-11-28 18:17:23 25 4
gpt4 key购买 nike

我有一个代码可以创建 this plot ,但我不知道如何删除“运河 1”中的第一个数据 - 红线,以及“运河 3”中的最后一个数据 - 蓝线,那些垂直线。两条运河都有266336条记录,请问有帮助吗?红色竖线是第一条记录,蓝色竖线是最后一条。

import iodata as io
import matplotlib.pyplot as plt
import numpy as np
import time

testInstance = io.InputConverter()
start = time.time()
conversionError = io.ConversionError()
#f = testInstance.convert(r"S:\Python\", 1", conversionError)
f = testInstance.convert(r"/Users/Hugo/20160401", "201604010000",
conversionError)
end = time.time()
print("time elapsed " + str(end - start))

if(conversionError.conversionSucces):
print("Conversion succesful")
if(conversionError.conversionSucces == False):
print("Conversion failed: " + conversionError.conversionErrorLog)
print "Done!"

# Create a new subplot for two canals 1 & 3
a = np.amin(f.f)
filename = 'C:/Users/Hugo/20160401/201604010000.dat'
d = open(filename,'rb')
t = u"\u00b0"
headersize = 64
header = d.read(headersize)
ax1 = plt.subplot(211)
ax1.set_title(header[:16] + ', ' + # station name
'Canals: '+header[32:33]+' and '+header[34:35]+ ', ' # canals
+'Temp'+header[38:43]+t+'C' # temperature
+', '+'Time:'+header[26:32]+', '+'Date'+' '+header[16:26]) # date

plt.ylabel('Pico Tesle [pT]')
plt.xlabel('Time [ms]')
plt.plot(f.f[0,], label='Canal 1', color='r', linewidth=0.75, linestyle="-")
plt.plot(f.f[1,], label='Canal 3', color='b', linewidth=0.75, linestyle="-")
plt.legend(loc='upper right', frameon=False)
plt.grid()
# Create a new subplot for FFT
plt.subplot(212)
plt.title('Fast Fourier Transform')
plt.ylabel('Power [a.u.]')
plt.xlabel('Frequency Hz')
FFTdata = np.sqrt(f.f[0,]*f.f[0,]+f.f[1,]*f.f[1,])**1
samples = FFTdata.size
duration = 300 # in seconds
Fs = float(samples)/duration # sampling frequency (sample/sec)
delta_t = 1.0/Fs
t = np.arange(0, samples, 1)*delta_t
FFTdata_freq = np.abs(np.fft.rfft(FFTdata))**2
freq = np.fft.rfftfreq(samples, d=delta_t)

# Printing data
plt.semilogy(freq, FFTdata_freq)
plt.grid()
#plt.savefig('S:/Hugo/'+"201604010000"+'.png', bbox_inches =
'tight')
plt.show()

f.f 的内容:

>>> print f.f[0,]
[ -59.57011259 -74.20675537 -90.53224156 ..., -1676.9703173 -1676.9703173 -1676.9703173 ]

>>> print f.f[1,]
[ 1.48413511e+00 4.96417605e+00 8.39303992e+00 ..., -1.67697032e+03 -1.67697032e+03 -1.67697032e+03]

数据代码:

import struct
import numpy as np

class ConversionError:
def __init__(self):
self.conversionSucces = True
self.conversionErrorLog = "Correct"

def reportFailure(self, errorlog):
self.conversionSucces = False
self.conversionErrorLog = errorlog

class DataStruct:
def __init__(self,f,initTime,headerString):
self.f = f
self.initTime = initTime
self.headerString = headerString

class InputConverter:
def __init__(self):
self.midAdc = 65536/2
self.convFactor = 19.54

def convert(self,filePath,fileName,conversionLog):
try:
d_file = open(filePath + "/" + fileName + ".dat", mode='rb')
except IOError as e:
conversionLog.reportFailure(e.strerror)

file = d_file.read()
datalen = len(file)
headerString = file[:43]
initime, = struct.unpack('>H', file[48:50])
expectedMatrixWidth = (datalen - 72)/4
outputMatrix = np.zeros((2, expectedMatrixWidth))
index = 0;
print "Processing..."
for i in range(64, datalen-8, 4):
e1, e2 = struct.unpack('>HH',file[i:i+4])
outputMatrix[0, index] = (e1 - self.midAdc)/self.convFactor
outputMatrix[1, index] = (e2 - self.midAdc)/self.convFactor
index += 1


return DataStruct(outputMatrix,initime,headerString)

最佳答案

您可以尝试使用数组切片:

plt.plot(f.f[0,][1:], label='Canal 1', color='r', linewidth=0.75, linestyle="-")
plt.plot(f.f[1,][:-1], label='Canal 3', color='b', linewidth=0.75, linestyle="-")

编辑:

由于数据的性质,正如@Dascienz 在评论中建议的那样,切掉的不仅仅是第一个/最后一个数据点是合适的。像这样,从两个系列中切出第一个和最后 50 个数据点:

plt.plot(f.f[0,][50:-50], label='Canal 1', color='r', linewidth=0.75, linestyle="-")
plt.plot(f.f[1,][50:-50], label='Canal 3', color='b', linewidth=0.75, linestyle="-")

关于python - 从 numpy 数组中删除变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47489580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com