Reputation: 602
I have a binary file of int_16's. They are sorted into an nx2 array where each column contains samples for a sinusoid.
I am checking each consecutive value to see if there is a magnitude difference between the two greater than some value. Here is some code that is performing the task. What is a better way of doing this? I am using Python 2.7
import numpy as np
DIFF_MAX = 100
errcnt = 0
f = open("samp.bin","rb")
a = np.fromfile(f, dtype=np.int16)
n = np.reshape(a, (-1,2))
for i in range(0,len(a)/2 - 2):
if(abs(abs(n[i,0])-abs(n[i+1,0])) > DIFF_MAX):
errcnt = errcnt + 1
print "ERROR!!!"
if(abs(abs(n[i,1])-abs(n[i+1,1])) > DIFF_MAX):
errcnt = errcnt + 1
print "ERROR!!!"
print str(errcnt) + " errors found out of " + str(len(a)/2)
Doing this I imagine will be inherently slow I was just curious if there was a better way. Thanks.
Runtime,
time python test.py
0 errors found out of 4329472
real 0m21.025s
user 0m20.950s
sys 0m0.075s
Upvotes: 1
Views: 38
Reputation: 715
You could do this by shifting the arrays by one position relative to each other and subtracting them.
abs(abs(n[1::,:]) - abs(n[:-1,:])) > DIFF_MAX
The resulting boolean array would indicate the positions with TRUE
wherever the difference between two consecutive values along the first dimension is greater then DIFF_MAX
and FALSE
elsewhere.
Upvotes: 1