I have 'n' microphones placed in square fashion and I want to make sure that all the channels must be aligned in time exactly the same if the signal is equidistant from all the 'n' microphones i.e. in the center of the square.
I have written below script to do the difference in zero crossing timings and if the difference is above some precision then print that and bail out.
from scipy.io import wavfile
import numpy as np
import argparse
parser = argparse.ArgumentParser(description='diff the zero crossing of two files')
parser.add_argument('-f1', '--file_name_1', help='provide first file name')
parser.add_argument('-f2', '--file_name_2', help='provide second file name')
parser.add_argument('-p', '--precision', help='precision to compare against', type=float, default=0.0001)
args = parser.parse_args()
print(args)
files = []
files.append(args.file_name_1)
files.append(args.file_name_2)
sampling_rates = []
signals = []
for file in files:
fs, signal = wavfile.read(file)
signal = signal / max(abs(signal)) # scale signal
sampling_rates.append(fs)
signals.append(signal)
assert min(signal) >= -1 and max(signal) <= 1
print 'fs ==> ', fs, 'Hz' # sampling rate
print 'len(signal) ==> ', len(signal), 'samples'
sampsPerMilli = 0
#files should have same sampling rates and length
prev_rate = sampling_rates[0]
for rate in sampling_rates[1:]:
cur_rate = rate
sampsPerMilli = int(rate / 1000)
if prev_rate != cur_rate:
print("rates doesn't match %d %d"% (prev_rate, cur_rate))
exit(0)
cur_rate = rate
#signal length also should be same
prev_length = len(signals[0])
for signal in signals[1:]:
cur_length = len(signal)
if prev_length != cur_length:
print("length of signals doesn't match for %d %d"% (prev_length, cur_length))
exit(0)
cur_length = prev_length
zccs = []
for signal in signals:
zcc = []
DC = np.mean(signal)
newSignal = signal - DC
for i in range(1, int(len(newSignal))):
if((newSignal[i] * newSignal[i-1]) < 0):
#print("crossing at %f seconds"% ((i/sampsPerMilli) * 0.001))
zcc.append((i/sampsPerMilli) * 0.001)
zccs.append(zcc)
for a, b in zip(zccs, zccs[1:]):
if len(a) != len(b):
print("length doesn't match %d %d"% (len(a), len(b)))
for c, d in zip(a, b):
if c-d > args.precision:
print("precision %f c %f d %f exceeded"% (args.precision, c, d))
exit(0)
Is there any better approach or can this script be improved?