- Using "delayMicroseconds()" instead of "delay()" when a delay smaller than 1 milisecond is needed.
- "fopen()" should not be used for "serialport" objects.
- Minimize noise and reflections: Noise and reflections can affect the accuracy of TDOA-based sound source localization. Try to minimize background noise and avoid surfaces that can cause reflections or echoes. This will help improve the accuracy of the measurements.
I am not able to get the desired angle of the sound source using microphone sensors connected to arduino where the data is sent serial to matlab
5 次查看(过去 30 天)
显示 更早的评论
So I have written the code to determine the direction of sound source using two microphone sensors connected to arduino where the data is sent serially to matlab. So the problem is when I set the delay of 0.57 I am getting the sampling frequency to be 1754 so I am able to play the sound recored butwhen I sample with this frequency and I place my sound source at 90 I am getting 90 and when I place it at zero I am getting zero but anything between 0 and 90 I am getting 30 I don't know what is the problem. So when I increase by sampling frequency the recored sound will be played at a faster rate and the angles will decrease so can you guys help me hoe to overcome this problem. I am using the method called TDOA
Code for arduino
const int microphonePin1 = A0;
const int microphonePin2 = A1;
void setup() {
pinMode(microphonePin1, INPUT);
pinMode(microphonePin2, INPUT);
Serial.begin(250000);
}
void loop() {
// Read the analog value from the microphone
int microphoneValue = analogRead(microphonePin1);
int microphoneValue1 = analogRead(microphonePin2);
// Send the microphone value over Serial
Serial.println(microphoneValue);
Serial.println(microphoneValue1);
delay(0.57);
}
Code for matlab
% Function to call the arduino
arduinoPort = "COM3";
s = serialport(arduinoPort,250000);
% We are opening the the data present in arduino
fopen(s);
% Parameters for real-time data acquisition
Fs=1754;
% Note we know that fs= num_samples/duration
num_samples =80000; % No of samples
Data = zeros(num_samples, 1); % Creating an empty array to store the voltage data coming from arduino
tic
% tic and toc is used to calculate the time taken to run the program
% Read voltage data in real-time and store it in Data
for i = 1:num_samples
Data(i) = str2double(readline(s)); % Read data from Arduino
end
toc
% Since we are using two microphones the data coming from arduino is a an
% array in order to split we use a reshape function two get a row of two
% data
z=reshape(Data,[2,length(Data)/2]);
signal1=z(1,:); % Signal1 is from mic1
signal2=z(2,:); % Signal2 is from mic2
% Close the serial port connection
clear s;
% Scale the voltage data to the audio range (-1 to 1)
scaledData = (signal1 - min(signal1)) / (max(signal1) - min(signal1)) * 2 - 1; % for microphone 1
scaledData1 = (signal2 - min(signal2)) / (max(signal2) - min(signal2)) * 2 - 1; % for microphone 2
figure(1);
plot(scaledData)
figure(2);
plot(scaledData1)
% Once we get the data we store it a .wav file
audiowrite('sab3.wav',scaledData,Fs);
audiowrite('sab4.wav',scaledData1,Fs);
[y,Fs1]=audioread('sab3.wav');
[l,Fs]=audioread('sab4.wav');
% % Play the audio
% sound(y, Fs);
% sound(l,Fs);
% Get delays
delay1 = getDelay(y,l,Fs); % Writing the function to call delay
tdoa=delay1;
% Define the microphone spacing and sound speed
micSpacing = 0.4; % Distance between the microphones in meters
soundSpeed = 348; % Speed of sound in meters per second
% Calculate the angle of arrival (AoA) based on the TDOA
angleRad = asin(tdoa * soundSpeed / micSpacing);
angleDeg = rad2deg(angleRad);
disp(angleDeg)
% Function to calculate the delay
function [delayTime,delaySamples] = getDelay(y,l,fs)
len = length(y);
y = y(1:len);
l = l(1:len);
[r,lags] = xcorr(y,l); % get the cross-correlation and view the lags
figure
stem(lags,r)
xlabel('Lags [samples]')
ylabel('Cross-correlation')
[~,maxlags] = max(abs(r)); % position of maximum lag
delaySamples =lags(maxlags);% number of samples difference
delayTime = delaySamples/fs; % convert to time
fprintf('\nSignal 2 lags behind Signal 1 by ~%.1f microseconds\n',1e6*abs(delayTime))
end
0 个评论
回答(1 个)
Brahmadev
2023-9-11
Hi Abhilash,
I understand that you would like to read data from the serial port using MATLAB and calculate the angle of arrival from two microphones.
The implementation of AoA seems mathematically correct. Since you mentioned that you can hear the correct audio in both cases, here are a few things to consider:
Hope this helps!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Code Generation and Deployment 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!