% Quick check for grating lobes
channel_data = uff.channel_data();
channel_data.read('./sonar_ping.uff','/channel_data');

c = channel_data.sound_speed;
fc = channel_data.modulation_frequency;
lambda = c / fc;
d = channel_data.probe.element_width;  % Element spacing

fprintf('Wavelength: %.5f m\n', lambda);
fprintf('Element spacing: %.5f m\n', d);
fprintf('d/lambda ratio: %.2f\n', d/lambda);
fprintf('\n');
fprintf('For no grating lobes, need d < lambda/2 = %.5f m\n', lambda/2);
fprintf('Current d/lambda = %.2f (should be < 0.5 for no grating lobes)\n', d/lambda);
fprintf('\n');

% Calculate grating lobe angles
if d > lambda/2
    fprintf('WARNING: Grating lobes present!\n');
    % Grating lobes occur at angles where sin(theta) = n*lambda/d
    for n = 1:3
        sin_theta = n * lambda / d;
        if abs(sin_theta) <= 1
            theta_deg = asind(sin_theta);
            fprintf('Grating lobe %d at: ±%.1f degrees\n', n, theta_deg);
        end
    end
else
    fprintf('No grating lobes - element spacing is fine.\n');
end
