hi everyone,

I am having issues with an assignment for university. I know that you are not here to do my work for me but I could really use a little help.

My assignment involves a parse file that the uni tutors have written for us and we have to add to it to create a program that creates graphs of the data in the file that the parse file program reads.

I am stuck on one of them, what I want to do is access the voltages in the parse file (it has put the data into 3 arrays, one is a time, another a voltage and the third the numData) and if the voltage is over a certain point create another array that records the voltage as 1 and if its under records it as a 0. that way I can show when for each time step the voltage is over a certain point.

I have been trying to write this for 2 days.... I just cant figure it out.
Please help!

Recommended Answers

All 3 Replies

Okay, so post your code and tell us what the errors that you're getting are. What are you stuck on?

ok this is the code they wrote for us:

from __future__ import division
from pylab import *
from numpy import *

# This function reads in a file containing space-separated pairs of
# values (one pair of values per line) and parses it.
#
# To use this function, have the file with the data in the same folder
# as this program. Use this function as follows:
#
# (xs, ys, n) = parseFile()
#
# The function will then create an input prompt in the Python command window:
#
# >>> Please enter the filename containing the data:
#
# Then type the name of the data file.
# The function will then read the data in that file (note that if the
# filename given is invalid, the function will print an error message
# and ask for the file name again).
#
# This function will return the first column of numbers as xs, the second
# column as ys and the number of values in each array as n.
def parseFile() :

while True :
filename = raw_input("Please enter the filename containing the data: ")

try :
f = open(filename, 'r')
except IOError :
print "Invalid filename"
continue

break

# Create empty lists to store the parsed data
xs = []
ys = []

# For each separate line (splitlines takes into account all
# possible file endings).
for rec in f.read().splitlines() :

# Attempt to read another two space-separated values, silently
# ignore otherwise.
try :
(x,y) = rec.strip().split()
except ValueError :
continue

# Try to convert the two values to floating-point numbers,
# silently ignore otherwise.
try :
x = float(x)
y = float(y)
except ValueError :
continue

# Append the two values to the appropriate lists.
xs.append(x)
ys.append(y)

# Finished reading the file
f.close()

# Convert the lists to arrays
xs = array(xs)
ys = array(ys)

print size(xs), "datapoints read from", filename

# Return the array of x values, the array of y values and the number of values.
return (xs, ys, size(xs))


and this is what i have written so far:

# Setting up our initial parameters.

# Read in the data to analyse
(times, voltages, numData) = parseFile()

# Enter threshold
threshold = input('Enter threshold voltage: ')

# Enter bin size
bin_size = input('Enter bin size for times ')

# plot graph D1

plot(times, voltages)
title('Data')
xlabel('Times (ns)')
ylabel('Voltage')
show()

this is the part I am struggling to get to work. I want to use the voltages array and if its above the threshold voltage entered then I want it to have a value of 1 and if its below the threshold I want it to have a value of 0. that way when i plot the times v the 1 or 0 I can see spikes where it has gone over the threshold voltage>

# plot D2

A = array(voltages)
B = A => threshold
print B

(this is just giving me a list of true or false for each value)

Please use code tags when posting code in this forum like so:
[code=python] # Code goes inside these tags

[/code]

As far as your "plot D2" code, that should be giving you a syntax error, since => is not valid in Python. What exactly is that line supposed to do? As I read it: B = A => threshold means you want to evaluate A >= threshold (if the value of A is greater than or equal to the value of threshold ) and then store it in B , which would naturally return either True or False.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.