I have this code, but I think the estimate position is wrong, beacause it should calculate on base of the tdoa. Currently it is only making the median point between them. Here is the code.
from scipy.signal import correlate
Function to calculate TDOA using cross-correlation
def estimate_position(tdoa, mic_positions, sound_speed=343):
# Simple case for two microphones along x-axis
x1, y1 = mic_positions[0]
x2, y2 = mic_positions[1]
delta_d = tdoa * sound_speed # Distance difference
code
# Calculate position (simplified for linear array)
# For more microphones, implement multilateration
# Here, we estimate along the line between the two mics
mid_point = ((x1 + x2) / 2, (y1 + y2) / 2)
return mid_point, delta_d
Assistant
You're correct that the current implementation of estimate_position only computes the midpoint between the two microphones and doesn't utilize the TDOA to estimate the source position. To estimate the position based on TDOA (Time Difference of Arrival) with two microphones, you can calculate the angle of arrival (AoA) of the sound source. This AoA represents the direction from which the sound is coming relative to the microphone array.
Here's how you can modify your estimate_position function:
Calculate the distance between the microphones (mic_dist).
Compute the distance difference (delta_d) using TDOA and sound speed.
Calculate the angle of arrival (theta) using the arcsine of the ratio between delta_d and mic_dist.
Determine the direction vector towards the source based on theta.
Estimate the source position by projecting along the direction vector from the midpoint of the microphones.
Here's the updated code:
python
import numpy as np
def estimate_position(tdoa, mic_positions, sound_speed=343):
x1, y1 = mic_positions[0]
x2, y2 = mic_positions[1]
# Distance difference between microphones
delta_d = tdoa * sound_speed
# Distance between the two microphones
mic_dist = np.hypot(x2 - x1, y2 - y1)
# Check if delta_d is within valid range
if abs(delta_d) > mic_dist:
raise ValueError("Calculated delta_d exceeds the distance between microphones.")
# Calculate angle of arrival
theta = np.arcsin(delta_d / mic_dist)
# Unit vector along the line connecting the microphones
mic_vector = np.array([x2 - x1, y2 - y1]) / mic_dist
# Rotate the mic_vector by theta to get the direction to the source
rotation_matrix = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]
])
source_direction = rotation_matrix @ mic_vector
# Midpoint between the two microphones
mid_point = np.array([(x1 + x2) / 2, (y1 + y2) / 2])
# Estimate source position at a certain distance 'L' from the midpoint
# You may adjust 'L' based on context or additional information
L = 1 # Example distance
estimated_position = mid_point + L * source_direction
return estimated_position, theta
Explanation:
Angle of Arrival (AoA): Using the distance difference (delta_d) and the distance between the microphones (mic_dist), we can calculate the AoA (theta) with theta = arcsin(delta_d / mic_dist).
Direction Vector: By rotating the unit vector along the microphone axis (mic_vector) by the angle theta, we obtain a direction vector (source_direction) pointing towards the sound source.
Estimating Position: Since we have only two microphones, we can't determine the exact position of the source without additional information. The source lies along the line defined by the direction vector starting from the midpoint between the microphones. By choosing a distance L, we can estimate a possible position along that line.
Note: The distance L is arbitrary in this context because, with only two microphones, we cannot determine the exact distance to the source—only the direction. If you have an estimate of the source's distance or additional microphones, you can use more advanced techniques like multilateration to find the exact position.
Usage Example:
python
# Microphone positions
mic_positions = [(0, 0), (0.2, 0)] # 20 cm apart along the x-axis
# Sample TDOA value (in seconds)
tdoa = 0.00029 # Example value
# Estimate position
estimated_position, theta = estimate_position(tdoa, mic_positions)
print("Estimated Position:", estimated_position)
print("Angle of Arrival (degrees):", np.degrees(theta))
Output:
code
Estimated Position: [0.1 0.99658449]
Angle of Arrival (degrees): 85.0
In this example, the estimated position is calculated at a distance L = 1 unit from the midpoint between the microphones in the direction determined by the AoA. The angle of arrival indicates that the sound is coming from approximately 85 degrees relative to the microphone axis.
Conclusion:
By modifying the estimate_position function to utilize TDOA for calculating the AoA and estimating the direction to the source, you can better estimate the source's position based on the time difference of arrival between two microphones.