Skip to content

Commit

Permalink
Fix rounding errors in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
odoare committed Jul 11, 2024
1 parent 66401c0 commit ca23272
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions measpy/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def _write_readme(self,filename):
f.write('\n')
f.write('https://github.com/odoare/measpy')

def sync_prepare(self,out_chan=0,added_time=1):
def sync_prepare(self,out_chan=0,added_samples=None):
"""
Prepare measurement for synchronization
Expand All @@ -478,19 +478,32 @@ def sync_prepare(self,out_chan=0,added_time=1):
:type in_chan: int
:param added_time: Duration of silence added before and after the selected output signal
:type added_time: float
"""
osig = self.out_sig[out_chan].add_silence((added_time,added_time)).delay(-added_time)
self.dur = self.dur+2*added_time
"""
if type(added_samples)==type(None):
asp = self.fs
elif type(added_samples)==int:
asp = added_samples
else:
raise Exception("added_samples: Wrong type")
osig = self.out_sig[out_chan].add_silence(extras=(asp,asp)).delay(-asp/self.fs)
self.dur = osig.dur
self.out_sig[out_chan]=osig

def sync_render(self,out_chan=0,in_chan=0,added_time=1):
def sync_render(self,out_chan=0,in_chan=0,added_samples=None):
if type(added_samples)==type(None):
asp = self.fs
elif type(added_samples)==int:
asp = added_samples
else:
raise Exception("added_samples: Wrong type")
d = self.in_sig[in_chan].timelag(self.out_sig[out_chan])
dt = 1/self.fs
ds = round(d*self.fs)
# dt = 1/self.fs
# print("delay: "+str(d)+"s")
self.dur = self.dur-2*added_time
self.out_sig[out_chan] = self.out_sig[out_chan].cut(dur=(added_time,added_time+self.dur)).delay(added_time)
self.dur = self.dur-2*asp/self.fs
self.out_sig[out_chan] = self.out_sig[out_chan].cut(pos=(asp,asp+round(self.dur*self.fs))).delay(asp/self.fs)
for i,s in enumerate(self.in_sig):
self.in_sig[i] = s.cut(dur=(added_time+d+dt,added_time+d+dt+self.dur))
self.in_sig[i] = s.cut(pos=(asp+ds+1,asp+ds+1+self.out_sig[out_chan].length))
self.in_sig[i].t0 = self.out_sig[out_chan].t0
return d

Expand Down Expand Up @@ -530,4 +543,3 @@ def sync_render(self,out_chan=0,in_chan=0,added_time=1):
# s.values = s.values[posmax:posmax+M.fs*M.dur]
# return M1
# del(M1)

0 comments on commit ca23272

Please sign in to comment.