This project aims to localize chickens by capturing and analyzing their vocalizations. It uses microphone arrays for sound collection, along with speakers to simulate chickens. The goal is to integrate hardware and software into a system that tracks chicken position over time and monitors health through sound. Such a system would help farmers and agricultural companies organize, track, and better manage their livestock. It is comprised of two systems, the enclosure and the program.
The Enclosure
The enclosure contains 8 microphones, with two in each wall centered. The enclosure is surrounded by a metal mesh to block high voltage AC noise in the building, it is grounded to the table the enclosure is on. Sound proofing sponges were added to the corners of the enclosure as an attempt to dampen reverberations.
The resulting signals after the addition of the faraday cage. The signal is still quite variant due to reverberations within the enclosure however the noise within the signals was significantly reduced.
The Program
The program begins by first retrieving the microphones in the order that they are located in the enclosure. The names of the microphones are retrieved as the user turns them on one by one. The number of recordings and length of recordings is then chosen, determining the amount of times the entire program will run. The microphones are then looped through and record the sound source. The delay between recordings is then removed since the microphones are recording sequentially. A delay and sum beamforming method is then applied to the signals and either mode, mean, or median data processing is applied to find the most likely location. The delay and sum first identifies the phase shift that would associate with a certain microphone location if the sound source was placed at each cell in the grid of the enclosure. Should the phase shifts match up, the amplitudes of the real signal and the estimated signal are added and become the representation of the beam power at that location in the beamforming map.
% Beamforming Simulation for Chicken Vocalization Localization (2D)
clear; clc;
warning('off', 'all');
%% === Manual Options ===
scenario = 1; % 1: 20 wall mics, 2: wall + center mic
use_wav = false; % false = use chirp; true = load .wav file
%% === Basic Parameters ===
fs = 44100; % Sampling rate (Hz)
nBits = 16; % Bit rate (bits)
nChannels = 1;
c = 13504; % Speed of sound (in/s)
room_size_x = 40; % Room size x (inches)
room_size_y = 25.5; % Room size y (inches)
f_min = 20; f_max = 4000; % Frequency range (Hz)
prenormValues = [0.50208 0.25076 0.48801 0.53677 0.42502 0.52087 0.44482 0.66245];
normCenter = 0.5;
normValues = normCenter ./ prenormValues;
% Begin extracting MOVOs
command = 'SoundVolumeView.exe /scomma "C:\Users\hybri\Downloads\devices.csv"'; % SoundVolumeView command line prompt
micStrings = {}; % create a string to store mic names based on order they are plugged in
while (length(micStrings) < 8) % loop until all 8 mics are connected
system(command); % runs SoundVolumeView.exe /scomma which shows all audio devices in PC
t = readtable('C:\Users\hybri\Downloads\devices.csv'); % read the file
% Loop through matches
for k = 1:length(t.DeviceName)
if contains(t.DeviceName{k}, 'UAC')
micName = t.DeviceName{k};
end
% Only add if unique
if ~ismember(micName, micStrings)
micStrings{end+1} = micName;
fprintf("Added mic: %s\n", micName);
end
end
end
disp(micStrings)
fixedMicStrings = [];
fixedMicStrings = "Microphone (" + micStrings + ") (Windows DirectSound)";
recorders = cell(1, numel(micStrings)); % to store objects
info = audiodevinfo();
inputs = info.input;
conMics = 0;
idList = [];
while (conMics < 8)
micNum = conMics + 1;
for i = 1:numel(inputs)
if (strcmp(fixedMicStrings(micNum), inputs(i).Name) == 1)
idList(end+1) = inputs(i).ID;
conMics = conMics + 1;
end
end
end
for k = 1:numel(micStrings)
recorders{k} = audiorecorder(fs, nBits, nChannels, idList(k));
end
% === Microphone Positions ===
wall_mics = []; % matrix of locations of microphones centered on each wall of the room
wall_mics(end+1, :) = [17.5,0];
wall_mics(end+1, :) = [22.5,0]; % ^ Bottom wall
wall_mics(end+1, :) = [40,10.25];
wall_mics(end+1, :) = [40,15.25]; % ^ Right wall
wall_mics(end+1, :) = [22.5,25.5];
wall_mics(end+1, :) = [17.5,25.5]; % ^ Top wall
wall_mics(end+1, :) = [0,15.25];
wall_mics(end+1, :) = [0,10.25]; % ^ Left wall
% wall_mics = []; % matrix of locations of microphones spread evenly accross each wall of the room
% wall_mics(end+1, :) = [13.33,0];
% wall_mics(end+1, :) = [26.66,0]; % ^ Bottom wall
% wall_mics(end+1, :) = [40,8.5];
% wall_mics(end+1, :) = [40,17]; % ^ Right wall
% wall_mics(end+1, :) = [26.66,25.5];
% wall_mics(end+1, :) = [13.33,25.5]; % ^ Top wall
% wall_mics(end+1, :) = [0,17];
% wall_mics(end+1, :) = [0,8.5]; % ^ Left wall
mic_positions = wall_mics;
num_mics = size(mic_positions,1);
num_attempts = 4; % Number of maps generated to be averaged
duration = .5; % Signal duration (seconds)
%% === Generate or Load Signal ===
numTrials = 1; % Number of total individual run-throughs of the code
trial = 0;
for trials = 1:numTrials
trial = trial + 1;
allaudio = zeros(fs*duration, 8, num_attempts);
for test = 1:num_attempts
micDelays = zeros(8,1);
tic
for k = 1:num_mics
record(recorders{k}, duration);
micDelays(k) = toc - micDelays(1);
end
pause(duration*4);
disp("Done Recording, turn that off!")
for mm = 1:num_mics
audiodata = getaudiodata(recorders{mm});
audiodata = audiodata .* normValues(mm);
if (mm >= 2)
audiodata = advanceAudio(audiodata, micDelays(mm), fs);
end
filteredaudiodata = bandpass(audiodata, [600 800], fs);
allaudio(:,mm,test) = filteredaudiodata;
end
end
grid_res = 0.1; % 1 m
xg = 0.1:grid_res:room_size_x-0.1;
yg = 0.1:grid_res:room_size_y-0.1;
all_power_maps = zeros(length(yg), length(xg), num_attempts);
for test = 1:num_attempts
for mm = 1:num_mics % loop through all mics
%filename = "TESTSOUND" + mm + ".mp3";
% audiodata = getaudiodata(recorders{mm});
% audiodata = audiodata .* normValues(mm);
% if (mm >= 2)
% audiodata = advanceAudio(audiodata, micDelays(mm), fs);
% end
% filteredaudiodata = bandpass(audiodata, [600 800], fs);
sig = allaudio(:, mm, test);
%audiowrite(filename, filteredaudiodata, fs);
sig = sig / max(abs(sig)); % normalize
sig = sig(1:fs*duration); % trim if needed
NFFT = 512;
freqs = fs/2*linspace(0, 1, NFFT/2+1);
stft_sig = stft(sig, fs,"FFTLength",NFFT,"Window",hann(NFFT),"OverlapLength",NFFT/2);
mic_sigs(:, :, mm) = stft_sig(NFFT/2:end, :);
% time_micsig(mm, :) = real(istft([conj(flipud(squeeze(mic_sigs(2:end-1, :, mm)))); squeeze(mic_sigs(:, :, mm))], fs, "FFTLength",NFFT, "Window",hann(NFFT), "OverlapLength",NFFT/2));
end
t = (0:length(sig)-1)/fs;
%% === Beamforming Grid === !!!!!!!!! Display
[Xg, Yg] = meshgrid(xg, yg);
power_map = zeros(length(yg), length(xg), length(freqs));
%% === Delay-and-Sum Beamforming (DAS) ===
m_idx = 9; % freq bins (one-sided) = NFFT/2 + 1 = 257, So 10th bin ≈ 9 * 93.75 = 843.75 Hz
% when it is needed, integrate frequency ranges of interest,
% i.e., chicken vocal range
for i = 40:(size(Xg, 1)-40)
for j = 40:(size(Xg, 2)-40)
target = [Xg(i,j), Yg(i,j)];
%sum_sig = zeros(1, length(sig));
% for m = 1:length(freqs)
rr = sqrt(sum((target - mic_positions).^2, 2));
a = exp(-1j*2*pi*freqs(m_idx)/c*rr)./rr;
w = a/(a'*a + 1e-10); %Add a tiny amount to avoid divided by 0
for tt = 1:size(mic_sigs, 2)
y = w' * squeeze(mic_sigs(m_idx, tt, :));
%power_map(i, j, m_idx) = power_map(i, j, m_idx) +abs(y)^2; %squared power
power_map(i, j, m_idx) = power_map(i, j, m_idx) + abs(y);
end
% sum_sig = sum_sig
% end
% power_map(i,j) = sum(sum_sig.^2);
end
end
%% === Estimate Source ===
power_map = squeeze(power_map(:, :, m_idx));
power_map = rescale(power_map);
all_power_maps(:,:,test) = power_map;
end
mode_power_map = mode(all_power_maps, 3);
median_power_map = median(all_power_maps, 3);
mean_power_map = mean(all_power_maps, 3);
[mode_maxVal_, mode_idy] = max(mode_power_map);
[mode_maxVal, mode_idx] = max(mode_maxVal_);
mode_est = [xg(mode_idx), yg(mode_idy(mode_idx))];
[median_maxVal_, median_idy] = max(median_power_map);
[median_maxVal, median_idx] = max(median_maxVal_);
median_est = [xg(median_idx), yg(median_idy(median_idx))];
[mean_maxVal_, mean_idy] = max(mean_power_map);
[mean_maxVal, mean_idx] = max(mean_maxVal_);
mean_est = [xg(mean_idx), yg(mean_idy(mean_idx))];
fprintf('\nScenario %d\n', scenario);
fprintf('\nTrial No. %d\n', trial);
fprintf('Estimated Source (Mode) : [%.2f, %.2f]\n', mode_est);
fprintf('Estimated Source (Median) : [%.2f, %.2f]\n', median_est);
fprintf('Estimated Source (Mean) : [%.2f, %.2f]\n', mean_est);
%fprintf('Max Beam Power : %.3f\n\n', maxVal);
%% === Visualization === !!!!!!!!! Display
modeFig = figure;
imagesc(xg, yg, mode_power_map); axis xy equal; hold on;
scatter(mic_positions(:,1), mic_positions(:,2), 60, 'k', 'filled');
plot(mode_est(1), mode_est(2), 'bo', 'LineWidth', 2, 'MarkerSize', 10);
xlabel('X (m)'); ylabel('Y (m)');
title(sprintf('Mode Beamforming Localization, Trial %d', trial));
legend('Microphones', 'Estimated Source');
colorbar;
medianFig = figure;
imagesc(xg, yg, median_power_map); axis xy equal; hold on;
scatter(mic_positions(:,1), mic_positions(:,2), 60, 'k', 'filled');
plot(median_est(1), median_est(2), 'bo', 'LineWidth', 2, 'MarkerSize', 10);
xlabel('X (m)'); ylabel('Y (m)');
title(sprintf('Median Beamforming Localization, Trial %d', trial));
legend('Microphones', 'Estimated Source');
colorbar;
meanFig = figure;
imagesc(xg, yg, mean_power_map); axis xy equal; hold on;
scatter(mic_positions(:,1), mic_positions(:,2), 60, 'k', 'filled');
plot(mean_est(1), mean_est(2), 'bo', 'LineWidth', 2, 'MarkerSize', 10);
xlabel('X (m)'); ylabel('Y (m)');
title(sprintf('Mean Beamforming Localization, Trial %d', trial));
legend('Microphones', 'Estimated Source');
colorbar;
end
%displaySignals(recorders, fs, 700);