Ich bin sehr Anfänger, wenn es um Python geht. Ich habe den größten Teil meiner Programmierung in C++ gemacht. Ich habe ein Programm, das die schnelle Fourier-Transformation eines Datensatzes erzeugt und sowohl die Daten als auch die FFT in zwei Fenstern mit Matplotlib darstellt. Anstatt zu plotten, möchte ich die Daten in eine Datei ausgeben. Dies wäre eine einfache Aufgabe für mich in C++, aber ich kann das in Python nicht herausfinden. Die Frage ist also: „Wie kann ich Ausgang POWERx und Powery in eine Datendatei, in der beide Datensätze in separaten Spalten Nachfolgend finden Sie das Programm ist:Python - Ausgabe von zwei Datensätzen (Listen?) Zu Datei als zwei Spalten
import matplotlib.pyplot as plt
from fft import fft
from fft import fft_power
from numpy import array
import math
import time
# data downloaded from ftp://ftp.cmdl.noaa.gov/ccg/co2/trends/co2_mm_mlo.txt
print ' C02 Data from Mauna Loa'
data_file_name = 'co2_mm_mlo.txt'
file = open(data_file_name, 'r')
lines = file.readlines()
file.close()
print ' read', len(lines), 'lines from', data_file_name
window = False
yinput = []
xinput = []
for line in lines :
if line[0] != '#' :
try:
words = line.split()
xval = float(words[2])
yval = float(words[4])
yinput.append(yval)
xinput.append(xval)
except ValueError :
print 'bad data:',line
N = len(yinput)
log2N = math.log(N, 2)
if log2N - int(log2N) > 0.0 :
print 'Padding with zeros!'
pads = [300.0] * (pow(2, int(log2N)+1) - N)
yinput = yinput + pads
N = len(yinput)
print 'Padded : '
print len(yinput)
# Apply a window to reduce ringing from the 2^n cutoff
if window :
for iy in xrange(len(yinput)) :
yinput[iy] = yinput[iy] * (0.5 - 0.5 * math.cos(2*math.pi*iy/float(N-1)))
y = array(yinput)
x = array([ float(i) for i in xrange(len(y)) ])
Y = fft(y)
powery = fft_power(Y)
powerx = array([ float(i) for i in xrange(len(powery)) ])
Yre = [math.sqrt(Y[i].real**2+Y[i].imag**2) for i in xrange(len(Y))]
plt.subplot(2, 1, 1)
plt.plot(x, y)
ax = plt.subplot(2, 1, 2)
p1, = plt.plot(powerx, powery)
p2, = plt.plot(x, Yre)
ax.legend([p1, p2], ["Power", "Magnitude"])
plt.yscale('log')
plt.show()
Ziemlich ein bisschen Array/list Objekte hier. Welche möchten Sie in die Datei schreiben? Yinput? Xinput? y? x? Machtvoll? powerx? – Parfait
Die vier Dinge, die ich in eine Datei geschrieben haben möchte, sind die vier Dinge, die gezeichnet werden: powerx, powery, x, Yre –