Search code examples
pythonnumpybinaryseekfromfile

Wrong Signal Amplitude reading .bin-File (seems randomly wrong?)


I got a problem I don't really understand.

I have a .bin-File with a timeseries of signals and noise in it. I have the exact timedata, to cut out only the interesting parts.

My problem is that sometimes the amplitudes are way to high, and sometimes they are like expected. I think I broke down the problem to the following:

sampling_rate = 2e6  
dt = np.dtype(np.int32) 
# get Timedata
start_raw_L1 = 261.2    # good_signal
count_raw_L1 = 1.315

# start_raw_L1 = 261.4  bad_signal
# count_raw_L1 = 1.315


start_L1 = np.int64(start_raw_L1*sampling_rate*4)
count_L1 = np.int64(count_raw_L1 * sampling_rate)

# L1
bin_data = open(bin_file, "rb")
bin_data.seek(start_L1, os.SEEK_SET)
data_L1 = np.fromfile(bin_data, dtype=dt, count=count_L1, sep='')
bin_data.close()

# Plot
plt.plot(data_L1)

So it looks like that it matters a lot which time I choose? If I just change the start time a little bit, the signal changes in the amplitude height, I dont get why? Maybe someone can help me out.

Thanks a lot! Best regards Bastian

good_signal bad_signal


Solution

  • int(261.4*2e6*4) gives 2091199999. That is not a multiple of 4. The problem is that 261.4*2e6 gives 522799999.99999994, not 522800000 as you might have expected.

    Move the multiplication by 4 outside of the conversion to an integer: 4*int(261.4*2e6) gives 2091199996. You might prefer 4*round(261.4*2e6), which gives 2091200000. In your code, that means using, say,

    start_L1 = 4*np.int64(start_raw_L1*sampling_rate)