Question: 3 . 3 Automating the estimates Once we have a process for doing the estimation we would like to automate it . For this part

3.3 Automating the estimates
Once we have a process for doing the estimation we would like to automate it. For this part generate signals for tin[0,5.0].
(a) Now we would like to automate the process of finding the delays. Use the max function to find the
peak in the autocorrelation and convert the corresponding lag into an estimate of the relative time
shift. Write a MATLAB function lab1est (A, B, y1, y2) that outputs both the estimated angle and
L given the model parameters and the received signals.
In this function, replace (\tau _(1)-\tau _(2))(c_(*))/(A) with .
(b) Using evenly spaced values of L from 20 meters to L=120 meters in increments of 5 meter, write
a loop that generates signals from a speaker at location L using lab1sim() and then estimates L
using lab1est (). Plot the relative error between the true Lhat(L) :
\epsi _(rel )=1-((hat(L)))/(L)
Plot |\epsi _(rel )| as a function of L. Use axis to make the vertical axis go up to 0.5. You should see the
relative error is no more than 5%. Here is my code so far but it doesn't work and is very scrambled. Parameters A =0.025; % Vertical distance between microphones (in meters) B =200; % Horizontal distance from microphones to sound source (in meters) fs =1e6; % Sampling frequency (Hz)%% Signal function s(t) sig = @(t)100* cos(2* pi *5000* t).*(heaviside(t)- heaviside(t -1e-3)); %% Initialize storage for L estimates L_values =20:5:120; % True distances from 20m to 120m in increments of 5m estimated_L_values = zeros(size(L_values)); % Preallocate for estimated L %% Loop through each true L value to estimate L_hat for i =1:length(L_values) L_true = L_values(i); % Current true L value % Generate the signals using lab1sim [y1sig, y2sig]= lab1sim(A, B, L_true, sig); % Generate time vector for simulation t =0:1/fs:5e-3; %5 ms of data % Compute y1(t) and y2(t) y1_t = y1sig(t); y2_t = y2sig(t); % Ensure y1_t and y2_t are column vectors for xcorr compatibility y1_t = y1_t(:); y2_t = y2_t(:); % Estimate angle and L using Lab1est function [~, L_est]= Lab1est(A, B, y1_t, y2_t, fs); % Capture the estimated L % Store the estimated L estimated_L_values(i)= L_est; end
figure; plot(L_values, estimated_L_values, '-o', 'LineWidth', 2); % Plot estimated L hold on; plot([min(L_values) max(L_values)],[min(L_values) max(L_values)],'r--', 'LineWidth', 1.5); % Diagonal line for perfect estimation xlabel('True L (meters)'); ylabel('Estimated \hat{L}(meters)'); title('True L vs. Estimated \hat{L}'); grid on; legend('Estimated \hat{L}', 'Perfect Estimation'); hold off; %% Calculate and display relative error relative_error = abs((estimated_L_values - L_values)./ L_values); disp('Relative Error for Each True L Value:'); disp(relative_error); %% Function to generate signals with delays function [y1sig, y2sig]= lab1sim(A, B, L, sig)% Creates tau1 and tau2 using the A, B, and L in meters t1=(sqrt((B)^2+(L - A)^2)/343); t2=(sqrt((B)^2+(L -(2* A))^2)/343); % Creates y1sig and y2sig by using the input signal sig and delays it y1sig = @(t) sig(t - t1); y2sig = @(t) sig(t - t2); end %% Function to estimate theta and L using cross-correlation function [theta_hat, L_hat]= Lab1est(A, B, y1_t, y2_t, fs)% Calculate the cross-correlation between y1_t and y2_t [c, lags]= xcorr(y1_t, y2_t); % Normalize the cross-correlation by the energy of the two signals cNorm = c /(sqrt(sum(y1_t.^2)* sum(y2_t.^2))); % scales cross-correlation between -1 and 1% Convert lags to time by dividing by fs lags_fs = lags / fs; % Plot the normalized cross-correlation c versus time lags figure; plot(lags_fs, cNorm); xlabel('Time Lag (s)'); ylabel('Normalized Cross-Correlation'); title('Normalized Cross-Correlation between y1(t) and y2(t)'); grid on; % Find the peak in the cross-correlation to estimate the relative time shift [~, peak_index]= max(cNorm); % Find the index of the maximum value in cNorm relative_time_shift = lags_fs(peak_index); % Convert the peak index to time shift % Calculate the estimated angle theta_hat using the relative time shift cs =343; % Speed of sound in air in m/s ratio = min((cs * relative_time_shift)/ A,1); theta_hat = asin(ratio); % Estimate the distance L L_hat = A * cos(theta_hat)+ B; fprintf('Estimated theta_hat: %.8f radians
', theta_hat);
fprintf('Estimated L_hat: %.8f meters
', L_hat);
end
3 . 3 Automating the estimates Once we have a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!