Page 1 of 1

Subtracting values from the same list

Posted: Tue Apr 11, 2017 5:44 pm
by mccoy
Hi, I have what I feel like is a pretty easy problem but my I'm not knowledgeable enough about Python to figure it out.
I was given a script that takes time.time before and after a GPIO output command to test the jitter of the commands. It stored the data in a text file like:
  • tp0
    1491592477.284164
    1491592477.284201
    1491592477.284322
    1491592477.294415
    tp1
    1491592482.30128
    1491592482.301307
    1491592482.301389
    1491592482.311484
This repeats to 300 trials. I need to take to subtract the second time by the first and the fourth from the third. So
  • tp0
    1491592477.284164 (Subtract this)
    1491592477.284201 (From this)
    1491592477.284322 (and this one)
    1491592477.294415 (from this one)

Code: Select all

infile = "/Users/Caghain/Desktop/LAB CODE/TIME/GPIO_times_Edit.txt"
editfile = "/Users/Caghain/Desktop/LAB CODE/TIME/GPIO_times_Subtract.txt"

fin = open(infile, "w+")
fedit = open(editfile, "w+")

for x in fin:
    line_TTL=[x+2]
    line_SNDOUT=[x+3]
    GPIO_HIGH = line_SNDOUT - line_TTL
    fedit.write(GPIO_HIGH)

fedit.close() 
The above doesn't work, but that was my attempt. Thanks for any help.

Re: Subtracting values from the same list

Posted: Tue Apr 11, 2017 6:02 pm
by ghp
Hello, in pseudocode I would write:

Code: Select all

open fin
open fout
try
    while True:
        a = fin readline
        b = fin readline
        fout write ( float(a) - float(b) )
except:
    pass
finally
    close fin
    close fout
Hope this helps,
Gerhard

Re: Subtracting values from the same list

Posted: Tue Apr 11, 2017 10:15 pm
by pcmanbob
Hi.

so not having your original file to test against created a test file from your posted results

Code: Select all

tp0
477.284164
477.284201
477.284322
477.294415
tp1
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp2
477.284164
477.284201
477.284322
477.294415
tp3
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp4
477.284164
477.284201
477.284322
477.294415
I hope this is the correct layout.

so this is the code I came up with

Code: Select all

#!/usr/bin/python
infile = "/home/pi/edit.txt"
outfile = "/home/pi/answer.txt"
count = sum(1 for line in open(infile,"r"))

result = open(infile,"r")
answer = open(outfile,"a")
while count > 0:
	
	valuea = (result.readline())
	if len(valuea) <= 5:
		print valuea
		answer.write (str(valuea))
		count = count - 1
	else:
		valuea = float(valuea)
		print "a", valuea
		valueb = float(result.readline())
		print "b", valueb
		valuec = valueb - valuea
		print "result", valuec
		answer.write (str(valuec) + "\n")
		count = count - 2
		
result.close
answer.close
and these were the results in the answer file

Code: Select all

tp0
3.70000000203e-05
0.010093
tp1
2.69412994385e-05
0.0100951194763
tp2
3.70000000203e-05
0.010093
tp3
2.69412994385e-05
0.0100951194763
tp4
3.70000000203e-05
0.010093
now you will have to change the infile and outfile references to match you original posted values
so hope it works and does as you wish, you can remove all the print statements if you wish, please check I have the math round the right way.
if it does not work as expected please let me know with any error messages.

Re: Subtracting values from the same list

Posted: Thu Apr 13, 2017 2:27 pm
by mccoy
ghp wrote:Hello, in pseudocode I would write:

Code: Select all

open fin
open fout
try
    while True:
        a = fin readline
        b = fin readline
        fout write ( float(a) - float(b) )
except:
    pass
finally
    close fin
    close fout
Hope this helps,
Gerhard
Thanks!

Re: Subtracting values from the same list

Posted: Thu Apr 13, 2017 2:27 pm
by mccoy
pcmanbob wrote:Hi.

so not having your original file to test against created a test file from your posted results

Code: Select all

tp0
477.284164
477.284201
477.284322
477.294415
tp1
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp2
477.284164
477.284201
477.284322
477.294415
tp3
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp4
477.284164
477.284201
477.284322
477.294415
I hope this is the correct layout.

so this is the code I came up with

Code: Select all

#!/usr/bin/python
infile = "/home/pi/edit.txt"
outfile = "/home/pi/answer.txt"
count = sum(1 for line in open(infile,"r"))

result = open(infile,"r")
answer = open(outfile,"a")
while count > 0:
	
	valuea = (result.readline())
	if len(valuea) <= 5:
		print valuea
		answer.write (str(valuea))
		count = count - 1
	else:
		valuea = float(valuea)
		print "a", valuea
		valueb = float(result.readline())
		print "b", valueb
		valuec = valueb - valuea
		print "result", valuec
		answer.write (str(valuec) + "\n")
		count = count - 2
		
result.close
answer.close
and these were the results in the answer file

Code: Select all

tp0
3.70000000203e-05
0.010093
tp1
2.69412994385e-05
0.0100951194763
tp2
3.70000000203e-05
0.010093
tp3
2.69412994385e-05
0.0100951194763
tp4
3.70000000203e-05
0.010093
now you will have to change the infile and outfile references to match you original posted values
so hope it works and does as you wish, you can remove all the print statements if you wish, please check I have the math round the right way.
if it does not work as expected please let me know with any error messages.
Perfect! Thanks, this got me definitely in the right direction.